-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_test.go
101 lines (91 loc) · 1.79 KB
/
config_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
package timeago
import (
"testing"
"time"
"github.com/SerhiiCho/timeago/v3/internal/utils"
)
func TestIsLocationProvided(t *testing.T) {
cases := []struct {
name string
loc string
expect bool
}{
{
name: "Location is set to Europe/Paris",
loc: "Europe/Paris",
expect: true,
},
{
name: "Location is set to Asia/Shanghai",
loc: "Asia/Shanghai",
expect: true,
},
{
name: "Location is not set",
loc: "UTC",
expect: false,
},
{
name: "Location is empty",
loc: "",
expect: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
c := NewConfig("en", tc.loc, []LangSet{}, 60, 60)
actual := c.isLocationProvided()
if actual != tc.expect {
t.Fatalf("Expected %v, but got %v", tc.expect, actual)
}
})
}
}
func TestCustomTranslations(t *testing.T) {
cases := []struct {
expect string
time time.Duration
langSet LangSet
}{
{
expect: "10 h a",
time: time.Hour * 10,
langSet: LangSet{
Lang: "en",
Ago: "a",
Hour: LangForms{"other": "h"},
},
},
{
expect: "1м н",
time: time.Minute,
langSet: LangSet{
Format: "{num}{timeUnit} {ago}",
Lang: "ru",
Ago: "н",
Minute: LangForms{"one": "м"},
},
},
{
expect: "2 d ago",
time: time.Hour * 24 * 2,
langSet: LangSet{
Lang: "en",
Day: LangForms{"other": "d"},
},
},
}
for _, tc := range cases {
name := tc.langSet.Lang + " " + tc.expect
t.Run(name, func(t *testing.T) {
Reconfigure(Config{
Language: tc.langSet.Lang,
Translations: []LangSet{tc.langSet},
})
date := utils.UnixFromPastDate(tc.time)
if res, _ := Parse(date); res != tc.expect {
t.Errorf("Result must be %q, but got %q instead", tc.expect, res)
}
})
}
}