-
Notifications
You must be signed in to change notification settings - Fork 46
/
accessor_mock.go
513 lines (425 loc) · 16.5 KB
/
accessor_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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package jet
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
import (
"context"
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock"
"github.com/insolar/insolar/insolar"
)
// AccessorMock implements Accessor
type AccessorMock struct {
t minimock.Tester
funcAll func(ctx context.Context, pulse insolar.PulseNumber) (ja1 []insolar.JetID)
inspectFuncAll func(ctx context.Context, pulse insolar.PulseNumber)
afterAllCounter uint64
beforeAllCounter uint64
AllMock mAccessorMockAll
funcForID func(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID) (j1 insolar.JetID, b1 bool)
inspectFuncForID func(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID)
afterForIDCounter uint64
beforeForIDCounter uint64
ForIDMock mAccessorMockForID
}
// NewAccessorMock returns a mock for Accessor
func NewAccessorMock(t minimock.Tester) *AccessorMock {
m := &AccessorMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.AllMock = mAccessorMockAll{mock: m}
m.AllMock.callArgs = []*AccessorMockAllParams{}
m.ForIDMock = mAccessorMockForID{mock: m}
m.ForIDMock.callArgs = []*AccessorMockForIDParams{}
return m
}
type mAccessorMockAll struct {
mock *AccessorMock
defaultExpectation *AccessorMockAllExpectation
expectations []*AccessorMockAllExpectation
callArgs []*AccessorMockAllParams
mutex sync.RWMutex
}
// AccessorMockAllExpectation specifies expectation struct of the Accessor.All
type AccessorMockAllExpectation struct {
mock *AccessorMock
params *AccessorMockAllParams
results *AccessorMockAllResults
Counter uint64
}
// AccessorMockAllParams contains parameters of the Accessor.All
type AccessorMockAllParams struct {
ctx context.Context
pulse insolar.PulseNumber
}
// AccessorMockAllResults contains results of the Accessor.All
type AccessorMockAllResults struct {
ja1 []insolar.JetID
}
// Expect sets up expected params for Accessor.All
func (mmAll *mAccessorMockAll) Expect(ctx context.Context, pulse insolar.PulseNumber) *mAccessorMockAll {
if mmAll.mock.funcAll != nil {
mmAll.mock.t.Fatalf("AccessorMock.All mock is already set by Set")
}
if mmAll.defaultExpectation == nil {
mmAll.defaultExpectation = &AccessorMockAllExpectation{}
}
mmAll.defaultExpectation.params = &AccessorMockAllParams{ctx, pulse}
for _, e := range mmAll.expectations {
if minimock.Equal(e.params, mmAll.defaultExpectation.params) {
mmAll.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAll.defaultExpectation.params)
}
}
return mmAll
}
// Inspect accepts an inspector function that has same arguments as the Accessor.All
func (mmAll *mAccessorMockAll) Inspect(f func(ctx context.Context, pulse insolar.PulseNumber)) *mAccessorMockAll {
if mmAll.mock.inspectFuncAll != nil {
mmAll.mock.t.Fatalf("Inspect function is already set for AccessorMock.All")
}
mmAll.mock.inspectFuncAll = f
return mmAll
}
// Return sets up results that will be returned by Accessor.All
func (mmAll *mAccessorMockAll) Return(ja1 []insolar.JetID) *AccessorMock {
if mmAll.mock.funcAll != nil {
mmAll.mock.t.Fatalf("AccessorMock.All mock is already set by Set")
}
if mmAll.defaultExpectation == nil {
mmAll.defaultExpectation = &AccessorMockAllExpectation{mock: mmAll.mock}
}
mmAll.defaultExpectation.results = &AccessorMockAllResults{ja1}
return mmAll.mock
}
//Set uses given function f to mock the Accessor.All method
func (mmAll *mAccessorMockAll) Set(f func(ctx context.Context, pulse insolar.PulseNumber) (ja1 []insolar.JetID)) *AccessorMock {
if mmAll.defaultExpectation != nil {
mmAll.mock.t.Fatalf("Default expectation is already set for the Accessor.All method")
}
if len(mmAll.expectations) > 0 {
mmAll.mock.t.Fatalf("Some expectations are already set for the Accessor.All method")
}
mmAll.mock.funcAll = f
return mmAll.mock
}
// When sets expectation for the Accessor.All which will trigger the result defined by the following
// Then helper
func (mmAll *mAccessorMockAll) When(ctx context.Context, pulse insolar.PulseNumber) *AccessorMockAllExpectation {
if mmAll.mock.funcAll != nil {
mmAll.mock.t.Fatalf("AccessorMock.All mock is already set by Set")
}
expectation := &AccessorMockAllExpectation{
mock: mmAll.mock,
params: &AccessorMockAllParams{ctx, pulse},
}
mmAll.expectations = append(mmAll.expectations, expectation)
return expectation
}
// Then sets up Accessor.All return parameters for the expectation previously defined by the When method
func (e *AccessorMockAllExpectation) Then(ja1 []insolar.JetID) *AccessorMock {
e.results = &AccessorMockAllResults{ja1}
return e.mock
}
// All implements Accessor
func (mmAll *AccessorMock) All(ctx context.Context, pulse insolar.PulseNumber) (ja1 []insolar.JetID) {
mm_atomic.AddUint64(&mmAll.beforeAllCounter, 1)
defer mm_atomic.AddUint64(&mmAll.afterAllCounter, 1)
if mmAll.inspectFuncAll != nil {
mmAll.inspectFuncAll(ctx, pulse)
}
params := &AccessorMockAllParams{ctx, pulse}
// Record call args
mmAll.AllMock.mutex.Lock()
mmAll.AllMock.callArgs = append(mmAll.AllMock.callArgs, params)
mmAll.AllMock.mutex.Unlock()
for _, e := range mmAll.AllMock.expectations {
if minimock.Equal(e.params, params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.ja1
}
}
if mmAll.AllMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmAll.AllMock.defaultExpectation.Counter, 1)
want := mmAll.AllMock.defaultExpectation.params
got := AccessorMockAllParams{ctx, pulse}
if want != nil && !minimock.Equal(*want, got) {
mmAll.t.Errorf("AccessorMock.All got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
}
results := mmAll.AllMock.defaultExpectation.results
if results == nil {
mmAll.t.Fatal("No results are set for the AccessorMock.All")
}
return (*results).ja1
}
if mmAll.funcAll != nil {
return mmAll.funcAll(ctx, pulse)
}
mmAll.t.Fatalf("Unexpected call to AccessorMock.All. %v %v", ctx, pulse)
return
}
// AllAfterCounter returns a count of finished AccessorMock.All invocations
func (mmAll *AccessorMock) AllAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmAll.afterAllCounter)
}
// AllBeforeCounter returns a count of AccessorMock.All invocations
func (mmAll *AccessorMock) AllBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmAll.beforeAllCounter)
}
// Calls returns a list of arguments used in each call to AccessorMock.All.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmAll *mAccessorMockAll) Calls() []*AccessorMockAllParams {
mmAll.mutex.RLock()
argCopy := make([]*AccessorMockAllParams, len(mmAll.callArgs))
copy(argCopy, mmAll.callArgs)
mmAll.mutex.RUnlock()
return argCopy
}
// MinimockAllDone returns true if the count of the All invocations corresponds
// the number of defined expectations
func (m *AccessorMock) MinimockAllDone() bool {
for _, e := range m.AllMock.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.AllMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAllCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcAll != nil && mm_atomic.LoadUint64(&m.afterAllCounter) < 1 {
return false
}
return true
}
// MinimockAllInspect logs each unmet expectation
func (m *AccessorMock) MinimockAllInspect() {
for _, e := range m.AllMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to AccessorMock.All with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.AllMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAllCounter) < 1 {
if m.AllMock.defaultExpectation.params == nil {
m.t.Error("Expected call to AccessorMock.All")
} else {
m.t.Errorf("Expected call to AccessorMock.All with params: %#v", *m.AllMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcAll != nil && mm_atomic.LoadUint64(&m.afterAllCounter) < 1 {
m.t.Error("Expected call to AccessorMock.All")
}
}
type mAccessorMockForID struct {
mock *AccessorMock
defaultExpectation *AccessorMockForIDExpectation
expectations []*AccessorMockForIDExpectation
callArgs []*AccessorMockForIDParams
mutex sync.RWMutex
}
// AccessorMockForIDExpectation specifies expectation struct of the Accessor.ForID
type AccessorMockForIDExpectation struct {
mock *AccessorMock
params *AccessorMockForIDParams
results *AccessorMockForIDResults
Counter uint64
}
// AccessorMockForIDParams contains parameters of the Accessor.ForID
type AccessorMockForIDParams struct {
ctx context.Context
pulse insolar.PulseNumber
recordID insolar.ID
}
// AccessorMockForIDResults contains results of the Accessor.ForID
type AccessorMockForIDResults struct {
j1 insolar.JetID
b1 bool
}
// Expect sets up expected params for Accessor.ForID
func (mmForID *mAccessorMockForID) Expect(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID) *mAccessorMockForID {
if mmForID.mock.funcForID != nil {
mmForID.mock.t.Fatalf("AccessorMock.ForID mock is already set by Set")
}
if mmForID.defaultExpectation == nil {
mmForID.defaultExpectation = &AccessorMockForIDExpectation{}
}
mmForID.defaultExpectation.params = &AccessorMockForIDParams{ctx, pulse, recordID}
for _, e := range mmForID.expectations {
if minimock.Equal(e.params, mmForID.defaultExpectation.params) {
mmForID.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmForID.defaultExpectation.params)
}
}
return mmForID
}
// Inspect accepts an inspector function that has same arguments as the Accessor.ForID
func (mmForID *mAccessorMockForID) Inspect(f func(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID)) *mAccessorMockForID {
if mmForID.mock.inspectFuncForID != nil {
mmForID.mock.t.Fatalf("Inspect function is already set for AccessorMock.ForID")
}
mmForID.mock.inspectFuncForID = f
return mmForID
}
// Return sets up results that will be returned by Accessor.ForID
func (mmForID *mAccessorMockForID) Return(j1 insolar.JetID, b1 bool) *AccessorMock {
if mmForID.mock.funcForID != nil {
mmForID.mock.t.Fatalf("AccessorMock.ForID mock is already set by Set")
}
if mmForID.defaultExpectation == nil {
mmForID.defaultExpectation = &AccessorMockForIDExpectation{mock: mmForID.mock}
}
mmForID.defaultExpectation.results = &AccessorMockForIDResults{j1, b1}
return mmForID.mock
}
//Set uses given function f to mock the Accessor.ForID method
func (mmForID *mAccessorMockForID) Set(f func(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID) (j1 insolar.JetID, b1 bool)) *AccessorMock {
if mmForID.defaultExpectation != nil {
mmForID.mock.t.Fatalf("Default expectation is already set for the Accessor.ForID method")
}
if len(mmForID.expectations) > 0 {
mmForID.mock.t.Fatalf("Some expectations are already set for the Accessor.ForID method")
}
mmForID.mock.funcForID = f
return mmForID.mock
}
// When sets expectation for the Accessor.ForID which will trigger the result defined by the following
// Then helper
func (mmForID *mAccessorMockForID) When(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID) *AccessorMockForIDExpectation {
if mmForID.mock.funcForID != nil {
mmForID.mock.t.Fatalf("AccessorMock.ForID mock is already set by Set")
}
expectation := &AccessorMockForIDExpectation{
mock: mmForID.mock,
params: &AccessorMockForIDParams{ctx, pulse, recordID},
}
mmForID.expectations = append(mmForID.expectations, expectation)
return expectation
}
// Then sets up Accessor.ForID return parameters for the expectation previously defined by the When method
func (e *AccessorMockForIDExpectation) Then(j1 insolar.JetID, b1 bool) *AccessorMock {
e.results = &AccessorMockForIDResults{j1, b1}
return e.mock
}
// ForID implements Accessor
func (mmForID *AccessorMock) ForID(ctx context.Context, pulse insolar.PulseNumber, recordID insolar.ID) (j1 insolar.JetID, b1 bool) {
mm_atomic.AddUint64(&mmForID.beforeForIDCounter, 1)
defer mm_atomic.AddUint64(&mmForID.afterForIDCounter, 1)
if mmForID.inspectFuncForID != nil {
mmForID.inspectFuncForID(ctx, pulse, recordID)
}
params := &AccessorMockForIDParams{ctx, pulse, recordID}
// Record call args
mmForID.ForIDMock.mutex.Lock()
mmForID.ForIDMock.callArgs = append(mmForID.ForIDMock.callArgs, params)
mmForID.ForIDMock.mutex.Unlock()
for _, e := range mmForID.ForIDMock.expectations {
if minimock.Equal(e.params, params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.j1, e.results.b1
}
}
if mmForID.ForIDMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmForID.ForIDMock.defaultExpectation.Counter, 1)
want := mmForID.ForIDMock.defaultExpectation.params
got := AccessorMockForIDParams{ctx, pulse, recordID}
if want != nil && !minimock.Equal(*want, got) {
mmForID.t.Errorf("AccessorMock.ForID got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
}
results := mmForID.ForIDMock.defaultExpectation.results
if results == nil {
mmForID.t.Fatal("No results are set for the AccessorMock.ForID")
}
return (*results).j1, (*results).b1
}
if mmForID.funcForID != nil {
return mmForID.funcForID(ctx, pulse, recordID)
}
mmForID.t.Fatalf("Unexpected call to AccessorMock.ForID. %v %v %v", ctx, pulse, recordID)
return
}
// ForIDAfterCounter returns a count of finished AccessorMock.ForID invocations
func (mmForID *AccessorMock) ForIDAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmForID.afterForIDCounter)
}
// ForIDBeforeCounter returns a count of AccessorMock.ForID invocations
func (mmForID *AccessorMock) ForIDBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmForID.beforeForIDCounter)
}
// Calls returns a list of arguments used in each call to AccessorMock.ForID.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmForID *mAccessorMockForID) Calls() []*AccessorMockForIDParams {
mmForID.mutex.RLock()
argCopy := make([]*AccessorMockForIDParams, len(mmForID.callArgs))
copy(argCopy, mmForID.callArgs)
mmForID.mutex.RUnlock()
return argCopy
}
// MinimockForIDDone returns true if the count of the ForID invocations corresponds
// the number of defined expectations
func (m *AccessorMock) MinimockForIDDone() bool {
for _, e := range m.ForIDMock.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.ForIDMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterForIDCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcForID != nil && mm_atomic.LoadUint64(&m.afterForIDCounter) < 1 {
return false
}
return true
}
// MinimockForIDInspect logs each unmet expectation
func (m *AccessorMock) MinimockForIDInspect() {
for _, e := range m.ForIDMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to AccessorMock.ForID with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.ForIDMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterForIDCounter) < 1 {
if m.ForIDMock.defaultExpectation.params == nil {
m.t.Error("Expected call to AccessorMock.ForID")
} else {
m.t.Errorf("Expected call to AccessorMock.ForID with params: %#v", *m.ForIDMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcForID != nil && mm_atomic.LoadUint64(&m.afterForIDCounter) < 1 {
m.t.Error("Expected call to AccessorMock.ForID")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *AccessorMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockAllInspect()
m.MinimockForIDInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *AccessorMock) 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 *AccessorMock) minimockDone() bool {
done := true
return done &&
m.MinimockAllDone() &&
m.MinimockForIDDone()
}