forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter_test.go
executable file
·278 lines (236 loc) · 6.7 KB
/
setter_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package carbon
import (
"fmt"
"testing"
)
var TimezoneTests = []struct {
input string // 输入值
timezone string // 输入参数
output string // 期望输出值
}{
{"2020-08-05 13:14:15", PRC, "2020-08-05 13:14:15"},
{"2020-08-05", Tokyo, "2020-08-05 01:00:00"},
{"2020-08-05", "Hangzhou", "panic"}, // 异常情况
}
func TestCarbon_SetTimezone1(t *testing.T) {
for _, v := range TimezoneTests {
output := SetTimezone(v.timezone).Parse(v.input)
if output.Error != nil {
fmt.Println("catch an exception in SetTimezone():", output.Error)
return
}
if output.ToDateTimeString() != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output.ToDateTimeString())
}
}
}
func TestCarbon_SetTimezone2(t *testing.T) {
for _, v := range TimezoneTests {
output := SetTimezone(PRC).SetTimezone(v.timezone).Parse(v.input)
if output.Error != nil {
fmt.Println("catch an exception in SetTimezone():", output.Error)
return
}
if output.ToDateTimeString() != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output.ToDateTimeString())
}
}
}
var LocaleTests = []struct {
input string // 输入参数
output string // 期望输出值
}{
{"en", "en"},
{"zh-CN", "zh-CN"},
{"XXXX", "panic"}, // 异常情况
}
func TestCarbon_SetLocale(t *testing.T) {
for _, v := range LocaleTests {
output := SetLocale(v.input)
if output.Error != nil {
fmt.Println("catch an exception in SetLocale():", output.Error)
return
}
if output.Locale() != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output.ToDateTimeString())
}
}
}
func TestCarbon_SetLanguage1(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
"day": "%dd",
}
if lang.SetLocale("en") == nil {
lang.SetResources(resources)
}
Tests := []struct {
input Carbon // 输入值
output string // 期望输出值
}{
{Now(), "just now"},
{Now().AddYears(1), "1 year from now"},
{Now().SubYears(1), "1 year ago"},
{Now().AddYears(10), "10 years from now"},
{Now().SubYears(10), "10 years ago"},
{Now().AddMonths(1), "1 month from now"},
{Now().SubMonths(1), "1 month ago"},
{Now().AddMonths(10), "10 months from now"},
{Now().SubMonths(10), "10 months ago"},
{Now().AddDays(1), "1d from now"},
{Now().SubDays(1), "1d ago"},
{Now().AddDays(10), "1 week from now"},
{Now().SubDays(10), "1 week ago"},
}
for _, v := range Tests {
output := (v.input).SetLanguage(lang).DiffForHumans()
if output != v.output {
t.Errorf("Input time %s, expected %s, but got %s", v.input.ToDateTimeString(), v.output, output)
}
}
}
func TestCarbon_SetLanguage2(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
"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 {
input Carbon // 输入值
output string // 期望输出值
}{
{Now(), "just now"},
{Now().AddYears(1), "in 1 yr"},
{Now().SubYears(1), "1 yr ago"},
{Now().AddYears(10), "in 10 yrs"},
{Now().SubYears(10), "10 yrs ago"},
{Now().AddMonths(1), "in 1 mo"},
{Now().SubMonths(1), "1 mo ago"},
{Now().AddMonths(10), "in 10 mos"},
{Now().SubMonths(10), "10 mos ago"},
{Now().AddDays(1), "in 1d"},
{Now().SubDays(1), "1d ago"},
{Now().AddDays(10), "in 1w"},
{Now().SubDays(10), "1w ago"},
}
for _, v := range Tests {
output := (v.input).SetLanguage(lang).DiffForHumans()
if output != v.output {
t.Errorf("Input time %s, expected %s, but got %s", v.input.ToDateTimeString(), v.output, output)
}
}
}
func TestCarbon_SetYear(t *testing.T) {
Tests := []struct {
input string // 输入值
year int // 输入参数
output string // 期望输出值
}{
{"2020-01-01", 2019, "2019-01-01"},
{"2020-01-31", 2019, "2019-01-31"},
{"2020-02-01", 2019, "2019-02-01"},
{"2020-02-28", 2019, "2019-02-28"},
{"2020-02-29", 2019, "2019-03-01"},
}
for _, v := range Tests {
output := Parse(v.input).SetYear(v.year).ToDateString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}
func TestCarbon_SetMonth(t *testing.T) {
Tests := []struct {
input string // 输入值
month int // 输入参数
output string // 期望输出值
}{
{"2020-01-01", 2, "2020-02-01"},
{"2020-01-30", 2, "2020-03-01"},
{"2020-01-31", 2, "2020-03-02"},
{"2020-08-05", 2, "2020-02-05"},
}
for _, v := range Tests {
output := Parse(v.input).SetMonth(v.month).ToDateString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}
func TestCarbon_SetDay(t *testing.T) {
Tests := []struct {
input string // 输入值
day int // 输入参数
output string // 期望输出值
}{
{"2020-01-01", 31, "2020-01-31"},
{"2020-02-01", 31, "2020-03-02"},
{"2020-02-28", 31, "2020-03-02"},
{"2020-02-29", 31, "2020-03-02"},
}
for _, v := range Tests {
output := Parse(v.input).SetDay(v.day).ToDateString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}
func TestCarbon_SetHour(t *testing.T) {
Tests := []struct {
input string // 输入值
hour int // 输入参数
output string // 期望输出值
}{
{"2020-08-05 13:14:15", 10, "2020-08-05 10:14:15"},
{"2020-08-05 13:14:15", 24, "2020-08-06 00:14:15"},
}
for _, v := range Tests {
output := Parse(v.input).SetHour(v.hour).ToDateTimeString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}
func TestCarbon_SetMinute(t *testing.T) {
Tests := []struct {
input string // 输入值
minute int // 输入参数
output string // 期望输出值
}{
{"2020-08-05 13:14:15", 10, "2020-08-05 13:10:15"},
{"2020-08-05 13:14:15", 60, "2020-08-05 14:00:15"},
}
for _, v := range Tests {
output := Parse(v.input).SetMinute(v.minute).ToDateTimeString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}
func TestCarbon_SetSecond(t *testing.T) {
Tests := []struct {
input string // 输入值
second int // 输入参数
output string // 期望输出值
}{
{"2020-08-05 13:14:15", 10, "2020-08-05 13:14:10"},
{"2020-08-05 13:14:15", 60, "2020-08-05 13:15:00"},
}
for _, v := range Tests {
output := Parse(v.input).SetSecond(v.second).ToDateTimeString()
if output != v.output {
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
}
}
}