Codebase list golang-github-frankban-quicktest / ceeaf4a
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
4 changed file(s) with 70 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
1818 // Additional args (not consumed by the checker), when provided, are included as
1919 // comments in the failure output when the check fails.
2020 func Check(t testing.TB, got interface{}, checker Checker, args ...interface{}) bool {
21 t.Helper()
2122 return New(t).Check(got, checker, args...)
2223 }
2324
3031 // Additional args (not consumed by the checker), when provided, are included as
3132 // comments in the failure output when the check fails.
3233 func Assert(t testing.TB, got interface{}, checker Checker, args ...interface{}) bool {
34 t.Helper()
3335 return New(t).Assert(got, checker, args...)
3436 }
3537
182184 // Additional args (not consumed by the checker), when provided, are included
183185 // as comments in the failure output when the check fails.
184186 func (c *C) Check(got interface{}, checker Checker, args ...interface{}) bool {
185 return check(checkParams{
187 c.TB.Helper()
188 return check(c, checkParams{
186189 fail: c.TB.Error,
187 format: c.getFormat(),
188190 checker: checker,
189191 got: got,
190192 args: args,
200202 // Additional args (not consumed by the checker), when provided, are included
201203 // as comments in the failure output when the check fails.
202204 func (c *C) Assert(got interface{}, checker Checker, args ...interface{}) bool {
203 return check(checkParams{
205 c.TB.Helper()
206 return check(c, checkParams{
204207 fail: c.TB.Fatal,
205 format: c.getFormat(),
206208 checker: checker,
207209 got: got,
208210 args: args,
293295 // check performs the actual check with the provided params.
294296 // In case of failure p.fail is called. In the fail report values are formatted
295297 // using p.format.
296 func check(p checkParams) bool {
298 func check(c *C, p checkParams) bool {
299 c.TB.Helper()
297300 rp := reportParams{
298301 got: p.got,
299302 args: p.args,
300 format: p.format,
303 format: c.getFormat(),
301304 }
302305 if rp.format == nil {
303306 // No format set; use the default: Format.
353356 // checkParams holds parameters for executing a check.
354357 type checkParams struct {
355358 fail func(...interface{})
356 format formatFunc
357359 checker Checker
358360 got interface{}
359361 args []interface{}
508508 `)
509509 }
510510
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
511528 func TestCParallel(t *testing.T) {
512529 tt := &testingT{}
513530 c := qt.New(tt)
624641 subTestName string
625642 subTestT *testing.T
626643
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.
631649 func (t *testingT) Error(a ...interface{}) {
632650 fmt.Fprint(&t.errorBuf, a...)
633651 }
634652
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
636654 // goroutine is not killed.
637655 func (t *testingT) Fatal(a ...interface{}) {
638656 fmt.Fprint(&t.fatalBuf, a...)
639657 }
640658
659 // Parallel overrides testing.TB.Parallel in order to record the call.
641660 func (t *testingT) Parallel() {
642661 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
643667 }
644668
645669 // Fatal overrides *testing.T.Fatal so that messages are collected and the
662686
663687 // assertPrefix fails if the got value does not have the given prefix.
664688 func assertPrefix(t testing.TB, got, prefix string) {
665 if h, ok := t.(helper); ok {
666 h.Helper()
667 }
689 t.Helper()
668690 if prefix == "" {
669691 t.Fatal("prefix: empty value provided")
670692 }
683705 // assertErrHasPrefix fails if the given error is nil or does not have the
684706 // given prefix.
685707 func assertErrHasPrefix(t testing.TB, err error, prefix string) {
686 if h, ok := t.(helper); ok {
687 h.Helper()
688 }
708 t.Helper()
689709 if err == nil {
690710 t.Fatalf("error:\ngot nil\nwant %q", prefix)
691711 }
694714
695715 // assertErrIsNil fails if the given error is not nil.
696716 func assertErrIsNil(t testing.TB, err error) {
697 if h, ok := t.(helper); ok {
698 h.Helper()
699 }
717 t.Helper()
700718 if err != nil {
701719 t.Fatalf("error:\ngot %q\nwant nil", err)
702720 }
704722
705723 // assertBool fails if the given boolean values don't match.
706724 func assertBool(t testing.TB, got, want bool) {
707 if h, ok := t.(helper); ok {
708 h.Helper()
709 }
725 t.Helper()
710726 if got != want {
711727 t.Fatalf("bool:\ngot %v\nwant %v", got, want)
712728 }
713 }
714
715 // helper is used to check whether the current Go version supports testing
716 // helpers.
717 type helper interface {
718 Helper()
719729 }
720730
721731 // testingChecker is a quicktest.Checker used in tests. It receives the
111111 thisPackage := reflect.TypeOf(C{}).PkgPath() + "."
112112 for {
113113 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.
116125 break
117126 }
118127 fmt.Fprint(w, prefixf(prefix, "%s:%d", frame.File, frame.Line))
129129 assertReport(t, tt, want)
130130 }
131131
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
132149 func assertReport(t *testing.T, tt *testingT, want string) {
133150 got := strings.Replace(tt.fatalString(), "\t", " ", -1)
134151 // go-cmp can include non-breaking spaces in its output.