-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_test.go
More file actions
649 lines (566 loc) · 12 KB
/
assert_test.go
File metadata and controls
649 lines (566 loc) · 12 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package assert
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/fs"
"strings"
"testing"
"time"
)
func Test_equal(t *testing.T) {
type T struct {
t *time.Time
ts []*time.Time
}
True(t, equal[*int](nil, nil))
False(t, equal[any](nil, 0))
True(t, equal(0, 0))
False(t, equal(1, 0))
True(t, equal([]byte("hello"), []byte("hello")))
False(t, equal([]byte("hello"), []byte("-")))
//
// interface Equal(V) bool
now := time.Now()
now1 := now.Add(1)
utc := now.In(time.UTC)
wlc := Must(time.LoadLocation("Europe/Warsaw"))
waw := now.In(wlc)
True(t, equal(utc, waw))
True(t, equal(&utc, &waw))
True(t, equal(
[]*T{{t: &utc}},
[]*T{{t: &waw}},
))
True(t, equal(
&T{t: &utc},
&T{t: &waw},
))
True(t, equal(
&T{t: &utc, ts: []*time.Time{&utc}},
&T{t: &waw, ts: []*time.Time{&waw}},
))
True(t, equal(
[]T{{t: &utc, ts: []*time.Time{&utc}}},
[]T{{t: &waw, ts: []*time.Time{&waw}}},
))
False(t, equal(
[]T{{t: &utc, ts: []*time.Time{&utc}}},
[]T{{t: &waw, ts: []*time.Time{&now1}}},
))
False(t, equal(now, time.Time{}))
// dereference pointers
a, b := 0, 0
True(t, equal(&a, &b))
True(t, equal([]*int{&a}, []*int{&b}))
}
func TestEqual(t *testing.T) {
atb := &assertTB{TB: t}
Equal(atb, 0, 0)
atb.pass(t)
atb = &assertTB{TB: t}
Equal(atb, time.Now(), time.Time{})
atb.fail(t, "expected equal")
atb = &assertTB{TB: t}
Equal(atb, bytes.NewReader([]byte("a")), bytes.NewReader(nil))
atb.fail(t, "expected equal")
Panic(t, func() {
Equal(t, fmt.Errorf("0"), fmt.Errorf("0"))
})
}
func TestEqualUnexported(t *testing.T) {
type T struct {
A int
b int
}
atb := &assertTB{TB: t}
got := T{A: 1, b: 2}
want := T{A: 1, b: 3}
Equal(atb, got, want, IgnoreUnexported())
atb.pass(t)
}
func TestEqualSkipEmptyFields(t *testing.T) {
type T struct {
A int
b int
C time.Time
D []int
E [0]int
F [16]uint8
}
atb := &assertTB{TB: t}
got := T{
A: 1,
b: 2,
C: time.Now(),
D: []int{1},
F: [16]uint8{1},
}
want := T{
b: 2,
D: []int{},
}
Equal(atb, got, want, SkipEmptyFields())
atb.pass(t)
}
func TestEqualSkipZeroFields(t *testing.T) {
type T struct {
A int
b int
C time.Time
D []int
E [0]int
F [16]uint8
}
atb := &assertTB{TB: t}
got := T{
A: 1,
b: 2,
C: time.Now(),
D: []int{1},
F: [16]uint8{1},
}
want := T{
b: 2,
C: time.Time{}.In(time.Local),
}
Equal(atb, got, want, SkipZeroFields())
atb.pass(t)
}
func TestEqualIgnoreFieldNames(t *testing.T) {
type T0 struct {
E int
F struct {
G int
}
}
type T struct {
A int
B int
C time.Time
D T0
h int
}
got := T{
A: 1,
B: 2,
D: T0{
E: 3,
F: struct{ G int }{5},
},
h: 1,
}
want := T{
B: 2,
D: T0{
E: 3,
},
}
atb := &assertTB{TB: t}
Equal(atb, got, want, SkipFieldNames("A", "C", "D.F.G", "h"))
atb.pass(t)
atb = &assertTB{TB: t}
Equal(atb, &got, &want, SkipFieldNames("A", "C", "D.F.G", "h"))
atb.pass(t)
// non-existing field name
atb = &assertTB{TB: t}
Panic(t, func() {
Equal(atb, got, want, SkipFieldNames("X"))
})
// empty field name
atb = &assertTB{TB: t}
Panic(t, func() {
Equal(atb, got, want, SkipFieldNames(""))
})
// non-struct type
atb = &assertTB{TB: t}
Panic(t, func() {
m := map[string]int{"A": 1}
Equal(atb, m, m, SkipFieldNames("A"))
})
}
func TestNotEqual(t *testing.T) {
atb := &assertTB{TB: t}
NotEqual(atb, 0, 1)
atb.pass(t)
atb = &assertTB{TB: t}
NotEqual(atb, 0, 0)
atb.fail(t, "expected not equal, but got equal")
Panic(t, func() {
NotEqual(t, fmt.Errorf("0"), fmt.Errorf("0"))
})
}
func TestError(t *testing.T) {
atb := &assertTB{TB: t}
Error(atb, fmt.Errorf("0"))
atb.pass(t)
atb = &assertTB{TB: t}
Error(atb, nil)
atb.fail(t, "expected error, got nil")
}
func TestNoError(t *testing.T) {
atb := &assertTB{TB: t}
NoError(atb, nil)
atb.pass(t)
atb = &assertTB{TB: t}
NoError(atb, fmt.Errorf("0"))
atb.fail(t, "unexpected error: 0")
}
func TestErrorContains(t *testing.T) {
err := fmt.Errorf(
"closed socket: %w %w",
io.EOF,
&fs.PathError{Op: "read", Path: "socket", Err: io.ErrClosedPipe},
)
tests := []struct {
name string
err error
target any
fail string
}{
{
name: "string match",
err: err,
target: "closed socket:",
},
{
name: "string regex match",
err: err,
target: "closed socket: .*",
},
{
name: "error match",
err: err,
target: io.EOF,
},
{
name: "error wrap",
err: err,
target: io.ErrClosedPipe,
},
{
name: "fs.PathError match",
err: err,
target: func() **fs.PathError {
var pathError *fs.PathError
return &pathError
}(),
},
{
name: "nil error",
err: nil,
target: "",
fail: "error is nil",
},
{
name: "string not found",
err: err,
target: "open socket",
fail: "unexpected error:",
},
{
name: "error not found",
err: err,
target: io.ErrNoProgress,
fail: "unexpected error:",
},
{
name: "json.SyntaxError not found",
err: err,
target: func() **json.SyntaxError {
var jsonError *json.SyntaxError
return &jsonError
}(),
fail: "unexpected error:",
},
{
name: "string not found in error",
err: err,
target: "[",
fail: "does not contain \"[\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
atb := &assertTB{TB: t}
ErrorContains(atb, tt.err, tt.target)
atb.check(t, tt.fail)
})
}
}
func TestErrorWant(t *testing.T) {
atb := &assertTB{TB: t}
ErrorWant(atb, true, fmt.Errorf("0"))
atb.pass(t)
atb = &assertTB{TB: t}
ErrorWant(atb, false, nil)
atb.pass(t)
atb = &assertTB{TB: t}
ErrorWant(atb, true, nil)
atb.fail(t, "expected error: got nil")
atb = &assertTB{TB: t}
ErrorWant(atb, false, fmt.Errorf("0"))
atb.fail(t, "unexpected error: 0")
}
func TestNil(t *testing.T) {
atb := &assertTB{TB: t}
Nil(atb, nil)
atb.pass(t)
atb = &assertTB{TB: t}
Nil(atb, map[string]int(nil))
atb.pass(t)
atb = &assertTB{TB: t}
Nil(atb, chan int(nil))
atb.pass(t)
atb = &assertTB{TB: t}
Nil(atb, 0)
atb.fail(t, "expected nil, got 0")
Panic(t, func() {
Nil(t, fmt.Errorf("0"))
})
}
func TestNotNil(t *testing.T) {
atb := &assertTB{TB: t}
NotNil(atb, 0)
atb.pass(t)
atb = &assertTB{TB: t}
NotNil(atb, nil)
atb.fail(t, "expected not nil, got nil")
Panic(t, func() {
NotNil(t, fmt.Errorf("0"))
})
}
func TestZero(t *testing.T) {
tests := []struct {
value any
fail string
}{
{value: nil, fail: ""},
{value: time.Time{}, fail: ""},
{value: time.Time{}.In(time.Local), fail: ""},
{value: 0, fail: ""},
{value: .0, fail: ""},
{value: make(chan int), fail: "expected zero, got 0x"},
{value: map[string]string(nil), fail: ""},
{value: make(map[string]string), fail: "expected zero, got map[]"},
{value: []int(nil), fail: ""},
{value: []int{}, fail: "expected zero, got []"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%T", tt.value), func(t *testing.T) {
atb := &assertTB{TB: t}
Zero(atb, tt.value)
atb.check(t, tt.fail)
})
}
}
func TestNotZero(t *testing.T) {
tests := []struct {
value any
fail string
}{
{value: nil, fail: "expected not zero, got <nil>"},
{value: time.Time{}, fail: "expected not zero, got 0001-01-01 00:00:00 +0000 UTC"},
{value: time.Time{}.In(time.Local), fail: "expected not zero, got 0001-01"},
{value: 0, fail: "expected not zero, got 0"},
{value: .0, fail: "expected not zero, got 0"},
{value: make(chan int), fail: ""},
{value: map[string]string(nil), fail: "expected not zero, got map[]"},
{value: make(map[string]string), fail: ""},
{value: []int(nil), fail: "expected not zero, got []"},
{value: []int{}, fail: ""},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%T", tt.value), func(t *testing.T) {
atb := &assertTB{TB: t}
NotZero(atb, tt.value)
atb.check(t, tt.fail)
})
}
}
func TestEmpty(t *testing.T) {
tests := []struct {
value any
fail string
}{
{value: nil, fail: ""},
{value: time.Time{}, fail: ""},
{value: time.Time{}.In(time.Local), fail: "expected empty, got 0001-01-01"},
{value: 0, fail: ""},
{value: .0, fail: ""},
{value: make(chan int), fail: ""},
{value: map[string]string(nil), fail: ""},
{value: make(map[string]string), fail: ""},
{value: []int(nil), fail: ""},
{value: []int{}, fail: ""},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%T", tt.value), func(t *testing.T) {
atb := &assertTB{TB: t}
Empty(atb, tt.value)
atb.check(t, tt.fail)
})
}
}
func TestNotEmpty(t *testing.T) {
tests := []struct {
value any
fail string
}{
{value: nil, fail: "expected not empty"},
{value: time.Time{}, fail: "expected not empty"},
{value: time.Time{}.In(time.Local), fail: ""},
{value: 0, fail: "expected not empty"},
{value: .0, fail: "expected not empty"},
{value: make(chan int), fail: "expected not empty"},
{value: map[string]string(nil), fail: "expected not empty"},
{value: make(map[string]string), fail: "expected not empty"},
{value: []int(nil), fail: "expected not empty"},
{value: []int{}, fail: "expected not empty"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%T", tt.value), func(t *testing.T) {
atb := &assertTB{TB: t}
NotEmpty(atb, tt.value)
atb.check(t, tt.fail)
})
}
}
func TestLen(t *testing.T) {
atb := &assertTB{TB: t}
Len(atb, []int{1, 2, 3}, 3)
atb.pass(t)
atb = &assertTB{TB: t}
Len(atb, [3]int{1, 2, 3}, 3)
atb.pass(t)
atb = &assertTB{TB: t}
Len(atb, map[int]int{1: 1}, 1)
atb.pass(t)
atb = &assertTB{TB: t}
Len(atb, "hello", 5)
atb.pass(t)
atb = &assertTB{TB: t}
Len(atb, make(chan int), 0)
atb.pass(t)
atb = &assertTB{TB: t}
Len(atb, []int{1, 2, 3}, 2)
atb.fail(t, "expected length 2, got 3")
}
func TestTrue(t *testing.T) {
atb := &assertTB{TB: t}
True(atb, true)
atb.pass(t)
atb = &assertTB{TB: t}
True(atb, false)
atb.fail(t, "expected true, got false")
}
func TestFalse(t *testing.T) {
atb := &assertTB{TB: t}
False(atb, false)
atb.pass(t)
atb = &assertTB{TB: t}
False(atb, true)
atb.fail(t, "expected false, got true")
}
func TestPanic(t *testing.T) {
atb := &assertTB{TB: t}
Panic(atb, func() { panic(0) })
atb.pass(t)
atb = &assertTB{TB: t}
Panic(atb, func() {})
atb.fail(t, "expected panic, got nothing")
}
func TestNotPanic(t *testing.T) {
atb := &assertTB{TB: t}
NotPanic(atb, func() {})
atb.pass(t)
atb = &assertTB{TB: t}
NotPanic(atb, func() { panic(0) })
atb.fail(t, "unexpected panic: 0")
}
func TestDefer(t *testing.T) {
atb := &assertTB{TB: t}
func() {
defer Defer(atb, func() error { return nil })
}()
atb.pass(t)
atb = &assertTB{TB: t}
func() {
fn := func() error { return fmt.Errorf("0") }
defer Defer(atb, fn)()
}()
atb.fail(t, "unexpected defer error: 0")
}
func TestTypeAssert(t *testing.T) {
atb := &assertTB{TB: t}
TypeAssert[int](atb, 0)
atb.pass(t)
atb = &assertTB{TB: t}
TypeAssert[io.Reader](atb, &bytes.Buffer{})
atb.pass(t)
atb = &assertTB{TB: t}
TypeAssert[string](atb, 0)
atb.fail(t, "assertion int.(string) failed")
}
func TestMust(t *testing.T) {
Panic(t, func() {
Must(0, fmt.Errorf("err"))
})
}
func TestMust2(t *testing.T) {
Panic(t, func() {
Must2("", 0, fmt.Errorf("err"))
})
}
func TestMust3(t *testing.T) {
Panic(t, func() {
Must3(true, "", 0, fmt.Errorf("err"))
})
}
type assertTB struct {
testing.TB
helper bool
failed bool
format string
args []any
message string
}
func (atb *assertTB) Helper() {
atb.helper = true
}
func (atb *assertTB) Fatalf(format string, args ...any) {
atb.failed = true
atb.format = format
atb.args = args
atb.message = fmt.Sprintf(format, args...)
}
func (atb *assertTB) check(t testing.TB, fail string) {
t.Helper()
if fail != "" {
atb.fail(t, fail)
} else {
atb.pass(t)
}
}
func (atb *assertTB) pass(t testing.TB) {
t.Helper()
if !atb.helper {
t.Fatal("Helper not called")
}
if atb.failed {
t.Fatalf("expected pass, got failed")
}
}
func (atb *assertTB) fail(t testing.TB, message string) {
t.Helper()
if !atb.helper {
t.Fatal("Helper not called")
}
if !atb.failed {
t.Fatalf("expected failed, got pass")
}
if !strings.Contains(atb.message, message) {
t.Fatalf("expected message %q, got %q", message, atb.message)
}
}