Debian Codegraph @

td/equal_test.go

raw
1 // Copyright (c) 2018-2022, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td_test
8
9 import (
10 "testing"
11 "time"
12
13 "github.com/maxatome/go-testdeep/internal/ctxerr"
14 "github.com/maxatome/go-testdeep/internal/test"
15 "github.com/maxatome/go-testdeep/td"
16 )
17
18 type ItemPropertyKind uint8
19
20 type ItemProperty struct {
21 name string
22 kind ItemPropertyKind
23 value any
24 }
25
26 // Array.
27 func TestEqualArray(t *testing.T) {
28 checkOK(t, [8]int{1, 2}, [8]int{1, 2})
29
30 checkError(t, [8]int{1, 2}, [8]int{1, 3},
31 expectedError{
32 Message: mustBe("values differ"),
33 Path: mustBe("DATA[1]"),
34 Got: mustBe("2"),
35 Expected: mustBe("3"),
36 })
37
38 oldMaxErrors := td.DefaultContextConfig.MaxErrors
39 defer func() { td.DefaultContextConfig.MaxErrors = oldMaxErrors }()
40
41 t.Run("DefaultContextConfig.MaxErrors = 2",
42 func(t *testing.T) {
43 td.DefaultContextConfig.MaxErrors = 2
44 err := td.EqDeeplyError([8]int{1, 2, 3, 4}, [8]int{1, 42, 43, 44})
45 if err == nil {
46 t.Errorf("An Error should have occurred")
47 return
48 }
49 matchError(t, err.(*ctxerr.Error),
50 expectedError{
51 Message: mustBe("values differ"),
52 Path: mustBe("DATA[1]"),
53 Got: mustBe("2"),
54 Expected: mustBe("42"),
55 Next: &expectedError{
56 Message: mustBe("values differ"),
57 Path: mustBe("DATA[2]"),
58 Got: mustBe("3"),
59 Expected: mustBe("43"),
60 Next: &expectedError{
61 Message: mustBe(ctxerr.ErrTooManyErrors.Message),
62 },
63 },
64 }, false)
65 })
66
67 t.Run("DefaultContextConfig.MaxErrors = -1 (aka all errors)",
68 func(t *testing.T) {
69 td.DefaultContextConfig.MaxErrors = -1
70 err := td.EqDeeplyError([8]int{1, 2, 3, 4}, [8]int{1, 42, 43, 44})
71 if err == nil {
72 t.Errorf("An Error should have occurred")
73 return
74 }
75 if !matchError(t, err.(*ctxerr.Error),
76 expectedError{
77 Message: mustBe("values differ"),
78 Path: mustBe("DATA[1]"),
79 Got: mustBe("2"),
80 Expected: mustBe("42"),
81 Next: &expectedError{
82 Message: mustBe("values differ"),
83 Path: mustBe("DATA[2]"),
84 Got: mustBe("3"),
85 Expected: mustBe("43"),
86 Next: &expectedError{
87 Message: mustBe("values differ"),
88 Path: mustBe("DATA[3]"),
89 Got: mustBe("4"),
90 Expected: mustBe("44"),
91 },
92 },
93 }, false) {
94 return
95 }
96 })
97 }
98
99 // Slice.
100 func TestEqualSlice(t *testing.T) {
101 checkOK(t, []int{1, 2}, []int{1, 2})
102
103 // Same pointer
104 array := [...]int{2, 1, 4, 3}
105 checkOK(t, array[:], array[:])
106 checkOK(t, ([]int)(nil), ([]int)(nil))
107
108 // Same pointer, but not same len
109 checkError(t, array[:2], array[:],
110 expectedError{
111 Message: mustBe("comparing slices, from index #2"),
112 Path: mustBe("DATA"),
113 // Missing items are not sorted
114 Summary: mustBe(`Missing 2 items: (4,
115 3)`),
116 })
117
118 checkError(t, []int{1, 2}, []int{1, 2, 3},
119 expectedError{
120 Message: mustBe("comparing slices, from index #2"),
121 Path: mustBe("DATA"),
122 Summary: mustBe(`Missing item: (3)`),
123 })
124
125 checkError(t, []int{1, 2, 3}, []int{1, 2},
126 expectedError{
127 Message: mustBe("comparing slices, from index #2"),
128 Path: mustBe("DATA"),
129 Summary: mustBe(`Extra item: (3)`),
130 })
131
132 checkError(t, []int{1, 2}, ([]int)(nil),
133 expectedError{
134 Message: mustBe("nil slice"),
135 Path: mustBe("DATA"),
136 Got: mustBe("not nil"),
137 Expected: mustBe("nil"),
138 })
139
140 checkError(t, ([]int)(nil), []int{1, 2},
141 expectedError{
142 Message: mustBe("nil slice"),
143 Path: mustBe("DATA"),
144 Got: mustBe("nil"),
145 Expected: mustBe("not nil"),
146 })
147
148 checkError(t, []int{1, 2}, []int{1, 3},
149 expectedError{
150 Message: mustBe("values differ"),
151 Path: mustBe("DATA[1]"),
152 Got: mustBe("2"),
153 Expected: mustBe("3"),
154 })
155 }
156
157 // Interface.
158 func TestEqualInterface(t *testing.T) {
159 checkOK(t, []any{1, "foo"}, []any{1, "foo"})
160 checkOK(t, []any{1, nil}, []any{1, nil})
161
162 checkError(t, []any{1, nil}, []any{1, "foo"},
163 expectedError{
164 Message: mustBe("values differ"),
165 Path: mustBe("DATA[1]"),
166 Got: mustBe("nil"),
167 Expected: mustBe(`"foo"`),
168 })
169
170 checkError(t, []any{1, "foo"}, []any{1, nil},
171 expectedError{
172 Message: mustBe("values differ"),
173 Path: mustBe("DATA[1]"),
174 Got: mustBe(`"foo"`),
175 Expected: mustBe("nil"),
176 })
177
178 checkError(t, []any{1, "foo"}, []any{1, 12},
179 expectedError{
180 Message: mustBe("type mismatch"),
181 Path: mustBe("DATA[1]"),
182 Got: mustBe("string"),
183 Expected: mustBe("int"),
184 })
185 }
186
187 // Ptr.
188 func TestEqualPtr(t *testing.T) {
189 expected := 12
190 gotOK := expected
191 gotBad := 13
192
193 checkOK(t, &gotOK, &expected)
194 checkOK(t, &expected, &expected) // Same pointer
195
196 checkError(t, &gotBad, &expected,
197 expectedError{
198 Message: mustBe("values differ"),
199 Path: mustBe("*DATA"),
200 Got: mustBe("13"),
201 Expected: mustBe("12"),
202 })
203 }
204
205 // Struct.
206 func TestEqualStruct(t *testing.T) {
207 checkOK(t,
208 ItemProperty{ // got
209 name: "foo",
210 kind: 12,
211 value: "bar",
212 },
213 ItemProperty{ // expected
214 name: "foo",
215 kind: 12,
216 value: "bar",
217 })
218
219 checkError(t,
220 ItemProperty{ // got
221 name: "foo",
222 kind: 12,
223 value: 12,
224 },
225 ItemProperty{ // expected
226 name: "foo",
227 kind: 12,
228 value: "bar",
229 },
230 expectedError{
231 Message: mustBe("type mismatch"),
232 Path: mustBe("DATA.value"),
233 Got: mustBe("int"),
234 Expected: mustBe("string"),
235 })
236
237 type SType struct {
238 Public int
239 private string
240 }
241
242 checkOK(t,
243 SType{Public: 42, private: "test"},
244 SType{Public: 42, private: "test"})
245
246 checkError(t,
247 SType{Public: 42, private: "test"},
248 SType{Public: 42},
249 expectedError{
250 Message: mustBe("values differ"),
251 Path: mustBe("DATA.private"),
252 Got: mustBe(`"test"`),
253 Expected: mustBe(`""`),
254 })
255
256 defer func() { td.DefaultContextConfig.IgnoreUnexported = false }()
257 td.DefaultContextConfig.IgnoreUnexported = true
258
259 checkOK(t,
260 SType{Public: 42, private: "test"},
261 SType{Public: 42})
262
263 // Be careful with structs containing only private fields
264 checkOK(t,
265 ItemProperty{
266 name: "foo",
267 kind: 12,
268 value: "bar",
269 },
270 ItemProperty{})
271 }
272
273 // Map.
274 func TestEqualMap(t *testing.T) {
275 checkOK(t, map[string]int{}, map[string]int{})
276 checkOK(t, (map[string]int)(nil), (map[string]int)(nil))
277
278 expected := map[string]int{"foo": 1, "bar": 4}
279 checkOK(t, map[string]int{"foo": 1, "bar": 4}, expected)
280 checkOK(t, expected, expected) // Same pointer
281
282 checkError(t, map[string]int{"foo": 1, "bar": 4}, (map[string]int)(nil),
283 expectedError{
284 Message: mustBe("nil map"),
285 Path: mustBe("DATA"),
286 Got: mustBe("not nil"),
287 Expected: mustBe("nil"),
288 })
289
290 checkError(t, (map[string]int)(nil), map[string]int{"foo": 1, "bar": 4},
291 expectedError{
292 Message: mustBe("nil map"),
293 Path: mustBe("DATA"),
294 Got: mustBe("nil"),
295 Expected: mustBe("not nil"),
296 })
297
298 checkError(t, map[string]int{"foo": 1, "bar": 4},
299 map[string]int{"foo": 1, "bar": 5},
300 expectedError{
301 Message: mustBe("values differ"),
302 Path: mustBe(`DATA["bar"]`),
303 Got: mustBe("4"),
304 Expected: mustBe("5"),
305 })
306
307 checkError(t, map[string]int{"foo": 1, "bar": 4, "test": 12},
308 map[string]int{"foo": 1, "bar": 4},
309 expectedError{
310 Message: mustBe("comparing map"),
311 Path: mustBe("DATA"),
312 Summary: mustMatch(`Extra key:[^"]+"test"`),
313 })
314
315 checkError(t, map[string]int{"foo": 1, "bar": 4},
316 map[string]int{"foo": 1, "bar": 4, "test": 12},
317 expectedError{
318 Message: mustBe("comparing map"),
319 Path: mustBe("DATA"),
320 Summary: mustMatch(`Missing key:[^"]+"test"`),
321 })
322
323 // Extra and missing keys are sorted
324 checkError(t, map[string]int{"foo": 1, "bar": 4, "test1+": 12, "test2+": 13},
325 map[string]int{"foo": 1, "bar": 4, "test1-": 12, "test2-": 13},
326 expectedError{
327 Message: mustBe("comparing map"),
328 Path: mustBe("DATA"),
329 Summary: mustBe(`Missing 2 keys: ("test1-",
330 "test2-")
331 Extra 2 keys: ("test1+",
332 "test2+")`),
333 })
334 }
335
336 // Func.
337 func TestEqualFunc(t *testing.T) {
338 checkOK(t, (func())(nil), (func())(nil))
339
340 checkError(t, func() {}, func() {},
341 expectedError{
342 Message: mustBe("functions mismatch"),
343 Path: mustBe("DATA"),
344 Summary: mustBe("<can not be compared>"),
345 })
346 }
347
348 // Channel.
349 func TestEqualChannel(t *testing.T) {
350 var gotCh, expectedCh chan int
351
352 checkOK(t, gotCh, expectedCh) // nil channels
353
354 gotCh = make(chan int, 1)
355 checkOK(t, gotCh, gotCh) // exactly the same
356
357 checkError(t, gotCh, make(chan int, 1),
358 expectedError{
359 Message: mustBe("values differ"),
360 Path: mustBe("DATA"),
361 Got: mustContain("0x"), // hexadecimal pointer
362 Expected: mustContain("0x"), // hexadecimal pointer
363 })
364 }
365
366 // Others.
367 func TestEqualOthers(t *testing.T) {
368 type Private struct { //nolint: maligned
369 num int
370 num8 int8
371 num16 int16
372 num32 int32
373 num64 int64
374
375 numu uint
376 numu8 uint8
377 numu16 uint16
378 numu32 uint32
379 numu64 uint64
380
381 numf32 float32
382 numf64 float64
383
384 numc64 complex64
385 numc128 complex128
386
387 boolean bool
388 }
389
390 checkOK(t,
391 Private{ // got
392 num: 1,
393 num8: 8,
394 num16: 16,
395 num32: 32,
396 num64: 64,
397 numu: 1,
398 numu8: 8,
399 numu16: 16,
400 numu32: 32,
401 numu64: 64,
402 numf32: 32,
403 numf64: 64,
404 numc64: complex(64, 1),
405 numc128: complex(128, -1),
406 boolean: true,
407 },
408 Private{
409 num: 1,
410 num8: 8,
411 num16: 16,
412 num32: 32,
413 num64: 64,
414 numu: 1,
415 numu8: 8,
416 numu16: 16,
417 numu32: 32,
418 numu64: 64,
419 numf32: 32,
420 numf64: 64,
421 numc64: complex(64, 1),
422 numc128: complex(128, -1),
423 boolean: true,
424 })
425
426 checkError(t, Private{num: 1}, Private{num: 2},
427 expectedError{
428 Message: mustBe("values differ"),
429 Path: mustBe("DATA.num"),
430 Got: mustBe("1"),
431 Expected: mustBe("2"),
432 })
433
434 checkError(t, Private{num8: 1}, Private{num8: 2},
435 expectedError{
436 Message: mustBe("values differ"),
437 Path: mustBe("DATA.num8"),
438 Got: mustBe("(int8) 1"),
439 Expected: mustBe("(int8) 2"),
440 })
441
442 checkError(t, Private{num16: 1}, Private{num16: 2},
443 expectedError{
444 Message: mustBe("values differ"),
445 Path: mustBe("DATA.num16"),
446 Got: mustBe("(int16) 1"),
447 Expected: mustBe("(int16) 2"),
448 })
449
450 checkError(t, Private{num32: 1}, Private{num32: 2},
451 expectedError{
452 Message: mustBe("values differ"),
453 Path: mustBe("DATA.num32"),
454 Got: mustBe("(int32) 1"),
455 Expected: mustBe("(int32) 2"),
456 })
457
458 checkError(t, Private{num64: 1}, Private{num64: 2},
459 expectedError{
460 Message: mustBe("values differ"),
461 Path: mustBe("DATA.num64"),
462 Got: mustBe("(int64) 1"),
463 Expected: mustBe("(int64) 2"),
464 })
465
466 checkError(t, Private{numu: 1}, Private{numu: 2},
467 expectedError{
468 Message: mustBe("values differ"),
469 Path: mustBe("DATA.numu"),
470 Got: mustBe("(uint) 1"),
471 Expected: mustBe("(uint) 2"),
472 })
473
474 checkError(t, Private{numu8: 1}, Private{numu8: 2},
475 expectedError{
476 Message: mustBe("values differ"),
477 Path: mustBe("DATA.numu8"),
478 Got: mustBe("(uint8) 1"),
479 Expected: mustBe("(uint8) 2"),
480 })
481
482 checkError(t, Private{numu16: 1}, Private{numu16: 2},
483 expectedError{
484 Message: mustBe("values differ"),
485 Path: mustBe("DATA.numu16"),
486 Got: mustBe("(uint16) 1"),
487 Expected: mustBe("(uint16) 2"),
488 })
489
490 checkError(t, Private{numu32: 1}, Private{numu32: 2},
491 expectedError{
492 Message: mustBe("values differ"),
493 Path: mustBe("DATA.numu32"),
494 Got: mustBe("(uint32) 1"),
495 Expected: mustBe("(uint32) 2"),
496 })
497
498 checkError(t, Private{numu64: 1}, Private{numu64: 2},
499 expectedError{
500 Message: mustBe("values differ"),
501 Path: mustBe("DATA.numu64"),
502 Got: mustBe("(uint64) 1"),
503 Expected: mustBe("(uint64) 2"),
504 })
505
506 checkError(t, Private{numf32: 1}, Private{numf32: 2},
507 expectedError{
508 Message: mustBe("values differ"),
509 Path: mustBe("DATA.numf32"),
510 Got: mustBe("(float32) 1"),
511 Expected: mustBe("(float32) 2"),
512 })
513
514 checkError(t, Private{numf64: 1}, Private{numf64: 2},
515 expectedError{
516 Message: mustBe("values differ"),
517 Path: mustBe("DATA.numf64"),
518 Got: mustBe("1.0"),
519 Expected: mustBe("2.0"),
520 })
521
522 checkError(t, Private{numc64: complex(1, 2)}, Private{numc64: complex(2, 1)},
523 expectedError{
524 Message: mustBe("values differ"),
525 Path: mustBe("DATA.numc64"),
526 Got: mustBe("(complex64) (1+2i)"),
527 Expected: mustBe("(complex64) (2+1i)"),
528 })
529
530 checkError(t, Private{numc128: complex(1, 2)},
531 Private{numc128: complex(2, 1)},
532 expectedError{
533 Message: mustBe("values differ"),
534 Path: mustBe("DATA.numc128"),
535 Got: mustBe("(complex128) (1+2i)"),
536 Expected: mustBe("(complex128) (2+1i)"),
537 })
538
539 checkError(t, Private{boolean: true}, Private{boolean: false},
540 expectedError{
541 Message: mustBe("values differ"),
542 Path: mustBe("DATA.boolean"),
543 Got: mustBe("true"),
544 Expected: mustBe("false"),
545 })
546 }
547
548 // Private non-copyable fields.
549 func TestEqualReallyPrivate(t *testing.T) {
550 type Private struct {
551 channel chan int
552 }
553
554 ch := make(chan int, 3)
555
556 checkOKOrPanicIfUnsafeDisabled(t, Private{channel: ch}, Private{channel: ch})
557 }
558
559 func TestEqualRecursPtr(t *testing.T) {
560 type S struct {
561 Next *S
562 OK bool
563 }
564
565 expected1 := &S{}
566 expected1.Next = expected1
567
568 got := &S{}
569 got.Next = got
570
571 expected2 := &S{}
572 expected2.Next = expected2
573
574 checkOK(t, got, expected1)
575 checkOK(t, got, expected2)
576
577 got.Next = &S{OK: true}
578 expected1.Next = &S{OK: false}
579 checkError(t, got, expected1,
580 expectedError{
581 Message: mustBe("values differ"),
582 Path: mustBe("DATA.Next.OK"),
583 Got: mustBe("true"),
584 Expected: mustBe("false"),
585 })
586 }
587
588 func TestEqualRecursMap(t *testing.T) { // issue #101
589 gen := func() any {
590 type S struct {
591 Map map[int]S
592 }
593
594 m := make(map[int]S)
595 m[1] = S{
596 Map: m,
597 }
598
599 return m
600 }
601
602 checkOK(t, gen(), gen())
603 }
604
605 func TestEqualPanic(t *testing.T) {
606 test.CheckPanic(t,
607 func() {
608 td.EqDeeply(td.Ignore(), td.Ignore())
609 },
610 "Found a TestDeep operator in got param, can only use it in expected one!")
611
612 type tdInside struct {
613 Operator td.TestDeep
614 }
615 test.CheckPanic(t,
616 func() {
617 td.EqDeeply(&tdInside{}, &tdInside{})
618 },
619 "Found a TestDeep operator in got param, can only use it in expected one!")
620
621 t.Cleanup(func() { td.DefaultContextConfig.TestDeepInGotOK = false })
622 td.DefaultContextConfig.TestDeepInGotOK = true
623
624 test.IsTrue(t, td.EqDeeply(td.Ignore(), td.Ignore()))
625 test.IsTrue(t, td.EqDeeply(&tdInside{}, &tdInside{}))
626 }
627
628 type AssignableType1 struct{ x, Ignore int }
629
630 func (a AssignableType1) Equal(b AssignableType1) bool {
631 return a.x == b.x
632 }
633
634 type AssignableType2 struct{ x, Ignore int }
635
636 func (a AssignableType2) Equal(b struct{ x, Ignore int }) bool {
637 return a.x == b.x
638 }
639
640 type AssignablePtrType3 struct{ x, Ignore int }
641
642 func (a *AssignablePtrType3) Equal(b *AssignablePtrType3) bool {
643 if a == nil {
644 return b == nil
645 }
646 return b != nil && a.x == b.x
647 }
648
649 type BadEqual1 int
650
651 func (b BadEqual1) Equal(o ...BadEqual1) bool { return true } // IsVariadic
652
653 type BadEqual2 int
654
655 func (b BadEqual2) Equal() bool { return true } // NumIn() ≠ 2
656
657 type BadEqual3 int
658
659 func (b BadEqual3) Equal(o BadEqual3) (int, int) { return 1, 2 } // NumOut() ≠ 1
660
661 type BadEqual4 int
662
663 func (b BadEqual4) Equal(o string) int { return 1 } // !AssignableTo
664
665 type BadEqual5 int
666
667 func (b BadEqual5) Equal(o BadEqual5) int { return 1 } // Out=bool
668
669 func TestUseEqualGlobal(t *testing.T) {
670 defer func() { td.DefaultContextConfig.UseEqual = false }()
671 td.DefaultContextConfig.UseEqual = true
672
673 // Real case with time.Time
674 time1 := time.Now()
675 time2 := time1.Truncate(0)
676 if !time1.Equal(time2) || !time2.Equal(time1) {
677 t.Fatal("time.Equal() does not work as expected")
678 }
679
680 checkOK(t, time1, time2)
681 checkOK(t, time2, time1)
682
683 // AssignableType1
684 a1 := AssignableType1{x: 13, Ignore: 666}
685 b1 := AssignableType1{x: 13, Ignore: 789}
686 checkOK(t, a1, b1)
687 checkOK(t, b1, a1)
688 checkError(t, a1, AssignableType1{x: 14, Ignore: 666},
689 expectedError{
690 Message: mustBe("got.Equal(expected) failed"),
691 Path: mustBe("DATA"),
692 Got: mustContain("x: (int) 13,"),
693 Expected: mustContain("x: (int) 14,"),
694 })
695
696 bs := struct{ x, Ignore int }{x: 13, Ignore: 789}
697 checkOK(t, a1, bs) // bs type is assignable to AssignableType1
698 checkError(t, bs, a1,
699 expectedError{
700 Message: mustBe("type mismatch"),
701 Path: mustBe("DATA"),
702 Got: mustBe("struct { x int; Ignore int }"),
703 Expected: mustBe("td_test.AssignableType1"),
704 })
705
706 // AssignableType2
707 a2 := AssignableType2{x: 13, Ignore: 666}
708 b2 := AssignableType2{x: 13, Ignore: 789}
709 checkOK(t, a2, b2)
710 checkOK(t, b2, a2)
711 checkOK(t, a2, bs) // bs type is assignable to AssignableType2
712 checkError(t, bs, a2,
713 expectedError{
714 Message: mustBe("type mismatch"),
715 Path: mustBe("DATA"),
716 Got: mustBe("struct { x int; Ignore int }"),
717 Expected: mustBe("td_test.AssignableType2"),
718 })
719
720 // AssignablePtrType3
721 a3 := &AssignablePtrType3{x: 13, Ignore: 666}
722 b3 := &AssignablePtrType3{x: 13, Ignore: 789}
723 checkOK(t, a3, b3)
724 checkOK(t, b3, a3)
725 checkError(t, a3, &bs, // &bs type not assignable to AssignablePtrType3
726 expectedError{
727 Message: mustBe("type mismatch"),
728 Path: mustBe("DATA"),
729 Got: mustBe("*td_test.AssignablePtrType3"),
730 Expected: mustBe("*struct { x int; Ignore int }"),
731 })
732 checkOK(t, (*AssignablePtrType3)(nil), (*AssignablePtrType3)(nil))
733 checkError(t, (*AssignablePtrType3)(nil), b3,
734 expectedError{
735 Message: mustBe("got.Equal(expected) failed"),
736 Path: mustBe("DATA"),
737 Got: mustBe("(*td_test.AssignablePtrType3)(<nil>)"),
738 Expected: mustContain("x: (int) 13,"),
739 })
740 checkError(t, b3, (*AssignablePtrType3)(nil),
741 expectedError{
742 Message: mustBe("got.Equal(expected) failed"),
743 Path: mustBe("DATA"),
744 Got: mustContain("x: (int) 13,"),
745 Expected: mustBe("(*td_test.AssignablePtrType3)(<nil>)"),
746 })
747
748 // (A) Equal(A) method not found
749 checkError(t, BadEqual1(1), BadEqual1(2),
750 expectedError{
751 Message: mustBe("values differ"),
752 })
753 checkError(t, BadEqual2(1), BadEqual2(2),
754 expectedError{
755 Message: mustBe("values differ"),
756 })
757 checkError(t, BadEqual3(1), BadEqual3(2),
758 expectedError{
759 Message: mustBe("values differ"),
760 })
761 checkError(t, BadEqual4(1), BadEqual4(2),
762 expectedError{
763 Message: mustBe("values differ"),
764 })
765 checkError(t, BadEqual5(1), BadEqual5(2),
766 expectedError{
767 Message: mustBe("values differ"),
768 })
769 }
770
771 func TestUseEqualGlobalVsAnchor(t *testing.T) {
772 defer func() { td.DefaultContextConfig.UseEqual = false }()
773 td.DefaultContextConfig.UseEqual = true
774
775 tt := test.NewTestingTB(t.Name())
776
777 assert := td.Assert(tt)
778
779 type timeAnchored struct {
780 Time time.Time
781 }
782 td.CmpTrue(t,
783 assert.Cmp(
784 timeAnchored{Time: timeParse(t, "2022-05-31T06:00:00Z")},
785 timeAnchored{
786 Time: assert.A(td.Between(
787 timeParse(t, "2022-05-31T00:00:00Z"),
788 timeParse(t, "2022-05-31T12:00:00Z"),
789 )).(time.Time),
790 }))
791 }
792
793 func TestBeLaxGlobalt(t *testing.T) {
794 defer func() { td.DefaultContextConfig.BeLax = false }()
795 td.DefaultContextConfig.BeLax = true
796
797 // expected float64 value first converted to int64 before comparison
798 checkOK(t, int64(123), float64(123.56))
799
800 type MyInt int32
801 checkOK(t, int64(123), MyInt(123))
802 checkOK(t, MyInt(123), int64(123))
803
804 type gotStruct struct {
805 name string
806 age int
807 }
808 type expectedStruct struct {
809 name string
810 age int
811 }
812 checkOK(t,
813 gotStruct{
814 name: "bob",
815 age: 42,
816 },
817 expectedStruct{
818 name: "bob",
819 age: 42,
820 })
821 checkOK(t,
822 &gotStruct{
823 name: "bob",
824 age: 42,
825 },
826 &expectedStruct{
827 name: "bob",
828 age: 42,
829 })
830 }