|
1
|
|
|
package timeago |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
"time" |
|
6
|
|
|
) |
|
7
|
|
|
|
|
8
|
|
|
func TestIsLocationProvided(t *testing.T) { |
|
9
|
|
|
cases := []struct { |
|
10
|
|
|
name string |
|
11
|
|
|
loc string |
|
12
|
|
|
expect bool |
|
13
|
|
|
}{ |
|
14
|
|
|
{ |
|
15
|
|
|
name: "Location is set to Europe/Paris", |
|
16
|
|
|
loc: "Europe/Paris", |
|
17
|
|
|
expect: true, |
|
18
|
|
|
}, |
|
19
|
|
|
{ |
|
20
|
|
|
name: "Location is set to Asia/Shanghai", |
|
21
|
|
|
loc: "Asia/Shanghai", |
|
22
|
|
|
expect: true, |
|
23
|
|
|
}, |
|
24
|
|
|
{ |
|
25
|
|
|
name: "Location is not set", |
|
26
|
|
|
loc: "UTC", |
|
27
|
|
|
expect: false, |
|
28
|
|
|
}, |
|
29
|
|
|
{ |
|
30
|
|
|
name: "Location is empty", |
|
31
|
|
|
loc: "", |
|
32
|
|
|
expect: false, |
|
33
|
|
|
}, |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
for _, tc := range cases { |
|
37
|
|
|
t.Run(tc.name, func(t *testing.T) { |
|
38
|
|
|
c := NewConfig("en", tc.loc, []LangSet{}, 60, 60) |
|
39
|
|
|
actual := c.isLocationProvided() |
|
40
|
|
|
|
|
41
|
|
|
if actual != tc.expect { |
|
42
|
|
|
t.Fatalf("Expected %v, but got %v", tc.expect, actual) |
|
43
|
|
|
} |
|
44
|
|
|
}) |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
func TestCustomTranslations(t *testing.T) { |
|
49
|
|
|
cases := []struct { |
|
50
|
|
|
expect string |
|
51
|
|
|
time time.Duration |
|
52
|
|
|
langSet LangSet |
|
53
|
|
|
}{ |
|
54
|
|
|
{ |
|
55
|
|
|
expect: "10 h a", |
|
56
|
|
|
time: time.Hour * 10, |
|
57
|
|
|
langSet: LangSet{ |
|
58
|
|
|
Lang: "en", |
|
59
|
|
|
Ago: "a", |
|
60
|
|
|
Hour: LangForms{"other": "h"}, |
|
61
|
|
|
}, |
|
62
|
|
|
}, |
|
63
|
|
|
{ |
|
64
|
|
|
expect: "1м н", |
|
65
|
|
|
time: time.Minute, |
|
66
|
|
|
langSet: LangSet{ |
|
67
|
|
|
Format: "{num}{timeUnit} {ago}", |
|
68
|
|
|
Lang: "ru", |
|
69
|
|
|
Ago: "н", |
|
70
|
|
|
Minute: LangForms{"one": "м"}, |
|
71
|
|
|
}, |
|
72
|
|
|
}, |
|
73
|
|
|
{ |
|
74
|
|
|
expect: "2 d ago", |
|
75
|
|
|
time: time.Hour * 24 * 2, |
|
76
|
|
|
langSet: LangSet{ |
|
77
|
|
|
Lang: "en", |
|
78
|
|
|
Day: LangForms{"other": "d"}, |
|
79
|
|
|
}, |
|
80
|
|
|
}, |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
for _, tc := range cases { |
|
84
|
|
|
name := tc.langSet.Lang + " " + tc.expect |
|
85
|
|
|
|
|
86
|
|
|
t.Run(name, func(t *testing.T) { |
|
87
|
|
|
Reconfigure(Config{ |
|
88
|
|
|
Language: tc.langSet.Lang, |
|
89
|
|
|
Translations: []LangSet{tc.langSet}, |
|
90
|
|
|
}) |
|
91
|
|
|
|
|
92
|
|
|
date := timestampFromPastDate(tc.time) |
|
93
|
|
|
|
|
94
|
|
|
if res, _ := Parse(date); res != tc.expect { |
|
95
|
|
|
t.Errorf("Result must be %q, but got %q instead", tc.expect, res) |
|
96
|
|
|
} |
|
97
|
|
|
}) |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|