forked from gotestyourself/gotest.tools
/
assert_test.go
470 lines (386 loc) · 11.6 KB
/
assert_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
package assert
import (
"fmt"
"os"
"testing"
gocmp "github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert/cmp"
)
type fakeTestingT struct {
failNowed bool
failed bool
msgs []string
}
func (f *fakeTestingT) FailNow() {
f.failNowed = true
}
func (f *fakeTestingT) Fail() {
f.failed = true
}
func (f *fakeTestingT) Log(args ...interface{}) {
f.msgs = append(f.msgs, args[0].(string))
}
func (f *fakeTestingT) Helper() {}
func TestAssert_WithBinaryExpression_Failures(t *testing.T) {
t.Run("equal", func(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 1 == 6)
expectFailNowed(t, fakeT, "assertion failed: 1 is not 6")
})
t.Run("not equal", func(t *testing.T) {
fakeT := &fakeTestingT{}
a := 1
Assert(fakeT, a != 1)
expectFailNowed(t, fakeT, "assertion failed: a is 1")
})
t.Run("greater than", func(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 1 > 5)
expectFailNowed(t, fakeT, "assertion failed: 1 is <= 5")
})
t.Run("less than", func(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 5 < 1)
expectFailNowed(t, fakeT, "assertion failed: 5 is >= 1")
})
t.Run("greater than or equal", func(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 1 >= 5)
expectFailNowed(t, fakeT, "assertion failed: 1 is less than 5")
})
t.Run("less than or equal", func(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 6 <= 2)
expectFailNowed(t, fakeT, "assertion failed: 6 is greater than 2")
})
}
func TestAssertWithBoolIdent(t *testing.T) {
fakeT := &fakeTestingT{}
var ok bool
Assert(fakeT, ok)
expectFailNowed(t, fakeT, "assertion failed: ok is false")
}
func TestAssertWithBoolFailureNotEqual(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
Assert(fakeT, err != nil)
expectFailNowed(t, fakeT, "assertion failed: err is nil")
}
func TestAssertWithBoolFailureNotTrue(t *testing.T) {
fakeT := &fakeTestingT{}
badNews := true
Assert(fakeT, !badNews)
expectFailNowed(t, fakeT, "assertion failed: badNews is true")
}
func TestAssertWithBoolFailureAndExtraMessage(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 1 > 5, "sometimes things fail")
expectFailNowed(t, fakeT, "assertion failed: 1 is <= 5: sometimes things fail")
}
func TestAssertWithBoolSuccess(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, 1 < 5)
expectSuccess(t, fakeT)
}
func TestAssertWithBoolMultiLineFailure(t *testing.T) {
fakeT := &fakeTestingT{}
Assert(fakeT, func() bool {
for range []int{1, 2, 3, 4} {
}
return false
}())
expectFailNowed(t, fakeT, `assertion failed: expression is false: func() bool {
for range []int{1, 2, 3, 4} {
}
return false
}()`)
}
type exampleComparison struct {
success bool
message string
}
func (c exampleComparison) Compare() (bool, string) {
return c.success, c.message
}
func TestAssertWithComparisonSuccess(t *testing.T) {
fakeT := &fakeTestingT{}
cmp := exampleComparison{success: true}
Assert(fakeT, cmp.Compare)
expectSuccess(t, fakeT)
}
func TestAssertWithComparisonFailure(t *testing.T) {
fakeT := &fakeTestingT{}
cmp := exampleComparison{message: "oops, not good"}
Assert(fakeT, cmp.Compare)
expectFailNowed(t, fakeT, "assertion failed: oops, not good")
}
func TestAssertWithComparisonAndExtraMessage(t *testing.T) {
fakeT := &fakeTestingT{}
cmp := exampleComparison{message: "oops, not good"}
Assert(fakeT, cmp.Compare, "extra stuff %v", true)
expectFailNowed(t, fakeT, "assertion failed: oops, not good: extra stuff true")
}
type customError struct {
field bool
}
func (e *customError) Error() string {
// access a field of the receiver to simulate the behaviour of most
// implementations, and test handling of non-nil typed errors.
e.field = true
return "custom error"
}
func TestNilError(t *testing.T) {
t.Run("nil interface", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
NilError(fakeT, err)
expectSuccess(t, fakeT)
})
t.Run("nil literal", func(t *testing.T) {
fakeT := &fakeTestingT{}
NilError(fakeT, nil)
expectSuccess(t, fakeT)
})
t.Run("interface with non-nil type", func(t *testing.T) {
fakeT := &fakeTestingT{}
var customErr *customError
NilError(fakeT, customErr)
expected := "assertion failed: error is not nil: error has type *assert.customError"
expectFailNowed(t, fakeT, expected)
})
t.Run("non-nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
NilError(fakeT, fmt.Errorf("this is the error"))
expectFailNowed(t, fakeT, "assertion failed: error is not nil: this is the error")
})
t.Run("non-nil error with struct type", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := structError{}
NilError(fakeT, err)
expectFailNowed(t, fakeT, "assertion failed: error is not nil: this is a struct")
})
t.Run("non-nil error with map type", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err mapError
NilError(fakeT, err)
expectFailNowed(t, fakeT, "assertion failed: error is not nil: ")
})
}
type structError struct{}
func (structError) Error() string {
return "this is a struct"
}
type mapError map[int]string
func (m mapError) Error() string {
return m[0]
}
func TestCheckFailure(t *testing.T) {
fakeT := &fakeTestingT{}
if Check(fakeT, 1 == 2) {
t.Error("expected check to return false on failure")
}
expectFailed(t, fakeT, "assertion failed: 1 is not 2")
}
func TestCheckSuccess(t *testing.T) {
fakeT := &fakeTestingT{}
if !Check(fakeT, true) {
t.Error("expected check to return true on success")
}
expectSuccess(t, fakeT)
}
func TestCheckEqualFailure(t *testing.T) {
fakeT := &fakeTestingT{}
actual, expected := 5, 9
Check(fakeT, cmp.Equal(actual, expected))
expectFailed(t, fakeT, "assertion failed: 5 (actual int) != 9 (expected int)")
}
func TestCheck_MultipleFunctionsOnTheSameLine(t *testing.T) {
fakeT := &fakeTestingT{}
f := func(b bool) {}
f(Check(fakeT, false))
// TODO: update the expected when there is a more correct fix
expectFailed(t, fakeT,
"assertion failed: but assert failed to find the expression to print")
}
func TestEqualSuccess(t *testing.T) {
fakeT := &fakeTestingT{}
Equal(fakeT, 1, 1)
expectSuccess(t, fakeT)
Equal(fakeT, "abcd", "abcd")
expectSuccess(t, fakeT)
}
func TestEqualFailure(t *testing.T) {
fakeT := &fakeTestingT{}
actual, expected := 1, 3
Equal(fakeT, actual, expected)
expectFailNowed(t, fakeT, "assertion failed: 1 (actual int) != 3 (expected int)")
}
func TestEqualFailureTypes(t *testing.T) {
fakeT := &fakeTestingT{}
Equal(fakeT, 3, uint(3))
expectFailNowed(t, fakeT, `assertion failed: 3 (int) != 3 (uint)`)
}
func TestEqualFailureWithSelectorArgument(t *testing.T) {
fakeT := &fakeTestingT{}
type tc struct {
expected string
}
var testcase = tc{expected: "foo"}
Equal(fakeT, "ok", testcase.expected)
expectFailNowed(t, fakeT,
"assertion failed: ok (string) != foo (testcase.expected string)")
}
func TestEqualFailureWithIndexExpr(t *testing.T) {
fakeT := &fakeTestingT{}
expected := map[string]string{"foo": "bar"}
Equal(fakeT, "ok", expected["foo"])
expectFailNowed(t, fakeT,
`assertion failed: ok (string) != bar (expected["foo"] string)`)
}
func TestEqualFailureWithCallExprArgument(t *testing.T) {
fakeT := &fakeTestingT{}
ce := customError{}
Equal(fakeT, "", ce.Error())
expectFailNowed(t, fakeT,
"assertion failed: (string) != custom error (string)")
}
func TestAssertFailureWithOfflineComparison(t *testing.T) {
fakeT := &fakeTestingT{}
a := 1
b := 2
// store comparison in a variable, so ast lookup can't find it
comparison := cmp.Equal(a, b)
Assert(fakeT, comparison)
// expected value wont have variable names
expectFailNowed(t, fakeT, "assertion failed: 1 (int) != 2 (int)")
}
type testingT interface {
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
}
func expectFailNowed(t testingT, fakeT *fakeTestingT, expected string) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if fakeT.failed {
t.Errorf("should not have failed, got messages %s", fakeT.msgs)
}
if !fakeT.failNowed {
t.Fatalf("should have failNowed with message %s", expected)
}
if fakeT.msgs[0] != expected {
t.Fatalf("should have failure message %q, got %q", expected, fakeT.msgs[0])
}
}
func expectFailed(t testingT, fakeT *fakeTestingT, expected string) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if fakeT.failNowed {
t.Errorf("should not have failNowed, got messages %s", fakeT.msgs)
}
if !fakeT.failed {
t.Fatalf("should have failed with message %s", expected)
}
if fakeT.msgs[0] != expected {
t.Fatalf("should have failure message %q, got %q", expected, fakeT.msgs[0])
}
}
func expectSuccess(t testingT, fakeT *fakeTestingT) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if fakeT.failNowed {
t.Errorf("should not have failNowed, got messages %s", fakeT.msgs)
}
if fakeT.failed {
t.Errorf("should not have failed, got messages %s", fakeT.msgs)
}
}
type stub struct {
a string
b int
}
func TestDeepEqualSuccess(t *testing.T) {
actual := stub{"ok", 1}
expected := stub{"ok", 1}
fakeT := &fakeTestingT{}
DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
expectSuccess(t, fakeT)
}
func TestDeepEqualFailure(t *testing.T) {
actual := stub{"ok", 1}
expected := stub{"ok", 2}
fakeT := &fakeTestingT{}
DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
if !fakeT.failNowed {
t.Fatal("should have failNowed")
}
}
func TestErrorFailure(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
Error(fakeT, err, "this error")
expectFailNowed(t, fakeT, "assertion failed: expected an error, got nil")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := fmt.Errorf("the actual error")
Error(fakeT, err, "this error")
expected := `assertion failed: expected error "this error", got "the actual error"`
expectFailNowed(t, fakeT, expected)
})
}
func TestErrorContainsFailure(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
ErrorContains(fakeT, err, "this error")
expectFailNowed(t, fakeT, "assertion failed: expected an error, got nil")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := fmt.Errorf("the actual error")
ErrorContains(fakeT, err, "this error")
expected := `assertion failed: expected error to contain "this error", got "the actual error"`
expectFailNowed(t, fakeT, expected)
})
}
func TestErrorTypeFailure(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
ErrorType(fakeT, err, os.IsNotExist)
expectFailNowed(t, fakeT, "assertion failed: error is nil, not os.IsNotExist")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := fmt.Errorf("the actual error")
ErrorType(fakeT, err, os.IsNotExist)
expected := `assertion failed: error is the actual error (*errors.errorString), not os.IsNotExist`
expectFailNowed(t, fakeT, expected)
})
}
func TestErrorIs(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
ErrorIs(fakeT, err, os.ErrNotExist)
expected := `assertion failed: error is nil, not "file does not exist" (os.ErrNotExist)`
expectFailNowed(t, fakeT, expected)
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := fmt.Errorf("the actual error")
ErrorIs(fakeT, err, os.ErrNotExist)
expected := `assertion failed: error is "the actual error", not "file does not exist" (os.ErrNotExist)`
expectFailNowed(t, fakeT, expected)
})
t.Run("same error", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := fmt.Errorf("some wrapping: %w", os.ErrNotExist)
ErrorIs(fakeT, err, os.ErrNotExist)
expectSuccess(t, fakeT)
})
}