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", |
16
|
|
|
loc: "Russia/Moscow", |
17
|
|
|
expect: true, |
18
|
|
|
}, |
19
|
|
|
{ |
20
|
|
|
name: "Location is not set", |
21
|
|
|
loc: "", |
22
|
|
|
expect: false, |
23
|
|
|
}, |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
for _, tc := range cases { |
27
|
|
|
t.Run(tc.name, func(t *testing.T) { |
28
|
|
|
c := defaultConfig() |
29
|
|
|
actual := c.isLocationProvided() |
30
|
|
|
|
31
|
|
|
if actual != tc.expect { |
32
|
|
|
t.Fatalf("Expected %v, but got %v", tc.expect, actual) |
33
|
|
|
} |
34
|
|
|
}) |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
func TestCustomTranslations(t *testing.T) { |
39
|
|
|
cases := []struct { |
40
|
|
|
expect string |
41
|
|
|
time time.Duration |
42
|
|
|
langSet LangSet |
43
|
|
|
}{ |
44
|
|
|
{ |
45
|
|
|
expect: "10 h a", |
46
|
|
|
time: time.Hour * 10, |
47
|
|
|
langSet: LangSet{ |
48
|
|
|
Lang: "en", |
49
|
|
|
Ago: "a", |
50
|
|
|
Hour: LangForms{"other": "h"}, |
51
|
|
|
}, |
52
|
|
|
}, |
53
|
|
|
{ |
54
|
|
|
expect: "1м н", |
55
|
|
|
time: time.Minute, |
56
|
|
|
langSet: LangSet{ |
57
|
|
|
Format: "{num}{timeUnit} {ago}", |
58
|
|
|
Lang: "ru", |
59
|
|
|
Ago: "н", |
60
|
|
|
Minute: LangForms{"one": "м"}, |
61
|
|
|
}, |
62
|
|
|
}, |
63
|
|
|
{ |
64
|
|
|
expect: "2 d ago", |
65
|
|
|
time: time.Hour * 24 * 2, |
66
|
|
|
langSet: LangSet{ |
67
|
|
|
Lang: "en", |
68
|
|
|
Day: LangForms{"other": "d"}, |
69
|
|
|
}, |
70
|
|
|
}, |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
for _, tc := range cases { |
74
|
|
|
name := tc.langSet.Lang + " " + tc.expect |
75
|
|
|
|
76
|
|
|
t.Run(name, func(t *testing.T) { |
77
|
|
|
Reconfigure(Config{ |
78
|
|
|
Language: tc.langSet.Lang, |
79
|
|
|
Translations: []LangSet{tc.langSet}, |
80
|
|
|
}) |
81
|
|
|
|
82
|
|
|
date := timestampFromPastDate(tc.time) |
83
|
|
|
|
84
|
|
|
if res, _ := Parse(date); res != tc.expect { |
85
|
|
|
t.Errorf("Result must be %q, but got %q instead", tc.expect, res) |
86
|
|
|
} |
87
|
|
|
}) |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|