-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathassert_test.go
More file actions
executable file
·227 lines (204 loc) · 5.35 KB
/
assert_test.go
File metadata and controls
executable file
·227 lines (204 loc) · 5.35 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
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 STACKIT GmbH & Co. KG
package testutils
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/google/go-cmp/cmp/cmpopts"
)
type customError struct{ msg string }
func (e *customError) Error() string { return e.msg }
type anotherError struct{ code int }
func (e *anotherError) Error() string { return fmt.Sprintf("code=%d", e.code) }
type mockTB struct {
testing.TB
failed bool
msg string
}
func (m *mockTB) Helper() {}
func (m *mockTB) Errorf(format string, args ...any) {
m.failed = true
m.msg = fmt.Sprintf(format, args...)
}
func TestAssertError(t *testing.T) {
t.Parallel()
sentinel := errors.New("sentinel")
tests := map[string]struct {
got error // The input provided as got to AssertError()
want any // The input provided as want to AssertError()
wantErr bool // Whether this comparison is expected to fail
}{
"exact match": {
got: &customError{msg: "boom"},
want: &customError{},
wantErr: false,
},
"error string message match": {
got: errors.New("same message"),
want: "same message",
wantErr: false,
},
"error string mismatch": {
got: errors.New("different"),
want: "same message",
wantErr: true,
},
"sentinel via errors.Is": {
got: fmt.Errorf("wrap: %w", sentinel),
want: sentinel,
wantErr: false,
},
"any error (true)": {
got: errors.New("any"),
want: true,
wantErr: false,
},
"nil expectation (nil)": {
got: nil,
want: nil,
wantErr: false,
},
"nil expectation (false)": {
got: nil,
want: false,
wantErr: false,
},
"nil error input with error expectation": {
got: nil,
want: true,
wantErr: true,
},
"unexpected error (nil want)": {
got: errors.New("unexpected"),
want: nil,
wantErr: true,
},
"type match without message": {
got: &customError{msg: "alpha"},
want: &customError{msg: "beta"},
wantErr: false,
},
"type mismatch": {
got: &customError{msg: "alpha"},
want: &anotherError{},
wantErr: true,
},
"no error when none expected": {
got: nil,
want: false,
wantErr: false,
},
"error but want false": {
got: errors.New("boom"),
want: false,
wantErr: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
mock := &mockTB{}
result := AssertError(mock, tt.got, tt.want)
// if the test failed but we didn't expect it to fail
if mock.failed != tt.wantErr {
t.Fatalf("AssertError() failed = %v, wantErr %v (msg: %s)", mock.failed, tt.wantErr, mock.msg)
}
// if we expected an error the result of AssertError() should be false (this is what AssertError() does in case of error)
if tt.wantErr && result != false {
t.Fatalf("AssertError() returned = %v, want %v", result, tt.wantErr)
}
})
}
}
func TestCheckErrorMatch(t *testing.T) {
t.Parallel()
underlying := &customError{msg: "root"}
wrapped := fmt.Errorf("wrap: %w", underlying)
if !checkErrorMatch(wrapped, &customError{}) {
t.Fatalf("expected wrapped customError to match via errors.As")
}
notMatch := errors.New("other")
if checkErrorMatch(notMatch, &anotherError{}) {
t.Fatalf("expected mismatch for unrelated error types")
}
}
func TestAssertValue(t *testing.T) {
t.Parallel()
type payload struct {
Visible string
hidden int
}
customDiff := func(got, want any) string {
if reflect.DeepEqual(got, want) {
return ""
}
return "custom diff"
}
tests := []struct {
name string
got any // The input provided as got to AssertValue()
want any // The input provided as want to AssertValue()
wantErr bool // Whether this comparison is expected to fail
opts []ValueComparisonOption
}{
{
name: "allow unexported success",
got: payload{Visible: "ok", hidden: 1},
want: payload{Visible: "ok", hidden: 1},
opts: []ValueComparisonOption{WithAllowUnexported(payload{})},
},
{
name: "allow unexported mismatch",
got: payload{Visible: "oops", hidden: 1},
want: payload{Visible: "ok", hidden: 1},
opts: []ValueComparisonOption{WithAllowUnexported(payload{})},
wantErr: true,
},
{
name: "cmp options sort",
got: []string{"b", "a", "c"},
want: []string{"a", "b", "c"},
opts: []ValueComparisonOption{WithAssertionCmpOptions(cmpopts.SortSlices(func(a, b string) bool { return a < b }))},
},
{
name: "custom diff mismatch",
got: 1,
want: 2,
opts: []ValueComparisonOption{WithDiffFunc(customDiff)},
wantErr: true,
},
{
name: "default diff success",
got: 42,
want: 42,
},
{
name: "default diff mismatch",
got: 1,
want: 2,
wantErr: true,
},
{
name: "diff func overrides cmp options",
got: []string{"b"},
want: []string{"a"},
opts: []ValueComparisonOption{
WithAssertionCmpOptions(cmpopts.SortSlices(func(a, b string) bool { return a < b })),
WithDiffFunc(func(_, _ any) string { return "" }),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mock := &mockTB{}
AssertValue(mock, tt.got, tt.want, tt.opts...)
// if the test failed but we didn't expect it to fail
if mock.failed != tt.wantErr {
t.Fatalf("AssertValue failed = %v, want %v (msg: %s)", mock.failed, tt.wantErr, mock.msg)
}
})
}
}