-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime_test.go
124 lines (107 loc) · 3.98 KB
/
time_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
package humanize
import (
"testing"
"time"
)
type timeTest struct {
t1, t2, expected string
}
func runTimeTest(t *testing.T, tests []timeTest, handler func(a time.Time, b time.Time) string) {
for _, test := range tests {
if actual := handler(parseTime(t, test.t1), parseTime(t, test.t2)); actual != test.expected {
t.Errorf("expected '%s' but got '%s'", test.expected, actual)
}
}
}
func parseTime(t *testing.T, value string) time.Time {
parsed, err := time.Parse(time.RFC3339, value)
if err != nil {
t.Errorf("could not parse time '%s'", value)
}
return parsed
}
func TestTimeTo(t *testing.T) {
tests := []timeTest{
{"2021-01-01T22:00:00+00:00", "2021-01-01T22:00:00+00:00", "just now"},
{"2021-01-01T22:00:00+00:00", "2021-01-01T22:00:02+00:00", "in a few seconds"},
{"2021-01-01T22:00:00+00:00", "2021-01-02T22:00:00+00:00", "in a day"},
{"2021-01-02T22:00:00+00:00", "2021-01-01T22:00:00+00:00", "a day ago"},
{"2019-01-01T22:00:00+00:00", "2021-01-01T22:00:00+00:00", "in 2 years"},
{"2021-01-02T22:00:00+00:00", "2021-01-24T22:00:00+00:00", "in 3 weeks"},
{"2021-01-02T22:00:00+00:00", "2021-05-05T22:00:00+00:00", "in 4 months"},
{"2021-01-02T22:00:00+00:00", "2065-05-05T22:00:00+00:00", "in a long time"},
{"2021-01-02T22:00:00+00:00", "2035-05-05T22:00:00+00:00", "in a decade"},
{"2020-01-01T22:00:00+00:00", "2021-02-01T22:00:00+00:00", "in a year"},
}
runTimeTest(t, tests, func(t1 time.Time, t2 time.Time) string {
return Time(t1).To(t2)
})
}
func TestTimeFrom(t *testing.T) {
tests := []timeTest{
{"2020-01-01T22:00:00+00:00", "2021-02-01T22:00:00+00:00", "a year ago"},
{"2021-05-05T22:00:00+00:00", "2021-05-10T22:00:00+00:00", "5 days ago"},
{"2016-01-01T00:00:00+00:00", "2018-05-01T00:00:00+00:00", "2 years ago"},
}
runTimeTest(t, tests, func(t1 time.Time, t2 time.Time) string {
return Time(t1).From(t2)
})
}
func TestExactTimeTo(t *testing.T) {
tests := []timeTest{
{"2021-01-01T22:00:00+00:00", "2021-01-01T22:00:00+00:00", "just now"},
{"2021-01-01T22:00:00+00:00", "2021-01-02T22:00:00+00:00", "in 1 day"},
{"2021-01-01T22:00:00+00:00", "2021-01-05T23:20:21+00:00", "in 4 days, 1 hour, 20 minutes and 21 seconds"},
{"2020-01-01T22:00:00+00:00", "2021-01-01T22:00:00+00:00", "in 1 year"},
{"2019-01-01T22:00:00+00:00", "2020-01-01T22:00:00+00:00", "in 1 year"},
{"2021-01-02T22:00:00+00:00", "2022-05-05T22:00:00+00:00", "in 1 year, 4 months and 3 days"},
{"2021-05-03T15:00:00+00:00", "2021-05-08T18:30:00+00:00", "in 5 days, 3 hours and 30 minutes"},
}
runTimeTest(t, tests, func(t1 time.Time, t2 time.Time) string {
return ExactTime(t1).To(t2)
})
}
func TestExactTimeFrom(t *testing.T) {
tests := []timeTest{
{"2020-01-01T22:05:00+00:00", "2021-02-01T02:50:22+00:00", "1 year, 30 days, 4 hours, 45 minutes and 22 seconds ago"},
{"2016-01-01T00:00:00+00:00", "2018-05-01T00:00:00+00:00", "2 years and 4 months ago"},
}
runTimeTest(t, tests, func(t1 time.Time, t2 time.Time) string {
return ExactTime(t1).From(t2)
})
}
func TestDuration(t *testing.T) {
tests := []struct {
input time.Duration
expected string
}{
{time.Hour * 48, "2 days"},
{time.Hour * 65, "2 days"},
{time.Hour * 70, "3 days"},
{time.Hour * 24 * 38, "a month"},
{time.Hour * 1000000, "a long time"},
}
for _, test := range tests {
if actual := Duration(test.input); actual != test.expected {
t.Errorf("expected `%s`, but got `%s`", test.expected, actual)
}
}
}
func TestExactDuration(t *testing.T) {
tests := []struct {
input time.Duration
expected string
}{
{time.Hour * 48, "2 days"},
{time.Hour * 65, "2 days and 17 hours"},
{time.Hour * 70, "2 days and 22 hours"},
{time.Hour * 24 * 8, "8 days"},
{time.Hour*3 + time.Minute*33 + time.Second*55, "3 hours, 33 minutes and 55 seconds"},
{time.Hour * 1000000, "114 years, 1 month, 26 days and 6 hours"},
}
for _, test := range tests {
if actual := ExactDuration(test.input); actual != test.expected {
t.Errorf("expected `%s`, but got `%s`", test.expected, actual)
}
}
}