-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertions_test.go
1187 lines (1025 loc) · 28 KB
/
assertions_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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gospec
import (
"bytes"
"errors"
"io"
"math"
"regexp"
"strings"
"testing"
"time"
)
var (
i interface{}
zeros = []interface{}{
false,
byte(0),
complex64(0),
complex128(0),
float32(0),
float64(0),
int(0),
int8(0),
int16(0),
int32(0),
int64(0),
rune(0),
uint(0),
uint8(0),
uint16(0),
uint32(0),
uint64(0),
uintptr(0),
"",
[0]interface{}{},
[]interface{}(nil),
struct{ x int }{},
(*interface{})(nil),
(func())(nil),
nil,
interface{}(nil),
map[interface{}]interface{}(nil),
(chan interface{})(nil),
(<-chan interface{})(nil),
(chan<- interface{})(nil),
}
nonZeros = []interface{}{
true,
byte(1),
complex64(1),
complex128(1),
float32(1),
float64(1),
int(1),
int8(1),
int16(1),
int32(1),
int64(1),
rune(1),
uint(1),
uint8(1),
uint16(1),
uint32(1),
uint64(1),
uintptr(1),
"s",
[1]interface{}{1},
[]interface{}{},
struct{ x int }{1},
(*interface{})(&i),
(func())(func() {}),
interface{}(1),
map[interface{}]interface{}{},
(chan interface{})(make(chan interface{})),
(<-chan interface{})(make(chan interface{})),
(chan<- interface{})(make(chan interface{})),
}
)
func TestIsType(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
expected, actual interface{}
}{
{"Hello", "world"},
{0, 0},
{0.0, 0.0},
{nil, nil},
{new(bytes.Buffer), bytes.NewBuffer(nil)},
}
for i, tc := range trueCases {
True(t, IsType(mockT, tc.expected, tc.actual), "IsType should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual interface{}
}{
{new(bytes.Reader), new(bytes.Buffer)},
{nil, 0},
{0, nil},
{0, 0.0},
{0.0, 0},
}
for i, fc := range falseCases {
False(t, IsType(mockT, fc.expected, fc.actual), "IsType should return false for false cashe(%d)", i)
}
}
func TestImplements(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
iface, value interface{}
}{
{(*io.Reader)(nil), new(bytes.Buffer)},
}
for i, tc := range trueCases {
True(t, Implements(mockT, tc.iface, tc.value), "Implements should return true for trueCases(%d)", i)
}
falseCases := []struct {
iface, value interface{}
}{
{(*io.Reader)(nil), nil},
{(*io.WriteCloser)(nil), new(bytes.Buffer)},
{(*io.WriteCloser)(nil), nil},
{nil, nil},
}
for i, fc := range falseCases {
False(t, Implements(mockT, fc.iface, fc.value), "Implements should return false for falseCases(%d)", i)
}
}
func TestEqual(t *testing.T) {
mockT := new(testing.T)
funcA := func() int { return 23 }
funcB := func() int { return 23 }
trueCases := []struct {
expected, actual interface{}
}{
{"Hello World", "Hello World"},
{[]byte("Hello World"), []byte("Hello World")},
{0, 0},
{0.0, 0.0},
{nil, nil},
{int32(0), int32(0)},
{uint64(0), uint64(0)},
{new(bytes.Buffer), new(bytes.Buffer)},
{&struct{}{}, &struct{}{}},
{funcA, funcA},
}
for i, tc := range trueCases {
True(t, Equal(mockT, tc.expected, tc.actual), "Equal should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual interface{}
}{
{"Hello, world!", ""},
{"", "Hello, world!"},
{"Hello World", []byte("Hello World")},
{0, 0.0},
{0.0, 0},
{nil, 0},
{0, nil},
{map[string]string{}, "something"},
{funcA, funcB},
{map[string]string{"foo": "bar"}, "foo"},
{map[string]string{"foo": "bar"}, "bar"},
}
for i, fc := range falseCases {
False(t, Equal(mockT, fc.expected, fc.actual), "Equal should return false for falseCases(%d)", i)
}
}
func TestEqualFormatting(t *testing.T) {
for i, currCase := range []struct {
expected string
actual string
extras []interface{}
message string
}{
{
expected: "want",
actual: "got",
message: "\tassertions.go:[0-9]+:\\s+?Error Trace:\t(\\S+:[0-9]+\\s+)+?Error:\tExpect to be equal\\s+Diff:\\s+--- Expected\\s+\\+\\+\\+ Actual\\s+?",
},
{expected: "want", actual: "got", extras: []interface{}{"Hello, %s", "world!"}, message: "\tassertions.go:[0-9]+:\\s+Hello, world!\\s+Error Trace:\t(\\S+:[0-9]+\\s+)+?Error:\tExpect to be equal\\s+Diff:\\s+--- Expected\\s+\\+\\+\\+ Actual\\s+?"},
} {
mockT := &gospec{}
Equal(mockT, currCase.expected, currCase.actual, currCase.extras...)
Match(t, regexp.MustCompile(currCase.message), mockT.String(), "Case %d", i)
}
}
func TestNotEqual(t *testing.T) {
mockT := new(testing.T)
funcA := func() int { return 23 }
funcB := func() int { return 23 }
trueCases := []struct {
expected, actual interface{}
}{
{"Hello, world!", ""},
{"", "Hello, world!"},
{"Hello World", []byte("Hello World")},
{0, 0.0},
{0.0, 0},
{nil, 0},
{0, nil},
{map[string]string{}, "something"},
{funcA, funcB},
{map[string]string{"foo": "bar"}, "foo"},
{map[string]string{"foo": "bar"}, "bar"},
}
for i, tc := range trueCases {
True(t, NotEqual(mockT, tc.expected, tc.actual), "NotEqual should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual interface{}
}{
{"Hello World", "Hello World"},
{[]byte("Hello World"), []byte("Hello World")},
{0, 0},
{0.0, 0.0},
{nil, nil},
{int32(0), int32(0)},
{uint64(0), uint64(0)},
{new(bytes.Buffer), new(bytes.Buffer)},
{&struct{}{}, &struct{}{}},
{funcA, funcA},
}
for i, fc := range falseCases {
False(t, NotEqual(mockT, fc.expected, fc.actual), "NotEqual should return false for falseCases(%d)", i)
}
}
func TestEqualValues(t *testing.T) {
mockT := new(testing.T)
funcA := func() int { return 23 }
funcB := func() int { return 23 }
trueCases := []struct {
expected, actual interface{}
}{
{"Hello World", "Hello World"},
{[]byte("Hello World"), []byte("Hello World")},
{"Hello World", []byte("Hello World")},
{[]byte("Hello World"), "Hello World"},
{0, 0},
{0.0, 0.0},
{0, 0.0},
{0.0, 0},
{nil, nil},
{int32(0), int32(0)},
{uint64(0), uint64(0)},
{new(bytes.Buffer), new(bytes.Buffer)},
{&struct{}{}, &struct{}{}},
{funcA, funcA},
}
for i, tc := range trueCases {
True(t, EqualValues(mockT, tc.expected, tc.actual), "EqualValues should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual interface{}
}{
{"Hello, world!", ""},
{"", "Hello, world!"},
{"Hello World", "Hello World!"},
{"Hello World!", "Hello World"},
{"Hello World", []byte("Hello World!")},
{[]byte("Hello World!"), "Hello World"},
{nil, 0},
{0, nil},
{map[string]string{}, "something"},
{funcA, funcB},
{map[string]string{"foo": "bar"}, "foo"},
{map[string]string{"foo": "bar"}, "bar"},
}
for i, fc := range falseCases {
False(t, EqualValues(mockT, fc.expected, fc.actual), "EqualValues should return false for falseCases(%d)", i)
}
}
func TestEqualJSON(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
expected, actual string
}{
{`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`},
{`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`},
{`{"numeric": 1.5,"array": [{"foo": "bar"}, 1, "string", ["nested", "array", 5.5]],"hash": {"nested": "hash", "nested_slice": ["this", "is", "nested"]},"string": "foo"}`,
`{"numeric": 1.5,"hash": {"nested": "hash", "nested_slice": ["this", "is", "nested"]},"string": "foo","array": [{"foo": "bar"}, 1, "string", ["nested", "array", 5.5]]}`},
{`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`},
}
for i, tc := range trueCases {
True(t, EqualJSON(mockT, tc.expected, tc.actual), "EqualJSON should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual string
}{
{`{"hello": "bar", "foo": "world"}`, `{"hello": "world", "foo": "bar"}`},
{`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`},
{`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`},
{`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`},
{`{"foo": "bar"}`, "Not JSON"},
{"Not JSON", `{"foo": "bar", "hello": "world"}`},
{"Not JSON", "Not JSON"},
}
for i, fc := range falseCases {
False(t, EqualJSON(mockT, fc.expected, fc.actual), "EqualJSON should return false for falseCases(%d)", i)
}
}
func TestExactly(t *testing.T) {
mockT := new(testing.T)
a := float32(1)
b := float64(1)
c := float32(1)
d := float32(2)
funcA := func() int { return 23 }
funcB := func() int { return 23 }
trueCases := []struct {
expected, actual interface{}
}{
{"Hello World", "Hello World"},
{[]byte("Hello World"), []byte("Hello World")},
{a, c},
{nil, nil},
{0, 0},
{0.0, 0.0},
}
for i, tc := range trueCases {
True(t, Exactly(mockT, tc.expected, tc.actual), "Exactly should return true for trueCases(%d)", i)
}
falseCases := []struct {
expected, actual interface{}
}{
{"Hello World", "Hello World!"},
{"Hello World!", "Hello World"},
{[]byte("Hello World"), []byte("Hello World!")},
{[]byte("Hello World!"), []byte("Hello World")},
{a, b},
{a, d},
{nil, a},
{a, nil},
{funcA, funcA},
{funcA, funcB},
}
for i, fc := range falseCases {
False(t, Exactly(mockT, fc.expected, fc.actual), "Exactly should return false for fase case(%d)", i)
}
}
func TestNil(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
value interface{}
}{
{nil},
{(*struct{})(nil)},
{(*io.Reader)(nil)},
}
for i, tc := range trueCases {
True(t, Nil(mockT, tc.value), "Nil should return true for trueCases(%d)", i)
}
falseCases := []struct {
value interface{}
}{
{""},
{0},
{0.0},
{new(bytes.Buffer)},
{func() int { return 23 }},
}
for i, fc := range falseCases {
False(t, Nil(mockT, fc.value), "Nil should return false for falseCases(%d)", i)
}
}
func TestNotNil(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
value interface{}
}{
{""},
{0},
{0.0},
{new(bytes.Buffer)},
{func() int { return 23 }},
}
for i, tc := range trueCases {
True(t, NotNil(mockT, tc.value), "NotNil should return false for trueCases(%d)", i)
}
falseCases := []struct {
value interface{}
}{
{nil},
{(*struct{})(nil)},
{(*io.Reader)(nil)},
}
for i, fc := range falseCases {
False(t, NotNil(mockT, fc.value), "NotNil should return true for falseCases(%d)", i)
}
}
func TestTrue(t *testing.T) {
mockT := new(testing.T)
if !True(mockT, true) {
t.Error("True should return true")
}
if True(mockT, false) {
t.Error("True should return false")
}
if True(mockT, 0) {
t.Error("True should return false")
}
if True(mockT, 0.0) {
t.Error("True should return false")
}
if True(mockT, nil) {
t.Error("True should return false")
}
if True(mockT, new(bytes.Buffer)) {
t.Error("True should return false")
}
if True(mockT, func() int { return 23 }) {
t.Error("True should return false")
}
}
func TestFalse(t *testing.T) {
mockT := new(testing.T)
if !False(mockT, false) {
t.Error("False should return true")
}
if False(mockT, true) {
t.Error("False should return false")
}
if False(mockT, 0) {
t.Error("False should return false")
}
if False(mockT, 0.0) {
t.Error("False should return false")
}
if False(mockT, nil) {
t.Error("False should return false")
}
if False(mockT, new(bytes.Buffer)) {
t.Error("False should return false")
}
if False(mockT, func() int { return 23 }) {
t.Error("False should return false")
}
}
func TestZero(t *testing.T) {
mockT := new(testing.T)
for i, v := range zeros {
True(t, Zero(mockT, v), "Zero should return true for zeros(%d)", i)
}
for i, v := range nonZeros {
False(t, Zero(mockT, v), "Zero should return false for nonZeros(%d)", i)
}
}
func TestNotZero(t *testing.T) {
mockT := new(testing.T)
for i, v := range nonZeros {
True(t, NotZero(mockT, v), "NotZero should return true for nonZeros(%d)", i)
}
for i, v := range zeros {
False(t, NotZero(mockT, v), "NotZero should return false for zeros(%d)", i)
}
}
func TestEmpty(t *testing.T) {
mockT := new(testing.T)
chWithValue := make(chan struct{}, 1)
chWithValue <- struct{}{}
chWithoutValue := make(chan struct{}, 1)
var (
sp *string
tp *time.Time
tnp time.Time
est = struct {
Name string
age int
}{}
estp = &est
st = struct {
Name string
age int
}{"", 1}
stp = &st
f func() int
)
True(t, Empty(mockT, ""), "Empty string is empty")
True(t, Empty(mockT, nil), "Nil is empty")
True(t, Empty(mockT, 0), "Zero int value is empty")
True(t, Empty(mockT, false), "False value is empty")
True(t, Empty(mockT, []string{}), "Empty string array is empty")
True(t, Empty(mockT, chWithoutValue), "Channel without values is empty")
True(t, Empty(mockT, sp), "Nil string pointer is empty")
True(t, Empty(mockT, tp), "Nil time.Time pointer is empty")
True(t, Empty(mockT, tnp), "time.Time is empty")
True(t, Empty(mockT, est), "Empty struct is empty")
True(t, Empty(mockT, estp), "Empty struct pointer is empty")
True(t, Empty(mockT, f), "Empty func type is empty")
False(t, Empty(mockT, "something"), "Non Empty string is not empty")
False(t, Empty(mockT, 1), "Non-zero int value is not empty")
False(t, Empty(mockT, true), "True value is not empty")
False(t, Empty(mockT, errors.New("something")), "Non nil object is not empty")
False(t, Empty(mockT, []string{"something"}), "Non empty string array is not empty")
False(t, Empty(mockT, st), "Struct value is not empty")
False(t, Empty(mockT, stp), "Struct pointer value is not empty")
False(t, Empty(mockT, chWithValue), "Channel with values is not empty")
False(t, Empty(mockT, func() int { return 23 }), "Func value is not empty")
}
func TestNotEmpty(t *testing.T) {
mockT := new(testing.T)
chWithValue := make(chan struct{}, 1)
chWithValue <- struct{}{}
chWithoutValue := make(chan struct{}, 1)
var (
sp *string
tp *time.Time
tnp time.Time
est = struct {
Name string
age int
}{}
estp = &est
st = struct {
Name string
age int
}{"", 1}
stp = &st
f func() int
)
False(t, NotEmpty(mockT, ""), "Empty string is empty")
False(t, NotEmpty(mockT, nil), "Nil is empty")
False(t, NotEmpty(mockT, 0), "Zero int value is empty")
False(t, NotEmpty(mockT, false), "False value is empty")
False(t, NotEmpty(mockT, []string{}), "Empty string array is empty")
False(t, NotEmpty(mockT, chWithoutValue), "Channel without values is empty")
False(t, NotEmpty(mockT, sp), "Nil string pointer is empty")
False(t, NotEmpty(mockT, tp), "Nil time.Time pointer is empty")
False(t, NotEmpty(mockT, tnp), "time.Time is empty")
False(t, NotEmpty(mockT, est), "Empty struct is empty")
False(t, NotEmpty(mockT, estp), "Empty struct pointer is empty")
False(t, NotEmpty(mockT, f), "Empty func type is empty")
True(t, NotEmpty(mockT, "something"), "Non Empty string is not empty")
True(t, NotEmpty(mockT, 1), "Non-zero int value is not empty")
True(t, NotEmpty(mockT, true), "True value is not empty")
True(t, NotEmpty(mockT, errors.New("something")), "Non nil object is not empty")
True(t, NotEmpty(mockT, []string{"something"}), "Non empty string array is not empty")
True(t, NotEmpty(mockT, st), "Struct value is not empty")
True(t, NotEmpty(mockT, stp), "Struct pointer value is not empty")
True(t, NotEmpty(mockT, chWithValue), "Channel with values is not empty")
True(t, NotEmpty(mockT, func() int { return 23 }), "Func value is not empty")
}
type kv struct {
Name, Value string
}
func TestContains(t *testing.T) {
mockT := new(testing.T)
list := []string{"Foo", "Bar"}
complexList := []*kv{
{"b", "c"},
{"d", "e"},
{"g", "h"},
{"j", "k"},
}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
ch := make(chan string, 1)
ch <- "Foo"
trueCases := []struct {
value, element interface{}
}{
{"Hello World", "Hello"},
{list, "Bar"},
{complexList, &kv{"g", "h"}},
{simpleMap, "Foo"},
{ch, "Foo"},
}
for i, tc := range trueCases {
True(t, Contains(mockT, tc.value, tc.element), "Contains should return true for trueCases(%d)", i)
}
falseCases := []struct {
value, element interface{}
}{
{"Hello World", "Salut"},
{nil, 0},
{0, nil},
{nil, 0.0},
{0.0, nil},
{nil, nil},
{0, 0.0},
{0.0, 0},
{list, "Salut"},
{complexList, &kv{"g", "e"}},
{simpleMap, "Bar"},
{ch, "Bar"},
{func() int { return 23 }, 23},
}
for i, fc := range falseCases {
False(t, Contains(mockT, fc.value, fc.element), "Contains should return false for falseCases(%d)", i)
}
}
func TestNotContains(t *testing.T) {
mockT := new(testing.T)
list := []string{"Foo", "Bar"}
complexList := []*kv{
{"b", "c"},
{"d", "e"},
{"g", "h"},
{"j", "k"},
}
simpleMap := map[interface{}]interface{}{"Foo": "Bar"}
ch := make(chan string, 1)
ch <- "Foo"
trueCases := []struct {
value, element interface{}
}{
{"Hello World", "Salut"},
{nil, 0},
{0, nil},
{nil, 0.0},
{0.0, nil},
{nil, nil},
{0, 0.0},
{0.0, 0},
{list, "Salut"},
{complexList, &kv{"g", "e"}},
{simpleMap, "Bar"},
{ch, "Bar"},
{func() int { return 23 }, 23},
}
for i, tc := range trueCases {
True(t, NotContains(mockT, tc.value, tc.element), "NotContains should return true for trueCases(%d)", i)
}
falseCases := []struct {
value, element interface{}
}{
{"Hello World", "Hello"},
{list, "Bar"},
{complexList, &kv{"g", "h"}},
{simpleMap, "Foo"},
{ch, "Foo"},
}
for i, fc := range falseCases {
False(t, NotContains(mockT, fc.value, fc.element), "NotContains should return false for falseCases(%d)", i)
}
}
func TestContainsForReader(t *testing.T) {
mockT := new(testing.T)
reader := strings.NewReader("Hello, World")
True(t, Contains(mockT, reader, "Hello"), "Contains should return true")
False(t, Contains(mockT, reader, "Salut"), "Contains should return false")
}
func TestNotContainsForReader(t *testing.T) {
mockT := new(testing.T)
reader := strings.NewReader("Hello, World")
True(t, NotContains(mockT, reader, "Salut"), "NotContains should return true")
False(t, NotContains(mockT, reader, "Hello"), "NotContains should return false")
}
func TestMatch(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
reg interface{}
values []interface{}
}{
{`^\d+$`,
[]interface{}{
0, 0.0, 1, 2.0,
}},
{regexp.MustCompile("^Hello"),
[]interface{}{
"Hello", "Hello, world!",
}},
}
for i, tc := range trueCases {
for j, v := range tc.values {
True(t, Match(mockT, tc.reg, v), "Match should return true for trueCases(%d, %d)", i, j)
}
}
falseCases := []struct {
reg interface{}
values []interface{}
}{
{`^\d+$`,
[]interface{}{
"", nil, true, false, new(bytes.Buffer),
}},
{regexp.MustCompile("^Hello"),
[]interface{}{
"hello, world!", "hi, Hello", nil, true, false, new(bytes.Buffer),
}},
}
for i, fc := range falseCases {
for j, v := range fc.values {
False(t, Match(mockT, fc.reg, v), "Match should return false for falseCases(%d, %d)", i, j)
}
}
}
func TestNotMatch(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
reg interface{}
values []interface{}
}{
{`^\d+$`,
[]interface{}{
"", nil, true, false, new(bytes.Buffer),
}},
{regexp.MustCompile("^Hello"),
[]interface{}{
"hello, world!", "hi, Hello", nil, true, false, new(bytes.Buffer),
}},
}
for i, tc := range trueCases {
for j, v := range tc.values {
True(t, NotMatch(mockT, tc.reg, v), "NotMatch should return true for trueCases(%d, %d)", i, j)
}
}
falseCases := []struct {
reg interface{}
values []interface{}
}{
{`^\d+$`,
[]interface{}{
0, 0.0, 1, 2.0,
}},
{regexp.MustCompile("^Hello"),
[]interface{}{
"Hello", "Hello, world!",
}},
}
for i, fc := range falseCases {
for j, v := range fc.values {
False(t, NotMatch(mockT, fc.reg, v), "NotMatch should return flase for falseCases(%d, %d)", i, j)
}
}
}
func TestCondition(t *testing.T) {
mockT := new(testing.T)
if !Condition(mockT, func() bool { return true }, "Truth") {
t.Error("Condition should return true")
}
if Condition(mockT, func() bool { return false }, "Lie") {
t.Error("Condition should return false")
}
}
func TestLen(t *testing.T) {
mockT := new(testing.T)
ch := make(chan int, 5)
ch <- 1
ch <- 2
ch <- 3
trueCases := []struct {
v interface{}
l int
}{
{[]int{1, 2, 3}, 3},
{[...]int{1, 2, 3}, 3},
{"ABC", 3},
{map[int]int{1: 2, 2: 4, 3: 6}, 3},
{ch, 3},
{[]int{}, 0},
{map[int]int{}, 0},
{make(chan int), 0},
{[]int(nil), 0},
{map[int]int(nil), 0},
{(chan int)(nil), 0},
}
for i, tc := range trueCases {
True(t, Len(mockT, tc.v, tc.l), "Len should return true for trueCase(%d)", i)
}
falseCases := []struct {
v interface{}
l int
}{
{[]int{1, 2, 3}, 4},
{[...]int{1, 2, 3}, 2},
{"ABC", 2},
{map[int]int{1: 2, 2: 4, 3: 6}, 4},
{ch, 2},
{[]int{}, 1},
{map[int]int{}, 1},
{make(chan int), 1},
{[]int(nil), 1},
{map[int]int(nil), 1},
{(chan int)(nil), 1},
{nil, 0},
{0, 0},
{0.0, 0},
{true, 0},
{false, 0},
{' ', 0},
{'0', 0},
{struct{}{}, 0},
}
for _, fc := range falseCases {
False(t, Len(mockT, fc.v, fc.l), "Len should return false for falseCases(%d)", i)
}
}
func TestInDelta(t *testing.T) {
mockT := new(testing.T)
trueCases := []struct {
a, b interface{}
delta float64
}{
{uint8(2), uint8(1), 1},
{uint16(2), uint16(1), 1},
{uint32(2), uint32(1), 1},
{uint64(2), uint64(1), 1},
{uint8(1), uint8(2), 1},
{uint16(1), uint16(2), 1},
{uint32(1), uint32(2), 1},
{uint64(1), uint64(2), 1},
{int(2), int(1), 1},
{int8(2), int8(1), 1},
{int16(2), int16(1), 1},
{int32(2), int32(1), 1},
{int64(2), int64(1), 1},
{int(1), int(2), 1},
{int8(1), int8(2), 1},
{int16(1), int16(2), 1},
{int32(1), int32(2), 1},
{int64(1), int64(2), 1},
{float32(2), float32(1), 1},
{float64(2), float64(1), 1},
{float32(1), float32(2), 1},
{float64(1), float64(2), 1},
}
for i, tc := range trueCases {
True(t, InDelta(mockT, tc.a, tc.b, tc.delta), "InDelta should return true for trueCases(%d)", i)
}
falseCases := []struct {
a, b interface{}
delta float64
}{
{1, 2, 0.5},
{2, 1, 0.5},
{"", 0, 1},
{0, "", 1},
{nil, 0, 1},
{0, nil, 1},
{nil, nil, 1},
{"", "", 1},
{0, math.NaN(), 1},
{math.NaN(), 0, 1},
{"", math.NaN(), 1},
{math.NaN(), "", 1},
{nil, math.NaN(), 1},
{math.NaN(), nil, 1},
}
for i, fc := range falseCases {
False(t, InDelta(mockT, fc.a, fc.b, fc.delta), "InDelta should return false for falseCases(%d)", i)
}
}
func TestWithinDuration(t *testing.T) {
mockT := new(testing.T)
a := time.Now()
b := a.Add(10 * time.Second)
trueCases := []struct {
a, b time.Time
duration time.Duration
}{
{a, b, 10 * time.Second},
{b, a, 10 * time.Second},
{a, b, 11 * time.Second},
{b, a, 11 * time.Second},
}
for i, tc := range trueCases {
True(t, WithinDuration(mockT, tc.a, tc.b, tc.duration), "A 10s difference is within a %v time difference for trueCases(%d)", tc.duration, i)
}
falseCases := []struct {
a, b time.Time
duration time.Duration
}{
{a, b, 9 * time.Second},
{b, a, 9 * time.Second},
{a, b, -9 * time.Second},
{b, a, -9 * time.Second},
{a, b, -11 * time.Second},
{b, a, -11 * time.Second},
}