1
|
|
|
package tests |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
"time" |
6
|
|
|
|
7
|
|
|
ago "github.com/SerhiiCho/timeago/v3" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
func TestParseZh(t *testing.T) { |
11
|
|
|
cases := []struct { |
12
|
|
|
date time.Time |
13
|
|
|
res string |
14
|
|
|
}{ |
15
|
|
|
{subMinutes(1), "1分钟前"}, |
16
|
|
|
{subMinutes(2), "2分钟前"}, |
17
|
|
|
{subMinutes(5), "5分钟前"}, |
18
|
|
|
{subMinutes(9), "9分钟前"}, |
19
|
|
|
{subMinutes(10), "10分钟前"}, |
20
|
|
|
{subMinutes(11), "11分钟前"}, |
21
|
|
|
{subMinutes(20), "20分钟前"}, |
22
|
|
|
{subMinutes(21), "21分钟前"}, |
23
|
|
|
{subMinutes(22), "22分钟前"}, |
24
|
|
|
{subMinutes(30), "30分钟前"}, |
25
|
|
|
{subMinutes(31), "31分钟前"}, |
26
|
|
|
{subMinutes(59), "59分钟前"}, |
27
|
|
|
{subHours(1), "1小时前"}, |
28
|
|
|
{subHours(2), "2小时前"}, |
29
|
|
|
{subHours(9), "9小时前"}, |
30
|
|
|
{subHours(10), "10小时前"}, |
31
|
|
|
{subHours(11), "11小时前"}, |
32
|
|
|
{subHours(20), "20小时前"}, |
33
|
|
|
{subHours(21), "21小时前"}, |
34
|
|
|
{subHours(23), "23小时前"}, |
35
|
|
|
{subDays(1), "1天前"}, |
36
|
|
|
{subDays(2), "2天前"}, |
37
|
|
|
{subDays(4), "4天前"}, |
38
|
|
|
{subDays(5), "5天前"}, |
39
|
|
|
{subDays(6), "6天前"}, |
40
|
|
|
{subWeeks(1), "1周前"}, |
41
|
|
|
{subWeeks(2), "2周前"}, |
42
|
|
|
{subWeeks(3), "3周前"}, |
43
|
|
|
{subMonths(1), "1个月前"}, |
44
|
|
|
{subMonths(2), "2个月前"}, |
45
|
|
|
{subMonths(9), "9个月前"}, |
46
|
|
|
{subMonths(11), "11个月前"}, |
47
|
|
|
{subYears(1), "1年前"}, |
48
|
|
|
{subYears(2), "2年前"}, |
49
|
|
|
{subYears(21), "21年前"}, |
50
|
|
|
{subYears(31), "31年前"}, |
51
|
|
|
{subYears(100), "100年前"}, |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
for _, tc := range cases { |
55
|
|
|
t.Run("result for "+tc.date.String(), func(test *testing.T) { |
56
|
|
|
ago.Reconfigure(ago.Config{Language: ago.LangZh}) |
57
|
|
|
|
58
|
|
|
res, err := ago.Parse(tc.date) |
59
|
|
|
|
60
|
|
|
if err != nil { |
61
|
|
|
test.Errorf("Error must be nil, but got %v instead", err) |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if res != tc.res { |
65
|
|
|
test.Errorf("Result must be %s, but got %s instead", tc.res, res) |
66
|
|
|
} |
67
|
|
|
}) |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func TestParseZhWithSeconds(t *testing.T) { |
72
|
|
|
cases := []struct { |
73
|
|
|
date time.Time |
74
|
|
|
res []string |
75
|
|
|
}{ |
76
|
|
|
{subSeconds(0), []string{"0秒前", "1秒前"}}, |
77
|
|
|
{subSeconds(1), []string{"1秒前", "2秒前"}}, |
78
|
|
|
{subSeconds(2), []string{"2秒前", "3秒前"}}, |
79
|
|
|
{subSeconds(9), []string{"9秒前", "10秒前"}}, |
80
|
|
|
{subSeconds(10), []string{"10秒前", "11秒前"}}, |
81
|
|
|
{subSeconds(11), []string{"11秒前", "12秒前"}}, |
82
|
|
|
{subSeconds(20), []string{"20秒前", "21秒前"}}, |
83
|
|
|
{subSeconds(21), []string{"21秒前", "22秒前"}}, |
84
|
|
|
{subSeconds(22), []string{"22秒前", "23秒前"}}, |
85
|
|
|
{subSeconds(30), []string{"30秒前", "31秒前"}}, |
86
|
|
|
{subSeconds(59), []string{"59秒前", "1分钟前"}}, |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
for _, tc := range cases { |
90
|
|
|
t.Run("result for "+tc.date.String(), func(test *testing.T) { |
91
|
|
|
ago.Reconfigure(ago.Config{Language: ago.LangZh}) |
92
|
|
|
|
93
|
|
|
res, err := ago.Parse(tc.date) |
94
|
|
|
|
95
|
|
|
if err != nil { |
96
|
|
|
test.Errorf("Error must be nil, but got %v instead", err) |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if res != tc.res[0] && res != tc.res[1] { |
100
|
|
|
test.Errorf("Result must be %s or %s, but got %s instead", tc.res[0], tc.res[1], res) |
101
|
|
|
} |
102
|
|
|
}) |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|