Debian Codegraph @

td/cmp_funcs_misc.go

raw
1 // Copyright (c) 2018, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td
8
9 import (
10 "runtime"
11
12 "github.com/maxatome/go-testdeep/internal/ctxerr"
13 "github.com/maxatome/go-testdeep/internal/types"
14 "github.com/maxatome/go-testdeep/internal/util"
15 )
16
17 // CmpTrue is a shortcut for:
18 //
19 // td.Cmp(t, got, true, args...)
20 //
21 // Returns true if the test is OK, false if it fails.
22 //
23 // td.CmpTrue(t, IsAvailable(x), "x should be available")
24 //
25 // args... are optional and allow to name the test. This name is
26 // used in case of failure to qualify the test. If len(args) > 1 and
27 // the first item of args is a string and contains a '%' rune then
28 // [fmt.Fprintf] is used to compose the name, else args are passed to
29 // [fmt.Fprint]. Do not forget it is the name of the test, not the
30 // reason of a potential failure.
31 //
32 // See also [CmpFalse].
33 func CmpTrue(t TestingT, got bool, args ...any) bool {
34 t.Helper()
35 return Cmp(t, got, true, args...)
36 }
37
38 // CmpFalse is a shortcut for:
39 //
40 // td.Cmp(t, got, false, args...)
41 //
42 // Returns true if the test is OK, false if it fails.
43 //
44 // td.CmpFalse(t, IsAvailable(x), "x should not be available")
45 //
46 // args... are optional and allow to name the test. This name is
47 // used in case of failure to qualify the test. If len(args) > 1 and
48 // the first item of args is a string and contains a '%' rune then
49 // [fmt.Fprintf] is used to compose the name, else args are passed to
50 // [fmt.Fprint]. Do not forget it is the name of the test, not the
51 // reason of a potential failure.
52 //
53 // See also [CmpTrue].
54 func CmpFalse(t TestingT, got bool, args ...any) bool {
55 t.Helper()
56 return Cmp(t, got, false, args...)
57 }
58
59 func cmpError(ctx ctxerr.Context, t TestingT, got error, args ...any) bool {
60 if got != nil {
61 return true
62 }
63
64 t.Helper()
65
66 formatError(t,
67 ctx.FailureIsFatal,
68 &ctxerr.Error{
69 Context: ctx,
70 Message: "should be an error",
71 Got: types.RawString("nil"),
72 Expected: types.RawString("non-nil error"),
73 },
74 args...)
75
76 return false
77 }
78
79 func cmpNoError(ctx ctxerr.Context, t TestingT, got error, args ...any) bool {
80 if got == nil {
81 return true
82 }
83
84 t.Helper()
85
86 formatError(t,
87 ctx.FailureIsFatal,
88 &ctxerr.Error{
89 Context: ctx,
90 Message: "should NOT be an error",
91 Got: got,
92 Expected: types.RawString("nil"),
93 },
94 args...)
95
96 return false
97 }
98
99 // CmpError checks that got is non-nil error.
100 //
101 // _, err := MyFunction(1, 2, 3)
102 // td.CmpError(t, err, "MyFunction(1, 2, 3) should return an error")
103 //
104 // args... are optional and allow to name the test. This name is
105 // used in case of failure to qualify the test. If len(args) > 1 and
106 // the first item of args is a string and contains a '%' rune then
107 // [fmt.Fprintf] is used to compose the name, else args are passed to
108 // [fmt.Fprint]. Do not forget it is the name of the test, not the
109 // reason of a potential failure.
110 //
111 // See also [CmpNoError].
112 func CmpError(t TestingT, got error, args ...any) bool {
113 t.Helper()
114 return cmpError(newContext(t), t, got, args...)
115 }
116
117 // CmpNoError checks that got is nil error.
118 //
119 // value, err := MyFunction(1, 2, 3)
120 // if td.CmpNoError(t, err) {
121 // // one can now check value...
122 // }
123 //
124 // args... are optional and allow to name the test. This name is
125 // used in case of failure to qualify the test. If len(args) > 1 and
126 // the first item of args is a string and contains a '%' rune then
127 // [fmt.Fprintf] is used to compose the name, else args are passed to
128 // [fmt.Fprint]. Do not forget it is the name of the test, not the
129 // reason of a potential failure.
130 //
131 // See also [CmpError].
132 func CmpNoError(t TestingT, got error, args ...any) bool {
133 t.Helper()
134 return cmpNoError(newContext(t), t, got, args...)
135 }
136
137 func cmpPanic(ctx ctxerr.Context, t TestingT, fn func(), expected any, args ...any) bool {
138 t.Helper()
139
140 if ctx.Path.Len() == 1 && ctx.Path.String() == contextDefaultRootName {
141 ctx.Path = ctxerr.NewPath(contextPanicRootName)
142 }
143
144 var (
145 panicked bool
146 panicParam any
147 )
148
149 func() {
150 defer func() { panicParam = recover() }()
151 panicked = true
152 fn()
153 panicked = false
154 }()
155
156 if !panicked {
157 formatError(t,
158 ctx.FailureIsFatal,
159 &ctxerr.Error{
160 Context: ctx,
161 Message: "should have panicked",
162 Summary: ctxerr.NewSummary("did not panic"),
163 },
164 args...)
165 return false
166 }
167
168 return cmpDeeply(ctx.AddCustomLevel("→panic()"), t, panicParam, expected, args...)
169 }
170
171 func cmpNotPanic(ctx ctxerr.Context, t TestingT, fn func(), args ...any) bool {
172 var (
173 panicked bool
174 stackTrace types.RawString
175 )
176
177 func() {
178 defer func() {
179 panicParam := recover()
180 if panicked {
181 buf := make([]byte, 8192)
182 n := runtime.Stack(buf, false)
183 for ; n > 0; n-- {
184 if buf[n-1] != '\n' {
185 break
186 }
187 }
188 stackTrace = types.RawString("panic: " + util.ToString(panicParam) + "\n\n" +
189 string(buf[:n]))
190 }
191 }()
192 panicked = true
193 fn()
194 panicked = false
195 }()
196
197 if !panicked {
198 return true
199 }
200
201 t.Helper()
202
203 if ctx.Path.Len() == 1 && ctx.Path.String() == contextDefaultRootName {
204 ctx.Path = ctxerr.NewPath(contextPanicRootName)
205 }
206
207 formatError(t,
208 ctx.FailureIsFatal,
209 &ctxerr.Error{
210 Context: ctx,
211 Message: "should NOT have panicked",
212 Got: stackTrace,
213 Expected: types.RawString("not panicking at all"),
214 },
215 args...)
216 return false
217 }
218
219 // CmpPanic calls fn and checks a panic() occurred with the
220 // expectedPanic parameter. It returns true only if both conditions
221 // are fulfilled.
222 //
223 // Note that calling panic(nil) in fn body is always detected as a
224 // panic. [runtime] package says: before Go 1.21, programs that called
225 // panic(nil) observed recover returning nil. Starting in Go 1.21,
226 // programs that call panic(nil) observe recover returning a
227 // [*runtime.PanicNilError]. Programs can change back to the old
228 // behavior by setting GODEBUG=panicnil=1.
229 //
230 // td.CmpPanic(t,
231 // func() { panic("I am panicking!") },
232 // "I am panicking!",
233 // "The function should panic with the right string") // succeeds
234 //
235 // td.CmpPanic(t,
236 // func() { panic("I am panicking!") },
237 // Contains("panicking!"),
238 // "The function should panic with a string containing `panicking!`") // succeeds
239 //
240 // td.CmpPanic(t, func() { panic(nil) }, nil, "Checks for panic(nil)") // succeeds
241 //
242 // args... are optional and allow to name the test. This name is
243 // used in case of failure to qualify the test. If len(args) > 1 and
244 // the first item of args is a string and contains a '%' rune then
245 // [fmt.Fprintf] is used to compose the name, else args are passed to
246 // [fmt.Fprint]. Do not forget it is the name of the test, not the
247 // reason of a potential failure.
248 //
249 // See also [CmpNotPanic].
250 func CmpPanic(t TestingT, fn func(), expectedPanic any, args ...any) bool {
251 t.Helper()
252 return cmpPanic(newContext(t), t, fn, expectedPanic, args...)
253 }
254
255 // CmpNotPanic calls fn and checks no panic() occurred. If a panic()
256 // occurred false is returned then the panic() parameter and the stack
257 // trace appear in the test report.
258 //
259 // Note that calling panic(nil) in fn body is always detected as a
260 // panic. [runtime] package says: before Go 1.21, programs that called
261 // panic(nil) observed recover returning nil. Starting in Go 1.21,
262 // programs that call panic(nil) observe recover returning a
263 // [*runtime.PanicNilError]. Programs can change back to the old
264 // behavior by setting GODEBUG=panicnil=1.
265 //
266 // td.CmpNotPanic(t, func() {}) // succeeds as function does not panic
267 //
268 // td.CmpNotPanic(t, func() { panic("I am panicking!") }) // fails
269 // td.CmpNotPanic(t, func() { panic(nil) }) // fails too
270 //
271 // args... are optional and allow to name the test. This name is
272 // used in case of failure to qualify the test. If len(args) > 1 and
273 // the first item of args is a string and contains a '%' rune then
274 // [fmt.Fprintf] is used to compose the name, else args are passed to
275 // [fmt.Fprint]. Do not forget it is the name of the test, not the
276 // reason of a potential failure.
277 //
278 // See also [CmpPanic].
279 func CmpNotPanic(t TestingT, fn func(), args ...any) bool {
280 t.Helper()
281 return cmpNotPanic(newContext(t), t, fn, args...)
282 }