Debian Codegraph @

checker_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Licensed under the MIT license, see LICENSE file for details.
2
3 package quicktest_test
4
5 import (
6 "bytes"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "regexp"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/google/go-cmp/cmp"
16 "github.com/google/go-cmp/cmp/cmpopts"
17
18 qt "github.com/frankban/quicktest"
19 )
20
21 // Fooer is an interface for testing.
22 type Fooer interface {
23 Foo()
24 }
25
26 var (
27 goTime = time.Date(2012, 3, 28, 0, 0, 0, 0, time.UTC)
28 chInt = func() chan int {
29 ch := make(chan int, 4)
30 ch <- 42
31 ch <- 47
32 return ch
33 }()
34 sameInts = cmpopts.SortSlices(func(x, y int) bool {
35 return x < y
36 })
37 cmpEqualsGot = struct {
38 Strings []interface{}
39 Ints []int
40 }{
41 Strings: []interface{}{"who", "dalek"},
42 Ints: []int{42, 47},
43 }
44 cmpEqualsWant = struct {
45 Strings []interface{}
46 Ints []int
47 }{
48 Strings: []interface{}{"who", "dalek"},
49 Ints: []int{42},
50 }
51 )
52
53 type InnerJSON struct {
54 First string
55 Second int `json:",omitempty" yaml:",omitempty"`
56 Third map[string]bool `json:",omitempty" yaml:",omitempty"`
57 }
58
59 type OuterJSON struct {
60 First float64
61 Second []*InnerJSON `json:"Last,omitempty" yaml:"last,omitempty"`
62 }
63
64 type boolean bool
65
66 var checkerTests = []struct {
67 about string
68 checker qt.Checker
69 got interface{}
70 args []interface{}
71 verbose bool
72 expectedCheckFailure string
73 expectedNegateFailure string
74 }{{
75 about: "Equals: same values",
76 checker: qt.Equals,
77 got: 42,
78 args: []interface{}{42},
79 expectedNegateFailure: `
80 error:
81 unexpected success
82 got:
83 int(42)
84 want:
85 <same as "got">
86 `,
87 }, {
88 about: "Equals: different values",
89 checker: qt.Equals,
90 got: "42",
91 args: []interface{}{"47"},
92 expectedCheckFailure: `
93 error:
94 values are not equal
95 got:
96 "42"
97 want:
98 "47"
99 `,
100 }, {
101 about: "Equals: different strings with quotes",
102 checker: qt.Equals,
103 got: `string "foo"`,
104 args: []interface{}{`string "bar"`},
105 expectedCheckFailure: tilde2bq(`
106 error:
107 values are not equal
108 got:
109 ~string "foo"~
110 want:
111 ~string "bar"~
112 `),
113 }, {
114 about: "Equals: same multiline strings",
115 checker: qt.Equals,
116 got: "a\nmultiline\nstring",
117 args: []interface{}{"a\nmultiline\nstring"},
118 expectedNegateFailure: `
119 error:
120 unexpected success
121 got:
122 "a\nmultiline\nstring"
123 want:
124 <same as "got">
125 `,
126 }, {
127 about: "Equals: different multi-line strings",
128 checker: qt.Equals,
129 got: "a\nlong\nmultiline\nstring",
130 args: []interface{}{"just\na\nlong\nmulti-line\nstring\n"},
131 expectedCheckFailure: fmt.Sprintf(`
132 error:
133 values are not equal
134 line diff (-got +want):
135 %s
136 got:
137 "a\nlong\nmultiline\nstring"
138 want:
139 "just\na\nlong\nmulti-line\nstring\n"
140 `, diff([]string{"a\n", "long\n", "multiline\n", "string"}, []string{"just\n", "a\n", "long\n", "multi-line\n", "string\n", ""})),
141 }, {
142 about: "Equals: different single-line strings ending with newline",
143 checker: qt.Equals,
144 got: "foo\n",
145 args: []interface{}{"bar\n"},
146 expectedCheckFailure: `
147 error:
148 values are not equal
149 got:
150 "foo\n"
151 want:
152 "bar\n"
153 `,
154 }, {
155 about: "Equals: different strings starting with newline",
156 checker: qt.Equals,
157 got: "\nfoo",
158 args: []interface{}{"\nbar"},
159 expectedCheckFailure: fmt.Sprintf(`
160 error:
161 values are not equal
162 line diff (-got +want):
163 %s
164 got:
165 "\nfoo"
166 want:
167 "\nbar"
168 `, diff([]string{"\n", "foo"}, []string{"\n", "bar"})),
169 }, {
170 about: "Equals: different types",
171 checker: qt.Equals,
172 got: 42,
173 args: []interface{}{"42"},
174 expectedCheckFailure: `
175 error:
176 values are not equal
177 got:
178 int(42)
179 want:
180 "42"
181 `,
182 }, {
183 about: "Equals: nil and nil",
184 checker: qt.Equals,
185 got: nil,
186 args: []interface{}{nil},
187 expectedNegateFailure: `
188 error:
189 unexpected success
190 got:
191 nil
192 want:
193 <same as "got">
194 `,
195 }, {
196 about: "Equals: error is not nil",
197 checker: qt.Equals,
198 got: errBadWolf,
199 args: []interface{}{nil},
200 expectedCheckFailure: `
201 error:
202 got non-nil error
203 got:
204 bad wolf
205 file:line
206 want:
207 nil
208 `,
209 }, {
210 about: "Equals: error is not nil: not formatted",
211 checker: qt.Equals,
212 got: &errTest{
213 msg: "bad wolf",
214 },
215 args: []interface{}{nil},
216 expectedCheckFailure: `
217 error:
218 got non-nil error
219 got:
220 e"bad wolf"
221 want:
222 nil
223 `,
224 }, {
225 about: "Equals: error does not guard against nil",
226 checker: qt.Equals,
227 got: (*errTest)(nil),
228 args: []interface{}{nil},
229 expectedCheckFailure: `
230 error:
231 got non-nil error
232 got:
233 e<nil>
234 want:
235 nil
236 `,
237 }, {
238 about: "Equals: error is not nil: not formatted and with quotes",
239 checker: qt.Equals,
240 got: &errTest{
241 msg: `failure: "bad wolf"`,
242 },
243 args: []interface{}{nil},
244 expectedCheckFailure: tilde2bq(`
245 error:
246 got non-nil error
247 got:
248 e~failure: "bad wolf"~
249 want:
250 nil
251 `),
252 }, {
253 about: "Equals: different errors with same message",
254 checker: qt.Equals,
255 got: &errTest{
256 msg: "bad wolf",
257 },
258 args: []interface{}{errors.New("bad wolf")},
259 expectedCheckFailure: `
260 error:
261 values are not equal
262 got type:
263 *quicktest_test.errTest
264 want type:
265 *errors.errorString
266 got:
267 e"bad wolf"
268 want:
269 <same as "got" but different pointer value>
270 `,
271 }, {
272 about: "Equals: different pointer errors with the same message",
273 checker: qt.Equals,
274 got: &errTest{
275 msg: "bad wolf",
276 },
277 args: []interface{}{&errTest{
278 msg: "bad wolf",
279 }},
280 expectedCheckFailure: `
281 error:
282 values are not equal
283 got:
284 e"bad wolf"
285 want:
286 <same as "got" but different pointer value>
287 `,
288 }, {
289 about: "Equals: different pointers with the same formatted output",
290 checker: qt.Equals,
291 got: new(int),
292 args: []interface{}{new(int)},
293 expectedCheckFailure: `
294 error:
295 values are not equal
296 got:
297 &int(0)
298 want:
299 <same as "got" but different pointer value>
300 `,
301 }, {
302 about: "Equals: nil struct",
303 checker: qt.Equals,
304 got: (*struct{})(nil),
305 args: []interface{}{nil},
306 expectedCheckFailure: `
307 error:
308 values are not equal
309 got:
310 (*struct {})(nil)
311 want:
312 nil
313 `,
314 }, {
315 about: "Equals: different booleans",
316 checker: qt.Equals,
317 got: true,
318 args: []interface{}{false},
319 expectedCheckFailure: `
320 error:
321 values are not equal
322 got:
323 bool(true)
324 want:
325 bool(false)
326 `,
327 }, {
328 about: "Equals: uncomparable types",
329 checker: qt.Equals,
330 got: struct {
331 Ints []int
332 }{
333 Ints: []int{42, 47},
334 },
335 args: []interface{}{struct {
336 Ints []int
337 }{
338 Ints: []int{42, 47},
339 }},
340 expectedCheckFailure: `
341 error:
342 runtime error: comparing uncomparable type struct { Ints []int }
343 got:
344 struct { Ints []int }{
345 Ints: {42, 47},
346 }
347 want:
348 <same as "got">
349 `,
350 }, {
351 about: "Equals: not enough arguments",
352 checker: qt.Equals,
353 expectedCheckFailure: `
354 error:
355 bad check: not enough arguments provided to checker: got 0, want 1
356 want args:
357 want
358 `,
359 expectedNegateFailure: `
360 error:
361 bad check: not enough arguments provided to checker: got 0, want 1
362 want args:
363 want
364 `,
365 }, {
366 about: "Equals: too many arguments",
367 checker: qt.Equals,
368 args: []interface{}{nil, 47},
369 expectedCheckFailure: `
370 error:
371 bad check: too many arguments provided to checker: got 2, want 1
372 got args:
373 []interface {}{
374 nil,
375 int(47),
376 }
377 want args:
378 want
379 `,
380 expectedNegateFailure: `
381 error:
382 bad check: too many arguments provided to checker: got 2, want 1
383 got args:
384 []interface {}{
385 nil,
386 int(47),
387 }
388 want args:
389 want
390 `,
391 }, {
392 about: "CmpEquals: same values",
393 checker: qt.CmpEquals(),
394 got: cmpEqualsGot,
395 args: []interface{}{cmpEqualsGot},
396 expectedNegateFailure: `
397 error:
398 unexpected success
399 got:
400 struct { Strings []interface {}; Ints []int }{
401 Strings: {
402 "who",
403 "dalek",
404 },
405 Ints: {42, 47},
406 }
407 want:
408 <same as "got">
409 `,
410 }, {
411 about: "CmpEquals: different values",
412 checker: qt.CmpEquals(),
413 got: cmpEqualsGot,
414 args: []interface{}{cmpEqualsWant},
415 expectedCheckFailure: fmt.Sprintf(`
416 error:
417 values are not deep equal
418 diff (-got +want):
419 %s
420 got:
421 struct { Strings []interface {}; Ints []int }{
422 Strings: {
423 "who",
424 "dalek",
425 },
426 Ints: {42, 47},
427 }
428 want:
429 struct { Strings []interface {}; Ints []int }{
430 Strings: {
431 "who",
432 "dalek",
433 },
434 Ints: {42},
435 }
436 `, diff(cmpEqualsGot, cmpEqualsWant)),
437 }, {
438 about: "CmpEquals: different values, long output",
439 checker: qt.CmpEquals(),
440 got: []interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"},
441 args: []interface{}{[]interface{}{cmpEqualsWant, "extra line 1"}},
442 expectedCheckFailure: fmt.Sprintf(`
443 error:
444 values are not deep equal
445 diff (-got +want):
446 %s
447 got:
448 <suppressed due to length (11 lines), use -v for full output>
449 want:
450 []interface {}{
451 struct { Strings []interface {}; Ints []int }{
452 Strings: {
453 "who",
454 "dalek",
455 },
456 Ints: {42},
457 },
458 "extra line 1",
459 }
460 `, diff([]interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []interface{}{cmpEqualsWant, "extra line 1"})),
461 }, {
462 about: "CmpEquals: different values: long output and verbose",
463 checker: qt.CmpEquals(),
464 got: []interface{}{cmpEqualsWant, "extra line 1", "extra line 2"},
465 args: []interface{}{[]interface{}{cmpEqualsWant, "extra line 1"}},
466 verbose: true,
467 expectedCheckFailure: fmt.Sprintf(`
468 error:
469 values are not deep equal
470 diff (-got +want):
471 %s
472 got:
473 []interface {}{
474 struct { Strings []interface {}; Ints []int }{
475 Strings: {
476 "who",
477 "dalek",
478 },
479 Ints: {42},
480 },
481 "extra line 1",
482 "extra line 2",
483 }
484 want:
485 []interface {}{
486 struct { Strings []interface {}; Ints []int }{
487 Strings: {
488 "who",
489 "dalek",
490 },
491 Ints: {42},
492 },
493 "extra line 1",
494 }
495 `, diff([]interface{}{cmpEqualsWant, "extra line 1", "extra line 2"}, []interface{}{cmpEqualsWant, "extra line 1"})),
496 }, {
497 about: "CmpEquals: different values, long output, same number of lines",
498 checker: qt.CmpEquals(),
499 got: []interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"},
500 args: []interface{}{[]interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line three"}},
501 expectedCheckFailure: fmt.Sprintf(`
502 error:
503 values are not deep equal
504 diff (-got +want):
505 %s
506 got:
507 <suppressed due to length (11 lines), use -v for full output>
508 want:
509 <suppressed due to length (11 lines), use -v for full output>
510 `, diff([]interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line 3"}, []interface{}{cmpEqualsWant, "extra line 1", "extra line 2", "extra line three"})),
511 }, {
512 about: "CmpEquals: same values with options",
513 checker: qt.CmpEquals(sameInts),
514 got: []int{1, 2, 3},
515 args: []interface{}{
516 []int{3, 2, 1},
517 },
518 expectedNegateFailure: `
519 error:
520 unexpected success
521 got:
522 []int{1, 2, 3}
523 want:
524 []int{3, 2, 1}
525 `,
526 }, {
527 about: "CmpEquals: different values with options",
528 checker: qt.CmpEquals(sameInts),
529 got: []int{1, 2, 4},
530 args: []interface{}{
531 []int{3, 2, 1},
532 },
533 expectedCheckFailure: fmt.Sprintf(`
534 error:
535 values are not deep equal
536 diff (-got +want):
537 %s
538 got:
539 []int{1, 2, 4}
540 want:
541 []int{3, 2, 1}
542 `, diff([]int{1, 2, 4}, []int{3, 2, 1}, sameInts)),
543 }, {
544 about: "CmpEquals: structs with unexported fields not allowed",
545 checker: qt.CmpEquals(),
546 got: struct{ answer int }{
547 answer: 42,
548 },
549 args: []interface{}{
550 struct{ answer int }{
551 answer: 42,
552 },
553 },
554 expectedCheckFailure: `
555 error:
556 bad check: cannot handle unexported field at root.answer:
557 "github.com/frankban/quicktest_test".(struct { answer int })
558 consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
559 `,
560 expectedNegateFailure: `
561 error:
562 bad check: cannot handle unexported field at root.answer:
563 "github.com/frankban/quicktest_test".(struct { answer int })
564 consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported
565 `,
566 }, {
567 about: "CmpEquals: structs with unexported fields ignored",
568 checker: qt.CmpEquals(cmpopts.IgnoreUnexported(struct{ answer int }{})),
569 got: struct{ answer int }{
570 answer: 42,
571 },
572 args: []interface{}{
573 struct{ answer int }{
574 answer: 42,
575 },
576 },
577 expectedNegateFailure: `
578 error:
579 unexpected success
580 got:
581 struct { answer int }{answer:42}
582 want:
583 <same as "got">
584 `,
585 }, {
586 about: "CmpEquals: same times",
587 checker: qt.CmpEquals(),
588 got: goTime,
589 args: []interface{}{
590 goTime,
591 },
592 expectedNegateFailure: `
593 error:
594 unexpected success
595 got:
596 s"2012-03-28 00:00:00 +0000 UTC"
597 want:
598 <same as "got">
599 `,
600 }, {
601 about: "CmpEquals: different times: verbose",
602 checker: qt.CmpEquals(),
603 got: goTime.Add(24 * time.Hour),
604 args: []interface{}{
605 goTime,
606 },
607 verbose: true,
608 expectedCheckFailure: fmt.Sprintf(`
609 error:
610 values are not deep equal
611 diff (-got +want):
612 %s
613 got:
614 s"2012-03-29 00:00:00 +0000 UTC"
615 want:
616 s"2012-03-28 00:00:00 +0000 UTC"
617 `, diff(goTime.Add(24*time.Hour), goTime)),
618 }, {
619 about: "CmpEquals: not enough arguments",
620 checker: qt.CmpEquals(),
621 expectedCheckFailure: `
622 error:
623 bad check: not enough arguments provided to checker: got 0, want 1
624 want args:
625 want
626 `,
627 expectedNegateFailure: `
628 error:
629 bad check: not enough arguments provided to checker: got 0, want 1
630 want args:
631 want
632 `,
633 }, {
634 about: "CmpEquals: too many arguments",
635 checker: qt.CmpEquals(),
636 got: []int{42},
637 args: []interface{}{[]int{42}, "bad wolf"},
638 expectedCheckFailure: `
639 error:
640 bad check: too many arguments provided to checker: got 2, want 1
641 got args:
642 []interface {}{
643 []int{42},
644 "bad wolf",
645 }
646 want args:
647 want
648 `,
649 expectedNegateFailure: `
650 error:
651 bad check: too many arguments provided to checker: got 2, want 1
652 got args:
653 []interface {}{
654 []int{42},
655 "bad wolf",
656 }
657 want args:
658 want
659 `,
660 }, {
661 about: "DeepEquals: different values",
662 checker: qt.DeepEquals,
663 got: cmpEqualsGot,
664 args: []interface{}{cmpEqualsWant},
665 expectedCheckFailure: fmt.Sprintf(`
666 error:
667 values are not deep equal
668 diff (-got +want):
669 %s
670 got:
671 struct { Strings []interface {}; Ints []int }{
672 Strings: {
673 "who",
674 "dalek",
675 },
676 Ints: {42, 47},
677 }
678 want:
679 struct { Strings []interface {}; Ints []int }{
680 Strings: {
681 "who",
682 "dalek",
683 },
684 Ints: {42},
685 }
686 `, diff(cmpEqualsGot, cmpEqualsWant)),
687 }, {
688 about: "DeepEquals: different values: long output",
689 checker: qt.DeepEquals,
690 got: []interface{}{cmpEqualsWant, cmpEqualsWant},
691 args: []interface{}{[]interface{}{cmpEqualsWant, cmpEqualsWant, 42}},
692 expectedCheckFailure: fmt.Sprintf(`
693 error:
694 values are not deep equal
695 diff (-got +want):
696 %s
697 got:
698 <suppressed due to length (15 lines), use -v for full output>
699 want:
700 <suppressed due to length (16 lines), use -v for full output>
701 `, diff([]interface{}{cmpEqualsWant, cmpEqualsWant}, []interface{}{cmpEqualsWant, cmpEqualsWant, 42})),
702 }, {
703 about: "DeepEquals: different values: long output and verbose",
704 checker: qt.DeepEquals,
705 got: []interface{}{cmpEqualsWant, cmpEqualsWant},
706 args: []interface{}{[]interface{}{cmpEqualsWant, cmpEqualsWant, 42}},
707 verbose: true,
708 expectedCheckFailure: fmt.Sprintf(`
709 error:
710 values are not deep equal
711 diff (-got +want):
712 %s
713 got:
714 []interface {}{
715 struct { Strings []interface {}; Ints []int }{
716 Strings: {
717 "who",
718 "dalek",
719 },
720 Ints: {42},
721 },
722 struct { Strings []interface {}; Ints []int }{
723 Strings: {
724 "who",
725 "dalek",
726 },
727 Ints: {42},
728 },
729 }
730 want:
731 []interface {}{
732 struct { Strings []interface {}; Ints []int }{
733 Strings: {
734 "who",
735 "dalek",
736 },
737 Ints: {42},
738 },
739 struct { Strings []interface {}; Ints []int }{
740 Strings: {
741 "who",
742 "dalek",
743 },
744 Ints: {42},
745 },
746 int(42),
747 }
748 `, diff([]interface{}{cmpEqualsWant, cmpEqualsWant}, []interface{}{cmpEqualsWant, cmpEqualsWant, 42})),
749 }, {
750 about: "ContentEquals: same values",
751 checker: qt.ContentEquals,
752 got: []string{"these", "are", "the", "voyages"},
753 args: []interface{}{
754 []string{"these", "are", "the", "voyages"},
755 },
756 expectedNegateFailure: `
757 error:
758 unexpected success
759 got:
760 []string{"these", "are", "the", "voyages"}
761 want:
762 <same as "got">
763 `,
764 }, {
765 about: "ContentEquals: same contents",
766 checker: qt.ContentEquals,
767 got: []int{1, 2, 3},
768 args: []interface{}{
769 []int{3, 2, 1},
770 },
771 expectedNegateFailure: `
772 error:
773 unexpected success
774 got:
775 []int{1, 2, 3}
776 want:
777 []int{3, 2, 1}
778 `,
779 }, {
780 about: "ContentEquals: same contents on complex slice",
781 checker: qt.ContentEquals,
782 got: []struct {
783 Strings []interface{}
784 Ints []int
785 }{cmpEqualsGot, cmpEqualsGot, cmpEqualsWant},
786 args: []interface{}{
787 []struct {
788 Strings []interface{}
789 Ints []int
790 }{cmpEqualsWant, cmpEqualsGot, cmpEqualsGot},
791 },
792 expectedNegateFailure: `
793 error:
794 unexpected success
795 got:
796 []struct { Strings []interface {}; Ints []int }{
797 {
798 Strings: {
799 "who",
800 "dalek",
801 },
802 Ints: {42, 47},
803 },
804 {
805 Strings: {
806 "who",
807 "dalek",
808 },
809 Ints: {42, 47},
810 },
811 {
812 Strings: {
813 "who",
814 "dalek",
815 },
816 Ints: {42},
817 },
818 }
819 want:
820 []struct { Strings []interface {}; Ints []int }{
821 {
822 Strings: {
823 "who",
824 "dalek",
825 },
826 Ints: {42},
827 },
828 {
829 Strings: {
830 "who",
831 "dalek",
832 },
833 Ints: {42, 47},
834 },
835 {
836 Strings: {
837 "who",
838 "dalek",
839 },
840 Ints: {42, 47},
841 },
842 }
843 `,
844 }, {
845 about: "ContentEquals: same contents on a nested slice",
846 checker: qt.ContentEquals,
847 got: struct {
848 Nums []int
849 }{
850 Nums: []int{1, 2, 3, 4},
851 },
852 args: []interface{}{
853 struct {
854 Nums []int
855 }{
856 Nums: []int{4, 3, 2, 1},
857 },
858 },
859 expectedNegateFailure: `
860 error:
861 unexpected success
862 got:
863 struct { Nums []int }{
864 Nums: {1, 2, 3, 4},
865 }
866 want:
867 struct { Nums []int }{
868 Nums: {4, 3, 2, 1},
869 }
870 `,
871 }, {
872 about: "ContentEquals: slices of different type",
873 checker: qt.ContentEquals,
874 got: []string{"bad", "wolf"},
875 args: []interface{}{
876 []interface{}{"bad", "wolf"},
877 },
878 expectedCheckFailure: fmt.Sprintf(`
879 error:
880 values are not deep equal
881 diff (-got +want):
882 %s
883 got:
884 []string{"bad", "wolf"}
885 want:
886 []interface {}{
887 "bad",
888 "wolf",
889 }
890 `, diff([]string{"bad", "wolf"}, []interface{}{"bad", "wolf"})),
891 }, {
892 about: "ContentEquals: not enough arguments",
893 checker: qt.ContentEquals,
894 expectedCheckFailure: `
895 error:
896 bad check: not enough arguments provided to checker: got 0, want 1
897 want args:
898 want
899 `,
900 expectedNegateFailure: `
901 error:
902 bad check: not enough arguments provided to checker: got 0, want 1
903 want args:
904 want
905 `,
906 }, {
907 about: "ContentEquals: too many arguments",
908 checker: qt.ContentEquals,
909 args: []interface{}{nil, nil},
910 expectedCheckFailure: `
911 error:
912 bad check: too many arguments provided to checker: got 2, want 1
913 got args:
914 []interface {}{
915 nil,
916 nil,
917 }
918 want args:
919 want
920 `,
921 expectedNegateFailure: `
922 error:
923 bad check: too many arguments provided to checker: got 2, want 1
924 got args:
925 []interface {}{
926 nil,
927 nil,
928 }
929 want args:
930 want
931 `,
932 }, {
933 about: "Matches: perfect match",
934 checker: qt.Matches,
935 got: "exterminate",
936 args: []interface{}{"exterminate"},
937 expectedNegateFailure: `
938 error:
939 unexpected success
940 got value:
941 "exterminate"
942 regexp:
943 <same as "got value">
944 `,
945 }, {
946 about: "Matches: match",
947 checker: qt.Matches,
948 got: "these are the voyages",
949 args: []interface{}{"these are the .*"},
950 expectedNegateFailure: `
951 error:
952 unexpected success
953 got value:
954 "these are the voyages"
955 regexp:
956 "these are the .*"
957 `,
958 }, {
959 about: "Matches: match with pre-compiled regexp",
960 checker: qt.Matches,
961 got: bytes.NewBufferString("resistance is futile"),
962 args: []interface{}{regexp.MustCompile("resistance is (futile|useful)")},
963 expectedNegateFailure: `
964 error:
965 unexpected success
966 got value:
967 s"resistance is futile"
968 regexp:
969 s"resistance is (futile|useful)"
970 `,
971 }, {
972 about: "Matches: mismatch with pre-compiled regexp",
973 checker: qt.Matches,
974 got: bytes.NewBufferString("resistance is cool"),
975 args: []interface{}{regexp.MustCompile("resistance is (futile|useful)")},
976 expectedCheckFailure: `
977 error:
978 value.String() does not match regexp
979 got value:
980 s"resistance is cool"
981 regexp:
982 s"resistance is (futile|useful)"
983 `,
984 }, {
985 about: "Matches: match with pre-compiled multi-line regexp",
986 checker: qt.Matches,
987 got: bytes.NewBufferString("line 1\nline 2"),
988 args: []interface{}{regexp.MustCompile(`line \d\nline \d`)},
989 expectedNegateFailure: `
990 error:
991 unexpected success
992 got value:
993 s"line 1\nline 2"
994 regexp:
995 s"line \\d\\nline \\d"
996 `,
997 }, {
998 about: "Matches: match with stringer",
999 checker: qt.Matches,
1000 got: bytes.NewBufferString("resistance is futile"),
1001 args: []interface{}{"resistance is (futile|useful)"},
1002 expectedNegateFailure: `
1003 error:
1004 unexpected success
1005 got value:
1006 s"resistance is futile"
1007 regexp:
1008 "resistance is (futile|useful)"
1009 `,
1010 }, {
1011 about: "Matches: mismatch",
1012 checker: qt.Matches,
1013 got: "voyages",
1014 args: []interface{}{"these are the voyages"},
1015 expectedCheckFailure: `
1016 error:
1017 value does not match regexp
1018 got value:
1019 "voyages"
1020 regexp:
1021 "these are the voyages"
1022 `,
1023 }, {
1024 about: "Matches: mismatch with stringer",
1025 checker: qt.Matches,
1026 got: bytes.NewBufferString("voyages"),
1027 args: []interface{}{"these are the voyages"},
1028 expectedCheckFailure: `
1029 error:
1030 value.String() does not match regexp
1031 got value:
1032 s"voyages"
1033 regexp:
1034 "these are the voyages"
1035 `,
1036 }, {
1037 about: "Matches: empty pattern",
1038 checker: qt.Matches,
1039 got: "these are the voyages",
1040 args: []interface{}{""},
1041 expectedCheckFailure: `
1042 error:
1043 value does not match regexp
1044 got value:
1045 "these are the voyages"
1046 regexp:
1047 ""
1048 `,
1049 }, {
1050 about: "Matches: complex pattern",
1051 checker: qt.Matches,
1052 got: "end of the universe",
1053 args: []interface{}{"bad wolf|end of the .*"},
1054 expectedNegateFailure: `
1055 error:
1056 unexpected success
1057 got value:
1058 "end of the universe"
1059 regexp:
1060 "bad wolf|end of the .*"
1061 `,
1062 }, {
1063 about: "Matches: invalid pattern",
1064 checker: qt.Matches,
1065 got: "voyages",
1066 args: []interface{}{"("},
1067 expectedCheckFailure: `
1068 error:
1069 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1070 regexp:
1071 "("
1072 `,
1073 expectedNegateFailure: `
1074 error:
1075 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1076 regexp:
1077 "("
1078 `,
1079 }, {
1080 about: "Matches: pattern not a string",
1081 checker: qt.Matches,
1082 got: "",
1083 args: []interface{}{[]int{42}},
1084 expectedCheckFailure: `
1085 error:
1086 bad check: regexp is not a string
1087 regexp:
1088 []int{42}
1089 `,
1090 expectedNegateFailure: `
1091 error:
1092 bad check: regexp is not a string
1093 regexp:
1094 []int{42}
1095 `,
1096 }, {
1097 about: "Matches: not a string or as stringer",
1098 checker: qt.Matches,
1099 got: 42,
1100 args: []interface{}{".*"},
1101 expectedCheckFailure: `
1102 error:
1103 bad check: value is not a string or a fmt.Stringer
1104 value:
1105 int(42)
1106 `,
1107 expectedNegateFailure: `
1108 error:
1109 bad check: value is not a string or a fmt.Stringer
1110 value:
1111 int(42)
1112 `,
1113 }, {
1114 about: "Matches: not enough arguments",
1115 checker: qt.Matches,
1116 expectedCheckFailure: `
1117 error:
1118 bad check: not enough arguments provided to checker: got 0, want 1
1119 want args:
1120 regexp
1121 `,
1122 expectedNegateFailure: `
1123 error:
1124 bad check: not enough arguments provided to checker: got 0, want 1
1125 want args:
1126 regexp
1127 `,
1128 }, {
1129 about: "Matches: too many arguments",
1130 checker: qt.Matches,
1131 got: "these are the voyages",
1132 args: []interface{}{"these are the .*", nil},
1133 expectedCheckFailure: `
1134 error:
1135 bad check: too many arguments provided to checker: got 2, want 1
1136 got args:
1137 []interface {}{
1138 "these are the .*",
1139 nil,
1140 }
1141 want args:
1142 regexp
1143 `,
1144 expectedNegateFailure: `
1145 error:
1146 bad check: too many arguments provided to checker: got 2, want 1
1147 got args:
1148 []interface {}{
1149 "these are the .*",
1150 nil,
1151 }
1152 want args:
1153 regexp
1154 `,
1155 }, {
1156 about: "ErrorMatches: perfect match",
1157 checker: qt.ErrorMatches,
1158 got: errBadWolf,
1159 args: []interface{}{"bad wolf"},
1160 expectedNegateFailure: `
1161 error:
1162 unexpected success
1163 got error:
1164 bad wolf
1165 file:line
1166 regexp:
1167 "bad wolf"
1168 `,
1169 }, {
1170 about: "ErrorMatches: match",
1171 checker: qt.ErrorMatches,
1172 got: errBadWolf,
1173 args: []interface{}{"bad .*"},
1174 expectedNegateFailure: `
1175 error:
1176 unexpected success
1177 got error:
1178 bad wolf
1179 file:line
1180 regexp:
1181 "bad .*"
1182 `,
1183 }, {
1184 about: "ErrorMatches: mismatch",
1185 checker: qt.ErrorMatches,
1186 got: errBadWolf,
1187 args: []interface{}{"exterminate"},
1188 expectedCheckFailure: `
1189 error:
1190 error does not match regexp
1191 got error:
1192 bad wolf
1193 file:line
1194 regexp:
1195 "exterminate"
1196 `,
1197 }, {
1198 about: "ErrorMatches: empty pattern",
1199 checker: qt.ErrorMatches,
1200 got: errBadWolf,
1201 args: []interface{}{""},
1202 expectedCheckFailure: `
1203 error:
1204 error does not match regexp
1205 got error:
1206 bad wolf
1207 file:line
1208 regexp:
1209 ""
1210 `,
1211 }, {
1212 about: "ErrorMatches: complex pattern",
1213 checker: qt.ErrorMatches,
1214 got: errBadWolf,
1215 args: []interface{}{"bad wolf|end of the universe"},
1216 expectedNegateFailure: `
1217 error:
1218 unexpected success
1219 got error:
1220 bad wolf
1221 file:line
1222 regexp:
1223 "bad wolf|end of the universe"
1224 `,
1225 }, {
1226 about: "ErrorMatches: invalid pattern",
1227 checker: qt.ErrorMatches,
1228 got: errBadWolf,
1229 args: []interface{}{"("},
1230 expectedCheckFailure: `
1231 error:
1232 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1233 regexp:
1234 "("
1235 `,
1236 expectedNegateFailure: `
1237 error:
1238 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1239 regexp:
1240 "("
1241 `,
1242 }, {
1243 about: "ErrorMatches: pattern not a string",
1244 checker: qt.ErrorMatches,
1245 got: errBadWolf,
1246 args: []interface{}{[]int{42}},
1247 expectedCheckFailure: `
1248 error:
1249 bad check: regexp is not a string
1250 regexp:
1251 []int{42}
1252 `,
1253 expectedNegateFailure: `
1254 error:
1255 bad check: regexp is not a string
1256 regexp:
1257 []int{42}
1258 `,
1259 }, {
1260 about: "ErrorMatches: not an error",
1261 checker: qt.ErrorMatches,
1262 got: 42,
1263 args: []interface{}{".*"},
1264 expectedCheckFailure: `
1265 error:
1266 bad check: first argument is not an error
1267 got:
1268 int(42)
1269 `,
1270 expectedNegateFailure: `
1271 error:
1272 bad check: first argument is not an error
1273 got:
1274 int(42)
1275 `,
1276 }, {
1277 about: "ErrorMatches: nil error",
1278 checker: qt.ErrorMatches,
1279 got: nil,
1280 args: []interface{}{"some pattern"},
1281 expectedCheckFailure: `
1282 error:
1283 got nil error but want non-nil
1284 got error:
1285 nil
1286 regexp:
1287 "some pattern"
1288 `,
1289 }, {
1290 about: "ErrorMatches: not enough arguments",
1291 checker: qt.ErrorMatches,
1292 expectedCheckFailure: `
1293 error:
1294 bad check: not enough arguments provided to checker: got 0, want 1
1295 want args:
1296 regexp
1297 `,
1298 expectedNegateFailure: `
1299 error:
1300 bad check: not enough arguments provided to checker: got 0, want 1
1301 want args:
1302 regexp
1303 `,
1304 }, {
1305 about: "ErrorMatches: too many arguments",
1306 checker: qt.ErrorMatches,
1307 got: errBadWolf,
1308 args: []interface{}{"bad wolf", []string{"bad", "wolf"}},
1309 expectedCheckFailure: `
1310 error:
1311 bad check: too many arguments provided to checker: got 2, want 1
1312 got args:
1313 []interface {}{
1314 "bad wolf",
1315 []string{"bad", "wolf"},
1316 }
1317 want args:
1318 regexp
1319 `,
1320 expectedNegateFailure: `
1321 error:
1322 bad check: too many arguments provided to checker: got 2, want 1
1323 got args:
1324 []interface {}{
1325 "bad wolf",
1326 []string{"bad", "wolf"},
1327 }
1328 want args:
1329 regexp
1330 `,
1331 }, {
1332 about: "ErrorMatches: match with pre-compiled regexp",
1333 checker: qt.ErrorMatches,
1334 got: errBadWolf,
1335 args: []interface{}{regexp.MustCompile("bad (wolf|dog)")},
1336 expectedNegateFailure: `
1337 error:
1338 unexpected success
1339 got error:
1340 bad wolf
1341 file:line
1342 regexp:
1343 s"bad (wolf|dog)"
1344 `,
1345 }, {
1346 about: "ErrorMatches: match with pre-compiled multi-line regexp",
1347 checker: qt.ErrorMatches,
1348 got: errBadWolfMultiLine,
1349 args: []interface{}{regexp.MustCompile(`bad (wolf|dog)\nfaulty (logic|statement)`)},
1350 expectedNegateFailure: `
1351 error:
1352 unexpected success
1353 got error:
1354 bad wolf
1355 faulty logic
1356 file:line
1357 regexp:
1358 s"bad (wolf|dog)\\nfaulty (logic|statement)"
1359 `,
1360 }, {
1361 about: "ErrorMatches: mismatch with pre-compiled regexp",
1362 checker: qt.ErrorMatches,
1363 got: errBadWolf,
1364 args: []interface{}{regexp.MustCompile("good (wolf|dog)")},
1365 expectedCheckFailure: `
1366 error:
1367 error does not match regexp
1368 got error:
1369 bad wolf
1370 file:line
1371 regexp:
1372 s"good (wolf|dog)"
1373 `,
1374 }, {
1375 about: "PanicMatches: perfect match",
1376 checker: qt.PanicMatches,
1377 got: func() { panic("error: bad wolf") },
1378 args: []interface{}{"error: bad wolf"},
1379 expectedNegateFailure: `
1380 error:
1381 unexpected success
1382 panic value:
1383 "error: bad wolf"
1384 function:
1385 func() {...}
1386 regexp:
1387 <same as "panic value">
1388 `,
1389 }, {
1390 about: "PanicMatches: match",
1391 checker: qt.PanicMatches,
1392 got: func() { panic("error: bad wolf") },
1393 args: []interface{}{"error: .*"},
1394 expectedNegateFailure: `
1395 error:
1396 unexpected success
1397 panic value:
1398 "error: bad wolf"
1399 function:
1400 func() {...}
1401 regexp:
1402 "error: .*"
1403 `,
1404 }, {
1405 about: "PanicMatches: mismatch",
1406 checker: qt.PanicMatches,
1407 got: func() { panic("error: bad wolf") },
1408 args: []interface{}{"error: exterminate"},
1409 expectedCheckFailure: `
1410 error:
1411 panic value does not match regexp
1412 panic value:
1413 "error: bad wolf"
1414 function:
1415 func() {...}
1416 regexp:
1417 "error: exterminate"
1418 `,
1419 }, {
1420 about: "PanicMatches: empty pattern",
1421 checker: qt.PanicMatches,
1422 got: func() { panic("error: bad wolf") },
1423 args: []interface{}{""},
1424 expectedCheckFailure: `
1425 error:
1426 panic value does not match regexp
1427 panic value:
1428 "error: bad wolf"
1429 function:
1430 func() {...}
1431 regexp:
1432 ""
1433 `,
1434 }, {
1435 about: "PanicMatches: complex pattern",
1436 checker: qt.PanicMatches,
1437 got: func() { panic("bad wolf") },
1438 args: []interface{}{"bad wolf|end of the universe"},
1439 expectedNegateFailure: `
1440 error:
1441 unexpected success
1442 panic value:
1443 "bad wolf"
1444 function:
1445 func() {...}
1446 regexp:
1447 "bad wolf|end of the universe"
1448 `,
1449 }, {
1450 about: "PanicMatches: invalid pattern",
1451 checker: qt.PanicMatches,
1452 got: func() { panic("error: bad wolf") },
1453 args: []interface{}{"("},
1454 expectedCheckFailure: `
1455 error:
1456 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1457 panic value:
1458 "error: bad wolf"
1459 regexp:
1460 "("
1461 `,
1462 expectedNegateFailure: `
1463 error:
1464 bad check: cannot compile regexp: error parsing regexp: missing closing ): ` + "`^(()$`" + `
1465 panic value:
1466 "error: bad wolf"
1467 regexp:
1468 "("
1469 `,
1470 }, {
1471 about: "PanicMatches: pattern not a string",
1472 checker: qt.PanicMatches,
1473 got: func() { panic("error: bad wolf") },
1474 args: []interface{}{nil},
1475 expectedCheckFailure: `
1476 error:
1477 bad check: regexp is not a string
1478 panic value:
1479 "error: bad wolf"
1480 regexp:
1481 nil
1482 `,
1483 expectedNegateFailure: `
1484 error:
1485 bad check: regexp is not a string
1486 panic value:
1487 "error: bad wolf"
1488 regexp:
1489 nil
1490 `,
1491 }, {
1492 about: "PanicMatches: match with pre-compiled regexp",
1493 checker: qt.PanicMatches,
1494 got: func() { panic("error: bad wolf") },
1495 args: []interface{}{regexp.MustCompile("error: bad (wolf|dog)")},
1496 expectedNegateFailure: `
1497 error:
1498 unexpected success
1499 panic value:
1500 "error: bad wolf"
1501 function:
1502 func() {...}
1503 regexp:
1504 s"error: bad (wolf|dog)"
1505 `,
1506 }, {
1507 about: "PanicMatches: match with pre-compiled multi-line regexp",
1508 checker: qt.PanicMatches,
1509 got: func() { panic("error: bad wolf\nfaulty logic") },
1510 args: []interface{}{regexp.MustCompile(`error: bad (wolf|dog)\nfaulty (logic|statement)`)},
1511 expectedNegateFailure: `
1512 error:
1513 unexpected success
1514 panic value:
1515 "error: bad wolf\nfaulty logic"
1516 function:
1517 func() {...}
1518 regexp:
1519 s"error: bad (wolf|dog)\\nfaulty (logic|statement)"
1520 `,
1521 }, {
1522 about: "PanicMatches: mismatch with pre-compiled regexp",
1523 checker: qt.PanicMatches,
1524 got: func() { panic("error: bad wolf") },
1525 args: []interface{}{regexp.MustCompile("good (wolf|dog)")},
1526 expectedCheckFailure: `
1527 error:
1528 panic value does not match regexp
1529 panic value:
1530 "error: bad wolf"
1531 function:
1532 func() {...}
1533 regexp:
1534 s"good (wolf|dog)"
1535 `,
1536 }, {
1537 about: "PanicMatches: not a function",
1538 checker: qt.PanicMatches,
1539 got: map[string]int{"answer": 42},
1540 args: []interface{}{".*"},
1541 expectedCheckFailure: `
1542 error:
1543 bad check: first argument is not a function
1544 got:
1545 map[string]int{"answer":42}
1546 `,
1547 expectedNegateFailure: `
1548 error:
1549 bad check: first argument is not a function
1550 got:
1551 map[string]int{"answer":42}
1552 `,
1553 }, {
1554 about: "PanicMatches: not a proper function",
1555 checker: qt.PanicMatches,
1556 got: func(int) { panic("error: bad wolf") },
1557 args: []interface{}{".*"},
1558 expectedCheckFailure: `
1559 error:
1560 bad check: cannot use a function receiving arguments
1561 function:
1562 func(int) {...}
1563 `,
1564 expectedNegateFailure: `
1565 error:
1566 bad check: cannot use a function receiving arguments
1567 function:
1568 func(int) {...}
1569 `,
1570 }, {
1571 about: "PanicMatches: function returning something",
1572 checker: qt.PanicMatches,
1573 got: func() error { panic("error: bad wolf") },
1574 args: []interface{}{".*"},
1575 expectedNegateFailure: `
1576 error:
1577 unexpected success
1578 panic value:
1579 "error: bad wolf"
1580 function:
1581 func() error {...}
1582 regexp:
1583 ".*"
1584 `,
1585 }, {
1586 about: "PanicMatches: no panic",
1587 checker: qt.PanicMatches,
1588 got: func() {},
1589 args: []interface{}{".*"},
1590 expectedCheckFailure: `
1591 error:
1592 function did not panic
1593 function:
1594 func() {...}
1595 regexp:
1596 ".*"
1597 `,
1598 }, {
1599 about: "PanicMatches: not enough arguments",
1600 checker: qt.PanicMatches,
1601 expectedCheckFailure: `
1602 error:
1603 bad check: not enough arguments provided to checker: got 0, want 1
1604 want args:
1605 regexp
1606 `,
1607 expectedNegateFailure: `
1608 error:
1609 bad check: not enough arguments provided to checker: got 0, want 1
1610 want args:
1611 regexp
1612 `,
1613 }, {
1614 about: "PanicMatches: too many arguments",
1615 checker: qt.PanicMatches,
1616 got: func() { panic("error: bad wolf") },
1617 args: []interface{}{"error: bad wolf", 42},
1618 expectedCheckFailure: `
1619 error:
1620 bad check: too many arguments provided to checker: got 2, want 1
1621 got args:
1622 []interface {}{
1623 "error: bad wolf",
1624 int(42),
1625 }
1626 want args:
1627 regexp
1628 `,
1629 expectedNegateFailure: `
1630 error:
1631 bad check: too many arguments provided to checker: got 2, want 1
1632 got args:
1633 []interface {}{
1634 "error: bad wolf",
1635 int(42),
1636 }
1637 want args:
1638 regexp
1639 `,
1640 }, {
1641 about: "IsNil: nil",
1642 checker: qt.IsNil,
1643 got: nil,
1644 expectedNegateFailure: `
1645 error:
1646 got nil value but want non-nil
1647 got:
1648 nil
1649 `,
1650 }, {
1651 about: "IsNil: nil struct",
1652 checker: qt.IsNil,
1653 got: (*struct{})(nil),
1654 expectedNegateFailure: `
1655 error:
1656 got nil value but want non-nil
1657 got:
1658 (*struct {})(nil)
1659 `,
1660 }, {
1661 about: "IsNil: nil func",
1662 checker: qt.IsNil,
1663 got: (func())(nil),
1664 expectedNegateFailure: `
1665 error:
1666 got nil value but want non-nil
1667 got:
1668 func() {...}
1669 `,
1670 }, {
1671 about: "IsNil: nil map",
1672 checker: qt.IsNil,
1673 got: (map[string]string)(nil),
1674 expectedNegateFailure: `
1675 error:
1676 got nil value but want non-nil
1677 got:
1678 map[string]string{}
1679 `,
1680 }, {
1681 about: "IsNil: nil slice",
1682 checker: qt.IsNil,
1683 got: ([]int)(nil),
1684 expectedNegateFailure: `
1685 error:
1686 got nil value but want non-nil
1687 got:
1688 []int(nil)
1689 `,
1690 }, {
1691 about: "IsNil: nil error-implementing type",
1692 checker: qt.IsNil,
1693 got: (*errTest)(nil),
1694 expectedCheckFailure: `
1695 error:
1696 error containing nil value of type *quicktest_test.errTest. See https://golang.org/doc/faq#nil_error
1697 got:
1698 e<nil>
1699 `,
1700 }, {
1701 about: "IsNil: not nil",
1702 checker: qt.IsNil,
1703 got: 42,
1704 expectedCheckFailure: `
1705 error:
1706 got non-nil value
1707 got:
1708 int(42)
1709 `,
1710 }, {
1711 about: "IsNil: error is not nil",
1712 checker: qt.IsNil,
1713 got: errBadWolf,
1714 expectedCheckFailure: `
1715 error:
1716 got non-nil error
1717 got:
1718 bad wolf
1719 file:line
1720 `,
1721 }, {
1722 about: "IsNil: too many arguments",
1723 checker: qt.IsNil,
1724 args: []interface{}{"not nil"},
1725 expectedCheckFailure: `
1726 error:
1727 bad check: too many arguments provided to checker: got 1, want 0
1728 got args:
1729 []interface {}{
1730 "not nil",
1731 }
1732 `,
1733 expectedNegateFailure: `
1734 error:
1735 bad check: too many arguments provided to checker: got 1, want 0
1736 got args:
1737 []interface {}{
1738 "not nil",
1739 }
1740 `,
1741 }, {
1742 about: "IsNotNil: success",
1743 checker: qt.IsNotNil,
1744 got: 42,
1745 expectedNegateFailure: `
1746 error:
1747 got non-nil value
1748 got:
1749 int(42)
1750 `,
1751 }, {
1752 about: "IsNotNil: failure",
1753 checker: qt.IsNotNil,
1754 got: nil,
1755 expectedCheckFailure: `
1756 error:
1757 got nil value but want non-nil
1758 got:
1759 nil
1760 `,
1761 }, {
1762 about: "HasLen: arrays with the same length",
1763 checker: qt.HasLen,
1764 got: [4]string{"these", "are", "the", "voyages"},
1765 args: []interface{}{4},
1766 expectedNegateFailure: `
1767 error:
1768 unexpected success
1769 len(got):
1770 int(4)
1771 got:
1772 [4]string{"these", "are", "the", "voyages"}
1773 want length:
1774 <same as "len(got)">
1775 `,
1776 }, {
1777 about: "HasLen: channels with the same length",
1778 checker: qt.HasLen,
1779 got: chInt,
1780 args: []interface{}{2},
1781 expectedNegateFailure: fmt.Sprintf(`
1782 error:
1783 unexpected success
1784 len(got):
1785 int(2)
1786 got:
1787 (chan int)(%v)
1788 want length:
1789 <same as "len(got)">
1790 `, chInt),
1791 }, {
1792 about: "HasLen: maps with the same length",
1793 checker: qt.HasLen,
1794 got: map[string]bool{"true": true},
1795 args: []interface{}{1},
1796 expectedNegateFailure: `
1797 error:
1798 unexpected success
1799 len(got):
1800 int(1)
1801 got:
1802 map[string]bool{"true":true}
1803 want length:
1804 <same as "len(got)">
1805 `,
1806 }, {
1807 about: "HasLen: slices with the same length",
1808 checker: qt.HasLen,
1809 got: []int{},
1810 args: []interface{}{0},
1811 expectedNegateFailure: `
1812 error:
1813 unexpected success
1814 len(got):
1815 int(0)
1816 got:
1817 []int{}
1818 want length:
1819 <same as "len(got)">
1820 `,
1821 }, {
1822 about: "HasLen: strings with the same length",
1823 checker: qt.HasLen,
1824 got: "these are the voyages",
1825 args: []interface{}{21},
1826 expectedNegateFailure: `
1827 error:
1828 unexpected success
1829 len(got):
1830 int(21)
1831 got:
1832 "these are the voyages"
1833 want length:
1834 <same as "len(got)">
1835 `,
1836 }, {
1837 about: "HasLen: arrays with different lengths",
1838 checker: qt.HasLen,
1839 got: [4]string{"these", "are", "the", "voyages"},
1840 args: []interface{}{0},
1841 expectedCheckFailure: `
1842 error:
1843 unexpected length
1844 len(got):
1845 int(4)
1846 got:
1847 [4]string{"these", "are", "the", "voyages"}
1848 want length:
1849 int(0)
1850 `,
1851 }, {
1852 about: "HasLen: channels with different lengths",
1853 checker: qt.HasLen,
1854 got: chInt,
1855 args: []interface{}{4},
1856 expectedCheckFailure: fmt.Sprintf(`
1857 error:
1858 unexpected length
1859 len(got):
1860 int(2)
1861 got:
1862 (chan int)(%v)
1863 want length:
1864 int(4)
1865 `, chInt),
1866 }, {
1867 about: "HasLen: maps with different lengths",
1868 checker: qt.HasLen,
1869 got: map[string]bool{"true": true},
1870 args: []interface{}{42},
1871 expectedCheckFailure: `
1872 error:
1873 unexpected length
1874 len(got):
1875 int(1)
1876 got:
1877 map[string]bool{"true":true}
1878 want length:
1879 int(42)
1880 `,
1881 }, {
1882 about: "HasLen: slices with different lengths",
1883 checker: qt.HasLen,
1884 got: []int{42, 47},
1885 args: []interface{}{1},
1886 expectedCheckFailure: `
1887 error:
1888 unexpected length
1889 len(got):
1890 int(2)
1891 got:
1892 []int{42, 47}
1893 want length:
1894 int(1)
1895 `,
1896 }, {
1897 about: "HasLen: strings with different lengths",
1898 checker: qt.HasLen,
1899 got: "these are the voyages",
1900 args: []interface{}{42},
1901 expectedCheckFailure: `
1902 error:
1903 unexpected length
1904 len(got):
1905 int(21)
1906 got:
1907 "these are the voyages"
1908 want length:
1909 int(42)
1910 `,
1911 }, {
1912 about: "HasLen: value without a length",
1913 checker: qt.HasLen,
1914 got: 42,
1915 args: []interface{}{42},
1916 expectedCheckFailure: `
1917 error:
1918 bad check: first argument has no length
1919 got:
1920 int(42)
1921 `,
1922 expectedNegateFailure: `
1923 error:
1924 bad check: first argument has no length
1925 got:
1926 int(42)
1927 `,
1928 }, {
1929 about: "HasLen: expected value not a number",
1930 checker: qt.HasLen,
1931 got: "these are the voyages",
1932 args: []interface{}{"bad wolf"},
1933 expectedCheckFailure: `
1934 error:
1935 bad check: length is not an int
1936 length:
1937 "bad wolf"
1938 `,
1939 expectedNegateFailure: `
1940 error:
1941 bad check: length is not an int
1942 length:
1943 "bad wolf"
1944 `,
1945 }, {
1946 about: "HasLen: not enough arguments",
1947 checker: qt.HasLen,
1948 expectedCheckFailure: `
1949 error:
1950 bad check: not enough arguments provided to checker: got 0, want 1
1951 want args:
1952 want length
1953 `,
1954 expectedNegateFailure: `
1955 error:
1956 bad check: not enough arguments provided to checker: got 0, want 1
1957 want args:
1958 want length
1959 `,
1960 }, {
1961 about: "HasLen: too many arguments",
1962 checker: qt.HasLen,
1963 got: []int{42},
1964 args: []interface{}{42, 47},
1965 expectedCheckFailure: `
1966 error:
1967 bad check: too many arguments provided to checker: got 2, want 1
1968 got args:
1969 []interface {}{
1970 int(42),
1971 int(47),
1972 }
1973 want args:
1974 want length
1975 `,
1976 expectedNegateFailure: `
1977 error:
1978 bad check: too many arguments provided to checker: got 2, want 1
1979 got args:
1980 []interface {}{
1981 int(42),
1982 int(47),
1983 }
1984 want args:
1985 want length
1986 `,
1987 }, {
1988 about: "Implements: implements interface",
1989 checker: qt.Implements,
1990 got: errBadWolf,
1991 args: []interface{}{(*error)(nil)},
1992 expectedNegateFailure: `
1993 error:
1994 unexpected success
1995 got:
1996 bad wolf
1997 file:line
1998 want interface pointer:
1999 (*error)(nil)
2000 `,
2001 }, {
2002 about: "Implements: does not implement interface",
2003 checker: qt.Implements,
2004 got: errBadWolf,
2005 args: []interface{}{(*Fooer)(nil)},
2006 expectedCheckFailure: `
2007 error:
2008 got value does not implement wanted interface
2009 got:
2010 bad wolf
2011 file:line
2012 want interface:
2013 quicktest_test.Fooer
2014 `,
2015 }, {
2016 about: "Implements: fails if got nil",
2017 checker: qt.Implements,
2018 got: nil,
2019 args: []interface{}{(*Fooer)(nil)},
2020 expectedCheckFailure: `
2021 error:
2022 got nil value but want non-nil
2023 got:
2024 nil
2025 `,
2026 }, {
2027 about: "Implements: bad check if wanted is nil",
2028 checker: qt.Implements,
2029 got: errBadWolf,
2030 args: []interface{}{nil},
2031 expectedCheckFailure: `
2032 error:
2033 bad check: want a pointer to an interface variable but nil was provided
2034 `,
2035 expectedNegateFailure: `
2036 error:
2037 bad check: want a pointer to an interface variable but nil was provided
2038 `,
2039 }, {
2040 about: "Implements: bad check if wanted is not pointer",
2041 checker: qt.Implements,
2042 got: errBadWolf,
2043 args: []interface{}{struct{}{}},
2044 expectedCheckFailure: `
2045 error:
2046 bad check: want a pointer to an interface variable but a non-pointer value was provided
2047 want:
2048 struct {}
2049 `,
2050 expectedNegateFailure: `
2051 error:
2052 bad check: want a pointer to an interface variable but a non-pointer value was provided
2053 want:
2054 struct {}
2055 `,
2056 }, {
2057 about: "Implements: bad check if wanted is not pointer to interface",
2058 checker: qt.Implements,
2059 got: errBadWolf,
2060 args: []interface{}{(*struct{})(nil)},
2061 expectedCheckFailure: `
2062 error:
2063 bad check: want a pointer to an interface variable but a pointer to a concrete type was provided
2064 want pointer type:
2065 struct {}
2066 `,
2067 expectedNegateFailure: `
2068 error:
2069 bad check: want a pointer to an interface variable but a pointer to a concrete type was provided
2070 want pointer type:
2071 struct {}
2072 `,
2073 }, {
2074 about: "Implements: bad check if wanted is a pointer to the empty interface",
2075 checker: qt.Implements,
2076 got: 42,
2077 args: []interface{}{(*interface{})(nil)},
2078 expectedCheckFailure: `
2079 error:
2080 bad check: all types implement the empty interface, want a pointer to a variable that isn't the empty interface
2081 want pointer type:
2082 interface {}
2083 `,
2084 expectedNegateFailure: `
2085 error:
2086 bad check: all types implement the empty interface, want a pointer to a variable that isn't the empty interface
2087 want pointer type:
2088 interface {}
2089 `,
2090 }, {
2091 about: "Satisfies: success with an error",
2092 checker: qt.Satisfies,
2093 got: qt.BadCheckf("bad wolf"),
2094 args: []interface{}{qt.IsBadCheck},
2095 expectedNegateFailure: `
2096 error:
2097 unexpected success
2098 arg:
2099 e"bad check: bad wolf"
2100 predicate function:
2101 func(error) bool {...}
2102 `,
2103 }, {
2104 about: "Satisfies: success with an int",
2105 checker: qt.Satisfies,
2106 got: 42,
2107 args: []interface{}{
2108 func(v int) bool { return v == 42 },
2109 },
2110 expectedNegateFailure: `
2111 error:
2112 unexpected success
2113 arg:
2114 int(42)
2115 predicate function:
2116 func(int) bool {...}
2117 `,
2118 }, {
2119 about: "Satisfies: success with nil",
2120 checker: qt.Satisfies,
2121 got: nil,
2122 args: []interface{}{
2123 func(v []int) bool { return true },
2124 },
2125 expectedNegateFailure: `
2126 error:
2127 unexpected success
2128 arg:
2129 nil
2130 predicate function:
2131 func([]int) bool {...}
2132 `,
2133 }, {
2134 about: "Satisfies: failure with an error",
2135 checker: qt.Satisfies,
2136 got: nil,
2137 args: []interface{}{qt.IsBadCheck},
2138 expectedCheckFailure: `
2139 error:
2140 value does not satisfy predicate function
2141 arg:
2142 nil
2143 predicate function:
2144 func(error) bool {...}
2145 `,
2146 }, {
2147 about: "Satisfies: failure with a string",
2148 checker: qt.Satisfies,
2149 got: "bad wolf",
2150 args: []interface{}{
2151 func(string) bool { return false },
2152 },
2153 expectedCheckFailure: `
2154 error:
2155 value does not satisfy predicate function
2156 arg:
2157 "bad wolf"
2158 predicate function:
2159 func(string) bool {...}
2160 `,
2161 }, {
2162 about: "Satisfies: not a function",
2163 checker: qt.Satisfies,
2164 got: 42,
2165 args: []interface{}{42},
2166 expectedCheckFailure: `
2167 error:
2168 bad check: predicate function is not a func(T) bool
2169 predicate function:
2170 int(42)
2171 `,
2172 expectedNegateFailure: `
2173 error:
2174 bad check: predicate function is not a func(T) bool
2175 predicate function:
2176 int(42)
2177 `,
2178 }, {
2179 about: "Satisfies: function accepting no arguments",
2180 checker: qt.Satisfies,
2181 got: 42,
2182 args: []interface{}{
2183 func() bool { return true },
2184 },
2185 expectedCheckFailure: `
2186 error:
2187 bad check: predicate function is not a func(T) bool
2188 predicate function:
2189 func() bool {...}
2190 `,
2191 expectedNegateFailure: `
2192 error:
2193 bad check: predicate function is not a func(T) bool
2194 predicate function:
2195 func() bool {...}
2196 `,
2197 }, {
2198 about: "Satisfies: function accepting too many arguments",
2199 checker: qt.Satisfies,
2200 got: 42,
2201 args: []interface{}{
2202 func(int, string) bool { return false },
2203 },
2204 expectedCheckFailure: `
2205 error:
2206 bad check: predicate function is not a func(T) bool
2207 predicate function:
2208 func(int, string) bool {...}
2209 `,
2210 expectedNegateFailure: `
2211 error:
2212 bad check: predicate function is not a func(T) bool
2213 predicate function:
2214 func(int, string) bool {...}
2215 `,
2216 }, {
2217 about: "Satisfies: function returning no arguments",
2218 checker: qt.Satisfies,
2219 got: 42,
2220 args: []interface{}{
2221 func(error) {},
2222 },
2223 expectedCheckFailure: `
2224 error:
2225 bad check: predicate function is not a func(T) bool
2226 predicate function:
2227 func(error) {...}
2228 `,
2229 expectedNegateFailure: `
2230 error:
2231 bad check: predicate function is not a func(T) bool
2232 predicate function:
2233 func(error) {...}
2234 `,
2235 }, {
2236 about: "Satisfies: function returning too many argments",
2237 checker: qt.Satisfies,
2238 got: 42,
2239 args: []interface{}{
2240 func(int) (bool, error) { return true, nil },
2241 },
2242 expectedCheckFailure: `
2243 error:
2244 bad check: predicate function is not a func(T) bool
2245 predicate function:
2246 func(int) (bool, error) {...}
2247 `,
2248 expectedNegateFailure: `
2249 error:
2250 bad check: predicate function is not a func(T) bool
2251 predicate function:
2252 func(int) (bool, error) {...}
2253 `,
2254 }, {
2255 about: "Satisfies: function not returning a bool",
2256 checker: qt.Satisfies,
2257 got: 42,
2258 args: []interface{}{
2259 func(int) error { return nil },
2260 },
2261 expectedCheckFailure: `
2262 error:
2263 bad check: predicate function is not a func(T) bool
2264 predicate function:
2265 func(int) error {...}
2266 `,
2267 expectedNegateFailure: `
2268 error:
2269 bad check: predicate function is not a func(T) bool
2270 predicate function:
2271 func(int) error {...}
2272 `,
2273 }, {
2274 about: "Satisfies: type mismatch",
2275 checker: qt.Satisfies,
2276 got: 42,
2277 args: []interface{}{qt.IsBadCheck},
2278 expectedCheckFailure: `
2279 error:
2280 bad check: cannot use value of type int as type error in argument to predicate function
2281 arg:
2282 int(42)
2283 predicate function:
2284 func(error) bool {...}
2285 `,
2286 expectedNegateFailure: `
2287 error:
2288 bad check: cannot use value of type int as type error in argument to predicate function
2289 arg:
2290 int(42)
2291 predicate function:
2292 func(error) bool {...}
2293 `,
2294 }, {
2295 about: "Satisfies: nil value that cannot be nil",
2296 checker: qt.Satisfies,
2297 got: nil,
2298 args: []interface{}{
2299 func(string) bool { return true },
2300 },
2301 expectedCheckFailure: `
2302 error:
2303 bad check: cannot use nil as type string in argument to predicate function
2304 predicate function:
2305 func(string) bool {...}
2306 `,
2307 expectedNegateFailure: `
2308 error:
2309 bad check: cannot use nil as type string in argument to predicate function
2310 predicate function:
2311 func(string) bool {...}
2312 `,
2313 }, {
2314 about: "Satisfies: not enough arguments",
2315 checker: qt.Satisfies,
2316 expectedCheckFailure: `
2317 error:
2318 bad check: not enough arguments provided to checker: got 0, want 1
2319 want args:
2320 predicate function
2321 `,
2322 expectedNegateFailure: `
2323 error:
2324 bad check: not enough arguments provided to checker: got 0, want 1
2325 want args:
2326 predicate function
2327 `,
2328 }, {
2329 about: "Satisfies: too many arguments",
2330 checker: qt.Satisfies,
2331 got: 42,
2332 args: []interface{}{func() bool { return true }, 1, 2},
2333 expectedCheckFailure: `
2334 error:
2335 bad check: too many arguments provided to checker: got 3, want 1
2336 got args:
2337 []interface {}{
2338 func() bool {...},
2339 int(1),
2340 int(2),
2341 }
2342 want args:
2343 predicate function
2344 `,
2345 expectedNegateFailure: `
2346 error:
2347 bad check: too many arguments provided to checker: got 3, want 1
2348 got args:
2349 []interface {}{
2350 func() bool {...},
2351 int(1),
2352 int(2),
2353 }
2354 want args:
2355 predicate function
2356 `,
2357 }, {
2358 about: "IsTrue: success",
2359 checker: qt.IsTrue,
2360 got: true,
2361 expectedNegateFailure: `
2362 error:
2363 unexpected success
2364 got:
2365 bool(true)
2366 `,
2367 }, {
2368 about: "IsTrue: failure",
2369 checker: qt.IsTrue,
2370 got: false,
2371 expectedCheckFailure: `
2372 error:
2373 value is not true
2374 got:
2375 bool(false)
2376 `,
2377 }, {
2378 about: "IsTrue: success with subtype",
2379 checker: qt.IsTrue,
2380 got: boolean(true),
2381 expectedNegateFailure: `
2382 error:
2383 unexpected success
2384 got:
2385 quicktest_test.boolean(true)
2386 `,
2387 }, {
2388 about: "IsTrue: failure with subtype",
2389 checker: qt.IsTrue,
2390 got: boolean(false),
2391 expectedCheckFailure: `
2392 error:
2393 value is not true
2394 got:
2395 quicktest_test.boolean(false)
2396 `,
2397 }, {
2398 about: "IsTrue: nil value",
2399 checker: qt.IsTrue,
2400 got: nil,
2401 expectedCheckFailure: `
2402 error:
2403 bad check: value does not have a bool underlying type
2404 value:
2405 nil
2406 `,
2407 expectedNegateFailure: `
2408 error:
2409 bad check: value does not have a bool underlying type
2410 value:
2411 nil
2412 `,
2413 }, {
2414 about: "IsTrue: non-bool value",
2415 checker: qt.IsTrue,
2416 got: 42,
2417 expectedCheckFailure: `
2418 error:
2419 bad check: value does not have a bool underlying type
2420 value:
2421 int(42)
2422 `,
2423 expectedNegateFailure: `
2424 error:
2425 bad check: value does not have a bool underlying type
2426 value:
2427 int(42)
2428 `,
2429 }, {
2430 about: "IsFalse: success",
2431 checker: qt.IsFalse,
2432 got: false,
2433 expectedNegateFailure: `
2434 error:
2435 unexpected success
2436 got:
2437 bool(false)
2438 `,
2439 }, {
2440 about: "IsFalse: failure",
2441 checker: qt.IsFalse,
2442 got: true,
2443 expectedCheckFailure: `
2444 error:
2445 value is not false
2446 got:
2447 bool(true)
2448 `,
2449 }, {
2450 about: "IsFalse: success with subtype",
2451 checker: qt.IsFalse,
2452 got: boolean(false),
2453 expectedNegateFailure: `
2454 error:
2455 unexpected success
2456 got:
2457 quicktest_test.boolean(false)
2458 `,
2459 }, {
2460 about: "IsFalse: failure with subtype",
2461 checker: qt.IsFalse,
2462 got: boolean(true),
2463 expectedCheckFailure: `
2464 error:
2465 value is not false
2466 got:
2467 quicktest_test.boolean(true)
2468 `,
2469 }, {
2470 about: "IsFalse: nil value",
2471 checker: qt.IsFalse,
2472 got: nil,
2473 expectedCheckFailure: `
2474 error:
2475 bad check: value does not have a bool underlying type
2476 value:
2477 nil
2478 `,
2479 expectedNegateFailure: `
2480 error:
2481 bad check: value does not have a bool underlying type
2482 value:
2483 nil
2484 `,
2485 }, {
2486 about: "IsFalse: non-bool value",
2487 checker: qt.IsFalse,
2488 got: "bad wolf",
2489 expectedCheckFailure: `
2490 error:
2491 bad check: value does not have a bool underlying type
2492 value:
2493 "bad wolf"
2494 `,
2495 expectedNegateFailure: `
2496 error:
2497 bad check: value does not have a bool underlying type
2498 value:
2499 "bad wolf"
2500 `,
2501 }, {
2502 about: "Not: success",
2503 checker: qt.Not(qt.IsNil),
2504 got: 42,
2505 expectedNegateFailure: `
2506 error:
2507 got non-nil value
2508 got:
2509 int(42)
2510 `,
2511 }, {
2512 about: "Not: failure",
2513 checker: qt.Not(qt.Equals),
2514 got: 42,
2515 args: []interface{}{42},
2516 expectedCheckFailure: `
2517 error:
2518 unexpected success
2519 got:
2520 int(42)
2521 want:
2522 <same as "got">
2523 `,
2524 }, {
2525 about: "Not: IsNil failure",
2526 checker: qt.Not(qt.IsNil),
2527 got: nil,
2528 expectedCheckFailure: `
2529 error:
2530 got nil value but want non-nil
2531 got:
2532 nil
2533 `,
2534 }, {
2535 about: "Not: not enough arguments",
2536 checker: qt.Not(qt.PanicMatches),
2537 expectedCheckFailure: `
2538 error:
2539 bad check: not enough arguments provided to checker: got 0, want 1
2540 want args:
2541 regexp
2542 `,
2543 expectedNegateFailure: `
2544 error:
2545 bad check: not enough arguments provided to checker: got 0, want 1
2546 want args:
2547 regexp
2548 `,
2549 }, {
2550 about: "Not: too many arguments",
2551 checker: qt.Not(qt.Equals),
2552 args: []interface{}{42, nil},
2553 expectedCheckFailure: `
2554 error:
2555 bad check: too many arguments provided to checker: got 2, want 1
2556 got args:
2557 []interface {}{
2558 int(42),
2559 nil,
2560 }
2561 want args:
2562 want
2563 `,
2564 expectedNegateFailure: `
2565 error:
2566 bad check: too many arguments provided to checker: got 2, want 1
2567 got args:
2568 []interface {}{
2569 int(42),
2570 nil,
2571 }
2572 want args:
2573 want
2574 `,
2575 }, {
2576 about: "Contains with string",
2577 checker: qt.Contains,
2578 got: "hello, world",
2579 args: []interface{}{"world"},
2580 expectedNegateFailure: `
2581 error:
2582 unexpected success
2583 container:
2584 "hello, world"
2585 want:
2586 "world"
2587 `,
2588 }, {
2589 about: "Contains with string no match",
2590 checker: qt.Contains,
2591 got: "hello, world",
2592 args: []interface{}{"worlds"},
2593 expectedCheckFailure: `
2594 error:
2595 no substring match found
2596 container:
2597 "hello, world"
2598 want:
2599 "worlds"
2600 `,
2601 }, {
2602 about: "Contains with slice",
2603 checker: qt.Contains,
2604 got: []string{"a", "b", "c"},
2605 args: []interface{}{"a"},
2606 expectedNegateFailure: `
2607 error:
2608 unexpected success
2609 container:
2610 []string{"a", "b", "c"}
2611 want:
2612 "a"
2613 `,
2614 }, {
2615 about: "Contains with map",
2616 checker: qt.Contains,
2617 // Note: we can't use more than one element here because
2618 // pretty.Print output is non-deterministic.
2619 // https://github.com/kr/pretty/issues/47
2620 got: map[string]string{"a": "d"},
2621 args: []interface{}{"d"},
2622 expectedNegateFailure: `
2623 error:
2624 unexpected success
2625 container:
2626 map[string]string{"a":"d"}
2627 want:
2628 "d"
2629 `,
2630 }, {
2631 about: "Contains with non-string",
2632 checker: qt.Contains,
2633 got: "aa",
2634 args: []interface{}{5},
2635 expectedCheckFailure: `
2636 error:
2637 bad check: strings can only contain strings, not int
2638 `,
2639 expectedNegateFailure: `
2640 error:
2641 bad check: strings can only contain strings, not int
2642 `,
2643 }, {
2644 about: "All slice equals",
2645 checker: qt.All(qt.Equals),
2646 got: []string{"a", "a"},
2647 args: []interface{}{"a"},
2648 expectedNegateFailure: `
2649 error:
2650 unexpected success
2651 container:
2652 []string{"a", "a"}
2653 want:
2654 "a"
2655 `,
2656 }, {
2657 about: "All slice match",
2658 checker: qt.All(qt.Matches),
2659 got: []string{"red", "blue", "green"},
2660 args: []interface{}{".*e.*"},
2661 expectedNegateFailure: `
2662 error:
2663 unexpected success
2664 container:
2665 []string{"red", "blue", "green"}
2666 regexp:
2667 ".*e.*"
2668 `,
2669 }, {
2670 about: "All nested match",
2671 checker: qt.All(qt.All(qt.Matches)),
2672 got: [][]string{{"hello", "goodbye"}, {"red", "blue"}, {}},
2673 args: []interface{}{".*e.*"},
2674 expectedNegateFailure: `
2675 error:
2676 unexpected success
2677 container:
2678 [][]string{
2679 {"hello", "goodbye"},
2680 {"red", "blue"},
2681 {},
2682 }
2683 regexp:
2684 ".*e.*"
2685 `,
2686 }, {
2687 about: "All nested mismatch",
2688 checker: qt.All(qt.All(qt.Matches)),
2689 got: [][]string{{"hello", "goodbye"}, {"black", "blue"}, {}},
2690 args: []interface{}{".*e.*"},
2691 expectedCheckFailure: `
2692 error:
2693 mismatch at index 1
2694 error:
2695 mismatch at index 0
2696 error:
2697 value does not match regexp
2698 first mismatched element:
2699 "black"
2700 `,
2701 }, {
2702 about: "All slice mismatch",
2703 checker: qt.All(qt.Matches),
2704 got: []string{"red", "black"},
2705 args: []interface{}{".*e.*"},
2706 expectedCheckFailure: `
2707 error:
2708 mismatch at index 1
2709 error:
2710 value does not match regexp
2711 first mismatched element:
2712 "black"
2713 `,
2714 }, {
2715 about: "All slice mismatch with DeepEqual",
2716 checker: qt.All(qt.DeepEquals),
2717 got: [][]string{{"a", "b"}, {"a", "c"}},
2718 args: []interface{}{[]string{"a", "b"}},
2719 expectedCheckFailure: fmt.Sprintf(`
2720 error:
2721 mismatch at index 1
2722 error:
2723 values are not deep equal
2724 diff (-got +want):
2725 %s
2726 got:
2727 []string{"a", "c"}
2728 want:
2729 []string{"a", "b"}
2730 `, diff([]string{"a", "c"}, []string{"a", "b"})),
2731 }, {
2732 about: "All bad checker args count",
2733 checker: qt.All(qt.IsNil),
2734 got: []int{},
2735 args: []interface{}{5},
2736 expectedCheckFailure: `
2737 error:
2738 bad check: too many arguments provided to checker: got 1, want 0
2739 got args:
2740 []interface {}{
2741 int(5),
2742 }
2743 `,
2744 expectedNegateFailure: `
2745 error:
2746 bad check: too many arguments provided to checker: got 1, want 0
2747 got args:
2748 []interface {}{
2749 int(5),
2750 }
2751 `,
2752 }, {
2753 about: "All bad checker args",
2754 checker: qt.All(qt.Matches),
2755 got: []string{"hello"},
2756 args: []interface{}{5},
2757 expectedCheckFailure: `
2758 error:
2759 bad check: at index 0: bad check: regexp is not a string
2760 `,
2761 expectedNegateFailure: `
2762 error:
2763 bad check: at index 0: bad check: regexp is not a string
2764 `,
2765 }, {
2766 about: "All with non-container",
2767 checker: qt.All(qt.Equals),
2768 got: 5,
2769 args: []interface{}{5},
2770 expectedCheckFailure: `
2771 error:
2772 bad check: map, slice or array required
2773 `,
2774 expectedNegateFailure: `
2775 error:
2776 bad check: map, slice or array required
2777 `,
2778 }, {
2779 about: "All mismatch with map",
2780 checker: qt.All(qt.Matches),
2781 got: map[string]string{"a": "red", "b": "black"},
2782 args: []interface{}{".*e.*"},
2783 expectedCheckFailure: `
2784 error:
2785 mismatch at key "b"
2786 error:
2787 value does not match regexp
2788 first mismatched element:
2789 "black"
2790 `,
2791 }, {
2792 about: "Any with non-container",
2793 checker: qt.Any(qt.Equals),
2794 got: 5,
2795 args: []interface{}{5},
2796 expectedCheckFailure: `
2797 error:
2798 bad check: map, slice or array required
2799 `,
2800 expectedNegateFailure: `
2801 error:
2802 bad check: map, slice or array required
2803 `,
2804 }, {
2805 about: "Any no match",
2806 checker: qt.Any(qt.Equals),
2807 got: []int{},
2808 args: []interface{}{5},
2809 expectedCheckFailure: `
2810 error:
2811 no matching element found
2812 container:
2813 []int{}
2814 want:
2815 int(5)
2816 `,
2817 }, {
2818 about: "Any bad checker arg count",
2819 checker: qt.Any(qt.IsNil),
2820 got: []int{},
2821 args: []interface{}{5},
2822 expectedCheckFailure: `
2823 error:
2824 bad check: too many arguments provided to checker: got 1, want 0
2825 got args:
2826 []interface {}{
2827 int(5),
2828 }
2829 `,
2830 expectedNegateFailure: `
2831 error:
2832 bad check: too many arguments provided to checker: got 1, want 0
2833 got args:
2834 []interface {}{
2835 int(5),
2836 }
2837 `,
2838 }, {
2839 about: "Any bad checker args",
2840 checker: qt.Any(qt.Matches),
2841 got: []string{"hello"},
2842 args: []interface{}{5},
2843 expectedCheckFailure: `
2844 error:
2845 bad check: at index 0: bad check: regexp is not a string
2846 `,
2847 expectedNegateFailure: `
2848 error:
2849 bad check: at index 0: bad check: regexp is not a string
2850 `,
2851 }, {
2852 about: "JSONEquals simple",
2853 checker: qt.JSONEquals,
2854 got: `{"First": 47.11}`,
2855 args: []interface{}{
2856 &OuterJSON{
2857 First: 47.11,
2858 },
2859 },
2860 expectedNegateFailure: tilde2bq(`
2861 error:
2862 unexpected success
2863 got:
2864 ~{"First": 47.11}~
2865 want:
2866 &quicktest_test.OuterJSON{
2867 First: 47.11,
2868 Second: nil,
2869 }
2870 `),
2871 }, {
2872 about: "JSONEquals nested",
2873 checker: qt.JSONEquals,
2874 got: `{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}]}`,
2875 args: []interface{}{
2876 &OuterJSON{
2877 First: 47.11,
2878 Second: []*InnerJSON{
2879 {First: "Hello", Second: 42},
2880 },
2881 },
2882 },
2883 expectedNegateFailure: tilde2bq(`
2884 error:
2885 unexpected success
2886 got:
2887 ~{"First": 47.11, "Last": [{"First": "Hello", "Second": 42}]}~
2888 want:
2889 &quicktest_test.OuterJSON{
2890 First: 47.11,
2891 Second: {
2892 &quicktest_test.InnerJSON{
2893 First: "Hello",
2894 Second: 42,
2895 Third: {},
2896 },
2897 },
2898 }
2899 `),
2900 }, {
2901 about: "JSONEquals nested with newline",
2902 checker: qt.JSONEquals,
2903 got: `{"First": 47.11, "Last": [{"First": "Hello", "Second": 42},
2904 {"First": "World", "Third": {"F": false}}]}`,
2905 args: []interface{}{
2906 &OuterJSON{
2907 First: 47.11,
2908 Second: []*InnerJSON{
2909 {First: "Hello", Second: 42},
2910 {First: "World", Third: map[string]bool{
2911 "F": false,
2912 }},
2913 },
2914 },
2915 },
2916 expectedNegateFailure: `
2917 error:
2918 unexpected success
2919 got:
2920 "{\"First\": 47.11, \"Last\": [{\"First\": \"Hello\", \"Second\": 42},\n\t\t\t{\"First\": \"World\", \"Third\": {\"F\": false}}]}"
2921 want:
2922 &quicktest_test.OuterJSON{
2923 First: 47.11,
2924 Second: {
2925 &quicktest_test.InnerJSON{
2926 First: "Hello",
2927 Second: 42,
2928 Third: {},
2929 },
2930 &quicktest_test.InnerJSON{
2931 First: "World",
2932 Second: 0,
2933 Third: {"F":false},
2934 },
2935 },
2936 }
2937 `,
2938 }, {
2939 about: "JSONEquals extra field",
2940 checker: qt.JSONEquals,
2941 got: `{"NotThere": 1}`,
2942 args: []interface{}{
2943 &OuterJSON{
2944 First: 2,
2945 },
2946 },
2947 expectedCheckFailure: fmt.Sprintf(`
2948 error:
2949 values are not deep equal
2950 diff (-got +want):
2951 %s
2952 got:
2953 map[string]interface {}{
2954 "NotThere": float64(1),
2955 }
2956 want:
2957 map[string]interface {}{
2958 "First": float64(2),
2959 }
2960 `, diff(map[string]interface{}{"NotThere": 1.0}, map[string]interface{}{"First": 2.0})),
2961 }, {
2962 about: "JSONEquals cannot unmarshal obtained value",
2963 checker: qt.JSONEquals,
2964 got: `{"NotThere": `,
2965 args: []interface{}{nil},
2966 expectedCheckFailure: fmt.Sprintf(tilde2bq(`
2967 error:
2968 cannot unmarshal obtained contents: %s; "{\"NotThere\": "
2969 got:
2970 ~{"NotThere": ~
2971 want:
2972 nil
2973 `), mustJSONUnmarshalErr(`{"NotThere": `)),
2974 }, {
2975 about: "JSONEquals cannot marshal expected value",
2976 checker: qt.JSONEquals,
2977 got: `null`,
2978 args: []interface{}{
2979 jsonErrorMarshaler{},
2980 },
2981 expectedCheckFailure: `
2982 error:
•2983 bad check: cannot marshal expected contents: json: error calling MarshalJSON for type *quicktest_test.jsonErrorMarshaler: qt json marshal error
2984 `,
2985 expectedNegateFailure: `
2986 error:
•2987 bad check: cannot marshal expected contents: json: error calling MarshalJSON for type *quicktest_test.jsonErrorMarshaler: qt json marshal error
2988 `,
2989 }, {
2990 about: "JSONEquals with []byte",
2991 checker: qt.JSONEquals,
2992 got: []byte("null"),
2993 args: []interface{}{nil},
2994 expectedNegateFailure: `
2995 error:
2996 unexpected success
2997 got:
2998 []uint8("null")
2999 want:
3000 nil
3001 `,
3002 }, {
3003 about: "JSONEquals with RawMessage",
3004 checker: qt.JSONEquals,
3005 got: []byte("null"),
3006 args: []interface{}{json.RawMessage("null")},
3007 expectedNegateFailure: `
3008 error:
3009 unexpected success
3010 got:
3011 []uint8("null")
3012 want:
•3013 s"null"
3014 `,
3015 }, {
3016 about: "JSONEquals with bad type",
3017 checker: qt.JSONEquals,
3018 got: 0,
3019 args: []interface{}{nil},
3020 expectedCheckFailure: `
3021 error:
3022 bad check: expected string or byte, got int
3023 `,
3024 expectedNegateFailure: `
3025 error:
3026 bad check: expected string or byte, got int
3027 `,
3028 }, {
3029 about: "CodecEquals with bad marshal",
3030 checker: qt.CodecEquals(
3031 func(x interface{}) ([]byte, error) { return []byte("bad json"), nil },
3032 json.Unmarshal,
3033 ),
3034 got: "null",
3035 args: []interface{}{nil},
3036 expectedCheckFailure: fmt.Sprintf(`
3037 error:
3038 bad check: cannot unmarshal expected contents: %s
3039 `, mustJSONUnmarshalErr("bad json")),
3040 expectedNegateFailure: fmt.Sprintf(`
3041 error:
3042 bad check: cannot unmarshal expected contents: %s
3043 `, mustJSONUnmarshalErr("bad json")),
3044 }, {
3045 about: "CodecEquals with options",
3046 checker: qt.CodecEquals(
3047 json.Marshal,
3048 json.Unmarshal,
3049 cmpopts.SortSlices(func(x, y interface{}) bool { return x.(string) < y.(string) }),
3050 ),
3051 got: `["b", "z", "c", "a"]`,
3052 args: []interface{}{[]string{"a", "c", "z", "b"}},
3053 expectedNegateFailure: tilde2bq(`
3054 error:
3055 unexpected success
3056 got:
3057 ~["b", "z", "c", "a"]~
3058 want:
3059 []string{"a", "c", "z", "b"}
3060 `),
3061 }}
3062
3063 func TestCheckers(t *testing.T) {
3064 original := qt.TestingVerbose
3065 defer func() {
3066 qt.TestingVerbose = original
3067 }()
3068 for _, test := range checkerTests {
3069 *qt.TestingVerbose = func() bool {
3070 return test.verbose
3071 }
3072
3073 t.Run(test.about, func(t *testing.T) {
3074 tt := &testingT{}
3075 c := qt.New(tt)
3076 ok := c.Check(test.got, test.checker, test.args...)
3077 checkResult(t, ok, tt.errorString(), test.expectedCheckFailure)
3078 })
3079 t.Run("Not "+test.about, func(t *testing.T) {
3080 tt := &testingT{}
3081 c := qt.New(tt)
3082 ok := c.Check(test.got, qt.Not(test.checker), test.args...)
3083 checkResult(t, ok, tt.errorString(), test.expectedNegateFailure)
3084 })
3085 }
3086 }
3087
3088 func diff(x, y interface{}, opts ...cmp.Option) string {
3089 d := cmp.Diff(x, y, opts...)
3090 return strings.TrimSuffix(qt.Prefixf(" ", "%s", d), "\n")
3091 }
3092
3093 type jsonErrorMarshaler struct{}
3094
3095 func (jsonErrorMarshaler) MarshalJSON() ([]byte, error) {
3096 return nil, fmt.Errorf("qt json marshal error")
3097 }
3098
3099 func mustJSONUnmarshalErr(s string) error {
3100 var v interface{}
3101 err := json.Unmarshal([]byte(s), &v)
3102 if err == nil {
3103 panic("want JSON error, got nil")
3104 }
3105 return err
3106 }
3107
3108 func tilde2bq(s string) string {
3109 return strings.Replace(s, "~", "`", -1)
3110 }