Debian Codegraph @

example_test.go

raw
1 //nolint:testableexamples // No way to obtain a real *testing.T outside of `go test`.
2 package check_test
3
4 import (
5 "context"
6 "fmt"
7 "io"
8 "io/fs"
9 "os"
10 "testing"
11
12 "github.com/powerman/check"
13 )
14
15 // ExampleMust shows the recommended way to wrap a *testing.T:
16 // any failed check stops the test immediately, like testify/require.
17 func ExampleMust() {
18 tt := new(testing.T)
19 tt.Parallel()
20 t := check.Must(tt)
21
22 t.Equal(2+2, 4)
23 t.Match("build-42", `^build-\d+$`)
24 }
25
26 // ExampleMust_tableDriven shows the usual table-driven pattern:
27 // wrap each subtest's own *testing.T inside tt.Run, not the outer one.
28 func ExampleMust_tableDriven() {
29 tt := new(testing.T)
30 tt.Parallel()
31 t := check.Must(tt)
32 t.True(true, "outer setup check")
33
34 cases := []struct {
35 name string
36 got int
37 want int
38 }{
39 {"add one", 1 + 1, 2},
40 {"add two", 2 + 2, 4},
41 }
42 for _, c := range cases {
43 tt.Run(c.name, func(tt *testing.T) {
44 tt.Parallel()
45 t := check.Must(tt)
46 t.Equal(c.got, c.want)
47 })
48 }
49 }
50
51 // ExampleNew shows the softer, testify/assert-like alternative to [Must]:
52 // a failed check doesn't stop the test,
53 // so guard dependent checks with the bool every checker returns.
54 func ExampleNew() {
55 tt := new(testing.T)
56 t := check.New(tt)
57
58 obj, err := os.Open(os.DevNull)
59 if t.Nil(err) {
60 t.NotNil(obj)
61 _ = obj.Close()
62 }
63 }
64
65 // ExampleTB_TODO marks a known-broken check as expected-to-fail
66 // without disabling or deleting the test: it keeps running,
67 // and once the underlying defect is fixed this check starts failing again -
68 // a reminder to remove TODO.
69 func ExampleTB_TODO() {
70 tt := new(testing.T)
71 t := check.Must(tt)
72
73 t.TODO().Equal(2+2, 5)
74 }
75
76 // ExampleTB_Should plugs a custom checker (bePositive, defined in check_test.go)
77 // into check's usual report/Must/TODO machinery,
78 // for checks not covered by any built-in checker.
79 func ExampleTB_Should() {
80 tt := new(testing.T)
81 t := check.Must(tt)
82
83 t.Should(bePositive, 42, "custom check")
84 }
85
86 // Example_errorChecks contrasts the four ways to check an error,
87 // see package doc for when to use which.
88 func Example_errorChecks() {
89 tt := new(testing.T)
90 t := check.Must(tt)
91
92 wrapped := fmt.Errorf("wrap: %w", io.EOF)
93
94 // Err: unwraps to the root cause and compares by value.
95 t.Err(wrapped, io.EOF)
96
97 // ErrIs: pure errors.Is chain membership, no value comparison.
98 t.ErrIs(wrapped, io.EOF)
99
100 // ErrAs: extract the first error of a given type from the chain.
101 var pathErr *fs.PathError
102 if t.ErrAs(wrapped, &pathErr) {
103 t.NotNil(pathErr)
104 }
105
106 // Match: check by error text against a regexp.
107 t.Match(wrapped, `EOF$`)
108 }
109
110 // ExampleTB_MergeContext injects an application base context
111 // (e.g. one carrying a slog handler) into a test
112 // on top of the per-test cancellation/deadline [testing.TB.Context] already provides.
113 func ExampleTB_MergeContext() {
114 tt := new(testing.T)
115 t := check.Must(tt)
116
117 type slogHandlerKey struct{}
118 appCtx := context.WithValue(context.Background(), slogHandlerKey{}, "app-handler")
119
120 t = t.MergeContext(appCtx)
121 t.NotNil(t.Context().Value(slogHandlerKey{}))
122 }
123
124 // ExampleTB_SortEqual checks that two slices/arrays contain the same elements
125 // while ignoring their order.
126 func ExampleTB_SortEqual() {
127 tt := new(testing.T)
128 t := check.Must(tt)
129
130 t.SortEqual([]int{1, 2, 3}, []int{3, 1, 2})
131 }