-
Notifications
You must be signed in to change notification settings - Fork 27
/
quicktest_test.go
566 lines (533 loc) · 11.7 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Licensed under the MIT license, see LICENCE file for details.
package quicktest_test
import (
"bytes"
"errors"
"fmt"
"runtime"
"strings"
"testing"
qt "github.com/frankban/quicktest"
)
var cTests = []struct {
about string
checker qt.Checker
got interface{}
args []interface{}
expectedFailure string
}{{
about: "success",
checker: qt.Equals,
got: 42,
args: []interface{}{42},
}, {
about: "failure",
checker: qt.Equals,
got: "42",
args: []interface{}{"47"},
expectedFailure: `
error:
values are not equal
got:
"42"
want:
"47"
`,
}, {
about: "failure with comment",
checker: qt.Equals,
got: true,
args: []interface{}{false, 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,
got: 42,
args: []interface{}{qt.Commentf("bad wolf: %d", 42)},
expectedFailure: `
error:
42 is not nil
comment:
bad wolf: 42
got:
int(42)
`,
}, {
about: "failure with constant comment",
checker: qt.IsNil,
got: "something",
args: []interface{}{qt.Commentf("these are the voyages")},
expectedFailure: `
error:
"something" is not nil
comment:
these are the voyages
got:
"something"
`,
}, {
about: "failure with empty comment",
checker: qt.IsNil,
got: 47,
args: []interface{}{qt.Commentf("")},
expectedFailure: `
error:
47 is not nil
got:
int(47)
`,
}, {
about: "nil checker",
expectedFailure: `
error:
bad check: nil checker provided
`,
}, {
about: "not enough arguments",
checker: qt.Equals,
got: 42,
args: []interface{}{},
expectedFailure: `
error:
bad check: not enough arguments provided to checker: got 0, want 1
want args:
want
`,
}, {
about: "not enough arguments with comment",
checker: qt.DeepEquals,
got: 42,
args: []interface{}{qt.Commentf("test %d", 0)},
expectedFailure: `
error:
bad check: not enough arguments provided to checker: got 0, want 1
comment:
test 0
want args:
want
`,
}, {
about: "too many arguments",
checker: qt.Matches,
got: 42,
args: []interface{}{42, 47},
expectedFailure: `
error:
bad check: too many arguments provided to checker: got 2, want 1
got args:
[]interface {}{
int(42),
int(47),
}
want args:
regexp
`,
}, {
about: "really too many arguments",
checker: qt.DeepEquals,
got: 42,
args: []interface{}{42, 47, nil, "stop"},
expectedFailure: `
error:
bad check: too many arguments provided to checker: got 4, want 1
got args:
[]interface {}{
int(42),
int(47),
nil,
"stop",
}
want args:
want
`,
}, {
about: "too many arguments with comment",
checker: qt.IsNil,
got: 42,
args: []interface{}{nil, qt.Commentf("these are the voyages")},
expectedFailure: `
error:
bad check: too many arguments provided to checker: got 1, want 0
comment:
these are the voyages
got args:
[]interface {}{
nil,
}
`,
}, {
about: "many arguments and notes",
checker: &testingChecker{
argNames: []string{"arg1", "arg2", "arg3"},
addNotes: func(note func(key string, value interface{})) {
note("note1", "these")
note("note2", qt.Unquoted("are"))
note("note3", "the")
note("note4", "voyages")
note("note5", true)
},
err: errors.New("bad wolf"),
},
got: 42,
args: []interface{}{"val2", "val3"},
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{
argNames: []string{"arg1", "arg2", "arg3", "arg4"},
addNotes: func(note func(key string, value interface{})) {
note("note1", "value1")
note("note2", []int{42})
note("note3", "value1")
note("note4", nil)
},
err: errors.New("bad wolf"),
},
got: "value1",
args: []interface{}{"value1", []int{42}, nil},
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{
argNames: []string{"got", "want"},
addNotes: func(note func(key string, value interface{})) {
note("note", 42)
},
err: qt.BadCheckf("bad wolf"),
},
got: 42,
args: []interface{}{"want"},
expectedFailure: `
error:
bad check: bad wolf
note:
int(42)
`,
}, {
about: "silent failure with notes",
checker: &testingChecker{
argNames: []string{"got", "want"},
addNotes: func(note func(key string, value interface{})) {
note("note1", "first note")
note("note2", qt.Unquoted("second note"))
},
err: qt.ErrSilent,
},
got: 42,
args: []interface{}{"want"},
expectedFailure: `
note1:
"first note"
note2:
second note
`,
}}
func TestCAssertCheck(t *testing.T) {
for _, test := range cTests {
t.Run("Check: "+test.about, func(t *testing.T) {
tt := &testingT{}
c := qt.New(tt)
ok := c.Check(test.got, test.checker, test.args...)
checkResult(t, ok, tt.errorString(), test.expectedFailure)
if tt.fatalString() != "" {
t.Fatalf("no fatal messages expected, but got %q", tt.fatalString())
}
})
t.Run("Assert: "+test.about, func(t *testing.T) {
tt := &testingT{}
c := qt.New(tt)
ok := c.Assert(test.got, test.checker, test.args...)
checkResult(t, ok, tt.fatalString(), test.expectedFailure)
if tt.errorString() != "" {
t.Fatalf("no error messages expected, but got %q", tt.errorString())
}
})
}
}
func TestCRunSuccess(t *testing.T) {
tt := &testingT{}
c := qt.New(tt)
var run bool
subTestName := "my test"
ok := c.Run(subTestName, func(innerC *qt.C) {
run = true
if innerC == c {
t.Fatal("subtest C: same instance provided")
}
if innerC.TB != tt.subTestT {
t.Fatalf("subtest testing object: got %p, want %p", innerC.TB, tt.subTestT)
}
if tt.subTestName != subTestName {
t.Fatalf("subtest name: got %q, want %q", tt.subTestName, subTestName)
}
})
assertBool(t, run, true)
assertBool(t, ok, false)
// Simulate a test success.
tt.subTestResult = true
ok = c.Run(subTestName, func(innerC *qt.C) {})
assertBool(t, ok, true)
}
func TestCRunPanic(t *testing.T) {
c := qt.New(&testing.B{})
var run bool
defer func() {
r := recover()
if r != "cannot execute Run with underlying concrete type *testing.B" {
t.Fatalf("unexpected panic recover: %v", r)
}
}()
c.Run("panic", func(innerC *qt.C) {})
assertBool(t, run, true)
}
func TestCParallel(t *testing.T) {
tt := &testingT{}
c := qt.New(tt)
c.Parallel()
if !tt.parallel {
t.Fatalf("parallel not called")
}
}
func TestCParallelPanic(t *testing.T) {
c := qt.New(&testing.B{})
defer func() {
r := recover()
if r != "cannot execute Parallel with underlying concrete type *testing.B" {
t.Fatalf("unexpected panic recover: %v", r)
}
}()
c.Parallel()
}
func TestCDefer(t *testing.T) {
c := qt.New(t)
var defers []int
c.Defer(func() { defers = append(defers, 1) })
c.Defer(func() { defers = append(defers, 2) })
c.Done()
c.Assert(defers, qt.DeepEquals, []int{2, 1})
// Calling Done again should not do anything.
c.Done()
c.Assert(defers, qt.DeepEquals, []int{2, 1})
}
func TestCDeferCalledEvenAfterDeferPanic(t *testing.T) {
c := qt.New(t)
deferred1 := 0
deferred2 := 0
c.Defer(func() {
deferred1++
})
c.Defer(func() {
panic("scream and shout")
})
c.Defer(func() {
deferred2++
})
c.Defer(func() {
panic("run in circles")
})
func() {
defer func() {
c.Check(recover(), qt.Equals, "scream and shout")
}()
c.Done()
}()
c.Assert(deferred1, qt.Equals, 1)
c.Assert(deferred2, qt.Equals, 1)
c.Done()
c.Assert(deferred1, qt.Equals, 1)
c.Assert(deferred2, qt.Equals, 1)
}
func TestCDeferCalledEvenAfterGoexit(t *testing.T) {
// The testing package uses runtime.Goexit on
// assertion failure, so check that defers are still
// called in that case.
c := qt.New(t)
defers := 0
c.Defer(func() {
defers++
})
c.Defer(func() {
runtime.Goexit()
})
done := make(chan struct{})
go func() {
defer close(done)
c.Done()
select {}
}()
<-done
c.Assert(defers, qt.Equals, 1)
c.Done()
c.Assert(defers, qt.Equals, 1)
}
func TestCRunDefer(t *testing.T) {
c := qt.New(&testingT{})
outerDefer := 0
innerDefer := 0
c.Defer(func() { outerDefer++ })
c.Run("x", func(c *qt.C) {
c.Defer(func() { innerDefer++ })
})
c.Assert(innerDefer, qt.Equals, 1)
c.Assert(outerDefer, qt.Equals, 0)
}
func checkResult(t *testing.T, ok bool, got, want string) {
if want != "" {
assertPrefix(t, got, want)
assertBool(t, ok, false)
return
}
if got != "" {
t.Fatalf("output:\ngot %q\nwant empty", got)
}
assertBool(t, ok, true)
}
// testingT can be passed to qt.New for testing purposes.
type testingT struct {
testing.TB
errorBuf bytes.Buffer
fatalBuf bytes.Buffer
subTestResult bool
subTestName string
subTestT *testing.T
parallel bool
}
// Error overrides *testing.T.Error so that messages are collected.
func (t *testingT) Error(a ...interface{}) {
fmt.Fprint(&t.errorBuf, a...)
}
// Fatal overrides *testing.T.Fatal so that messages are collected and the
// goroutine is not killed.
func (t *testingT) Fatal(a ...interface{}) {
fmt.Fprint(&t.fatalBuf, a...)
}
func (t *testingT) Parallel() {
t.parallel = true
}
// 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) {
if h, ok := t.(helper); ok {
h.Helper()
}
if prefix == "" {
t.Fatal("prefix: empty value provided")
}
if !strings.HasPrefix(got, prefix) {
t.Fatalf("prefix:\ngot %q\nwant %q:\n-----------------------------------\n%s", got, prefix, got)
}
}
// assertErrHasPrefix fails if the given error is nil or does not have the
// given prefix.
func assertErrHasPrefix(t testing.TB, err error, prefix string) {
if h, ok := t.(helper); ok {
h.Helper()
}
if err == nil {
t.Fatalf("error:\ngot nil\nwant %q", prefix)
}
assertPrefix(t, err.Error(), prefix)
}
// assertErrIsNil fails if the given error is not nil.
func assertErrIsNil(t testing.TB, err error) {
if h, ok := t.(helper); ok {
h.Helper()
}
if err != nil {
t.Fatalf("error:\ngot %q\nwant nil", err)
}
}
// assertBool fails if the given boolean values don't match.
func assertBool(t testing.TB, got, want bool) {
if h, ok := t.(helper); ok {
h.Helper()
}
if got != want {
t.Fatalf("bool:\ngot %v\nwant %v", got, want)
}
}
// helper is used to check whether the current Go version supports testing
// helpers.
type helper interface {
Helper()
}
// 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 {
argNames []string
addNotes func(note func(key string, value interface{}))
err error
}
// Check implements quicktest.Checker by returning the stored error.
func (c *testingChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) error {
if c.addNotes != nil {
c.addNotes(note)
}
return c.err
}
// Info implements quicktest.Checker by returning the stored args.
func (c *testingChecker) ArgNames() []string {
return c.argNames
}