-
Notifications
You must be signed in to change notification settings - Fork 0
/
humantime_test.go
161 lines (153 loc) · 2.51 KB
/
humantime_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package humantime
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testcases = []struct {
duration string
expect string
}{
{
duration: "1s",
expect: "just now",
},
{
duration: "30s",
expect: "seconds ago",
},
{
duration: "60s",
expect: "a minute ago",
},
{
duration: "4m",
expect: "4 minutes ago",
},
{
duration: "1h",
expect: "an hour ago",
},
{
duration: "1.34h",
expect: "an hour ago",
},
{
duration: "2h",
expect: "2 hours ago",
},
{
duration: "23h",
expect: "23 hours ago",
},
{
duration: "24h",
expect: "a day ago",
},
{
duration: "32h",
expect: "a day ago",
},
{
duration: "48h",
expect: "2 days ago",
},
{
duration: "72h",
expect: "3 days ago",
},
{
duration: "168h",
expect: "a week ago",
},
{
duration: "200h",
expect: "a week ago",
},
{
duration: "336h",
expect: "2 weeks ago",
},
{
duration: "401h",
expect: "2 weeks ago",
},
{
duration: "672h",
expect: "4 weeks ago",
},
{
duration: "720h",
expect: "4 weeks ago",
},
{
duration: "730h",
expect: "about a month ago",
},
{
duration: "900h",
expect: "about a month ago",
},
{
duration: "1300h",
expect: "about a month ago",
},
{
duration: "1460h",
expect: "about 2 months ago",
},
{
duration: "2190h",
expect: "about 3 months ago",
},
{
duration: "8030h",
expect: "about 11 months ago",
},
{
duration: "8760h",
expect: "a year ago",
},
{
duration: "16000h",
expect: "a year ago",
},
{
duration: "17520h",
expect: "2 years ago",
},
{
duration: "271560h",
expect: "31 years ago",
},
{
duration: "274560h",
expect: "31 years ago",
},
{
duration: "876000h",
expect: "100 years ago",
},
}
func TestDuration(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.duration, func(t *testing.T) {
d, err := time.ParseDuration(tc.duration)
require.NoError(t, err, "bug in TEST CASE - parse duration failed")
timeStr := Duration(d)
assert.Equal(t, tc.expect, timeStr, "output does not match expect")
})
}
}
func TestSince(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.duration, func(t *testing.T) {
d, err := time.ParseDuration(tc.duration)
require.NoError(t, err, "bug in TEST CASE - parse duration failed")
nowLessDuration := time.Now().Add(-d)
timeStr := Since(nowLessDuration)
assert.Equal(t, tc.expect, timeStr, "output does not match expect")
})
}
}