-
Notifications
You must be signed in to change notification settings - Fork 0
/
customArguments_test.go
362 lines (292 loc) · 9.09 KB
/
customArguments_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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package mocka
import (
"errors"
"reflect"
"github.com/Bayer-Group/mocka/v2/match"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("CustomArguments", func() {
var (
fn func(string, int) (int, error)
stub *Stub
failTestReporter *mockTestReporter
)
BeforeEach(func() {
fn = func(str string, num int) (int, error) {
return len(str) + num, nil
}
stub = &Stub{
testReporter: GinkgoT(),
originalFunc: nil,
functionPtr: &fn,
outParameters: []interface{}{42, nil},
execFunc: func([]interface{}) {},
}
failTestReporter = &mockTestReporter{}
})
Describe("newCustomArguments", func() {
It("reports an error when provided arguments != stubbed function arguments length", func() {
stub.testReporter = failTestReporter
_ = newCustomArguments(stub, []interface{}{})
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected arguments of type (string, int), but received ()",
}))
})
It("reports an error if the provided matcher is not supported for the argument kind", func() {
stub.testReporter = failTestReporter
anything := match.Anything()
lengthOf10 := match.LengthOf(10)
_ = newCustomArguments(stub, []interface{}{
anything,
lengthOf10,
})
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected arguments of type (string, int), but received (*anything, *lengthOf)",
}))
})
It("reports an error if the provided argument is not of the correct type", func() {
stub.testReporter = failTestReporter
_ = newCustomArguments(stub, []interface{}{"hi", "ope"})
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected arguments of type (string, int), but received (string, string)",
}))
})
It("returns a valid custom arguments structs", func() {
ca := newCustomArguments(stub, []interface{}{"hi", match.IntGreaterThan(10)})
Expect(ca).ToNot(BeNil())
Expect(*ca).To(Equal(CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{match.Exactly("hi"), match.IntGreaterThan(10)},
}))
})
It("reports an error if the provided nil does not match the correct type", func() {
fn := func(msg string) error {
return errors.New(msg)
}
stub = &Stub{
testReporter: failTestReporter,
originalFunc: nil,
functionPtr: &fn,
outParameters: []interface{}{42, nil},
execFunc: func([]interface{}) {},
}
_ = newCustomArguments(stub, []interface{}{nil})
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected arguments of type (string), but received (<nil>)",
}))
})
It("returns a nil matcher when called with nil", func() {
fn := func(err error) error {
return err
}
stub = &Stub{
testReporter: GinkgoT(),
originalFunc: nil,
functionPtr: &fn,
outParameters: []interface{}{42, nil},
execFunc: func([]interface{}) {},
}
ca := newCustomArguments(stub, []interface{}{nil})
Expect(ca).ToNot(BeNil())
Expect(*ca).To(Equal(CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{match.Nil()},
}))
})
Context("variadic function", func() {
BeforeEach(func() {
var variadicFn func(string, ...interface{}) (int, error)
stub = &Stub{
testReporter: GinkgoT(),
originalFunc: nil,
functionPtr: &variadicFn,
outParameters: []interface{}{42, nil},
execFunc: func([]interface{}) {},
}
})
It("returns a CustomArguments struct with a nil matcher for omitted variadic arguments", func() {
ca := newCustomArguments(stub, []interface{}{"hi"})
Expect(ca).ToNot(BeNil())
Expect(*ca).To(Equal(CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{match.Exactly("hi"), match.Nil()},
}))
})
It("returns a CustomArguments struct with a sliceOf matcher for variadic arguments", func() {
ca := newCustomArguments(stub, []interface{}{"hi", nil, "A", match.Anything()})
Expect(ca).ToNot(BeNil())
Expect(*ca).To(Equal(CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{
match.Exactly("hi"),
match.SliceOf(match.Nil(), match.Exactly("A"), match.Anything())},
}))
})
It("reports an error if matcher does not suppor the variadic type", func() {
stub.testReporter = failTestReporter
_ = newCustomArguments(stub, []interface{}{"hi", match.ElementsContaining("A")})
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected arguments of type (string, ...), but received (string, *elementsContaining)",
}))
})
})
})
Describe("Return", func() {
It("returns an error if one or more of the return values are not valid", func() {
stub.testReporter = failTestReporter
ca := &CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{
match.Exactly(""), match.Exactly(42),
},
}
ca.Return("", 42)
Expect(failTestReporter.messages).To(Equal([]string{
"mocka: expected return values of type (int, error), but received (string, int)",
}))
})
It("assigns the OutParameters and returns nil if everything is valid", func() {
ca := &CustomArguments{
stub: stub,
argMatchers: []match.SupportedKindsMatcher{
match.Exactly(""), match.Exactly(42),
},
}
ca.Return(42, nil)
Expect(ca.out).To(Equal([]interface{}{42, nil}))
})
})
Describe("OnCall", func() {
var ca *CustomArguments
BeforeEach(func() {
ca = &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 1},
{stub: stub, index: 2},
{stub: stub, index: 0},
},
}
})
It("returns a pointer to an onCall struct", func() {
result := ca.OnCall(5)
Expect(*result).To(Equal(OnCall{stub: stub, index: 5}))
})
It("appends the new onCall struct to the onCalls slice", func() {
_ = ca.OnCall(5)
Expect(ca.onCalls).To(HaveLen(4))
})
It("returns an existing onCall object if one exists for that index", func() {
result := ca.OnCall(2)
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 2}))
})
})
Describe("OnFirstCall", func() {
It("creates a new onCall with a 0 index", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 1},
{stub: stub, index: 2},
},
}
result := ca.OnFirstCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 0}))
})
It("returns the existing first call", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 1},
{stub: stub, index: 2},
{stub: stub, index: 0},
},
}
result := ca.OnFirstCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 0}))
})
})
Describe("OnSecondCall", func() {
It("creates a new onCall with a 1 index", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 0},
{stub: stub, index: 2},
},
}
result := ca.OnSecondCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 1}))
})
It("returns the existing second call", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 1},
{stub: stub, index: 2},
{stub: stub, index: 0},
},
}
result := ca.OnSecondCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 1}))
})
})
Describe("OnThirdCall", func() {
It("creates a new onCall with a 2 index", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 0},
{stub: stub, index: 1},
},
}
result := ca.OnThirdCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 2}))
})
It("returns the existing third call", func() {
ca := &CustomArguments{
stub: stub,
onCalls: []*OnCall{
{stub: stub, index: 1},
{stub: stub, index: 2},
{stub: stub, index: 0},
},
}
result := ca.OnThirdCall()
Expect(ca.onCalls).To(HaveLen(3))
Expect(*result).To(Equal(OnCall{stub: stub, index: 2}))
})
})
Describe("isMatch", func() {
It("returns false if any matcher panics", func() {
ca := newCustomArguments(stub, []interface{}{&panicMatcher{}, match.IntGreaterThan(10)})
Expect(ca.isMatch([]interface{}{"hi", 11})).To(BeFalse())
})
It("returns false if any matcher returns false", func() {
ca := newCustomArguments(stub, []interface{}{"hi", match.IntGreaterThan(10)})
Expect(ca.isMatch([]interface{}{"hi", 5})).To(BeFalse())
})
It("returns true if all matchers return true", func() {
ca := newCustomArguments(stub, []interface{}{"hi", match.IntGreaterThan(10)})
Expect(ca.isMatch([]interface{}{"hi", 15})).To(BeTrue())
})
})
})
type panicMatcher struct {
}
func (*panicMatcher) SupportedKinds() map[reflect.Kind]struct{} {
return map[reflect.Kind]struct{}{
reflect.Int: {},
reflect.String: {},
}
}
func (*panicMatcher) Match(_ interface{}) bool {
panic("i am the panic matcher")
}