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