-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_assertions_test.go
More file actions
99 lines (82 loc) · 2.25 KB
/
custom_assertions_test.go
File metadata and controls
99 lines (82 loc) · 2.25 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
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
package check_test
import (
"testing"
"github.com/ctx42/testing/internal/affirm"
"github.com/ctx42/testing/pkg/notice"
"github.com/ctx42/testing/pkg/testcases"
"github.com/ctx42/testing/pkg/tester"
"github.com/ctx42/testing/pkg/check"
)
// AssertTA is custom assertion example.
func AssertTA(t tester.T, want, have *testcases.TA, opts ...any) bool {
t.Helper()
ops := check.DefaultOptions(opts...)
fName := check.FieldName(ops, "TA")
if e := check.NotNil(want, fName("")); e != nil {
t.Error(notice.From(e).Append("argument", "%s", "want"))
return false
}
if e := check.NotNil(have, fName("")); e != nil {
t.Error(notice.From(e).Append("argument", "%s", "have"))
return false
}
if e := check.Equal(want.Int, have.Int, fName("Int")); e != nil {
t.Error(e)
return false
}
if e := check.Equal(want.Str, have.Str, fName("Str")); e != nil {
t.Error(e)
return false
}
if e := check.Equal(want.TAp, have.TAp, fName("TAp")); e != nil {
t.Error(e)
return false
}
return true
}
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 ---
have := AssertTA(tspy, w, h)
// --- Then ---
affirm.Equal(t, true, have)
})
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 ---
have := AssertTA(tspy, w, h)
// --- Then ---
affirm.Equal(t, false, have)
})
t.Run("both want and have are nil", func(t *testing.T) {
// --- Given ---
tspy := tester.New(t)
tspy.ExpectError()
wMsg := "" +
"expected non-nil value:\n" +
" trail: TA\n" +
" argument: want"
tspy.ExpectLogEqual(wMsg)
tspy.Close()
// --- When ---
have := AssertTA(tspy, nil, nil)
// --- Then ---
affirm.Equal(t, false, have)
})
}