-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers_equal_to_test.go
179 lines (175 loc) · 5.6 KB
/
matchers_equal_to_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
package justest_test
import (
"regexp"
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
. "github.com/arikkfir/justest"
)
func TestEqualTo(t *testing.T) {
t.Parallel()
t.Run("Builtin comparators", func(t *testing.T) {
type testCase struct {
actuals, expected []any
verifier TestOutcomeVerifier
}
testCases := map[string]testCase{
"Single item equality succeeds": {
actuals: []any{1},
expected: []any{1},
verifier: SuccessVerifier(),
},
"Multi-item equality succeeds": {
actuals: []any{1, 2, 3},
expected: []any{1, 2, 3},
verifier: SuccessVerifier(),
},
"Single item difference fails": {
actuals: []any{1},
expected: []any{2},
verifier: FailureVerifier(regexp.QuoteMeta(`Unexpected difference ("-" lines are expected values; "+" lines are actual values):`) + "\n.*"),
},
"Multi-item difference fails": {
actuals: []any{1, 2, 2},
expected: []any{1, 2, 3},
verifier: FailureVerifier(regexp.QuoteMeta(`Unexpected difference ("-" lines are expected values; "+" lines are actual values):`) + "\n.*"),
},
"Length validated": {
actuals: []any{1},
expected: []any{1, 2},
verifier: FailureVerifier(`^Unexpected difference: received 1 actual values and 2 expected values.*`),
},
"Failure diff uses opts": {
actuals: []any{
struct {
Public string
private string
}{
Public: "public-value",
private: "private-value",
},
},
expected: []any{
struct {
Public string
private string
}{Public: "incorrect-value", private: "private-value"},
cmpopts.IgnoreUnexported(struct {
Public string
private string
}{})},
verifier: FailureVerifier(regexp.QuoteMeta(`Unexpected difference ("-" lines are expected values; "+" lines are actual values):`) + "\n.*"),
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
defer mt.Verify(tc.verifier)
With(mt).VerifyThat(tc.actuals...).Will(EqualTo(tc.expected...)).Now()
})
}
})
t.Run("Custom comparators", func(t *testing.T) {
type testCase struct {
actuals, expected []any
comparatorFactory func(t T, tc *testCase) (Comparator, func())
outcomeVerifier TestOutcomeVerifier
}
testCases := map[string]testCase{
"Custom comparator success propagates": {
actuals: []any{1},
expected: []any{1},
comparatorFactory: func(t T, tc *testCase) (Comparator, func()) {
comparatorCalled := false
comparatorFunc := func(t T, expected any, actual any) {
comparatorCalled = true
if expected != actual {
t.Fatalf("Expected & actual value differ: expected %+v, got %+v", expected, actual)
}
if expected != tc.expected[0] {
t.Fatalf("Incorrect 'expected' value provided - should be %+v, got %+v", tc.expected[0], expected)
}
if actual != tc.actuals[0] {
t.Fatalf("Incorrect 'actual' value provided - should be %+v, got %+v", tc.actuals[0], actual)
}
}
verifierFunc := func() {
if !comparatorCalled {
t.Fatalf("Comparator was not called")
}
}
return comparatorFunc, verifierFunc
},
outcomeVerifier: SuccessVerifier(),
},
"Custom comparator invoked once per pair": {
actuals: []any{1, 2, 3},
expected: []any{1, 2, 3},
comparatorFactory: func(t T, tc *testCase) (Comparator, func()) {
type invocation struct {
expected, actual any
}
var invocations []invocation
comparatorFunc := func(t T, expected any, actual any) {
invocations = append(invocations, invocation{expected, actual})
}
verifierFunc := func() {
if len(invocations) != len(tc.expected) {
t.Fatalf("Expected %d invocations, got %d", len(tc.expected), len(invocations))
}
for i, inv := range invocations {
if inv.expected != tc.expected[i] {
t.Fatalf("Incorrect 'expected' value provided - should be %+v, got %+v", tc.expected[i], inv.expected)
}
if inv.actual != tc.actuals[i] {
t.Fatalf("Incorrect 'actual' value provided - should be %+v, got %+v", tc.actuals[i], inv.actual)
}
}
}
return comparatorFunc, verifierFunc
},
outcomeVerifier: SuccessVerifier(),
},
"Custom comparator failure propagates": {
actuals: []any{1},
expected: []any{2},
comparatorFactory: func(t T, tc *testCase) (Comparator, func()) {
comparatorCalled := false
comparatorFunc := func(t T, expected any, actual any) {
comparatorCalled = true
if expected != actual {
t.Fatalf("Expected & actual value differ: expected %+v, got %+v", expected, actual)
}
if expected != tc.expected[0] {
t.Fatalf("Incorrect 'expected' value provided - should be %+v, got %+v", tc.expected[0], expected)
}
if actual != tc.actuals[0] {
t.Fatalf("Incorrect 'actual' value provided - should be %+v, got %+v", tc.actuals[0], actual)
}
}
verifierFunc := func() {
if !comparatorCalled {
t.Fatalf("Comparator was not called")
}
}
return comparatorFunc, verifierFunc
},
outcomeVerifier: FailureVerifier(`Expected & actual value differ: expected \d+, got \d+.*`),
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
defer mt.Verify(tc.outcomeVerifier)
comparatorFunc, verifierFunc := tc.comparatorFactory(mt, &tc)
With(mt).VerifyThat(tc.actuals...).Will(EqualTo(tc.expected...).Using(comparatorFunc)).Now()
if verifierFunc != nil {
verifierFunc()
}
})
}
})
}