-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
setter_test.go
executable file
·308 lines (261 loc) · 8.28 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
package carbon
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCarbon_SetTimezone(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
timezone string // 输入参数
expected string // 期望值
}{
{1, "2020-08-05 13:14:15", PRC, "2020-08-05 13:14:15"},
{2, "2020-08-05", Tokyo, "2020-08-05 00:00:00"},
}
for _, test := range tests {
c := SetTimezone(test.timezone).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetTimezone(t *testing.T) {
timezone := "xxx"
c := SetTimezone(timezone)
assert.Equal(t, invalidTimezoneError(timezone), c.Error, "Should catch an exception in SetTimezone()")
}
func TestCarbon_SetLanguage(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
"seasons": "spring|summer|autumn|winter",
}
if lang.SetLocale("en") == nil {
lang.SetResources(resources)
}
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
expected string // 期望值
}{
{1, "", ""},
{2, "2020-08-05", "summer"},
}
for _, test := range tests {
c := Parse(test.input).SetLanguage(lang)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Season(), "Current test id is "+strconv.Itoa(test.id))
}
for _, test := range tests {
c := SetLanguage(lang).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Season(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestCarbon_SetYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
year int // 输入参数
expected string // 期望值
}{
{1, "2020-01-01", 2019, "2019-01-01"},
{2, "2020-01-31", 2019, "2019-01-31"},
{3, "2020-02-01", 2019, "2019-02-01"},
{4, "2020-02-28", 2019, "2019-02-28"},
{5, "2020-02-29", 2019, "2019-03-01"},
}
for _, test := range tests {
c := Parse(test.input).SetYear(test.year)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetYear(t *testing.T) {
value, year := "2020-08-50", 2020
c := Parse(value).SetYear(year)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
month int // 输入参数
expected string // 期望值
}{
{1, "2020-01-01", 2, "2020-02-01"},
{2, "2020-01-30", 2, "2020-03-01"},
{3, "2020-01-31", 2, "2020-03-02"},
{4, "2020-08-05", 2, "2020-02-05"},
}
for _, test := range tests {
c := Parse(test.input).SetMonth(test.month)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetMonth(t *testing.T) {
value, month := "2020-08-50", 12
c1 := Parse(value).SetMonth(month)
assert.Equal(t, invalidValueError(value), c1.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetDay(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
day int // 输入参数
expected string // 期望值
}{
{1, "2020-01-01", 31, "2020-01-31"},
{2, "2020-02-01", 31, "2020-03-02"},
{3, "2020-02-28", 31, "2020-03-02"},
{4, "2020-02-29", 31, "2020-03-02"},
}
for _, test := range tests {
c := Parse(test.input).SetDay(test.day)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetDay(t *testing.T) {
value, day := "2020-08-50", 30
c := Parse(value).SetDay(day)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetHour(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
hour int // 输入参数
expected string // 期望值
}{
{1, "2020-08-05 13:14:15", 10, "2020-08-05 10:14:15"},
{2, "2020-08-05 13:14:15", 24, "2020-08-06 00:14:15"},
}
for _, test := range tests {
c := Parse(test.input).SetHour(test.hour)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetHour(t *testing.T) {
value, hour := "2020-08-50", 12
c := Parse(value).SetHour(hour)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetMinute(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
minute int // 输入参数
expected string // 期望值
}{
{1, "2020-08-05 13:14:15", 10, "2020-08-05 13:10:15"},
{2, "2020-08-05 13:14:15", 60, "2020-08-05 14:00:15"},
}
for _, test := range tests {
c := Parse(test.input).SetMinute(test.minute)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetMinute(t *testing.T) {
value, minute := "2020-08-50", 30
c := Parse(value).SetMinute(minute)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetSecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
second int // 输入参数
expected int // 期望值
}{
{1, "2020-08-05 13:14:15", 10, 10},
{2, "2020-08-05 13:14:15", 59, 59},
}
for _, test := range tests {
c := Parse(test.input).SetSecond(test.second)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Second(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetSecond(t *testing.T) {
value, second := "2020-08-50", 30
c := Parse(value).SetSecond(second)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetMillisecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
millisecond int // 输入参数
expected int // 期望值
}{
{1, "2020-08-05 13:14:15", 100, 100},
{2, "2020-08-05 13:14:15", 999, 999},
}
for _, test := range tests {
c := Parse(test.input).SetMillisecond(test.millisecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Millisecond(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetMillisecond(t *testing.T) {
value, millisecond := "2020-08-50", 100
c := Parse(value).SetMillisecond(millisecond)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetMicrosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
microsecond int // 输入参数
expected int // 期望值
}{
{1, "2020-08-05 13:14:15", 100000, 100000},
{2, "2020-08-05 13:14:15", 999999, 999999},
}
for _, test := range tests {
c := Parse(test.input).SetMicrosecond(test.microsecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Microsecond(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetMicrosecond(t *testing.T) {
value, microsecond := "2020-08-50", 100000
c := Parse(value).SetMicrosecond(microsecond)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}
func TestCarbon_SetNanosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
id int // 测试id
input string // 输入值
nanosecond int // 输入参数
expected int // 期望值
}{
{1, "2020-08-05 13:14:15", 100000000, 100000000},
{2, "2020-08-05 13:14:15", 999999999, 999999999},
}
for _, test := range tests {
c := Parse(test.input).SetNanosecond(test.nanosecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.Nanosecond(), "Current test id is "+strconv.Itoa(test.id))
}
}
func TestError_SetNanosecond(t *testing.T) {
value, nanosecond := "2020-08-50", 100000000
c := Parse(value).SetNanosecond(nanosecond)
assert.Equal(t, invalidValueError(value), c.Error, "Should catch an exception in SetYear()")
}