-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_assertions_test.go
More file actions
73 lines (58 loc) · 1.71 KB
/
custom_assertions_test.go
File metadata and controls
73 lines (58 loc) · 1.71 KB
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
package examples_test
import (
"testing"
"github.com/ctx42/testing/internal/affirm"
"github.com/ctx42/testing/pkg/check"
"github.com/ctx42/testing/pkg/testcases"
"github.com/ctx42/testing/pkg/tester"
"github.com/ctx42/testing/pkg/assert"
)
func AssertTA(t tester.T, want, have *testcases.TA, opts ...any) bool {
t.Helper()
ops := check.DefaultOptions(opts...)
fName := check.FieldName(ops, "TA")
if !assert.NotNil(t, want, fName("")) {
return false
}
assert.Equal(t, want.Int, have.Int, fName("Int"))
assert.Equal(t, want.Str, have.Str, fName("Str"))
assert.Equal(t, want.TAp, have.TAp, fName("TAp"))
return t.Failed()
}
func Test_AssertTA(t *testing.T) {
t.Run("equal", func(t *testing.T) {
// --- Given ---
tspy := tester.New(t)
tspy.Close()
w := &testcases.TA{Int: 1, Str: "a", TAp: &testcases.TA{Int: 2, Str: "b"}}
h := &testcases.TA{Int: 1, Str: "a", TAp: &testcases.TA{Int: 2, Str: "b"}}
// --- When ---
AssertTA(tspy, w, h)
})
t.Run("not equal field", func(t *testing.T) {
// --- Given ---
tspy := tester.New(t)
tspy.ExpectError()
wMsg := "" +
"expected values to be equal:\n" +
" trail: TA.TAp.Int\n" +
" want: 2\n" +
" have: 3"
tspy.ExpectLogEqual(wMsg)
tspy.Close()
w := &testcases.TA{Int: 1, Str: "a", TAp: &testcases.TA{Int: 2, Str: "b"}}
h := &testcases.TA{Int: 1, Str: "a", TAp: &testcases.TA{Int: 3, Str: "b"}}
// --- When ---
AssertTA(tspy, w, h)
})
t.Run("both want and have are nil", func(t *testing.T) {
// --- Given ---
tspy := tester.New(t)
tspy.ExpectFatal()
wMsg := "expected non-nil value:\n trail: TA"
tspy.ExpectLogEqual(wMsg)
tspy.Close()
// --- When ---
affirm.Panic(t, func() { AssertTA(tspy, nil, nil) })
})
}