Debian Codegraph @

quicktest_test.go

raw
1 // Licensed under the MIT license, see LICENCE file for details.
2
3 package qt_test
4
5 import (
6 "bytes"
7 "errors"
8 "fmt"
9 "strings"
10 "testing"
11
12 "github.com/go-quicktest/qt"
13 )
14
15 var qtTests = []struct {
16 about string
17 checker qt.Checker
18 comments []qt.Comment
19 expectedFailure string
20 }{{
21 about: "success",
22 checker: qt.Equals(42, 42),
23 }, {
24 about: "failure",
25 checker: qt.Equals("42", "47"),
26 expectedFailure: `
27 error:
28 values are not equal
29 got:
30 "42"
31 want:
32 "47"
33 `,
34 }, {
35 about: "failure with % signs",
36 checker: qt.Equals("42%x", "47%y"),
37 expectedFailure: `
38 error:
39 values are not equal
40 got:
41 "42%x"
42 want:
43 "47%y"
44 `,
45 }, {
46 about: "failure with comment",
47 checker: qt.Equals(true, false),
48 comments: []qt.Comment{qt.Commentf("apparently %v != %v", true, false)},
49 expectedFailure: `
50 error:
51 values are not equal
52 comment:
53 apparently true != false
54 got:
55 bool(true)
56 want:
57 bool(false)
58 `,
59 }, {
60 about: "another failure with comment",
61 checker: qt.IsNil(any(42)),
62 comments: []qt.Comment{qt.Commentf("bad wolf: %d", 42)},
63 expectedFailure: `
64 error:
65 got non-nil value
66 comment:
67 bad wolf: 42
68 got:
69 int(42)
70 `,
71 }, {
72 about: "failure with constant comment",
73 checker: qt.IsNil(any("something")),
74 comments: []qt.Comment{qt.Commentf("these are the voyages")},
75 expectedFailure: `
76 error:
77 got non-nil value
78 comment:
79 these are the voyages
80 got:
81 "something"
82 `,
83 }, {
84 about: "failure with empty comment",
85 checker: qt.IsNil(any(47)),
86 comments: []qt.Comment{qt.Commentf("")},
87 expectedFailure: `
88 error:
89 got non-nil value
90 got:
91 int(47)
92 `,
93 }, {
94 about: "failure with multiple comments",
95 checker: qt.IsNil(any(42)),
96 comments: []qt.Comment{
97 qt.Commentf("bad wolf: %d", 42),
98 qt.Commentf("second comment"),
99 },
100 expectedFailure: `
101 error:
102 got non-nil value
103 comment:
104 bad wolf: 42
105 comment:
106 second comment
107 got:
108 int(42)
109 `,
110 }, {
111 about: "nil checker",
112 expectedFailure: `
113 error:
114 bad check: nil checker provided
115 `,
116 }, {
117 about: "many arguments and notes",
118 checker: &testingChecker{
119 args: []qt.Arg{{
120 Name: "arg1",
121 Value: 42,
122 }, {
123 Name: "arg2",
124 Value: "val2",
125 }, {
126 Name: "arg3",
127 Value: "val3",
128 }},
129 addNotes: func(note func(key string, value any)) {
130 note("note1", "these")
131 note("note2", qt.Unquoted("are"))
132 note("note3", "the")
133 note("note4", "voyages")
134 note("note5", true)
135 },
136 err: errors.New("bad wolf"),
137 },
138 expectedFailure: `
139 error:
140 bad wolf
141 note1:
142 "these"
143 note2:
144 are
145 note3:
146 "the"
147 note4:
148 "voyages"
149 note5:
150 bool(true)
151 arg1:
152 int(42)
153 arg2:
154 "val2"
155 arg3:
156 "val3"
157 `,
158 }, {
159 about: "many arguments and notes with the same value",
160 checker: &testingChecker{
161 args: []qt.Arg{{
162 Name: "arg1",
163 Value: "value1",
164 }, {
165 Name: "arg2",
166 Value: "value1",
167 }, {
168 Name: "arg3",
169 Value: []int{42},
170 }, {
171 Name: "arg4",
172 Value: nil,
173 }},
174 addNotes: func(note func(key string, value any)) {
175 note("note1", "value1")
176 note("note2", []int{42})
177 note("note3", "value1")
178 note("note4", nil)
179 },
180 err: errors.New("bad wolf"),
181 },
182 expectedFailure: `
183 error:
184 bad wolf
185 note1:
186 "value1"
187 note2:
188 []int{42}
189 note3:
190 <same as "note1">
191 note4:
192 nil
193 arg1:
194 <same as "note1">
195 arg2:
196 <same as "note1">
197 arg3:
198 <same as "note2">
199 arg4:
200 <same as "note4">
201 `,
202 }, {
203 about: "bad check with notes",
204 checker: &testingChecker{
205 args: []qt.Arg{{
206 Name: "got",
207 Value: 42,
208 }, {
209 Name: "want",
210 Value: "want",
211 }},
212 addNotes: func(note func(key string, value any)) {
213 note("note", 42)
214 },
215 err: qt.BadCheckf("bad wolf"),
216 },
217 expectedFailure: `
218 error:
219 bad check: bad wolf
220 note:
221 int(42)
222 `,
223 }, {
224 about: "silent failure with notes",
225 checker: &testingChecker{
226 args: []qt.Arg{{
227 Name: "got",
228 Value: 42,
229 }, {
230 Name: "want",
231 Value: "want",
232 }},
233 addNotes: func(note func(key string, value any)) {
234 note("note1", "first note")
235 note("note2", qt.Unquoted("second note"))
236 },
237 err: qt.ErrSilent,
238 },
239 expectedFailure: `
240 note1:
241 "first note"
242 note2:
243 second note
244 `,
245 }}
246
247 func TestCAssertCheck(t *testing.T) {
248 for _, test := range qtTests {
249 t.Run("Assert: "+test.about, func(t *testing.T) {
250 tt := &testingT{}
251 ok := qt.Assert(tt, test.checker, test.comments...)
252 checkResult(t, ok, tt.fatalString(), test.expectedFailure)
253 if tt.errorString() != "" {
254 t.Fatalf("no error messages expected, but got %q", tt.errorString())
255 }
256 })
257 t.Run("Check: "+test.about, func(t *testing.T) {
258 tt := &testingT{}
259 ok := qt.Check(tt, test.checker, test.comments...)
260 checkResult(t, ok, tt.errorString(), test.expectedFailure)
261 if tt.fatalString() != "" {
262 t.Fatalf("no fatal messages expected, but got %q", tt.fatalString())
263 }
264 })
265
266 }
267 }
268
269 func TestHelperCalls(t *testing.T) {
270 tt := &testingT{}
271 qt.Assert(tt, qt.IsTrue(false))
272 if tt.helperCalls < 2 {
273 t.Error("Assert doesn't invoke TB.Helper()")
274 }
275
276 tt = &testingT{}
277 qt.Check(tt, qt.IsTrue(false))
278 if tt.helperCalls < 2 {
279 t.Error("Check doesn't invoke TB.Helper()")
280 }
281 }
282
283 func checkResult(t *testing.T, ok bool, got, want string) {
284 t.Helper()
285 if want != "" {
286 assertPrefix(t, got, want+"stack:\n")
287 assertBool(t, ok, false)
288 return
289 }
290 if got != "" {
291 t.Fatalf("output:\ngot %q\nwant empty", got)
292 }
293 assertBool(t, ok, true)
294 }
295
296 // testingT implements testing.TB for testing purposes.
297 type testingT struct {
298 testing.TB
299
300 errorBuf bytes.Buffer
301 fatalBuf bytes.Buffer
302
303 subTestResult bool
304 subTestName string
305 subTestT *testing.T
306
307 helperCalls int
308 parallel bool
309 }
310
311 // Error overrides testing.TB.Error so that messages are collected.
312 func (t *testingT) Error(a ...any) {
313 fmt.Fprint(&t.errorBuf, a...)
314 }
315
316 // Fatal overrides testing.TB.Fatal so that messages are collected and the
317 // goroutine is not killed.
318 func (t *testingT) Fatal(a ...any) {
319 fmt.Fprint(&t.fatalBuf, a...)
320 }
321
322 // Parallel overrides testing.TB.Parallel in order to record the call.
323 func (t *testingT) Parallel() {
324 t.parallel = true
325 }
326
327 // Helper overrides testing.TB.Helper in order to count calls.
328 func (t *testingT) Helper() {
329 t.helperCalls += 1
330 }
331
332 // Fatal overrides *testing.T.Fatal so that messages are collected and the
333 // goroutine is not killed.
334 func (t *testingT) Run(name string, f func(t *testing.T)) bool {
335 t.subTestName, t.subTestT = name, &testing.T{}
336 f(t.subTestT)
337 return t.subTestResult
338 }
339
340 // errorString returns the error message.
341 func (t *testingT) errorString() string {
342 return t.errorBuf.String()
343 }
344
345 // fatalString returns the fatal error message.
346 func (t *testingT) fatalString() string {
347 return t.fatalBuf.String()
348 }
349
350 // assertPrefix fails if the got value does not have the given prefix.
351 func assertPrefix(t testing.TB, got, prefix string) {
352 t.Helper()
353 if prefix == "" {
354 t.Fatal("prefix: empty value provided")
355 }
356 if !strings.HasPrefix(got, prefix) {
357 t.Fatalf(`prefix:
358 got %q
359 want %q
360 -------------------- got --------------------
361 %s
362 -------------------- want -------------------
363 %s
364 ---------------------------------------------`, got, prefix, got, prefix)
365 }
366 }
367
368 // assertBool fails if the given boolean values don't match.
369 func assertBool(t testing.TB, got, want bool) {
370 t.Helper()
371 if got != want {
372 t.Fatalf("bool:\ngot %v\nwant %v", got, want)
373 }
374 }
375
376 // testingChecker is a quicktest.Checker used in tests. It receives the
377 // provided argNames, adds notes via the provided addNotes function, and when
378 // the check is run the provided error is returned.
379 type testingChecker struct {
380 args []qt.Arg
381 addNotes func(note func(key string, value any))
382 err error
383 }
384
385 // Check implements quicktest.Checker by returning the stored error.
386 func (c *testingChecker) Check(note func(key string, value any)) error {
387 if c.addNotes != nil {
388 c.addNotes(note)
389 }
390 return c.err
391 }
392
393 // Args implements quicktest.Checker by returning the stored args.
394 func (c *testingChecker) Args() []qt.Arg {
395 return c.args
396 }