forked from go-openapi/testify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequal_test.go
More file actions
848 lines (741 loc) · 21.5 KB
/
equal_test.go
File metadata and controls
848 lines (741 loc) · 21.5 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
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
// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package assertions
import (
"errors"
"fmt"
"iter"
"os"
"reflect"
"regexp"
"slices"
"testing"
"time"
)
const shortpkg = "assertions"
func TestEqualNotNil(t *testing.T) {
t.Parallel()
mock := new(testing.T)
if !NotNil(mock, new(AssertionTesterConformingObject)) {
t.Error("NotNil should return true: object is not nil")
}
if NotNil(mock, nil) {
t.Error("NotNil should return false: object is nil")
}
if NotNil(mock, (*struct{})(nil)) {
t.Error("NotNil should return false: object is (*struct{})(nil)")
}
}
func TestEqualNil(t *testing.T) {
t.Parallel()
mock := new(testing.T)
if !Nil(mock, nil) {
t.Error("Nil should return true: object is nil")
}
if !Nil(mock, (*struct{})(nil)) {
t.Error("Nil should return true: object is (*struct{})(nil)")
}
if Nil(mock, new(AssertionTesterConformingObject)) {
t.Error("Nil should return false: object is not nil")
}
}
func TestEqualSameWithSliceTooLongToPrint(t *testing.T) {
t.Parallel()
mock := new(mockT)
longSlice := make([]int, 1_000_000)
Same(mock, &[]int{}, &longSlice)
Contains(t, mock.errorString(), `&[]int{0, 0, 0,`)
}
func TestEqualNotSameWithSliceTooLongToPrint(t *testing.T) {
t.Parallel()
mock := new(mockT)
longSlice := make([]int, 1_000_000)
NotSame(mock, &longSlice, &longSlice)
Contains(t, mock.errorString(), `&[]int{0, 0, 0,`)
}
func TestEqualNotEqualWithSliceTooLongToPrint(t *testing.T) {
t.Parallel()
mock := new(mockT)
longSlice := make([]int, 1_000_000)
NotEqual(mock, longSlice, longSlice)
Contains(t, mock.errorString(), `
Error Trace:
Error: Should not be: []int{0, 0, 0,`)
Contains(t, mock.errorString(), `<... truncated>`)
}
func TestEqualNotEqualValuesWithSliceTooLongToPrint(t *testing.T) {
t.Parallel()
mock := new(mockT)
longSlice := make([]int, 1_000_000)
NotEqualValues(mock, longSlice, longSlice)
Contains(t, mock.errorString(), `
Error Trace:
Error: Should not be: []int{0, 0, 0,`)
Contains(t, mock.errorString(), `<... truncated>`)
}
func TestEqual(t *testing.T) {
t.Parallel()
for c := range equalCases() {
t.Run(fmt.Sprintf("Equal(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
t.Parallel()
mock := new(testing.T)
res := Equal(mock, c.expected, c.actual)
if res != c.result {
t.Errorf("Equal(%#v, %#v) should return %#v: %s", c.expected, c.actual, c.result, c.remark)
}
})
}
}
func TestEqualSame(t *testing.T) {
t.Parallel()
mock := new(mockT)
if Same(mock, ptr(1), ptr(1)) {
t.Error("Same should return false")
}
if Same(mock, 1, 1) {
t.Error("Same should return false")
}
p := ptr(2)
if Same(mock, p, *p) {
t.Error("Same should return false")
}
if !Same(mock, p, p) {
t.Error("Same should return true")
}
t.Run("same object, different type", func(t *testing.T) {
type s struct {
i int
}
type sPtr *s
ps := &s{1}
dps := sPtr(ps)
if Same(mock, dps, ps) {
t.Error("Same should return false")
}
expPat := fmt.Sprintf(`expected: &%[1]s.s\{i:1\} \(%[1]s.sPtr\)\((0x[a-f0-9]+)\)\s*\n`, shortpkg) +
fmt.Sprintf(`\s+actual : &%[1]s.s\{i:1\} \(\*%[1]s.s\)\((0x[a-f0-9]+)\)`, shortpkg)
Regexp(t, regexp.MustCompile(expPat), mock.errorString())
})
}
func TestEqualNotSame(t *testing.T) {
t.Parallel()
mock := new(testing.T)
if !NotSame(mock, ptr(1), ptr(1)) {
t.Error("NotSame should return true; different pointers")
}
if !NotSame(mock, 1, 1) {
t.Error("NotSame should return true; constant inputs")
}
p := ptr(2)
if !NotSame(mock, p, *p) {
t.Error("NotSame should return true; mixed-type inputs")
}
if NotSame(mock, p, p) {
t.Error("NotSame should return false")
}
}
func TestEqualNotEqual(t *testing.T) {
t.Parallel()
for c := range equalNotEqualCases() {
t.Run(fmt.Sprintf("NotEqual(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
t.Parallel()
mock := new(testing.T)
res := NotEqual(mock, c.expected, c.actual)
if res != c.result {
t.Errorf("NotEqual(%#v, %#v) should return %#v", c.expected, c.actual, c.result)
}
})
}
}
func TestEqualValuesAndNotEqualValues(t *testing.T) {
t.Parallel()
for c := range equalValuesCases() {
mock := new(testing.T)
// Test NotEqualValues
t.Run(fmt.Sprintf("NotEqualValues(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
res := NotEqualValues(mock, c.expected, c.actual)
if res != c.notEqualResult {
t.Errorf("NotEqualValues(%#v, %#v) should return %#v", c.expected, c.actual, c.notEqualResult)
}
})
// Test EqualValues (inverse of NotEqualValues)
t.Run(fmt.Sprintf("EqualValues(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
expectedEqualResult := !c.notEqualResult // EqualValues should return opposite of NotEqualValues
res := EqualValues(mock, c.expected, c.actual)
if res != expectedEqualResult {
t.Errorf("EqualValues(%#v, %#v) should return %#v", c.expected, c.actual, expectedEqualResult)
}
})
}
}
func TestEqualEmpty(t *testing.T) {
t.Parallel()
// Proposal for enhancement: redundant test context declaration
chWithValue := make(chan struct{}, 1)
chWithValue <- struct{}{}
var tiP *time.Time
var tiNP time.Time
var s *string
var f *os.File
sP := &s
x := 1
xP := &x
type TString string
type TStruct struct {
x int
}
t.Run("should be empty", func(t *testing.T) {
mock := new(testing.T)
True(t, Empty(mock, ""), "Empty string is empty")
True(t, Empty(mock, nil), "Nil is empty")
True(t, Empty(mock, []string{}), "Empty string array is empty")
True(t, Empty(mock, 0), "Zero int value is empty")
True(t, Empty(mock, false), "False value is empty")
True(t, Empty(mock, make(chan struct{})), "Channel without values is empty")
True(t, Empty(mock, s), "Nil string pointer is empty")
True(t, Empty(mock, f), "Nil os.File pointer is empty")
True(t, Empty(mock, tiP), "Nil time.Time pointer is empty")
True(t, Empty(mock, tiNP), "time.Time is empty")
True(t, Empty(mock, TStruct{}), "struct with zero values is empty")
True(t, Empty(mock, TString("")), "empty aliased string is empty")
True(t, Empty(mock, sP), "ptr to nil value is empty")
True(t, Empty(mock, [1]int{}), "array is state")
})
t.Run("should not be empty", func(t *testing.T) {
mock := new(testing.T)
False(t, Empty(mock, "something"), "Non Empty string is not empty")
False(t, Empty(mock, errors.New("something")), "Non nil object is not empty")
False(t, Empty(mock, []string{"something"}), "Non empty string array is not empty")
False(t, Empty(mock, 1), "Non-zero int value is not empty")
False(t, Empty(mock, true), "True value is not empty")
False(t, Empty(mock, chWithValue), "Channel with values is not empty")
False(t, Empty(mock, TStruct{x: 1}), "struct with initialized values is empty")
False(t, Empty(mock, TString("abc")), "non-empty aliased string is empty")
False(t, Empty(mock, xP), "ptr to non-nil value is not empty")
False(t, Empty(mock, [1]int{42}), "array is not state")
})
// error messages validation
for tt := range equalEmptyCases() {
t.Run(tt.name, func(t *testing.T) {
mock := new(captureT)
res := Empty(mock, tt.value)
mock.checkResultAndErrMsg(t, res, tt.expectedResult, tt.expectedErrMsg)
})
}
}
func TestEqualNotEmpty(t *testing.T) {
t.Parallel()
t.Run("should not be empty", func(t *testing.T) {
mock := new(testing.T)
False(t, NotEmpty(mock, ""), "Empty string is empty")
False(t, NotEmpty(mock, nil), "Nil is empty")
False(t, NotEmpty(mock, []string{}), "Empty string array is empty")
False(t, NotEmpty(mock, 0), "Zero int value is empty")
False(t, NotEmpty(mock, false), "False value is empty")
False(t, NotEmpty(mock, make(chan struct{})), "Channel without values is empty")
False(t, NotEmpty(mock, [1]int{}), "array is state")
})
t.Run("should be empty", func(t *testing.T) {
mock := new(testing.T)
chWithValue := make(chan struct{}, 1)
chWithValue <- struct{}{}
False(t, NotEmpty(mock, ""), "Empty string is empty")
True(t, NotEmpty(mock, "something"), "Non Empty string is not empty")
True(t, NotEmpty(mock, errors.New("something")), "Non nil object is not empty")
True(t, NotEmpty(mock, []string{"something"}), "Non empty string array is not empty")
True(t, NotEmpty(mock, 1), "Non-zero int value is not empty")
True(t, NotEmpty(mock, true), "True value is not empty")
True(t, NotEmpty(mock, chWithValue), "Channel with values is not empty")
True(t, NotEmpty(mock, [1]int{42}), "array is not state")
})
// error messages validation
for tt := range equalNotEmptyCases() {
t.Run(tt.name, func(t *testing.T) {
mock := new(captureT)
res := NotEmpty(mock, tt.value)
mock.checkResultAndErrMsg(t, tt.expectedResult, res, tt.expectedErrMsg)
})
}
}
func TestEqualExactly(t *testing.T) {
t.Parallel()
for c := range equalExactlyCases() {
t.Run(fmt.Sprintf("Exactly(%#v, %#v)", c.expected, c.actual), func(t *testing.T) {
t.Parallel()
mock := new(testing.T)
res := Exactly(mock, c.expected, c.actual)
if res != c.result {
t.Errorf("Exactly(%#v, %#v) should return %#v", c.expected, c.actual, c.result)
}
})
}
}
func TestEqualBytes(t *testing.T) {
t.Parallel()
i := 0
for c := range equalBytesCases() {
Equal(t, reflect.DeepEqual(c.a, c.b), ObjectsAreEqual(c.a, c.b), "case %d failed", i)
i++
}
}
func TestEqualValuePanics(t *testing.T) {
t.Parallel()
for tt := range panicCases() {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mock := new(mockT)
NotPanics(t, func() {
Equal(mock, tt.value1, tt.value2)
}, "should not panic")
if !tt.expectEqual {
True(t, mock.Failed(), "should have failed")
Contains(t, mock.errorString(), "Not equal:", "error message should mention inequality")
return
}
False(t, mock.Failed(), "should have been successful")
Empty(t, mock.errorString())
})
}
}
type panicCase struct {
name string
value1 any
value2 any
expectEqual bool
}
func panicCases() iter.Seq[panicCase] {
type structWithUnexportedMapWithArrayKey struct {
m any
}
return slices.Values([]panicCase{
{
// from issue https://github.com/stretchr/testify/pull/1816
name: "panic behavior on struct with array key and unexported field (some keys vs none)",
value1: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: nil,
{2}: nil,
},
},
value2: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{},
},
expectEqual: false,
},
{
name: "panic behavior on struct with array key and unexported field (same keys)",
value1: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: nil,
{2}: nil,
},
},
value2: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{2}: nil,
{1}: nil,
},
},
expectEqual: true,
},
{
name: "panic behavior on struct with array key and unexported field (non-nil values)",
value1: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: {},
{2}: nil,
},
},
value2: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: {},
{2}: nil,
},
},
expectEqual: true,
},
{
name: "panic behavior on struct with array key and unexported field (different, non-nil values)",
value1: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: {},
{2}: nil,
},
},
value2: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: nil,
{2}: {},
},
},
expectEqual: false,
},
})
}
type equalCase struct {
expected any
actual any
result bool
remark string
}
func equalCases() iter.Seq[equalCase] {
type myType string
var m map[string]any
return slices.Values([]equalCase{
{"Hello World", "Hello World", true, ""},
{123, 123, true, ""},
{123.5, 123.5, true, ""},
{[]byte("Hello World"), []byte("Hello World"), true, ""},
{nil, nil, true, ""},
{int32(123), int32(123), true, ""},
{uint64(123), uint64(123), true, ""},
{myType("1"), myType("1"), true, ""},
{&struct{}{}, &struct{}{}, true, "pointer equality is based on equality of underlying value"},
// Not expected to be equal
{m["bar"], "something", false, ""},
{myType("1"), myType("2"), false, ""},
// A case that might be confusing, especially with numeric literals
{10, uint(10), false, ""},
{int(1), uint(1), false, ""},
})
}
type samePointersCase struct {
name string
args args
same BoolAssertionFunc
ok BoolAssertionFunc
}
type args struct {
first any
second any
}
func ptr(i int) *int {
return &i
}
func equalSamePointersCases() iter.Seq[samePointersCase] {
p := ptr(2)
return slices.Values([]samePointersCase{
{
name: "1 != 2",
args: args{first: 1, second: 2},
same: False,
ok: False,
},
{
name: "1 != 1 (not same ptr)",
args: args{first: 1, second: 1},
same: False,
ok: False,
},
{
name: "ptr(1) == ptr(1)",
args: args{first: p, second: p},
same: True,
ok: True,
},
{
name: "int(1) != float32(1)",
args: args{first: int(1), second: float32(1)},
same: False,
ok: False,
},
{
name: "array != slice",
args: args{first: [2]int{1, 2}, second: []int{1, 2}},
same: False,
ok: False,
},
{
name: "non-pointer vs pointer (1 != ptr(2))",
args: args{first: 1, second: p},
same: False,
ok: False,
},
{
name: "pointer vs non-pointer (ptr(2) != 1)",
args: args{first: p, second: 1},
same: False,
ok: False,
},
})
}
type equalNotEqualCase struct {
expected any
actual any
result bool
}
func equalNotEqualCases() iter.Seq[equalNotEqualCase] {
return slices.Values([]equalNotEqualCase{
// cases that are expected not to match
{"Hello World", "Hello World!", true},
{123, 1234, true},
{123.5, 123.55, true},
{[]byte("Hello World"), []byte("Hello World!"), true},
{nil, new(AssertionTesterConformingObject), true},
// cases that are expected to match
{nil, nil, false},
{"Hello World", "Hello World", false},
{123, 123, false},
{123.5, 123.5, false},
{[]byte("Hello World"), []byte("Hello World"), false},
{new(AssertionTesterConformingObject), new(AssertionTesterConformingObject), false},
{&struct{}{}, &struct{}{}, false},
{func() int { return 23 }, func() int { return 24 }, false},
// A case that might be confusing, especially with numeric literals
{int(10), uint(10), true},
})
}
type equalValuesCase struct {
expected any
actual any
notEqualResult bool // result for NotEqualValues
}
func equalValuesCases() iter.Seq[equalValuesCase] {
return slices.Values([]equalValuesCase{
// cases that are expected not to match
{"Hello World", "Hello World!", true},
{123, 1234, true},
{123.5, 123.55, true},
{[]byte("Hello World"), []byte("Hello World!"), true},
{nil, new(AssertionTesterConformingObject), true},
// cases that are expected to match
{nil, nil, false},
{"Hello World", "Hello World", false},
{123, 123, false},
{123.5, 123.5, false},
{[]byte("Hello World"), []byte("Hello World"), false},
{new(AssertionTesterConformingObject), new(AssertionTesterConformingObject), false},
{&struct{}{}, &struct{}{}, false},
// Different behavior from NotEqual()
{func() int { return 23 }, func() int { return 24 }, true},
{int(10), int(11), true},
{int(10), uint(10), false},
{struct{}{}, struct{}{}, false},
})
}
type equalEmptyCase struct {
name string
value any
expectedResult bool
expectedErrMsg string
}
func equalEmptyCases() iter.Seq[equalEmptyCase] {
chWithValue := make(chan struct{}, 1)
chWithValue <- struct{}{}
// var tiP *time.Time
// var tiNP time.Time
// var s *string
// var f *os.File
// sP := &s
x := 1
xP := &x
type TString string
type TStruct struct {
x int
}
return slices.Values([]equalEmptyCase{
{
name: "Non Empty string is not empty",
value: "something",
expectedResult: false,
expectedErrMsg: "Should be empty, but was something\n",
},
{
name: "Non nil object is not empty",
value: errors.New("something"),
expectedResult: false,
expectedErrMsg: "Should be empty, but was something\n",
},
{
name: "Non empty string array is not empty",
value: []string{"something"},
expectedResult: false,
expectedErrMsg: "Should be empty, but was [something]\n",
},
{
name: "Non-zero int value is not empty",
value: 1,
expectedResult: false,
expectedErrMsg: "Should be empty, but was 1\n",
},
{
name: "True value is not empty",
value: true,
expectedResult: false,
expectedErrMsg: "Should be empty, but was true\n",
},
{
name: "Channel with values is not empty",
value: chWithValue,
expectedResult: false,
expectedErrMsg: fmt.Sprintf("Should be empty, but was %v\n", chWithValue),
},
{
name: "struct with initialized values is empty",
value: TStruct{x: 1},
expectedResult: false,
expectedErrMsg: "Should be empty, but was {1}\n",
},
{
name: "non-empty aliased string is empty",
value: TString("abc"),
expectedResult: false,
expectedErrMsg: "Should be empty, but was abc\n",
},
{
name: "ptr to non-nil value is not empty",
value: xP,
expectedResult: false,
expectedErrMsg: fmt.Sprintf("Should be empty, but was %p\n", xP),
},
{
name: "array is not state",
value: [1]int{42},
expectedResult: false,
expectedErrMsg: "Should be empty, but was [42]\n",
},
// Here are some edge cases
{
name: "string with only spaces is not empty",
value: " ",
expectedResult: false,
expectedErrMsg: "Should be empty, but was \n", // Proposal for enhancement: FIX THIS strange error message
},
{
name: "string with a line feed is not empty",
value: "\n",
expectedResult: false,
// Proposal for enhancement: This is the exact same error message as for an empty string
expectedErrMsg: "Should be empty, but was \n", // Proposal for enhancement: FIX THIS strange error message
},
{
name: "string with only tabulation and lines feed is not empty",
value: "\n\t\n",
expectedResult: false,
// Proposal for enhancement: The line feeds and tab are not helping to spot what is expected
expectedErrMsg: "" + // this syntax is used to show how errors are reported.
"Should be empty, but was \n" +
"\t\n",
},
{
name: "string with trailing lines feed is not empty",
value: "foo\n\n",
expectedResult: false,
// Proposal for enhancement: it's not clear if one or two lines feed are expected
expectedErrMsg: "Should be empty, but was foo\n\n",
},
{
name: "string with leading and trailing tabulation and lines feed is not empty",
value: "\n\nfoo\t\n\t\n",
expectedResult: false,
// Proposal for enhancement: The line feeds and tab are not helping to figure what is expected
expectedErrMsg: "" +
"Should be empty, but was \n" +
"\n" +
"foo\t\n" +
"\t\n",
},
{
name: "non-printable character is not empty",
value: "\u00a0", // NO-BREAK SPACE UNICODE CHARACTER
expectedResult: false,
// Proposal for enhancement: here you cannot figure out what is expected
expectedErrMsg: "Should be empty, but was \u00a0\n",
},
// Here we are testing there is no error message on success
{
name: "Empty string is empty",
value: "",
expectedResult: true,
expectedErrMsg: "",
},
})
}
type equalNotEmptyCase struct {
name string
value any
expectedResult bool
expectedErrMsg string
}
func equalNotEmptyCases() iter.Seq[equalNotEmptyCase] {
return slices.Values([]equalNotEmptyCase{
{
name: "Empty string is empty",
value: "",
expectedResult: false,
expectedErrMsg: `Should NOT be empty, but was ` + "\n", // Proposal for enhancement: FIX THIS strange error message
},
{
name: "Nil is empty",
value: nil,
expectedResult: false,
expectedErrMsg: "Should NOT be empty, but was <nil>\n",
},
{
name: "Empty string array is empty",
value: []string{},
expectedResult: false,
expectedErrMsg: "Should NOT be empty, but was []\n",
},
{
name: "Zero int value is empty",
value: 0,
expectedResult: false,
expectedErrMsg: "Should NOT be empty, but was 0\n",
},
{
name: "False value is empty",
value: false,
expectedResult: false,
expectedErrMsg: "Should NOT be empty, but was false\n",
},
{
name: "array is state",
value: [1]int{},
expectedResult: false,
expectedErrMsg: "Should NOT be empty, but was [0]\n",
},
// Here we are testing there is no error message on success
{
name: "Non Empty string is not empty",
value: "something",
expectedResult: true,
expectedErrMsg: "",
},
})
}
type diffTestingStruct struct {
A string
B int
}
func (d *diffTestingStruct) String() string {
return d.A
}
type equalExactlyCase struct {
expected any
actual any
result bool
}
func equalExactlyCases() iter.Seq[equalExactlyCase] {
a := float32(1)
b := float64(1)
c := float32(1)
d := float32(2)
return slices.Values([]equalExactlyCase{
{a, b, false},
{a, d, false},
{a, c, true},
{nil, a, false},
{a, nil, false},
})
}
type equalBytesCase struct {
a, b []byte
}
func equalBytesCases() iter.Seq[equalBytesCase] {
return slices.Values([]equalBytesCase{
{make([]byte, 2), make([]byte, 2)},
{make([]byte, 2), make([]byte, 2, 3)},
{nil, make([]byte, 0)},
})
}