/
actor_mock.go
358 lines (290 loc) · 11.5 KB
/
actor_mock.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
package tests
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
//go:generate minimock -i github.com/gojuno/minimock/v3/tests.actor -o actor_mock.go -n ActorMock -p tests
import (
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
)
// ActorMock implements actor
type ActorMock struct {
t minimock.Tester
finishOnce sync.Once
funcAction func(firstParam string, secondParam int) (i1 int, err error)
inspectFuncAction func(firstParam string, secondParam int)
afterActionCounter uint64
beforeActionCounter uint64
ActionMock mActorMockAction
}
// NewActorMock returns a mock for actor
func NewActorMock(t minimock.Tester) *ActorMock {
m := &ActorMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.ActionMock = mActorMockAction{mock: m}
m.ActionMock.callArgs = []*ActorMockActionParams{}
t.Cleanup(m.MinimockFinish)
return m
}
type mActorMockAction struct {
mock *ActorMock
defaultExpectation *ActorMockActionExpectation
expectations []*ActorMockActionExpectation
callArgs []*ActorMockActionParams
mutex sync.RWMutex
}
// ActorMockActionExpectation specifies expectation struct of the actor.Action
type ActorMockActionExpectation struct {
mock *ActorMock
params *ActorMockActionParams
paramPtrs *ActorMockActionParamPtrs
results *ActorMockActionResults
Counter uint64
}
// ActorMockActionParams contains parameters of the actor.Action
type ActorMockActionParams struct {
firstParam string
secondParam int
}
// ActorMockActionParamPtrs contains pointers to parameters of the actor.Action
type ActorMockActionParamPtrs struct {
firstParam *string
secondParam *int
}
// ActorMockActionResults contains results of the actor.Action
type ActorMockActionResults struct {
i1 int
err error
}
// Expect sets up expected params for actor.Action
func (mmAction *mActorMockAction) Expect(firstParam string, secondParam int) *mActorMockAction {
if mmAction.mock.funcAction != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Set")
}
if mmAction.defaultExpectation == nil {
mmAction.defaultExpectation = &ActorMockActionExpectation{}
}
if mmAction.defaultExpectation.paramPtrs != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by ExpectParams functions")
}
mmAction.defaultExpectation.params = &ActorMockActionParams{firstParam, secondParam}
for _, e := range mmAction.expectations {
if minimock.Equal(e.params, mmAction.defaultExpectation.params) {
mmAction.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAction.defaultExpectation.params)
}
}
return mmAction
}
// ExpectFirstParamParam1 sets up expected param firstParam for actor.Action
func (mmAction *mActorMockAction) ExpectFirstParamParam1(firstParam string) *mActorMockAction {
if mmAction.mock.funcAction != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Set")
}
if mmAction.defaultExpectation == nil {
mmAction.defaultExpectation = &ActorMockActionExpectation{}
}
if mmAction.defaultExpectation.params != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Expect")
}
if mmAction.defaultExpectation.paramPtrs == nil {
mmAction.defaultExpectation.paramPtrs = &ActorMockActionParamPtrs{}
}
mmAction.defaultExpectation.paramPtrs.firstParam = &firstParam
return mmAction
}
// ExpectSecondParamParam2 sets up expected param secondParam for actor.Action
func (mmAction *mActorMockAction) ExpectSecondParamParam2(secondParam int) *mActorMockAction {
if mmAction.mock.funcAction != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Set")
}
if mmAction.defaultExpectation == nil {
mmAction.defaultExpectation = &ActorMockActionExpectation{}
}
if mmAction.defaultExpectation.params != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Expect")
}
if mmAction.defaultExpectation.paramPtrs == nil {
mmAction.defaultExpectation.paramPtrs = &ActorMockActionParamPtrs{}
}
mmAction.defaultExpectation.paramPtrs.secondParam = &secondParam
return mmAction
}
// Inspect accepts an inspector function that has same arguments as the actor.Action
func (mmAction *mActorMockAction) Inspect(f func(firstParam string, secondParam int)) *mActorMockAction {
if mmAction.mock.inspectFuncAction != nil {
mmAction.mock.t.Fatalf("Inspect function is already set for ActorMock.Action")
}
mmAction.mock.inspectFuncAction = f
return mmAction
}
// Return sets up results that will be returned by actor.Action
func (mmAction *mActorMockAction) Return(i1 int, err error) *ActorMock {
if mmAction.mock.funcAction != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Set")
}
if mmAction.defaultExpectation == nil {
mmAction.defaultExpectation = &ActorMockActionExpectation{mock: mmAction.mock}
}
mmAction.defaultExpectation.results = &ActorMockActionResults{i1, err}
return mmAction.mock
}
// Set uses given function f to mock the actor.Action method
func (mmAction *mActorMockAction) Set(f func(firstParam string, secondParam int) (i1 int, err error)) *ActorMock {
if mmAction.defaultExpectation != nil {
mmAction.mock.t.Fatalf("Default expectation is already set for the actor.Action method")
}
if len(mmAction.expectations) > 0 {
mmAction.mock.t.Fatalf("Some expectations are already set for the actor.Action method")
}
mmAction.mock.funcAction = f
return mmAction.mock
}
// When sets expectation for the actor.Action which will trigger the result defined by the following
// Then helper
func (mmAction *mActorMockAction) When(firstParam string, secondParam int) *ActorMockActionExpectation {
if mmAction.mock.funcAction != nil {
mmAction.mock.t.Fatalf("ActorMock.Action mock is already set by Set")
}
expectation := &ActorMockActionExpectation{
mock: mmAction.mock,
params: &ActorMockActionParams{firstParam, secondParam},
}
mmAction.expectations = append(mmAction.expectations, expectation)
return expectation
}
// Then sets up actor.Action return parameters for the expectation previously defined by the When method
func (e *ActorMockActionExpectation) Then(i1 int, err error) *ActorMock {
e.results = &ActorMockActionResults{i1, err}
return e.mock
}
// Action implements actor
func (mmAction *ActorMock) Action(firstParam string, secondParam int) (i1 int, err error) {
mm_atomic.AddUint64(&mmAction.beforeActionCounter, 1)
defer mm_atomic.AddUint64(&mmAction.afterActionCounter, 1)
if mmAction.inspectFuncAction != nil {
mmAction.inspectFuncAction(firstParam, secondParam)
}
mm_params := ActorMockActionParams{firstParam, secondParam}
// Record call args
mmAction.ActionMock.mutex.Lock()
mmAction.ActionMock.callArgs = append(mmAction.ActionMock.callArgs, &mm_params)
mmAction.ActionMock.mutex.Unlock()
for _, e := range mmAction.ActionMock.expectations {
if minimock.Equal(*e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.i1, e.results.err
}
}
if mmAction.ActionMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmAction.ActionMock.defaultExpectation.Counter, 1)
mm_want := mmAction.ActionMock.defaultExpectation.params
mm_want_ptrs := mmAction.ActionMock.defaultExpectation.paramPtrs
mm_got := ActorMockActionParams{firstParam, secondParam}
if mm_want_ptrs != nil {
if mm_want_ptrs.firstParam != nil && !minimock.Equal(*mm_want_ptrs.firstParam, mm_got.firstParam) {
mmAction.t.Errorf("ActorMock.Action got unexpected parameter firstParam, want: %#v, got: %#v%s\n", *mm_want_ptrs.firstParam, mm_got.firstParam, minimock.Diff(*mm_want_ptrs.firstParam, mm_got.firstParam))
}
if mm_want_ptrs.secondParam != nil && !minimock.Equal(*mm_want_ptrs.secondParam, mm_got.secondParam) {
mmAction.t.Errorf("ActorMock.Action got unexpected parameter secondParam, want: %#v, got: %#v%s\n", *mm_want_ptrs.secondParam, mm_got.secondParam, minimock.Diff(*mm_want_ptrs.secondParam, mm_got.secondParam))
}
} else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmAction.t.Errorf("ActorMock.Action got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmAction.ActionMock.defaultExpectation.results
if mm_results == nil {
mmAction.t.Fatal("No results are set for the ActorMock.Action")
}
return (*mm_results).i1, (*mm_results).err
}
if mmAction.funcAction != nil {
return mmAction.funcAction(firstParam, secondParam)
}
mmAction.t.Fatalf("Unexpected call to ActorMock.Action. %v %v", firstParam, secondParam)
return
}
// ActionAfterCounter returns a count of finished ActorMock.Action invocations
func (mmAction *ActorMock) ActionAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmAction.afterActionCounter)
}
// ActionBeforeCounter returns a count of ActorMock.Action invocations
func (mmAction *ActorMock) ActionBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmAction.beforeActionCounter)
}
// Calls returns a list of arguments used in each call to ActorMock.Action.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmAction *mActorMockAction) Calls() []*ActorMockActionParams {
mmAction.mutex.RLock()
argCopy := make([]*ActorMockActionParams, len(mmAction.callArgs))
copy(argCopy, mmAction.callArgs)
mmAction.mutex.RUnlock()
return argCopy
}
// MinimockActionDone returns true if the count of the Action invocations corresponds
// the number of defined expectations
func (m *ActorMock) MinimockActionDone() bool {
for _, e := range m.ActionMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.ActionMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcAction != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 {
return false
}
return true
}
// MinimockActionInspect logs each unmet expectation
func (m *ActorMock) MinimockActionInspect() {
for _, e := range m.ActionMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to ActorMock.Action with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.ActionMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 {
if m.ActionMock.defaultExpectation.params == nil {
m.t.Error("Expected call to ActorMock.Action")
} else {
m.t.Errorf("Expected call to ActorMock.Action with params: %#v", *m.ActionMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcAction != nil && mm_atomic.LoadUint64(&m.afterActionCounter) < 1 {
m.t.Error("Expected call to ActorMock.Action")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *ActorMock) MinimockFinish() {
m.finishOnce.Do(func() {
if !m.minimockDone() {
m.MinimockActionInspect()
m.t.FailNow()
}
})
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *ActorMock) MinimockWait(timeout mm_time.Duration) {
timeoutCh := mm_time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-mm_time.After(10 * mm_time.Millisecond):
}
}
}
func (m *ActorMock) minimockDone() bool {
done := true
return done &&
m.MinimockActionDone()
}