-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreporter.go
327 lines (287 loc) · 10.2 KB
/
reporter.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
package test
import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/tkrop/go-testing/internal/reflect"
"github.com/tkrop/go-testing/mock"
)
// Reporter is a minimal interface for abstracting test report methods that are
// needed to setup an isolated test environment for GoMock and Testify.
type Reporter interface {
// Error reports a failure messages when a test is supposed to continue.
Error(args ...any)
// Errorf reports a failure messages when a test is supposed to continue.
Errorf(format string, args ...any)
// Fatal reports a fatal failure message that immediate aborts of the test
// execution.
Fatal(args ...any)
// Fatalf reports a fatal failure message that immediate aborts of the test
// execution.
Fatalf(format string, args ...any)
// Fail reports a failure message that immediate aborts of the test
// execution.
Fail()
// FailNow reports fatal failure notifications without log output that
// aborts test execution immediately.
FailNow()
// Panic reports a panic.
Panic(arg any)
}
// Validator a test failure validator based on the test reporter interface.
type Validator struct {
ctrl *gomock.Controller
recorder *Recorder
}
// Recorder a test failure validator recorder.
type Recorder struct {
validator *Validator
}
// NewValidator creates a new test validator for validating error messages and
// panics created during test execution.
func NewValidator(ctrl *gomock.Controller) *Validator {
validator := &Validator{ctrl: ctrl}
validator.recorder = &Recorder{validator: validator}
if t, ok := ctrl.T.(*Context); ok {
// We need to install a second isolated test environment to break the
// reporter cycle on the failure issued by the mock controller.
ctrl.T = New(t.t, t.expect)
t.expect = Failure
t.Reporter(validator)
}
return validator
}
// EXPECT implements the usual `gomock.EXPECT` call to request the recorder.
func (v *Validator) EXPECT() *Recorder {
return v.recorder
}
// Error receive expected method call to `Error`.
func (v *Validator) Error(args ...any) {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Error", args...)
}
// Error indicate an expected method call to `Error`.
func (r *Recorder) Error(args ...any) *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Error",
reflect.TypeOf((*Validator)(nil).Error), args...)
}
// Error creates a validation method call setup for `Error`.
func Error(args ...any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Error(args...).Do(mocks.Do(Reporter.Error))
}
}
// Errorf receive expected method call to `Errorf`.
func (v *Validator) Errorf(format string, args ...any) {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Errorf", append([]any{format}, args...)...)
}
// Errorf indicate an expected method call to `Errorf`.
func (r *Recorder) Errorf(format string, args ...any) *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Errorf",
reflect.TypeOf((*Validator)(nil).Errorf),
append([]any{format}, args...)...)
}
// Errorf creates a validation method call setup for `Errorf`.
func Errorf(format string, args ...any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Errorf(format, args...).Do(mocks.Do(Reporter.Errorf))
}
}
// Fatal receive expected method call to `Fatal`.
func (v *Validator) Fatal(args ...any) {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Fatal", args...)
}
// Fatal indicate an expected method call to `Fatal`.
func (r *Recorder) Fatal(args ...any) *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Fatal",
reflect.TypeOf((*Validator)(nil).Fatal), args...)
}
// Fatal creates a validation method call setup for `Fatal`.
func Fatal(args ...any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Fatal(args...).Do(mocks.Do(Reporter.Fatal))
}
}
// Fatalf receive expected method call to `Fatalf`.
func (v *Validator) Fatalf(format string, args ...any) {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Fatalf", append([]any{format}, args...)...)
}
// Fatalf indicate an expected method call to `Fatalf`.
func (r *Recorder) Fatalf(format string, args ...any) *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Fatalf",
reflect.TypeOf((*Validator)(nil).Fatalf),
append([]any{format}, args...)...)
}
// Fatalf creates a validation method call setup for `Fatalf`.
func Fatalf(format string, args ...any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Fatalf(format, args...).Do(mocks.Do(Reporter.Fatalf))
}
}
// Fail receive expected method call to `Fail`.
func (v *Validator) Fail() {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Fail")
}
// Fail indicate an expected method call to `Fail`.
func (r *Recorder) Fail() *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Fail",
reflect.TypeOf((*Validator)(nil).Fail))
}
// Fail creates a validation method call setup for `Fail`.
func Fail() mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Fail().Do(mocks.Do(Reporter.Fail))
}
}
// FailNow receive expected method call to `FailNow`.
func (v *Validator) FailNow() {
v.ctrl.T.Helper()
v.ctrl.Call(v, "FailNow")
}
// FailNow indicate an expected method call to `FailNow`.
func (r *Recorder) FailNow() *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "FailNow",
reflect.TypeOf((*Validator)(nil).FailNow))
}
// FailNow creates a validation method call setup for `FailNow`.
func FailNow() mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
FailNow().Do(mocks.Do(Reporter.FailNow))
}
}
// Panic receive expected method call indicating a panic.
func (v *Validator) Panic(arg any) {
v.ctrl.T.Helper()
v.ctrl.Call(v, "Panic", []any{arg}...)
}
// Panic indicate an expected method call from panic.
func (r *Recorder) Panic(arg any) *gomock.Call {
r.validator.ctrl.T.Helper()
return r.validator.ctrl.RecordCallWithMethodType(r.validator, "Panic",
reflect.TypeOf((*Validator)(nil).Panic), []any{arg}...)
}
// Panic creates a validation method call setup for a panic. It allows to match
// the panic object, which usually is an error and alternatively the error
// string representing the error, since runtime errors may be irreproducible.
func Panic(arg any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Panic(EqError(arg)).Do(mocks.Do(Reporter.Panic))
}
}
// UnexpectedCall creates expectation for unexpected calls. We only support one
// unexpected call since the test execution stops in this case.
func UnexpectedCall[T any](
creator func(*gomock.Controller) *T,
method, caller string, args ...any,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(_ Test, mocks *mock.Mocks) mock.SetupFunc {
return Fatalf("Unexpected call to %T.%v(%v) at %s because: %s",
mock.Get(mocks, creator), method, args, caller,
//nolint:goerr113 // necessary
fmt.Errorf("there are no expected calls "+
"of the method \"%s\" for that receiver", method))
}
}
func ConsumedCall[T any](
creator func(*gomock.Controller) *T,
method, caller, ecaller string, args ...any,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(_ Test, mocks *mock.Mocks) mock.SetupFunc {
return Fatalf("Unexpected call to %T.%v(%v) at %s because: %s",
mock.Get(mocks, creator), method, args, caller,
fmt.Errorf("\nexpected call at %s has "+ //nolint:goerr113 // necessary
"already been called the max number of times", ecaller))
}
}
// MissingCalls creates an expectation for all missing calls.
func MissingCalls(
setups ...mock.SetupFunc,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(t Test, _ *mock.Mocks) mock.SetupFunc {
// Creates a new mock controller and test environment to isolate the
// validator used for sub-call creation/registration from the validator
// used for execution.
mocks := mock.NewMocks(New(t, false))
calls := make([]func(*mock.Mocks) any, 0, len(setups))
for _, setup := range setups {
calls = append(calls,
Errorf("missing call(s) to %v", EqCall(setup(mocks))))
}
calls = append(calls, Errorf("aborting test due to missing call(s)"))
return mock.Chain(calls...)
}
}
// errorMatcher is a matcher to improve capabilities of matching errors.
type errorMatcher struct {
x any
}
// EqError creates a new error matcher that allows to match either the error or
// alternatively the string describing the error.
func EqError(x any) gomock.Matcher {
return &errorMatcher{x: x}
}
// Matches executes the extended error matching.
func (m *errorMatcher) Matches(x any) bool {
switch a := m.x.(type) {
case string:
switch b := x.(type) {
case string:
return a == b
case error:
return a == b.Error()
}
case error:
switch b := x.(type) {
case string:
return a.Error() == b
case error:
return gomock.Eq(a).Matches(b)
}
}
return gomock.Eq(m.x).Matches(x)
}
// String creates a string of the expectation to match.
func (m *errorMatcher) String() string {
return fmt.Sprintf("is equal to %v (%T)", m.x, m.x)
}
// callMatcher is a matcher that supports matching of calls. Calls contain
// actions consisting of functions that cannot be matched successfully using
// [reflect.DeepEquals].
type callMatcher struct {
x any
}
// EqCall creates a new call matcher that allows to match calls by translating
// them to the string containing the core information instead of using the
// standard matcher using [reflect.DeepEquals] that fails for the contained
// actions.
func EqCall(x any) gomock.Matcher {
return &callMatcher{x: x}
}
// Matches executes the extended call matching algorithms.
func (m *callMatcher) Matches(x any) bool {
a, aok := m.x.(*gomock.Call)
if b, bok := x.(*gomock.Call); aok && bok {
return a.String() == b.String()
}
return gomock.Eq(m.x).Matches(x)
}
// String creates a string of the expectation to match.
func (m *callMatcher) String() string {
return fmt.Sprintf("is equal to %v (%T)", m.x, m.x)
}