forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter_test.go
executable file
·453 lines (379 loc) · 12.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package carbon
import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
func TestCarbon_SetTimezone(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
timezone string // 输入参数
expected string // 期望值
}{
{"0000-00-00 00:00:00", PRC, ""},
{"2020-08-05 13:14:15", PRC, "2020-08-05 13:14:15"},
{"2020-08-05 13:14:15", Tokyo, "2020-08-05 12:14:15"},
{"2020-08-05 13:14:15", London, "2020-08-05 20:14:15"},
}
for index, test := range tests {
c := SetTimezone(test.timezone).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(PRC), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetTimezone(t *testing.T) {
timezone := "xxx"
c1 := SetTimezone(timezone)
assert.NotNil(t, c1.Error, "It should catch an exception in SetTimezone()")
c2 := Now(timezone).SetTimezone(timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in SetTimezone()")
}
func TestCarbon_SetLocale(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
locale string // 输入参数
expected string // 期望值
}{
{"0000-00-00", "en", ""},
{"2020-08-05", "en", "August"},
{"2020-08-05", "jp", "はちがつ"},
{"2020-08-05", "zh-CN", "八月"},
}
for index, test := range tests {
c := SetLocale(test.locale).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToMonthString(PRC), "Current test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := Parse(test.input).SetLocale(test.locale)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToMonthString(PRC), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetLocale(t *testing.T) {
locale := "xxx"
c1 := SetLocale(locale)
assert.NotNil(t, c1.Error, "It should catch an exception in SetLocale()")
c2 := SetLocale(locale).SetLocale(PRC)
assert.NotNil(t, c2.Error, "It should catch an exception in SetLocale()")
}
func TestCarbon_SetLanguage(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
"seasons": "spring|summer|autumn|winter",
}
lang.SetLocale("en")
if lang.Error == nil {
lang.SetResources(resources)
}
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"2020-08-05", "summer"},
}
for index, test := range tests {
c := Parse(test.input).SetLanguage(lang)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Season(), "Current test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := SetLanguage(lang).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Season(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetLanguage(t *testing.T) {
lang, locale := NewLanguage(), "xxx"
lang.SetLocale(locale)
c1 := SetLanguage(lang)
assert.NotNil(t, c1.Error, "It should catch an exception in SetLanguage()")
c2 := SetLocale(locale).SetLanguage(lang)
assert.NotNil(t, c2.Error, "It should catch an exception in SetLanguage()")
}
func TestCarbon_SetYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year int // 输入参数
expected 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 index, test := range tests {
c := Parse(test.input).SetYear(test.year)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetYearNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year int // 输入参数
expected 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-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SetYearNoOverflow(test.year)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetYear(t *testing.T) {
value, year := "2020-08-50", 2020
c := Parse(value).SetYear(year)
assert.NotNil(t, c.Error, "It should catch an exception in SetYear()")
}
func TestError_SetYearNoOverflow(t *testing.T) {
value, year := "2020-08-50", 2020
c := Parse(value).SetYearNoOverflow(year)
assert.NotNil(t, c.Error, "It should catch an exception in SetYearNoOverflow()")
}
func TestCarbon_SetMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
month int // 输入参数
expected 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 index, test := range tests {
c := Parse(test.input).SetMonth(test.month)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetMonthNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
month int // 输入参数
expected string // 期望值
}{
{"2020-01-01", 2, "2020-02-01"},
{"2020-01-30", 2, "2020-02-29"},
{"2020-01-31", 2, "2020-02-29"},
{"2020-08-05", 2, "2020-02-05"},
}
for index, test := range tests {
c := Parse(test.input).SetMonthNoOverflow(test.month)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetMonth(t *testing.T) {
value, month := "2020-08-50", 12
c := Parse(value).SetMonth(month)
assert.NotNil(t, c.Error, "It should catch an exception in SetMonth()")
}
func TestError_SetMonthNoOverflow(t *testing.T) {
value, month := "2020-08-50", 12
c := Parse(value).SetMonthNoOverflow(month)
assert.NotNil(t, c.Error, "It should catch an exception in SetMonthNoOverflow()")
}
func TestCarbon_SetWeekStartsAt(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
week string // 输入参数
expected string // 期望值
}{
{"", Sunday, ""},
{"0000-00-00 00:00:00", Sunday, ""},
{"", Monday, ""},
{"0000-00-00 00:00:00", Monday, ""},
{"2021-06-13", Sunday, "2021-06-13 00:00:00"},
{"2021-06-14", Sunday, "2021-06-13 00:00:00"},
{"2021-06-18", Sunday, "2021-06-13 00:00:00"},
{"2021-06-13", Monday, "2021-06-07 00:00:00"},
{"2021-06-14", Monday, "2021-06-14 00:00:00"},
{"2021-06-18", Monday, "2021-06-14 00:00:00"},
{"2021-06-13", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-14", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-18", Tuesday, "2021-06-15 00:00:00"},
{"2021-06-13", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-14", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-18", Wednesday, "2021-06-16 00:00:00"},
{"2021-06-13", Thursday, "2021-06-10 00:00:00"},
{"2021-06-14", Thursday, "2021-06-10 00:00:00"},
{"2021-06-18", Thursday, "2021-06-17 00:00:00"},
{"2021-06-13", Friday, "2021-06-11 00:00:00"},
{"2021-06-14", Friday, "2021-06-11 00:00:00"},
{"2021-06-18", Friday, "2021-06-18 00:00:00"},
{"2021-06-13", Saturday, "2021-06-12 00:00:00"},
{"2021-06-14", Saturday, "2021-06-12 00:00:00"},
{"2021-06-18", Saturday, "2021-06-12 00:00:00"},
}
for index, test := range tests {
c := Parse(test.input).SetWeekStartsAt(test.week).StartOfWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetDay(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
day int // 输入参数
expected 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 index, test := range tests {
c := Parse(test.input).SetDay(test.day)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetDay(t *testing.T) {
value, day := "2020-08-50", 30
c := Parse(value).SetDay(day)
assert.NotNil(t, c.Error, "It should catch an exception in SetDay()")
}
func TestCarbon_SetHour(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
hour int // 输入参数
expected 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 index, test := range tests {
c := Parse(test.input).SetHour(test.hour)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetHour(t *testing.T) {
value, hour := "2020-08-50", 12
c := Parse(value).SetHour(hour)
assert.NotNil(t, c.Error, "It should catch an exception in SetHour()")
}
func TestCarbon_SetMinute(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
minute int // 输入参数
expected 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 index, test := range tests {
c := Parse(test.input).SetMinute(test.minute)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetMinute(t *testing.T) {
value, minute := "2020-08-50", 30
c := Parse(value).SetMinute(minute)
assert.NotNil(t, c.Error, "It should catch an exception in SetMinute()")
}
func TestCarbon_SetSecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
second int // 输入参数
expected int // 期望值
}{
{"2020-08-05 13:14:15", 10, 10},
{"2020-08-05 13:14:15", 59, 59},
}
for index, test := range tests {
c := Parse(test.input).SetSecond(test.second)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Second(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetSecond(t *testing.T) {
value, second := "2020-08-50", 30
c := Parse(value).SetSecond(second)
assert.NotNil(t, c.Error, "It should catch an exception in SetSecond()")
}
func TestCarbon_SetMillisecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
millisecond int // 输入参数
expected int // 期望值
}{
{"2020-08-05 13:14:15", 100, 100},
{"2020-08-05 13:14:15", 999, 999},
}
for index, test := range tests {
c := Parse(test.input).SetMillisecond(test.millisecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Millisecond(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetMillisecond(t *testing.T) {
value, millisecond := "2020-08-50", 100
c := Parse(value).SetMillisecond(millisecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetMillisecond()")
}
func TestCarbon_SetMicrosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
microsecond int // 输入参数
expected int // 期望值
}{
{"2020-08-05 13:14:15", 100000, 100000},
{"2020-08-05 13:14:15", 999999, 999999},
}
for index, test := range tests {
c := Parse(test.input).SetMicrosecond(test.microsecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Microsecond(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetMicrosecond(t *testing.T) {
value, microsecond := "2020-08-50", 100000
c := Parse(value).SetMicrosecond(microsecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetMicrosecond()")
}
func TestCarbon_SetNanosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
nanosecond int // 输入参数
expected int // 期望值
}{
{"2020-08-05 13:14:15", 100000000, 100000000},
{"2020-08-05 13:14:15", 999999999, 999999999},
}
for index, test := range tests {
c := Parse(test.input).SetNanosecond(test.nanosecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Nanosecond(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_SetNanosecond(t *testing.T) {
value, nanosecond := "2020-08-50", 100000000
c := Parse(value).SetNanosecond(nanosecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetNanosecond()")
}