Debian Codegraph @

tb_internal_test.go

raw
1 package check // Constructs *C around a fake testing.TB; needs the unexported checks field.
2
3 import (
4 "fmt"
5 "regexp"
6 "testing"
7 )
8
9 // fakeReportTB is a minimal testing.TB double that only captures Errorf
10 // calls, used to inspect the exact "Checker:" text check reports.
11 type fakeReportTB struct {
12 testing.TB
13
14 msgs []string
15 }
16
17 func (*fakeReportTB) Helper() {}
18 func (*fakeReportTB) Name() string { return "fakeReportTB" }
19 func (*fakeReportTB) FailNow() {}
20 func (f *fakeReportTB) Errorf(format string, args ...any) {
21 f.msgs = append(f.msgs, ansiTestRE.ReplaceAllString(fmt.Sprintf(format, args...), ""))
22 }
23
24 var ansiTestRE = regexp.MustCompile("\x1b\\[[0-9;]*m")
25
26 // TestCheckerNameInReportViaC is the *C half of the callerFuncName
27 // regression test (see TestCheckerNameInReport in tb_test.go for the *TB
28 // half): Error/Errorf are explicit overrides on *C (needed to win the
29 // ambiguity with *testing.T's own methods), so they must also report
30 // their bare method name, not "(*C).Error" and friends. *C embeds a
31 // concrete *testing.T (not testing.TB), so this can't be built with a
32 // fake from outside the package - hence this being an internal test.
33 //
34 // Fatal/Fatalf aren't exercised here: unlike TB.Fatal (whose FailNow goes
35 // through the wrapped testing.TB, which can be a fake), C.Fatal always
36 // calls the real, embedded *testing.T's FailNow - not safe to trigger
37 // from within this test.
38 func TestCheckerNameInReportViaC(tt *testing.T) {
39 tt.Parallel()
40
41 fake := &fakeReportTB{}
42 c := &C{checks: &checks{tb: fake}, T: tt}
43 c.Error("direct error")
44 c.Errorf("direct %s", "errorf")
45
46 t := T(tt)
47 t.Equal(len(fake.msgs), 2)
48 t.Match(fake.msgs[0], `Checker: Error\n`)
49 t.Match(fake.msgs[1], `Checker: Errorf\n`)
50 }