-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtime_test.go
355 lines (337 loc) · 6.71 KB
/
time_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
package dt
import (
"encoding/json"
"testing"
"time"
)
func TestTimeOf(t *testing.T) {
time := time.Date(2014, 8, 20, 15, 8, 43, 1, time.Local)
want := Time{15, 8, true}
if got := TimeOf(time); got != want {
t.Errorf("TimeOf(%v) = %+v, want %+v", time, got, want)
}
}
func TestTimeToString(t *testing.T) {
cases := []struct {
name string
req string
want Time
wantErr bool
}{
{
name: "Hours and minutes",
req: "15:51",
want: Time{15, 51, true},
},
{
name: "Hours, minutes and seconds",
req: "16:52:33",
want: Time{16, 52, true},
},
{
name: "Invalid time format",
req: "33:33:33",
wantErr: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseTime(tt.req)
if (err != nil) != tt.wantErr {
t.Error("expected error and got error do not match")
}
if got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}
func TestTimeString(t *testing.T) {
cases := []struct {
name string
req Time
want string
}{{
name: "Valid date",
req: Time{15, 12, true},
want: "15:12",
},
{
name: "Invalid date",
req: Time{},
want: "",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := tt.req.String(); got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}
func TestTimeToDate(t *testing.T) {
got := Time{15, 20, true}.ToDate()
if want := time.Date(0, 0, 0, 15, 20, 0, 0, time.UTC); got != want {
t.Errorf("expected %v, got %v", want, got)
}
}
func TestAfter(t *testing.T) {
cases := []struct {
name string
t1, t2 Time
isAfter bool
}{
{
name: "Hour after",
t1: Time{23, 59, true},
t2: Time{22, 59, true},
isAfter: true,
},
{
name: "Minute after",
t1: Time{23, 59, true},
t2: Time{23, 45, true},
isAfter: true,
},
{
name: "Equal",
t1: Time{23, 59, true},
t2: Time{23, 59, true},
isAfter: false,
},
{
name: "Hour before",
t1: Time{22, 59, true},
t2: Time{23, 59, true},
isAfter: false,
},
{
name: "Minute before",
t1: Time{11, 59, true},
t2: Time{23, 59, true},
isAfter: false,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.t1.After(tt.t2) != tt.isAfter {
t.Errorf("expected isAfter %v, got %v", tt.isAfter, tt.t1.After(tt.t2))
}
})
}
}
func TestBefore(t *testing.T) {
cases := []struct {
name string
t1, t2 Time
isBefore bool
}{
{
name: "Hour after",
t1: Time{23, 59, true},
t2: Time{22, 59, true},
isBefore: false,
},
{
name: "Minute after",
t1: Time{23, 59, true},
t2: Time{23, 45, true},
isBefore: false,
},
{
name: "Equal",
t1: Time{23, 59, true},
t2: Time{23, 59, true},
isBefore: false,
},
{
name: "Hour before",
t1: Time{22, 59, true},
t2: Time{23, 59, true},
isBefore: true,
},
{
name: "Minute before",
t1: Time{11, 59, true},
t2: Time{23, 59, true},
isBefore: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.t1.Before(tt.t2) != tt.isBefore {
t.Errorf("expected isBefore %v, got %v", tt.isBefore, tt.t1.Before(tt.t2))
}
})
}
}
func TestSubtract(t *testing.T) {
cases := []struct {
name string
t1, t2 Time
diff int
}{
{
name: "Diff positive",
t1: Time{23, 59, true},
t2: Time{22, 59, true},
diff: 60,
},
{
name: "Diff negative",
t1: Time{23, 59, true},
t2: Time{23, 45, true},
diff: 14,
},
{
name: "No diff",
t1: Time{23, 59, true},
t2: Time{23, 59, true},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if tt.t1.Subtract(tt.t2) != tt.diff {
t.Errorf("expected diff %v, got %v", tt.diff, tt.t1.Subtract(tt.t2))
}
})
}
}
func TestMarshalTime(t *testing.T) {
tm := Time{15, 25, true}
bts, err := json.Marshal(tm)
if err != nil {
t.Errorf("expected success but got error: %v", err)
}
str := string(bts)
if exp := `"` + tm.String() + `"`; exp != str {
t.Errorf("expected %s but got %s", exp, str)
}
}
type timeReq struct {
T Time `json:"time"`
}
func TestUnmarshalTime(t *testing.T) {
cases := []struct {
name string
req []byte
want Time
}{
{
name: "Invalid date",
req: []byte(`"date":"ABCD"`),
},
{
name: "Valid date",
req: []byte(`{"time":"15:25"}`),
want: Time{15, 25, true},
},
{
name: "Valid date with sevonds",
req: []byte(`{"time":"15:25:35"}`),
want: Time{15, 25, true},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
var tm timeReq
err := json.Unmarshal(tt.req, &tm)
if (err == nil) != tt.want.Valid {
t.Errorf("expected time to be empty, got err: %v", err)
}
if tm.T != tt.want {
t.Errorf("expected time %v, got %v", tt.want, tm)
}
})
}
}
func TestValueTime(t *testing.T) {
cases := []struct {
name string
req Time
wantValue bool
}{{
name: "Valid time",
req: Time{15, 25, true},
wantValue: true,
},
{
name: "Invalid time",
req: Time{12, 91, false},
}}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
val, _ := tt.req.Value()
if tt.wantValue != (val != nil) {
t.Error("value returned different from expected")
}
})
}
}
func TestScanTime(t *testing.T) {
cases := []struct {
name string
value interface{}
want Time
wantErr bool
}{
{
name: "Nil value",
want: Time{},
},
{
name: "Bytes value",
value: []byte("15:25"),
want: Time{15, 25, true},
},
{
name: "Bytes error",
value: []byte("15:91"),
wantErr: true,
},
{
name: "String value",
value: "15:41",
want: Time{15, 41, true},
},
{
name: "String error",
value: "91:12",
wantErr: true,
},
{
name: "Invalid type",
value: 8,
wantErr: true,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
tm := &Time{}
err := tm.Scan(tt.value)
if (err != nil) != tt.wantErr {
t.Error("expected error and got error do not match")
}
if *tm != tt.want {
t.Errorf("expected %v, got %v", tt.want, *tm)
}
})
}
}
func TestTimeCompare(t *testing.T) {
for _, test := range []struct {
t1, t2 Time
want int
}{
{Time{12, 0, true}, Time{14, 0, true}, -1},
{Time{12, 20, true}, Time{12, 30, true}, -1},
{Time{14, 0, true}, Time{12, 0, true}, +1},
{Time{12, 30, true}, Time{12, 20, true}, +1},
{Time{12, 20, true}, Time{12, 20, true}, 0},
} {
if got := test.t1.Compare(test.t2); got != test.want {
t.Errorf("%v.Compare(%v): got %d, want %d", test.t1, test.t2, got, test.want)
}
}
}