-
Notifications
You must be signed in to change notification settings - Fork 1
/
behaviour_test.go
396 lines (372 loc) · 12.1 KB
/
behaviour_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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package funcmock
import (
"errors"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
)
var mockTestFuncArgs []string
var mockTestFunc = func(msg string) {
mockTestFuncArgs = append(mockTestFuncArgs, msg)
}
var recordingValuesTestFunc = func(a int, b string) (int, string) {
Fail("This function should not be called")
return 0, ""
}
var interfaceReturnTestFunc = func() error {
Fail("This function should not be called")
return nil
}
var stressTestFunc = func(a int) {
Fail("This function should not be called")
}
var setBehaviourTestFunc = func(a, b int) int {
Fail("This function should not be called")
return 0
}
type preRecordTestStruct struct {
field int
}
var preRecordTestFunc = func(ts []preRecordTestStruct) *preRecordTestStruct {
Fail("This function should not be called")
return nil
}
var _ = Describe("Better Mock Test", func() {
Context("mocking/restoring", func() {
var mock *MockController
BeforeEach(func() {
mockTestFuncArgs = make([]string, 0)
})
It("should mock and restore the function correctly", func() {
mockTestFunc("call1")
mock = Mock(&mockTestFunc)
mockTestFunc("call2")
mock.Restore()
mockTestFunc("call3")
Expect(mockTestFuncArgs).To(Equal([]string{"call1", "call3"}))
})
It("should panic if Mock is called with non-pointer argument", func() {
var err interface{}
func() {
defer func() {
err = recover()
}()
mock = Mock(mockTestFunc)
}()
Expect(err).To(Equal("invalid argument to Mock! must be pointer to function"))
})
It("should panic if Mock is called with non-function type", func() {
var err interface{}
func() {
defer func() {
err = recover()
}()
mock = Mock(&struct{ field int }{})
}()
Expect(err).To(Equal("invalid argument to Mock! must be pointer to function"))
})
})
Context("recording values", func() {
var mock *MockController
BeforeEach(func() {
mock = Mock(&recordingValuesTestFunc)
})
AfterEach(func() {
mock.Restore()
})
It("should correctly record the number of calls", func() {
for i := 0; i < 5; i++ {
recordingValuesTestFunc(0, "")
}
Expect(mock.CallCount()).To(Equal(5))
for i := 0; i < 5; i++ {
Expect(mock.NthCall(0).Called()).To(BeTrue())
}
Expect(mock.NthCall(5).Called()).To(BeFalse())
})
It("should record and retrieve the call parameters", func() {
for i := 0; i < 25; i++ {
recordingValuesTestFunc(i%5, fmt.Sprintf("%d", i/5))
}
for i := 0; i < 25; i++ {
Expect(mock.NthCall(i).NthParam(0)).To(Equal(i % 5))
Expect(mock.NthCall(i).NthParam(1)).To(Equal(fmt.Sprintf("%d", i/5)))
}
})
It("should be able to retrieve all call parameters with NthParams", func() {
for i := 0; i < 5; i++ {
recordingValuesTestFunc(i*2, fmt.Sprintf("%d", i))
}
params1, ok := mock.NthParams(0).([]int)
Expect(ok).To(BeTrue())
params2, ok := mock.NthParams(1).([]string)
Expect(ok).To(BeTrue())
Expect(params1).To(Equal([]int{0, 2, 4, 6, 8}))
Expect(params2).To(Equal([]string{"0", "1", "2", "3", "4"}))
})
It("should have zero default return values", func() {
a, b := recordingValuesTestFunc(0, "")
Expect(a).To(Equal(0))
Expect(b).To(Equal(""))
})
It("should support setting default return values", func() {
mock.SetDefaultReturn(2, "test")
a, b := recordingValuesTestFunc(0, "")
Expect(a).To(Equal(2))
Expect(b).To(Equal("test"))
})
It("should panic if SetDefaultReturn is called with invalid type", func() {
var err error
func() {
defer func() {
err = errors.New(recover().(string))
}()
mock.SetDefaultReturn("test", 2)
}()
Expect(err).To(Equal(errors.New("MockController.sanitizeReturns: 0:th return value, reflect.Value.Convert: value of type string cannot be converted to type int")))
})
Context("per call return values", func() {
BeforeEach(func() {
for i := 0; i < 5; i++ {
mock.NthCall(i).SetReturn(i, fmt.Sprintf("%d", i*2))
}
})
It("should support setting return values for specific calls", func() {
for i := 0; i < 5; i++ {
a, b := recordingValuesTestFunc(0, "")
Expect(a).To(Equal(i))
Expect(b).To(Equal(fmt.Sprintf("%d", i*2)))
}
a, b := recordingValuesTestFunc(0, "")
Expect(a).To(Equal(0))
Expect(b).To(Equal(""))
})
It("should panic if SetReturn is called with invalid types", func() {
var err error
func() {
defer func() {
err = errors.New(recover().(string))
}()
mock.NthCall(0).SetReturn("test", 2)
}()
Expect(err).To(Equal(errors.New("MockController.sanitizeReturns: 0:th return value, reflect.Value.Convert: value of type string cannot be converted to type int")))
})
It("should record the returned values", func() {
for i := 0; i < 6; i++ {
recordingValuesTestFunc(0, "")
}
for i := 0; i < 5; i++ {
Expect(mock.NthCall(i).NthReturn(0)).To(Equal(i))
Expect(mock.NthCall(i).NthReturn(1)).To(Equal(fmt.Sprintf("%d", i*2)))
}
Expect(mock.NthCall(5).NthReturn(0)).To(Equal(0))
Expect(mock.NthCall(5).NthReturn(1)).To(Equal(""))
})
It("should be able to retrieve all returned values with NthReturns", func() {
for i := 0; i < 6; i++ {
recordingValuesTestFunc(0, "")
}
returns1, ok := mock.NthReturns(0).([]int)
Expect(ok).To(BeTrue())
returns2, ok := mock.NthReturns(1).([]string)
Expect(ok).To(BeTrue())
Expect(returns1).To(Equal([]int{0, 1, 2, 3, 4, 0}))
Expect(returns2).To(Equal([]string{"0", "2", "4", "6", "8", ""}))
})
})
})
Context("interface returns", func() {
var mock *MockController
BeforeEach(func() {
mock = Mock(&interfaceReturnTestFunc)
})
AfterEach(func() {
mock.Restore()
})
It("should return nil as a default value", func() {
ret := interfaceReturnTestFunc()
Expect(ret).To(BeNil())
})
It("should allow setting nil default return for functions returning interfaces", func() {
mock.SetDefaultReturn(nil)
ret := interfaceReturnTestFunc()
Expect(ret).To(BeNil())
})
It("should allow setting return values which satisfy the interface", func() {
mock.SetDefaultReturn(errors.New("test error"))
ret := interfaceReturnTestFunc()
Expect(ret).To(Equal(errors.New("test error")))
})
})
Context("stress test", func() {
var mock *MockController
BeforeEach(func() {
mock = Mock(&stressTestFunc)
})
AfterEach(func() {
mock.Restore()
})
It("should support calls from multiple goroutines without race conditions or deadlocks", func() {
goroutines := 20
callsPerGoroutine := 1000
totalCalls := goroutines * callsPerGoroutine
callParams := make([]int, totalCalls)
for i := 0; i < totalCalls; i++ {
callParams[i] = i
}
for i := 0; i < goroutines; i++ {
go func(slice []int) {
for j := 0; j < callsPerGoroutine; j++ {
stressTestFunc(slice[j])
}
}(callParams[i*callsPerGoroutine : (i+1)*callsPerGoroutine])
}
Eventually(func() int {
return mock.CallCount()
}, 1.0, 0.1).Should(Equal(totalCalls))
// this is done in this way instead of using gomega.ConsistOf since it's too slow
recordedParams := mock.NthParams(0).([]int)
for _, val := range recordedParams {
if val < 0 || val >= totalCalls || callParams[val] != val {
Fail("Recorded params are invalid")
} else {
callParams[val] = -1
}
}
})
})
Context("overriding behaviour", func() {
var mock *MockController
Context("SetBehaviour", func() {
BeforeEach(func() {
mock = Mock(&setBehaviourTestFunc)
})
AfterEach(func() {
mock.Restore()
})
It("should fail if SetBehaviour is called with invalid function type", func() {
var err error
func() {
defer func() {
err = errors.New(recover().(string))
}()
mock.SetBehaviour(func(x int) int { return x })
}()
Expect(err).To(Equal(errors.New("MockController.WithBehaviour: behaviour function has invalid type 'func(int) int', requires 'func(int, int) int'")))
})
It("should allow overriding mock function behaviour", func() {
type pair struct{ a, b int }
args := make([]pair, 0)
mock.SetBehaviour(func(a, b int) int {
args = append(args, pair{a, b})
return a * b
})
Expect(setBehaviourTestFunc(1, 2)).To(Equal(2))
Expect(setBehaviourTestFunc(3, 4)).To(Equal(12))
Expect(setBehaviourTestFunc(5, 6)).To(Equal(30))
Expect(args).To(Equal([]pair{{1, 2}, {3, 4}, {5, 6}}))
})
It("should store call info with overridden behaviour", func() {
mock.SetBehaviour(func(a, b int) int {
return a * b
})
Expect(setBehaviourTestFunc(1, 2)).To(Equal(2))
Expect(setBehaviourTestFunc(3, 4)).To(Equal(12))
Expect(setBehaviourTestFunc(5, 6)).To(Equal(30))
Expect(mock.CallCount()).To(Equal(3))
Expect(mock.NthCall(0).NthParam(0)).To(Equal(1))
Expect(mock.NthCall(0).NthParam(1)).To(Equal(2))
Expect(mock.NthCall(1).NthParam(0)).To(Equal(3))
Expect(mock.NthCall(1).NthParam(1)).To(Equal(4))
Expect(mock.NthCall(2).NthParam(0)).To(Equal(5))
Expect(mock.NthCall(2).NthParam(1)).To(Equal(6))
Expect(mock.NthCall(0).NthReturn(0)).To(Equal(2))
Expect(mock.NthCall(1).NthReturn(0)).To(Equal(12))
Expect(mock.NthCall(2).NthReturn(0)).To(Equal(30))
})
})
Context("SetPreRecord and SetPreReturn", func() {
BeforeEach(func() {
mock = Mock(&preRecordTestFunc)
})
AfterEach(func() {
mock.Restore()
})
It("should fail if SetPreRecord is called with invalid function type", func() {
var err error
func() {
defer func() {
err = errors.New(recover().(string))
}()
mock.SetPreRecord(func(ts []preRecordTestStruct) *preRecordTestStruct { return nil })
}()
Expect(err).To(Equal(errors.New(
"MockController.SetPreRecord: " +
"provided function has invalid type 'func([]funcmock.preRecordTestStruct) *funcmock.preRecordTestStruct', " +
"requires 'func([]funcmock.preRecordTestStruct) []funcmock.preRecordTestStruct'",
)))
})
It("should allow changing the recording behaviour", func() {
mock.SetPreRecord(func(ts []preRecordTestStruct) []preRecordTestStruct {
ret := make([]preRecordTestStruct, len(ts))
copy(ret, ts)
return ret
})
rets := []preRecordTestStruct{{field: 1}, {field: 3}}
preRecordTestFunc(rets)
rets[0].field = 2
rets[1].field = 4
preRecordTestFunc(rets)
Expect(mock.NthCall(1).NthParam(0)).To(Equal([]preRecordTestStruct{{field: 2}, {field: 4}}))
Expect(mock.NthCall(0).NthParam(0)).To(Equal([]preRecordTestStruct{{field: 1}, {field: 3}}))
})
It("should fail if SetPreReturn is called with invalid function type", func() {
var err error
func() {
defer func() {
err = errors.New(recover().(string))
}()
mock.SetPreReturn(func(ts []preRecordTestStruct) *preRecordTestStruct { return nil })
}()
Expect(err).To(Equal(errors.New(
"MockController.SetPreReturn: " +
"provided function has invalid type 'func([]funcmock.preRecordTestStruct) *funcmock.preRecordTestStruct', " +
"requires 'func(*funcmock.preRecordTestStruct) *funcmock.preRecordTestStruct'",
)))
})
It("should return the values returned by the function passed to SetPreReturn from the mocked function", func() {
mock.SetPreReturn(func(ts *preRecordTestStruct) *preRecordTestStruct {
temp := *ts
return &temp
})
mock.SetDefaultReturn(&preRecordTestStruct{field: 1})
var rets [3]*preRecordTestStruct
for i := 0; i < 3; i++ {
st := preRecordTestFunc(nil)
st.field++
rets[i] = st
}
Expect(rets[:]).To(Equal([]*preRecordTestStruct{
&preRecordTestStruct{field: 2},
&preRecordTestStruct{field: 2},
&preRecordTestStruct{field: 2},
}))
})
It("should record the values returned by the function passed to SetPreReturn", func() {
mock.SetPreReturn(func(ts *preRecordTestStruct) *preRecordTestStruct {
temp := *ts
return &temp
})
mock.SetDefaultReturn(&preRecordTestStruct{field: 1})
for i := 0; i < 3; i++ {
st := preRecordTestFunc(nil)
st.field++
}
for i := 0; i < 3; i++ {
Expect(mock.NthCall(i).NthReturn(0)).To(PointTo(Equal(preRecordTestStruct{field: 2})))
}
})
})
})
})