forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_test.go
executable file
·174 lines (144 loc) · 5.3 KB
/
language_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package carbon
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLanguage_SetLocale(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
expected error // 期望值
}{
{1, "en", nil},
{2, "zh-CN", nil},
}
for _, test := range tests {
assert.ErrorIs(test.expected, NewLanguage().SetLocale(test.input), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetLocale(t *testing.T) {
locale, lang := "xxx", NewLanguage()
actual := lang.SetLocale(locale)
assert.Equal(t, invalidLocaleError(locale, lang.dir), actual, "Should catch an exception in SetLocale()")
}
func TestLanguage_SetDir(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
expected error // 期望值
}{
{1, "lang", nil},
}
for _, test := range tests {
assert.ErrorIs(test.expected, NewLanguage().SetDir(test.input), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetDir(t *testing.T) {
dir := "./demo"
actual := NewLanguage().SetDir(dir)
assert.Equal(t, invalidDirError(dir), actual, "Should catch an exception in SetDir()")
}
func TestLanguage_SetResources1(t *testing.T) {
assert := assert.New(t)
lang := NewLanguage()
resources := map[string]string{
"seasons": "spring|summer|autumn|winter",
"year": "1 yr|%d yrs",
"month": "1 mo|%d mos",
"week": "%dw",
"day": "%dd",
"hour": "%dh",
"minute": "%dm",
"second": "%ds",
"now": "just now",
"ago": "%s ago",
"from_now": "in %s",
"before": "%s before",
"after": "%s after",
}
lang.SetResources(resources)
tests := []struct {
id int // 测试id
input1 string // 输入值
input2 string // 输入值
expected string // 期望值
}{
{1, "2020-08-05 13:14:15", "2020-08-05 13:14:15", "just now"},
{2, "2020-08-05 13:14:15", "2021-08-05 13:14:15", "1 yr before"},
{3, "2020-08-05 13:14:15", "2019-08-05 13:14:15", "1 yr after"},
{4, "2020-08-05 13:14:15", "2030-08-05 13:14:15", "10 yrs before"},
{5, "2020-08-05 13:14:15", "2010-08-05 13:14:15", "10 yrs after"},
{6, "2020-08-05 13:14:15", "2020-09-05 13:14:15", "1 mo before"},
{7, "2020-08-05 13:14:15", "2020-07-05 13:14:15", "1 mo after"},
{8, "2020-08-05 13:14:15", "2021-06-05 13:14:15", "10 mos before"},
{9, "2020-08-05 13:14:15", "2019-10-05 13:14:15", "10 mos after"},
{10, "2020-08-05 13:14:15", "2020-08-06 13:14:15", "1d before"},
{11, "2020-08-05 13:14:15", "2020-08-04 13:14:15", "1d after"},
{12, "2020-08-05 13:14:15", "2020-08-15 13:14:15", "1w before"},
{13, "2020-08-05 13:14:15", "2020-07-26 13:14:15", "1w after"},
{14, "2020-08-05 13:14:15", "2020-08-05 14:14:15", "1h before"},
{15, "2020-08-05 13:14:15", "2020-08-05 12:14:15", "1h after"},
{16, "2020-08-05 13:14:15", "2020-08-05 23:14:15", "10h before"},
{17, "2020-08-05 13:14:15", "2020-08-05 03:14:15", "10h after"},
{18, "2020-08-05 13:14:15", "2020-08-05 13:15:15", "1m before"},
{19, "2020-08-05 13:14:15", "2020-08-05 13:13:15", "1m after"},
{20, "2020-08-05 13:14:15", "2020-08-05 13:24:15", "10m before"},
{21, "2020-08-05 13:14:15", "2020-08-05 13:04:15", "10m after"},
{22, "2020-08-05 13:14:15", "2020-08-05 13:14:16", "1s before"},
{23, "2020-08-05 13:14:15", "2020-08-05 13:14:14", "1s after"},
{24, "2020-08-05 13:14:15", "2020-08-05 13:14:25", "10s before"},
{25, "2020-08-05 13:14:15", "2020-08-05 13:14:05", "10s after"},
}
for _, test := range tests {
c1 := Parse(test.input1)
c2 := Parse(test.input2)
assert.Nil(c1.Error)
assert.Nil(c2.Error)
assert.Equal(test.expected, c1.SetLanguage(lang).DiffForHumans(c2), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestLanguage_SetResources2(t *testing.T) {
assert := assert.New(t)
lang := NewLanguage()
resources := map[string]string{
"xxx": "xxx",
}
lang.SetResources(resources)
lang.SetLocale("en")
tests := []struct {
id int // 测试id
input string // 输入值
expected string // 期望值
}{
{1, "", ""},
{2, "0", ""},
{3, "0000-00-00", ""},
{4, "00:00:00", ""},
{5, "0000-00-00 00:00:00", ""},
{6, "2021-08-05 13:14:15", ""},
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).DiffForHumans(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).Constellation(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).Season(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).ToWeekString(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).ToShortWeekString(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).ToMonthString(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
assert.Equal(test.expected, Parse(test.input).SetLanguage(lang).ToShortMonthString(), "Current test id is "+strconv.Itoa(test.id))
}
}