| 1 |
package check_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"regexp" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/powerman/check" |
| 12 |
) |
| 13 |
|
| 14 |
var errBoom = errors.New("boom") |
| 15 |
|
| 16 |
// ansiRE strips ANSI color codes, since check colors the "Checker:" line's |
| 17 |
// checker name when GO_TEST_COLOR/a color terminal makes check emit them. |
| 18 |
var ansiRE = regexp.MustCompile("\x1b\\[[0-9;]*m") |
| 19 |
|
| 20 |
// fakeTB is a minimal [testing.TB] double used to observe how check reacts |
| 21 |
// to a failed check, without making a real (sub)test fail: unlike a real |
| 22 |
// *testing.T, it just records what happened instead of also propagating |
| 23 |
// failure up to the actual test binary's exit code. |
| 24 |
type fakeTB struct { |
| 25 |
testing.TB |
| 26 |
|
| 27 |
name string |
| 28 |
errorfCalls int |
| 29 |
msgs []string |
| 30 |
failNowCalled bool |
| 31 |
} |
| 32 |
|
| 33 |
func (*fakeTB) Helper() {} |
| 34 |
func (f *fakeTB) Name() string { return f.name } |
| 35 |
|
| 36 |
func (f *fakeTB) Errorf(format string, args ...any) { |
| 37 |
f.errorfCalls++ |
| 38 |
f.msgs = append(f.msgs, ansiRE.ReplaceAllString(fmt.Sprintf(format, args...), "")) |
| 39 |
} |
| 40 |
|
| 41 |
func (f *fakeTB) FailNow() { f.failNowCalled = true } |
| 42 |
|
| 43 |
func TestNewSoft(tt *testing.T) { |
| 44 |
tt.Parallel() |
| 45 |
fake := &fakeTB{name: "fakeTestNewSoft"} |
| 46 |
t := check.New(fake) |
| 47 |
var reachedAfter bool |
| 48 |
t.Nil(errBoom) // Fails, but New is soft: execution continues. |
| 49 |
reachedAfter = true |
| 50 |
|
| 51 |
realT := check.T(tt) |
| 52 |
realT.Equal(fake.errorfCalls, 1) |
| 53 |
realT.False(fake.failNowCalled) |
| 54 |
realT.True(reachedAfter) |
| 55 |
} |
| 56 |
|
| 57 |
func TestMustConstructor(tt *testing.T) { |
| 58 |
tt.Parallel() |
| 59 |
fake := &fakeTB{name: "fakeTestMustConstructor"} |
| 60 |
t := check.Must(fake) |
| 61 |
t.Nil(errBoom) // Fails: Must stops the test right here (fake.FailNow is a no-op). |
| 62 |
|
| 63 |
realT := check.T(tt) |
| 64 |
realT.Equal(fake.errorfCalls, 1) |
| 65 |
realT.True(fake.failNowCalled) |
| 66 |
} |
| 67 |
|
| 68 |
func TestMustWorksWithBenchmark(tt *testing.T) { |
| 69 |
tt.Parallel() |
| 70 |
res := testing.Benchmark(BenchmarkSmoke) |
| 71 |
t := check.T(tt) |
| 72 |
t.True(res.N > 0) |
| 73 |
} |
| 74 |
|
| 75 |
func BenchmarkSmoke(b *testing.B) { |
| 76 |
t := check.Must(b) |
| 77 |
for b.Loop() { |
| 78 |
t.Equal(1+1, 2) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func FuzzSmoke(f *testing.F) { |
| 83 |
f.Add(2) |
| 84 |
f.Fuzz(func(tt *testing.T, n int) { |
| 85 |
t := check.Must(tt) |
| 86 |
t.Equal(n+0, n) |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
func TestTBTODOMustAll(tt *testing.T) { |
| 91 |
tt.Parallel() |
| 92 |
t := check.Must(tt).TODO() |
| 93 |
t.True(false) // Swapped by TODO: counted as passed. |
| 94 |
|
| 95 |
tAll := check.New(tt).MustAll() |
| 96 |
tAll.Nil(nil) |
| 97 |
tAll.TODO().Nil(false) |
| 98 |
} |
| 99 |
|
| 100 |
func TestTBShould(tt *testing.T) { |
| 101 |
tt.Parallel() |
| 102 |
t := check.New(tt) |
| 103 |
t.Should(func(_ *check.TB, actual any) bool { |
| 104 |
return actual.(int) > 0 //nolint:forcetypeassert // Test-only. |
| 105 |
}, 42, "custom check via TB") |
| 106 |
} |
| 107 |
|
| 108 |
func TestMergeContext(tt *testing.T) { |
| 109 |
tt.Parallel() |
| 110 |
|
| 111 |
type keyA struct{} |
| 112 |
type keyB struct{} |
| 113 |
|
| 114 |
t := check.New(tt) |
| 115 |
base := context.WithValue(t.Context(), keyA{}, "from-base") |
| 116 |
|
| 117 |
merged := t.MergeContext(base) |
| 118 |
t.Equal(merged.Context().Value(keyA{}), "from-base") |
| 119 |
t.Nil(merged.Context().Value(keyB{})) |
| 120 |
|
| 121 |
// The original t is untouched. |
| 122 |
t.Nil(t.Context().Value(keyA{})) |
| 123 |
} |
| 124 |
|
| 125 |
func TestMergeContextValuePrecedence(tt *testing.T) { |
| 126 |
tt.Parallel() |
| 127 |
|
| 128 |
type key struct{} |
| 129 |
|
| 130 |
t := check.New(tt) |
| 131 |
tWithValue := t.MergeContext(context.WithValue(t.Context(), key{}, "current")) |
| 132 |
merged := tWithValue.MergeContext(context.WithValue(t.Context(), key{}, "new")) |
| 133 |
|
| 134 |
// Values are looked up in the newly merged ctx first. |
| 135 |
t.Equal(merged.Context().Value(key{}), "new") |
| 136 |
} |
| 137 |
|
| 138 |
func TestMergeContextCancellation(tt *testing.T) { |
| 139 |
tt.Parallel() |
| 140 |
t := check.New(tt) |
| 141 |
|
| 142 |
baseCtx, baseCancel := context.WithCancel(t.Context()) |
| 143 |
tt.Cleanup(baseCancel) |
| 144 |
merged := t.MergeContext(baseCtx) |
| 145 |
|
| 146 |
select { |
| 147 |
case <-merged.Context().Done(): |
| 148 |
t.Error("merged context should not be done yet") |
| 149 |
default: |
| 150 |
} |
| 151 |
|
| 152 |
baseCancel() |
| 153 |
|
| 154 |
select { |
| 155 |
case <-merged.Context().Done(): |
| 156 |
case <-time.After(time.Second): |
| 157 |
t.Error("merged context should be cancelled after base is cancelled") |
| 158 |
} |
| 159 |
t.Err(merged.Context().Err(), context.Canceled) |
| 160 |
} |
| 161 |
|
| 162 |
// TestCheckerNameInReport is a regression test for the reported name of a |
| 163 |
// failed checker in the "Checker:" line: it must be the bare method name |
| 164 |
// (e.g. "Nil"), not a receiver-qualified one like "(*checks).Nil" - the |
| 165 |
// checker methods live on the unexported *checks type (promoted into both |
| 166 |
// TB and C), and Error/Errorf/Fatal/Fatalf additionally exist as explicit |
| 167 |
// overrides on *TB itself, so callerFuncName must strip whichever |
| 168 |
// "(*Receiver)." prefix is present, not just one hardcoded receiver name. |
| 169 |
// Fatal/Fatalf are safe to exercise directly here because fakeTB's |
| 170 |
// FailNow is a no-op (New(fake).TB is the fake, so TB.Fatal's |
| 171 |
// t.TB.FailNow() never reaches a real *testing.T). See |
| 172 |
// tb_internal_test.go for the *C-path (Error/Errorf) half of this |
| 173 |
// regression, which needs package-internal access to construct a *C |
| 174 |
// around a fake testing.TB - *C.Fatal/Fatalf always call the real, |
| 175 |
// wrapped *testing.T.FailNow() (not the fake), so they aren't safe to |
| 176 |
// exercise directly from a test. |
| 177 |
func TestCheckerNameInReport(tt *testing.T) { |
| 178 |
tt.Parallel() |
| 179 |
|
| 180 |
fake := &fakeTB{name: "fakeCheckerNameInReport"} |
| 181 |
t := check.New(fake) |
| 182 |
t.Nil("not-nil-value") // Checker method, lives on *checks. |
| 183 |
t.Equal(1, 2) // Checker method, lives on *checks. |
| 184 |
t.Error("direct error") |
| 185 |
t.Errorf("direct %s", "errorf") |
| 186 |
t.Fatal("direct fatal") //nolint:revive // fakeTB.FailNow is a no-op: execution continues. |
| 187 |
t.Fatalf("direct %s", "fatalf") |
| 188 |
|
| 189 |
realT := check.T(tt) |
| 190 |
realT.Equal(len(fake.msgs), 6) |
| 191 |
realT.Match(fake.msgs[0], `Checker: Nil\n`) |
| 192 |
realT.Match(fake.msgs[1], `Checker: Equal\n`) |
| 193 |
realT.Match(fake.msgs[2], `Checker: Error\n`) |
| 194 |
realT.Match(fake.msgs[3], `Checker: Errorf\n`) |
| 195 |
realT.Match(fake.msgs[4], `Checker: Fatal\n`) |
| 196 |
realT.Match(fake.msgs[5], `Checker: Fatalf\n`) |
| 197 |
} |