-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.go
498 lines (474 loc) · 11.3 KB
/
test_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
package test_test
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"os"
"slices"
"testing"
"github.com/FollowTheProcess/snapshot"
"github.com/FollowTheProcess/test"
)
var update = flag.Bool("update", false, "Update snapshots")
// TB is a fake implementation of [testing.TB] that simply records in internal
// state whether or not it would have failed and what it would have written.
type TB struct {
testing.TB
out io.Writer
failed bool
}
func (t *TB) Helper() {} //nolint: revive // We don't use t because it does nothing
func (t *TB) Fatal(args ...any) {
t.failed = true
fmt.Fprint(t.out, args...)
}
func (t *TB) Fatalf(format string, args ...any) {
t.failed = true
fmt.Fprintf(t.out, format, args...)
}
func TestTest(t *testing.T) {
tests := []struct {
fn func(tb testing.TB) // The test function we're... testing?
name string // Name of the test case
wantFail bool // Whether it should fail
}{
{
name: "Equal/pass",
fn: func(tb testing.TB) {
test.Equal(tb, "apples", "apples")
},
wantFail: false,
},
{
name: "Equal/fail",
fn: func(tb testing.TB) {
test.Equal(tb, "apples", "oranges")
},
wantFail: true,
},
{
name: "Equal/fail with context",
fn: func(tb testing.TB) {
test.Equal(tb, "apples", "oranges", test.Context("Apples are not oranges!"))
},
wantFail: true,
},
{
name: "Equal/fail context format",
fn: func(tb testing.TB) {
test.Equal(tb, "apples", "oranges", test.Context("Apples == Oranges: %v", false))
},
wantFail: true,
},
{
name: "Equal/fail with title",
fn: func(tb testing.TB) {
test.Equal(tb, "apples", "oranges", test.Title("My fruit test"))
},
wantFail: true,
},
{
name: "NotEqual/pass",
fn: func(tb testing.TB) {
test.NotEqual(tb, "apples", "oranges")
},
wantFail: false,
},
{
name: "NotEqual/fail",
fn: func(tb testing.TB) {
test.NotEqual(tb, "apples", "apples")
},
wantFail: true,
},
{
name: "NotEqual/fail with context",
fn: func(tb testing.TB) {
test.NotEqual(tb, 42, 42, test.Context("42 is the meaning of life"))
},
wantFail: true,
},
{
name: "NotEqual/fail context format",
fn: func(tb testing.TB) {
test.NotEqual(tb, 42, 42, test.Context("42 == meaning of life: %v", true))
},
wantFail: true,
},
{
name: "NotEqual/fail with title",
fn: func(tb testing.TB) {
test.NotEqual(tb, "apples", "apples", test.Title("My fruit test"))
},
wantFail: true,
},
{
name: "EqualFunc/pass",
fn: func(tb testing.TB) {
test.EqualFunc(tb, []int{1, 2, 3, 4}, []int{1, 2, 3, 4}, slices.Equal)
},
wantFail: false,
},
{
name: "EqualFunc/fail",
fn: func(tb testing.TB) {
cmp := func(_, _ []string) bool { return false } // Cheating
test.EqualFunc(tb, []string{"hello"}, []string{"there"}, cmp)
},
wantFail: true,
},
{
name: "EqualFunc/fail with context",
fn: func(tb testing.TB) {
test.EqualFunc(
tb,
[]string{"hello"},
[]string{"there"},
slices.Equal,
test.Context("some context here"),
)
},
wantFail: true,
},
{
name: "EqualFunc/fail context format",
fn: func(tb testing.TB) {
test.EqualFunc(
tb,
[]string{"hello"},
[]string{"there"},
slices.Equal,
test.Context("who's bad at testing... %s", "you"),
)
},
wantFail: true,
},
{
name: "EqualFunc/fail with title",
fn: func(tb testing.TB) {
test.EqualFunc(tb, []string{"hello"}, []string{"there"}, slices.Equal, test.Title("Hello!"))
},
wantFail: true,
},
{
name: "NotEqualFunc/pass",
fn: func(tb testing.TB) {
test.NotEqualFunc(tb, []int{1, 2, 3, 4}, []int{5, 6, 7, 8}, slices.Equal)
},
wantFail: false,
},
{
name: "NotEqualFunc/fail",
fn: func(tb testing.TB) {
cmp := func(_, _ []string) bool { return true } // Cheating
test.NotEqualFunc(tb, []string{"hello"}, []string{"there"}, cmp)
},
wantFail: true,
},
{
name: "NotEqualFunc/fail with context",
fn: func(tb testing.TB) {
test.NotEqualFunc(
tb,
[]string{"hello"},
[]string{"hello"},
slices.Equal,
test.Context("some context here"),
)
},
wantFail: true,
},
{
name: "NotEqualFunc/fail context format",
fn: func(tb testing.TB) {
test.NotEqualFunc(
tb,
[]string{"hello"},
[]string{"hello"},
slices.Equal,
test.Context("who's bad at testing... %s", "you"),
)
},
wantFail: true,
},
{
name: "NotEqualFunc/fail with title",
fn: func(tb testing.TB) {
test.NotEqualFunc(tb, []string{"hello"}, []string{"hello"}, slices.Equal, test.Title("Hello!"))
},
wantFail: true,
},
{
name: "NearlyEqual/pass",
fn: func(tb testing.TB) {
test.NearlyEqual(tb, 3.0000000001, 3.0)
},
wantFail: false,
},
{
name: "NearlyEqual/fail",
fn: func(tb testing.TB) {
test.NearlyEqual(tb, 3.0000001, 3.0)
},
wantFail: true,
},
{
name: "NearlyEqual/fail custom tolerance",
fn: func(tb testing.TB) {
test.NearlyEqual(tb, 3.2, 3.0, test.FloatEqualityThreshold(0.1))
},
wantFail: true,
},
{
name: "NearlyEqual/fail with context",
fn: func(tb testing.TB) {
test.NearlyEqual(tb, 3.0000001, 3.0, test.Context("Numbers don't work that way"))
},
wantFail: true,
},
{
name: "Ok/pass",
fn: func(tb testing.TB) {
test.Ok(tb, nil)
},
wantFail: false,
},
{
name: "Ok/fail",
fn: func(tb testing.TB) {
test.Ok(tb, errors.New("uh oh"))
},
wantFail: true,
},
{
name: "Ok/fail with context",
fn: func(tb testing.TB) {
test.Ok(tb, errors.New("uh oh"), test.Context("Could not frobnicate the baz"))
},
wantFail: true,
},
{
name: "Ok/fail with title",
fn: func(tb testing.TB) {
test.Ok(tb, errors.New("uh oh"), test.Title("Bang!"))
},
wantFail: true,
},
{
name: "Err/pass",
fn: func(tb testing.TB) {
test.Err(tb, errors.New("bang"))
},
wantFail: false,
},
{
name: "Err/fail",
fn: func(tb testing.TB) {
test.Err(tb, nil)
},
wantFail: true,
},
{
name: "Err/fail with context",
fn: func(tb testing.TB) {
test.Err(tb, nil, test.Context("Frobnicated the baz when it should have failed"))
},
wantFail: true,
},
{
name: "Err/fail with title",
fn: func(tb testing.TB) {
test.Err(tb, nil, test.Title("Everything is fine?"))
},
wantFail: true,
},
{
name: "WantErr/pass error",
fn: func(tb testing.TB) {
test.WantErr(tb, errors.New("bang"), true) // Wanted an error and got one - should pass
},
wantFail: false,
},
{
name: "WantErr/pass nil",
fn: func(tb testing.TB) {
test.WantErr(tb, nil, false) // Didn't want an error and got nil - should pass
},
wantFail: false,
},
{
name: "WantErr/fail error",
fn: func(tb testing.TB) {
test.WantErr(tb, errors.New("bang"), false) // Got an error but didn't want one - should fail
},
wantFail: true,
},
{
name: "WantErr/fail nil",
fn: func(tb testing.TB) {
test.WantErr(tb, nil, true) // Didn't get an error but wanted one - should fail
},
wantFail: true,
},
{
name: "WantErr/fail with context",
fn: func(tb testing.TB) {
test.WantErr(tb, errors.New("bang"), false, test.Context("Errors are bad!"))
},
wantFail: true,
},
{
name: "WantErr/fail with title",
fn: func(tb testing.TB) {
test.WantErr(tb, errors.New("bang"), false, test.Title("A very bad test"))
},
wantFail: true,
},
{
name: "True/pass",
fn: func(tb testing.TB) {
test.True(tb, true)
},
wantFail: false,
},
{
name: "True/fail",
fn: func(tb testing.TB) {
test.True(tb, false)
},
wantFail: true,
},
{
name: "True/fail with context",
fn: func(tb testing.TB) {
test.True(tb, false, test.Context("must always be true"))
},
wantFail: true,
},
{
name: "True/fail with title",
fn: func(tb testing.TB) {
test.True(tb, false, test.Title("Argh!"))
},
wantFail: true,
},
{
name: "False/pass",
fn: func(tb testing.TB) {
test.False(tb, false)
},
wantFail: false,
},
{
name: "False/fail",
fn: func(tb testing.TB) {
test.False(tb, true)
},
wantFail: true,
},
{
name: "False/fail with context",
fn: func(tb testing.TB) {
test.False(tb, true, test.Context("must always be false"))
},
wantFail: true,
},
{
name: "False/fail with title",
fn: func(tb testing.TB) {
test.False(tb, true, test.Title("Argh!"))
},
wantFail: true,
},
{
name: "Diff/pass",
fn: func(tb testing.TB) {
got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n"
want := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n"
test.Diff(tb, got, want)
},
wantFail: false,
},
{
name: "Diff/fail",
fn: func(tb testing.TB) {
got := "Some\nstuff here in this file\nlines as well wow\nsome more stuff\n"
want := "Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n"
test.Diff(tb, got, want)
},
wantFail: true,
},
{
name: "DiffBytes/pass",
fn: func(tb testing.TB) {
got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n")
want := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n")
test.DiffBytes(tb, got, want)
},
wantFail: false,
},
{
name: "DiffBytes/fail",
fn: func(tb testing.TB) {
got := []byte("Some\nstuff here in this file\nlines as well wow\nsome more stuff\n")
want := []byte("Some\ndifferent stuff here in this file\nthis line is different\nsome more stuff\n")
test.DiffBytes(tb, got, want)
},
wantFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
tb := &TB{out: buf}
snap := snapshot.New(t, snapshot.Update(*update), snapshot.Color(false))
if tb.failed {
t.Fatalf("%s initial failed state should be false", tt.name)
}
// Call the test function, passing in the mock TB that just records
// what a "real" TB would have done
tt.fn(tb)
if tb.failed != tt.wantFail {
t.Fatalf("\nIncorrect Failure\n\ntb.failed:\t%v\nwanted:\t%v\n", tb.failed, tt.wantFail)
}
// Test the output matches our snapshot file, only for failed tests
// as there should be no output for passed tests
if !tb.failed {
if buf.Len() != 0 {
t.Fatalf("\nIncorrect Output\n\nA passed test should have no output, got: %s\n", buf.String())
}
} else {
snap.Snap(buf.String())
}
})
}
}
func TestCapture(t *testing.T) {
t.Run("happy", func(t *testing.T) {
// Some fake user function that writes to stdout and stderr
fn := func() error {
fmt.Fprintln(os.Stdout, "hello stdout")
fmt.Fprintln(os.Stderr, "hello stderr")
return nil
}
stdout, stderr := test.CaptureOutput(t, fn)
test.Equal(t, stdout, "hello stdout\n")
test.Equal(t, stderr, "hello stderr\n")
})
t.Run("sad", func(t *testing.T) {
// This time the user function returns an error
fn := func() error {
return errors.New("it broke")
}
buf := &bytes.Buffer{}
testTB := &TB{out: buf}
stdout, stderr := test.CaptureOutput(testTB, fn)
// Test should have failed
test.True(t, testTB.failed)
// stdout and stderr should be empty
test.Equal(t, stdout, "")
test.Equal(t, stderr, "")
})
}