Update upstream source from tag 'upstream/1.11.1'
Update to upstream version '1.11.1'
with Debian dir 43c5a800a726be02d26dc4250819dec4411663f1
Anthony Fok
3 years ago
18 | 18 | // Additional args (not consumed by the checker), when provided, are included as |
19 | 19 | // comments in the failure output when the check fails. |
20 | 20 | func Check(t testing.TB, got interface{}, checker Checker, args ...interface{}) bool { |
21 | t.Helper() | |
21 | 22 | return New(t).Check(got, checker, args...) |
22 | 23 | } |
23 | 24 | |
30 | 31 | // Additional args (not consumed by the checker), when provided, are included as |
31 | 32 | // comments in the failure output when the check fails. |
32 | 33 | func Assert(t testing.TB, got interface{}, checker Checker, args ...interface{}) bool { |
34 | t.Helper() | |
33 | 35 | return New(t).Assert(got, checker, args...) |
34 | 36 | } |
35 | 37 | |
182 | 184 | // Additional args (not consumed by the checker), when provided, are included |
183 | 185 | // as comments in the failure output when the check fails. |
184 | 186 | func (c *C) Check(got interface{}, checker Checker, args ...interface{}) bool { |
185 | return check(checkParams{ | |
187 | c.TB.Helper() | |
188 | return check(c, checkParams{ | |
186 | 189 | fail: c.TB.Error, |
187 | format: c.getFormat(), | |
188 | 190 | checker: checker, |
189 | 191 | got: got, |
190 | 192 | args: args, |
200 | 202 | // Additional args (not consumed by the checker), when provided, are included |
201 | 203 | // as comments in the failure output when the check fails. |
202 | 204 | func (c *C) Assert(got interface{}, checker Checker, args ...interface{}) bool { |
203 | return check(checkParams{ | |
205 | c.TB.Helper() | |
206 | return check(c, checkParams{ | |
204 | 207 | fail: c.TB.Fatal, |
205 | format: c.getFormat(), | |
206 | 208 | checker: checker, |
207 | 209 | got: got, |
208 | 210 | args: args, |
293 | 295 | // check performs the actual check with the provided params. |
294 | 296 | // In case of failure p.fail is called. In the fail report values are formatted |
295 | 297 | // using p.format. |
296 | func check(p checkParams) bool { | |
298 | func check(c *C, p checkParams) bool { | |
299 | c.TB.Helper() | |
297 | 300 | rp := reportParams{ |
298 | 301 | got: p.got, |
299 | 302 | args: p.args, |
300 | format: p.format, | |
303 | format: c.getFormat(), | |
301 | 304 | } |
302 | 305 | if rp.format == nil { |
303 | 306 | // No format set; use the default: Format. |
353 | 356 | // checkParams holds parameters for executing a check. |
354 | 357 | type checkParams struct { |
355 | 358 | fail func(...interface{}) |
356 | format formatFunc | |
357 | 359 | checker Checker |
358 | 360 | got interface{} |
359 | 361 | args []interface{} |
508 | 508 | `) |
509 | 509 | } |
510 | 510 | |
511 | func TestHelper(t *testing.T) { | |
512 | tt := &testingT{} | |
513 | qt.Assert(tt, true, qt.IsFalse) | |
514 | if tt.helperCalls != 3 { | |
515 | t.Fatalf("want 3 calls (Assert, c.Assert, check), got %d", tt.helperCalls) | |
516 | } | |
517 | } | |
518 | ||
519 | func TestCHelper(t *testing.T) { | |
520 | tt := &testingT{} | |
521 | c := qt.New(tt) | |
522 | c.Assert(true, qt.IsFalse) | |
523 | if tt.helperCalls != 2 { | |
524 | t.Fatalf("want 2 calls (c.Assert, check), got %d", tt.helperCalls) | |
525 | } | |
526 | } | |
527 | ||
511 | 528 | func TestCParallel(t *testing.T) { |
512 | 529 | tt := &testingT{} |
513 | 530 | c := qt.New(tt) |
624 | 641 | subTestName string |
625 | 642 | subTestT *testing.T |
626 | 643 | |
627 | parallel bool | |
628 | } | |
629 | ||
630 | // Error overrides *testing.T.Error so that messages are collected. | |
644 | helperCalls int | |
645 | parallel bool | |
646 | } | |
647 | ||
648 | // Error overrides testing.TB.Error so that messages are collected. | |
631 | 649 | func (t *testingT) Error(a ...interface{}) { |
632 | 650 | fmt.Fprint(&t.errorBuf, a...) |
633 | 651 | } |
634 | 652 | |
635 | // Fatal overrides *testing.T.Fatal so that messages are collected and the | |
653 | // Fatal overrides testing.TB.Fatal so that messages are collected and the | |
636 | 654 | // goroutine is not killed. |
637 | 655 | func (t *testingT) Fatal(a ...interface{}) { |
638 | 656 | fmt.Fprint(&t.fatalBuf, a...) |
639 | 657 | } |
640 | 658 | |
659 | // Parallel overrides testing.TB.Parallel in order to record the call. | |
641 | 660 | func (t *testingT) Parallel() { |
642 | 661 | t.parallel = true |
662 | } | |
663 | ||
664 | // Helper overrides testing.TB.Helper in order to count calls. | |
665 | func (t *testingT) Helper() { | |
666 | t.helperCalls += 1 | |
643 | 667 | } |
644 | 668 | |
645 | 669 | // Fatal overrides *testing.T.Fatal so that messages are collected and the |
662 | 686 | |
663 | 687 | // assertPrefix fails if the got value does not have the given prefix. |
664 | 688 | func assertPrefix(t testing.TB, got, prefix string) { |
665 | if h, ok := t.(helper); ok { | |
666 | h.Helper() | |
667 | } | |
689 | t.Helper() | |
668 | 690 | if prefix == "" { |
669 | 691 | t.Fatal("prefix: empty value provided") |
670 | 692 | } |
683 | 705 | // assertErrHasPrefix fails if the given error is nil or does not have the |
684 | 706 | // given prefix. |
685 | 707 | func assertErrHasPrefix(t testing.TB, err error, prefix string) { |
686 | if h, ok := t.(helper); ok { | |
687 | h.Helper() | |
688 | } | |
708 | t.Helper() | |
689 | 709 | if err == nil { |
690 | 710 | t.Fatalf("error:\ngot nil\nwant %q", prefix) |
691 | 711 | } |
694 | 714 | |
695 | 715 | // assertErrIsNil fails if the given error is not nil. |
696 | 716 | func assertErrIsNil(t testing.TB, err error) { |
697 | if h, ok := t.(helper); ok { | |
698 | h.Helper() | |
699 | } | |
717 | t.Helper() | |
700 | 718 | if err != nil { |
701 | 719 | t.Fatalf("error:\ngot %q\nwant nil", err) |
702 | 720 | } |
704 | 722 | |
705 | 723 | // assertBool fails if the given boolean values don't match. |
706 | 724 | func assertBool(t testing.TB, got, want bool) { |
707 | if h, ok := t.(helper); ok { | |
708 | h.Helper() | |
709 | } | |
725 | t.Helper() | |
710 | 726 | if got != want { |
711 | 727 | t.Fatalf("bool:\ngot %v\nwant %v", got, want) |
712 | 728 | } |
713 | } | |
714 | ||
715 | // helper is used to check whether the current Go version supports testing | |
716 | // helpers. | |
717 | type helper interface { | |
718 | Helper() | |
719 | 729 | } |
720 | 730 | |
721 | 731 | // testingChecker is a quicktest.Checker used in tests. It receives the |
111 | 111 | thisPackage := reflect.TypeOf(C{}).PkgPath() + "." |
112 | 112 | for { |
113 | 113 | frame, more := frames.Next() |
114 | if strings.HasPrefix(frame.Function, "testing.") || strings.HasPrefix(frame.Function, thisPackage) { | |
115 | // Do not include stdlib test runner and quicktest checker calls. | |
114 | if strings.HasPrefix(frame.Function, "testing.") { | |
115 | // Stop before getting back to stdlib test runner calls. | |
116 | break | |
117 | } | |
118 | if fname := strings.TrimPrefix(frame.Function, thisPackage); fname != frame.Function { | |
119 | if ast.IsExported(fname) { | |
120 | // Continue without printing frames for quicktest exported API. | |
121 | continue | |
122 | } | |
123 | // Stop when entering quicktest internal calls. | |
124 | // This is useful for instance when using qtsuite. | |
116 | 125 | break |
117 | 126 | } |
118 | 127 | fmt.Fprint(w, prefixf(prefix, "%s:%d", frame.File, frame.Line)) |
129 | 129 | assertReport(t, tt, want) |
130 | 130 | } |
131 | 131 | |
132 | func TestTopLevelAssertReportOutput(t *testing.T) { | |
133 | tt := &testingT{} | |
134 | qt.Assert(tt, 42, qt.Equals, 47) | |
135 | want := ` | |
136 | error: | |
137 | values are not equal | |
138 | got: | |
139 | int(42) | |
140 | want: | |
141 | int(47) | |
142 | stack: | |
143 | $file:135 | |
144 | qt.Assert(tt, 42, qt.Equals, 47) | |
145 | ` | |
146 | assertReport(t, tt, want) | |
147 | } | |
148 | ||
132 | 149 | func assertReport(t *testing.T, tt *testingT, want string) { |
133 | 150 | got := strings.Replace(tt.fatalString(), "\t", " ", -1) |
134 | 151 | // go-cmp can include non-breaking spaces in its output. |