1
|
|
|
package timeago |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
"time" |
6
|
|
|
|
7
|
|
|
"github.com/SerhiiCho/timeago/v3/config" |
8
|
|
|
"github.com/SerhiiCho/timeago/v3/option" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
func TestParseFunctionCanExceptTimestamp(t *testing.T) { |
12
|
|
|
cases := []struct { |
13
|
|
|
timestamp int |
14
|
|
|
result string |
15
|
|
|
}{ |
16
|
|
|
{getTimestampOfPastDate(time.Minute), "1 minute ago"}, |
17
|
|
|
{getTimestampOfPastDate(time.Minute * 5), "5 minutes ago"}, |
18
|
|
|
{getTimestampOfPastDate(time.Hour), "1 hour ago"}, |
19
|
|
|
{getTimestampOfPastDate(time.Hour * 3), "3 hours ago"}, |
20
|
|
|
{getTimestampOfPastDate(time.Hour * 5), "5 hours ago"}, |
21
|
|
|
{getTimestampOfPastDate(time.Hour * 24), "1 day ago"}, |
22
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 2), "2 days ago"}, |
23
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 3), "3 days ago"}, |
24
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 4), "4 days ago"}, |
25
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 5), "5 days ago"}, |
26
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 6), "6 days ago"}, |
27
|
|
|
{getTimestampOfPastDate(time.Hour * 24 * 7), "1 week ago"}, |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
Configure(&config.Config{Language: "en"}) |
31
|
|
|
|
32
|
|
|
for _, tc := range cases { |
33
|
|
|
t.Run(tc.result, func(test *testing.T) { |
34
|
|
|
if res := Parse(tc.timestamp); res != tc.result { |
35
|
|
|
test.Errorf("Result must be %v, but got %v instead", tc.result, res) |
36
|
|
|
} |
37
|
|
|
}) |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
func TestParseFunctionCanExceptTimePackage(t *testing.T) { |
42
|
|
|
cases := []struct { |
43
|
|
|
time time.Time |
44
|
|
|
result string |
45
|
|
|
}{ |
46
|
|
|
{time.Now().Add(-time.Minute), "1 minute ago"}, |
47
|
|
|
{time.Now().Add(-time.Minute * 2), "2 minutes ago"}, |
48
|
|
|
{time.Now().Add(-time.Minute * 3), "3 minutes ago"}, |
49
|
|
|
{time.Now().Add(-time.Minute * 4), "4 minutes ago"}, |
50
|
|
|
{time.Now().Add(-time.Minute * 5), "5 minutes ago"}, |
51
|
|
|
{time.Now().Add(-time.Minute * 6), "6 minutes ago"}, |
52
|
|
|
{time.Now().Add(-time.Hour * 7), "7 hours ago"}, |
53
|
|
|
{time.Now().Add(-time.Hour * 8), "8 hours ago"}, |
54
|
|
|
{time.Now().Add(-time.Hour * 9), "9 hours ago"}, |
55
|
|
|
{time.Now().Add(-time.Hour * 10), "10 hours ago"}, |
56
|
|
|
{time.Now().Add(-time.Hour * 11), "11 hours ago"}, |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
Configure(&config.Config{Language: "en"}) |
60
|
|
|
|
61
|
|
|
for _, tc := range cases { |
62
|
|
|
t.Run("Test for date "+tc.time.String(), func(test *testing.T) { |
63
|
|
|
if res := Parse(tc.time); res != tc.result { |
64
|
|
|
test.Errorf("Result must be %v, but got %v instead", tc.result, res) |
65
|
|
|
} |
66
|
|
|
}) |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
func TestParseFuncWillCalculateIntervalToFutureDate(t *testing.T) { |
71
|
|
|
testCases := []struct { |
72
|
|
|
time time.Time |
73
|
|
|
result string |
74
|
|
|
}{ |
75
|
|
|
{time.Now().Add(time.Minute * 2), "2 minutes"}, |
76
|
|
|
{time.Now().Add(time.Minute * 5), "5 minutes"}, |
77
|
|
|
{time.Now().Add(time.Minute * 10), "10 minutes"}, |
78
|
|
|
{time.Now().Add(time.Hour), "1 hour"}, |
79
|
|
|
{time.Now().Add(time.Hour * 24), "1 day"}, |
80
|
|
|
{time.Now().Add(time.Hour * 48), "2 days"}, |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
Configure(&config.Config{Language: "en"}) |
84
|
|
|
|
85
|
|
|
for _, tc := range testCases { |
86
|
|
|
t.Run("Test for date: "+tc.time.String(), func(test *testing.T) { |
87
|
|
|
if res := Parse(tc.time); res != tc.result { |
88
|
|
|
test.Errorf("Result must be %v, but got %v instead", tc.result, res) |
89
|
|
|
} |
90
|
|
|
}) |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
func TestOptionIsEnabled(t *testing.T) { |
95
|
|
|
t.Run("returns true if option is enabled", func(test *testing.T) { |
96
|
|
|
options = []option.Option{"noSuffix"} |
97
|
|
|
|
98
|
|
|
if res := optionIsEnabled("noSuffix"); res == false { |
99
|
|
|
test.Error("Result must be true, but got false instead") |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
options = []option.Option{} |
103
|
|
|
}) |
104
|
|
|
|
105
|
|
|
t.Run("returns true if option is enabled with other option", func(test *testing.T) { |
106
|
|
|
options = []option.Option{"noSuffix", "upcoming"} |
107
|
|
|
|
108
|
|
|
if res := optionIsEnabled("upcoming"); res == false { |
109
|
|
|
test.Error("Result must be true, but got false instead") |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
options = []option.Option{} |
113
|
|
|
}) |
114
|
|
|
|
115
|
|
|
t.Run("returns false if option is disabled", func(test *testing.T) { |
116
|
|
|
if res := optionIsEnabled("noSuffix"); res == true { |
117
|
|
|
test.Error("Result must be true, but got false instead") |
118
|
|
|
} |
119
|
|
|
}) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
func getTimestampOfPastDate(subDuration time.Duration) int { |
123
|
|
|
return int(time.Now().Add(-subDuration).UnixNano() / 1000000000) |
124
|
|
|
} |
125
|
|
|
|