-
Notifications
You must be signed in to change notification settings - Fork 6
/
timeago_test.go
149 lines (129 loc) · 4.35 KB
/
timeago_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
package timeago
import (
"testing"
"time"
)
func TestGetWords(t *testing.T) {
cases := []struct {
timeKind string
num int
result string
lang string
}{
// english
{"days", 11, "11 days ago", "en"},
{"days", 21, "21 days ago", "en"},
{"seconds", 30, "30 seconds ago", "en"},
{"seconds", 31, "31 seconds ago", "en"},
{"hours", 10, "10 hours ago", "en"},
{"years", 2, "2 years ago", "en"},
// russian
{"hours", 5, "5 часов назад", "ru"},
{"days", 11, "11 дней назад", "ru"},
{"years", 21, "21 год назад", "ru"},
{"minutes", 59, "59 минут назад", "ru"},
}
for _, tc := range cases {
t.Run(tc.result, func(test *testing.T) {
SetConfig(Config{Language: tc.lang})
if res := getWords(tc.timeKind, tc.num); res != tc.result {
test.Errorf("Result must be `%s` but got `%s` instead", tc.result, res)
}
})
}
}
func TestParseFunctionCanExceptTimestamp(t *testing.T) {
cases := []struct {
timestamp int
result string
}{
{getTimestampOfPastDate(time.Minute), "1 minute ago"},
{getTimestampOfPastDate(time.Minute * 5), "5 minutes ago"},
{getTimestampOfPastDate(time.Hour), "1 hour ago"},
{getTimestampOfPastDate(time.Hour * 3), "3 hours ago"},
{getTimestampOfPastDate(time.Hour * 5), "5 hours ago"},
{getTimestampOfPastDate(time.Hour * 24), "1 day ago"},
{getTimestampOfPastDate(time.Hour * 24 * 2), "2 days ago"},
{getTimestampOfPastDate(time.Hour * 24 * 3), "3 days ago"},
{getTimestampOfPastDate(time.Hour * 24 * 4), "4 days ago"},
{getTimestampOfPastDate(time.Hour * 24 * 5), "5 days ago"},
{getTimestampOfPastDate(time.Hour * 24 * 6), "6 days ago"},
{getTimestampOfPastDate(time.Hour * 24 * 7), "1 week ago"},
}
SetConfig(Config{Language: "en"})
for _, tc := range cases {
t.Run(tc.result, func(test *testing.T) {
if res := Parse(tc.timestamp); res != tc.result {
test.Errorf("Result must be %v, but got %v instead", tc.result, res)
}
})
}
}
func TestParseFunctionCanExceptTimePackage(t *testing.T) {
cases := []struct {
time time.Time
result string
}{
{time.Now().Add(-time.Minute), "1 minute ago"},
{time.Now().Add(-time.Minute * 2), "2 minutes ago"},
{time.Now().Add(-time.Minute * 3), "3 minutes ago"},
{time.Now().Add(-time.Minute * 4), "4 minutes ago"},
{time.Now().Add(-time.Minute * 5), "5 minutes ago"},
{time.Now().Add(-time.Minute * 6), "6 minutes ago"},
{time.Now().Add(-time.Hour * 7), "7 hours ago"},
{time.Now().Add(-time.Hour * 8), "8 hours ago"},
{time.Now().Add(-time.Hour * 9), "9 hours ago"},
{time.Now().Add(-time.Hour * 10), "10 hours ago"},
{time.Now().Add(-time.Hour * 11), "11 hours ago"},
}
SetConfig(Config{Language: "en"})
for _, tc := range cases {
t.Run("Test for date "+tc.time.String(), func(test *testing.T) {
if res := Parse(tc.time); res != tc.result {
test.Errorf("Result must be %v, but got %v instead", tc.result, res)
}
})
}
}
func TestParseFuncWillCalculateIntervalToFutureDate(t *testing.T) {
testCases := []struct {
time time.Time
result string
}{
{time.Now().Add(time.Minute * 2), "2 minutes"},
{time.Now().Add(time.Minute * 5), "5 minutes"},
{time.Now().Add(time.Minute * 10), "10 minutes"},
{time.Now().Add(time.Hour), "1 hour"},
{time.Now().Add(time.Hour * 24), "1 day"},
{time.Now().Add(time.Hour * 48), "2 days"},
}
SetConfig(Config{Language: "en"})
for _, tc := range testCases {
t.Run("Test for date: "+tc.time.String(), func(test *testing.T) {
if res := Parse(tc.time); res != tc.result {
test.Errorf("Result must be %v, but got %v instead", tc.result, res)
}
})
}
}
func TestOptionIsEnabled(t *testing.T) {
t.Run("returns true if option is enabled", func(test *testing.T) {
globalOptions = []string{"noSuffix"}
if res := optionIsEnabled("noSuffix"); res == false {
test.Error("Result must be true, but got false instead")
}
globalOptions = []string{}
})
t.Run("returns true if option is enabled with other option", func(test *testing.T) {
globalOptions = []string{"noSuffix", "upcoming"}
if res := optionIsEnabled("upcoming"); res == false {
test.Error("Result must be true, but got false instead")
}
globalOptions = []string{}
})
t.Run("returns false if option is disabled", func(test *testing.T) {
if res := optionIsEnabled("noSuffix"); res == true {
test.Error("Result must be true, but got false instead")
}
})
}