-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
580 lines (552 loc) · 14.1 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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package test_test
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/FollowTheProcess/test"
"github.com/google/go-cmp/cmp"
)
// 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() {}
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 TestPassFail(t *testing.T) {
tests := []struct {
testFunc func(tb testing.TB) // The test function we're... testing
wantOut string // What we wanted the TB to print
name string // Name of the test case
wantFail bool // Whether we wanted the testFunc to fail it's TB
}{
{
name: "equal string pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Equal(tb, "apples", "apples") // These obviously are equal
},
wantFail: false, // Should pass
wantOut: "", // And write no output
},
{
name: "equal string fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Equal(tb, "apples", "oranges")
},
wantFail: true,
wantOut: "\nNot Equal\n---------\nGot:\tapples\nWanted:\toranges\n",
},
{
name: "equal string fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Equal(tb, "apples", "oranges") // apples are not oranges
},
wantFail: true,
wantOut: "\nNot Equal // apples are not oranges\n---------\nGot:\tapples\nWanted:\toranges\n",
},
{
name: "equal int pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Equal(tb, 1, 1)
},
wantFail: false,
wantOut: "",
},
{
name: "equal int fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Equal(tb, 1, 42)
},
wantFail: true,
wantOut: "\nNot Equal\n---------\nGot:\t1\nWanted:\t42\n",
},
{
name: "nearly equal pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NearlyEqual(tb, 3.0000000001, 3.0)
},
wantFail: false,
wantOut: "",
},
{
name: "nearly equal fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NearlyEqual(tb, 3.0000001, 3.0)
},
wantFail: true,
wantOut: "\nNot NearlyEqual\n---------------\nGot:\t3.0000001\nWanted:\t3\n\nDifference 9.999999983634211e-08 exceeds maximum tolerance of 1e-08\n",
},
{
name: "nearly equal fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NearlyEqual(tb, 3.0000001, 3.0) // Ooof so close
},
wantFail: true,
wantOut: "\nNot NearlyEqual // Ooof so close\n---------------\nGot:\t3.0000001\nWanted:\t3\n\nDifference 9.999999983634211e-08 exceeds maximum tolerance of 1e-08\n",
},
{
name: "not equal string pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, "apples", "oranges") // Should pass, these aren't equal
},
wantFail: false,
wantOut: "",
},
{
name: "not equal string fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, "apples", "apples")
},
wantFail: true,
wantOut: "\nEqual\n-----\nGot:\tapples\n\nExpected values to be different\n",
},
{
name: "not equal string fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, "apples", "apples") // different apples
},
wantFail: true,
wantOut: "\nEqual // different apples\n-----\nGot:\tapples\n\nExpected values to be different\n",
},
{
name: "not equal int pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, 1, 42)
},
wantFail: false,
wantOut: "",
},
{
name: "not equal int fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, 1, 1)
},
wantFail: true,
wantOut: "\nEqual\n-----\nGot:\t1\n\nExpected values to be different\n",
},
{
name: "not equal int fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.NotEqual(tb, 1, 1) // 1 != 1?
},
wantFail: true,
wantOut: "\nEqual // 1 != 1?\n-----\nGot:\t1\n\nExpected values to be different\n",
},
{
name: "ok pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Ok(tb, nil)
},
wantFail: false,
wantOut: "",
},
{
name: "ok fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Ok(tb, errors.New("uh oh"))
},
wantFail: true,
wantOut: "\nNot Ok\n------\nGot:\tuh oh\nWanted:\t<nil>\n",
},
{
name: "ok fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Ok(tb, errors.New("uh oh")) // Calling some function
},
wantFail: true,
wantOut: "\nNot Ok // Calling some function\n------\nGot:\tuh oh\nWanted:\t<nil>\n",
},
{
name: "err pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Err(tb, errors.New("uh oh"))
},
wantFail: false,
wantOut: "",
},
{
name: "err fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Err(tb, nil)
},
wantFail: true,
wantOut: "\nNot Err\n-------\nGot:\t<nil>\nWanted:\terror\n",
},
{
name: "err fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Err(tb, nil) // Should have failed
},
wantFail: true,
wantOut: "\nNot Err // Should have failed\n-------\nGot:\t<nil>\nWanted:\terror\n",
},
{
name: "true pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.True(tb, true)
},
wantFail: false,
wantOut: "",
},
{
name: "true fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.True(tb, false)
},
wantFail: true,
wantOut: "\nNot True\n--------\nGot:\tfalse\nWanted:\ttrue\n",
},
{
name: "true fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.True(tb, false) // Comment here
},
wantFail: true,
wantOut: "\nNot True // Comment here\n--------\nGot:\tfalse\nWanted:\ttrue\n",
},
{
name: "false pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.False(tb, false)
},
wantFail: false,
wantOut: "",
},
{
name: "false fail",
testFunc: func(tb testing.TB) {
tb.Helper()
test.False(tb, true)
},
wantFail: true,
wantOut: "\nNot False\n---------\nGot:\ttrue\nWanted:\tfalse\n",
},
{
name: "false fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.False(tb, true) // Should always be false
},
wantFail: true,
wantOut: "\nNot False // Should always be false\n---------\nGot:\ttrue\nWanted:\tfalse\n",
},
{
name: "equal func pass",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishEqual := func(a, b string) bool {
return true // Always equal
}
test.EqualFunc(tb, "word", "different word", rubbishEqual)
},
wantFail: false,
wantOut: "",
},
{
name: "equal func fail",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishEqual := func(a, b string) bool {
return false // Never equal
}
test.EqualFunc(tb, "word", "word", rubbishEqual)
},
wantFail: true,
wantOut: "\nNot Equal\n---------\nGot:\tword\nWanted:\tword\n\nequal(got, want) returned false\n",
},
{
name: "equal func fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishEqual := func(a, b string) bool {
return false // Never equal
}
test.EqualFunc(tb, "word", "word", rubbishEqual) // Uh oh
},
wantFail: true,
wantOut: "\nNot Equal // Uh oh\n---------\nGot:\tword\nWanted:\tword\n\nequal(got, want) returned false\n",
},
{
name: "not equal func pass",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishNotEqual := func(a, b string) bool {
return false // Never equal
}
test.NotEqualFunc(tb, "word", "word", rubbishNotEqual)
},
wantFail: false,
wantOut: "",
},
{
name: "not equal func fail",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishNotEqual := func(a, b string) bool {
return true // Always equal
}
test.NotEqualFunc(tb, "word", "different word", rubbishNotEqual)
},
wantFail: true,
wantOut: "\nEqual\n-----\nGot:\tword\n\nequal(got, want) returned true\n",
},
{
name: "not equal func fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
rubbishNotEqual := func(a, b string) bool {
return true // Always equal
}
test.NotEqualFunc(tb, "word", "different word", rubbishNotEqual) // Bad equal
},
wantFail: true,
wantOut: "\nEqual // Bad equal\n-----\nGot:\tword\n\nequal(got, want) returned true\n",
},
{
name: "deep equal pass",
testFunc: func(tb testing.TB) {
tb.Helper()
a := []string{"a", "b", "c"}
b := []string{"a", "b", "c"}
test.DeepEqual(tb, a, b)
},
wantFail: false,
wantOut: "",
},
{
name: "deep equal fail",
testFunc: func(tb testing.TB) {
tb.Helper()
a := []string{"a", "b", "c"}
b := []string{"d", "e", "f"}
test.DeepEqual(tb, a, b)
},
wantFail: true,
wantOut: "\nNot Equal\n---------\nGot:\t[a b c]\nWanted:\t[d e f]\n\nreflect.DeepEqual(got, want) returned false\n",
},
{
name: "deep equal fail with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
a := []string{"a", "b", "c"}
b := []string{"d", "e", "f"}
test.DeepEqual(tb, a, b) // Oh no!
},
wantFail: true,
wantOut: "\nNot Equal // Oh no!\n---------\nGot:\t[a b c]\nWanted:\t[d e f]\n\nreflect.DeepEqual(got, want) returned false\n",
},
{
name: "want err pass when got and wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, errors.New("uh oh"), true) // We wanted an error and got one
},
wantFail: false,
wantOut: "",
},
{
name: "want err fail when got and not wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, errors.New("uh oh"), false)
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot:\tuh oh\nWanted:\t<nil>\n\nGot an unexpected error: uh oh\n",
},
{
name: "want err fail when got and not wanted with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, errors.New("uh oh"), false) // comment
},
wantFail: true,
wantOut: "\nWantErr // comment\n-------\nGot:\tuh oh\nWanted:\t<nil>\n\nGot an unexpected error: uh oh\n",
},
{
name: "want err pass when not got and not wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, nil, false) // Didn't want an error and didn't get one
},
wantFail: false,
wantOut: "",
},
{
name: "want err fail when not got but wanted",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, nil, true)
},
wantFail: true,
wantOut: "\nWantErr\n-------\nGot:\t<nil>\nWanted:\terror\n\nWanted an error but got <nil>\n",
},
{
name: "want err fail when not got but wanted with comment",
testFunc: func(tb testing.TB) {
tb.Helper()
test.WantErr(tb, nil, true) // comment
},
wantFail: true,
wantOut: "\nWantErr // comment\n-------\nGot:\t<nil>\nWanted:\terror\n\nWanted an error but got <nil>\n",
},
{
name: "file pass",
testFunc: func(tb testing.TB) {
tb.Helper()
test.File(tb, "hello\n", filepath.Join(test.Data(t), "file.txt"))
},
wantFail: false,
wantOut: "",
},
{
name: "diff pass string",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Diff(tb, "hello", "hello")
},
wantFail: false,
wantOut: "",
},
{
name: "diff fail string",
testFunc: func(tb testing.TB) {
tb.Helper()
test.Diff(tb, "hello", "hello there")
},
wantFail: true,
wantOut: fmt.Sprintf(
"\nMismatch (-want, +got):\n%s\n",
cmp.Diff("hello there", "hello"),
), // Output equivalent to diff
},
{
name: "diff pass string slice",
testFunc: func(tb testing.TB) {
tb.Helper()
got := []string{"hello", "there"}
want := []string{"hello", "there"}
test.Diff(tb, got, want)
},
wantFail: false,
wantOut: "",
},
{
name: "diff fail string slice",
testFunc: func(tb testing.TB) {
tb.Helper()
got := []string{"hello", "there"}
want := []string{"not", "me"}
test.Diff(tb, got, want)
},
wantFail: true,
wantOut: fmt.Sprintf(
"\nMismatch (-want, +got):\n%s\n",
cmp.Diff([]string{"not", "me"}, []string{"hello", "there"}),
), // Output equivalent to diff
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
tb := &TB{out: buf}
if tb.failed {
t.Fatalf("%s initial failed state should be false", tt.name)
}
// Call the test function, passing in our mock TB that simply
// records whether or not it would have failed and what it would
// have written
tt.testFunc(tb)
if tb.failed != tt.wantFail {
t.Fatalf(
"\n%s failure mismatch\n--------------\nfailed:\t%v\nwanted failure:\t%v\n",
tt.name,
tb.failed,
tt.wantFail,
)
}
if got := buf.String(); got != tt.wantOut {
t.Errorf(
"\n%s output mismatch\n---------------\nGot:\t%s\nWanted:\t%s\n",
tt.name,
got,
tt.wantOut,
)
}
})
}
}
func TestData(t *testing.T) {
got := test.Data(t)
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Test for Data could not get cwd: %v", err)
}
want := filepath.Join(cwd, "testdata")
if got != want {
t.Errorf("\nGot:\t%s\nWanted:\t%s\n", got, want)
}
}
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, "")
})
}