-
Notifications
You must be signed in to change notification settings - Fork 38
/
generic_inout.go
349 lines (281 loc) · 10.9 KB
/
generic_inout.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
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
package tests
//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o generic_inout.go -n GenericInoutMock -p tests
import (
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
)
// GenericInoutMock implements genericInout
type GenericInoutMock[T any] struct {
t minimock.Tester
finishOnce sync.Once
funcName func(t1 T) (t2 T)
inspectFuncName func(t1 T)
afterNameCounter uint64
beforeNameCounter uint64
NameMock mGenericInoutMockName[T]
}
// NewGenericInoutMock returns a mock for genericInout
func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] {
m := &GenericInoutMock[T]{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.NameMock = mGenericInoutMockName[T]{mock: m}
m.NameMock.callArgs = []*GenericInoutMockNameParams[T]{}
t.Cleanup(m.MinimockFinish)
return m
}
type mGenericInoutMockName[T any] struct {
mock *GenericInoutMock[T]
defaultExpectation *GenericInoutMockNameExpectation[T]
expectations []*GenericInoutMockNameExpectation[T]
callArgs []*GenericInoutMockNameParams[T]
mutex sync.RWMutex
expectedInvocations uint64
}
// GenericInoutMockNameExpectation specifies expectation struct of the genericInout.Name
type GenericInoutMockNameExpectation[T any] struct {
mock *GenericInoutMock[T]
params *GenericInoutMockNameParams[T]
paramPtrs *GenericInoutMockNameParamPtrs[T]
results *GenericInoutMockNameResults[T]
Counter uint64
}
// GenericInoutMockNameParams contains parameters of the genericInout.Name
type GenericInoutMockNameParams[T any] struct {
t1 T
}
// GenericInoutMockNameParamPtrs contains pointers to parameters of the genericInout.Name
type GenericInoutMockNameParamPtrs[T any] struct {
t1 *T
}
// GenericInoutMockNameResults contains results of the genericInout.Name
type GenericInoutMockNameResults[T any] struct {
t2 T
}
// Expect sets up expected params for genericInout.Name
func (mmName *mGenericInoutMockName[T]) Expect(t1 T) *mGenericInoutMockName[T] {
if mmName.mock.funcName != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set")
}
if mmName.defaultExpectation == nil {
mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{}
}
if mmName.defaultExpectation.paramPtrs != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by ExpectParams functions")
}
mmName.defaultExpectation.params = &GenericInoutMockNameParams[T]{t1}
for _, e := range mmName.expectations {
if minimock.Equal(e.params, mmName.defaultExpectation.params) {
mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params)
}
}
return mmName
}
// ExpectT1Param1 sets up expected param t1 for genericInout.Name
func (mmName *mGenericInoutMockName[T]) ExpectT1Param1(t1 T) *mGenericInoutMockName[T] {
if mmName.mock.funcName != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set")
}
if mmName.defaultExpectation == nil {
mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{}
}
if mmName.defaultExpectation.params != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Expect")
}
if mmName.defaultExpectation.paramPtrs == nil {
mmName.defaultExpectation.paramPtrs = &GenericInoutMockNameParamPtrs[T]{}
}
mmName.defaultExpectation.paramPtrs.t1 = &t1
return mmName
}
// Inspect accepts an inspector function that has same arguments as the genericInout.Name
func (mmName *mGenericInoutMockName[T]) Inspect(f func(t1 T)) *mGenericInoutMockName[T] {
if mmName.mock.inspectFuncName != nil {
mmName.mock.t.Fatalf("Inspect function is already set for GenericInoutMock.Name")
}
mmName.mock.inspectFuncName = f
return mmName
}
// Return sets up results that will be returned by genericInout.Name
func (mmName *mGenericInoutMockName[T]) Return(t2 T) *GenericInoutMock[T] {
if mmName.mock.funcName != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set")
}
if mmName.defaultExpectation == nil {
mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{mock: mmName.mock}
}
mmName.defaultExpectation.results = &GenericInoutMockNameResults[T]{t2}
return mmName.mock
}
// Set uses given function f to mock the genericInout.Name method
func (mmName *mGenericInoutMockName[T]) Set(f func(t1 T) (t2 T)) *GenericInoutMock[T] {
if mmName.defaultExpectation != nil {
mmName.mock.t.Fatalf("Default expectation is already set for the genericInout.Name method")
}
if len(mmName.expectations) > 0 {
mmName.mock.t.Fatalf("Some expectations are already set for the genericInout.Name method")
}
mmName.mock.funcName = f
return mmName.mock
}
// When sets expectation for the genericInout.Name which will trigger the result defined by the following
// Then helper
func (mmName *mGenericInoutMockName[T]) When(t1 T) *GenericInoutMockNameExpectation[T] {
if mmName.mock.funcName != nil {
mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set")
}
expectation := &GenericInoutMockNameExpectation[T]{
mock: mmName.mock,
params: &GenericInoutMockNameParams[T]{t1},
}
mmName.expectations = append(mmName.expectations, expectation)
return expectation
}
// Then sets up genericInout.Name return parameters for the expectation previously defined by the When method
func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] {
e.results = &GenericInoutMockNameResults[T]{t2}
return e.mock
}
// Times sets number of times genericInout.Name should be invoked
func (mmName *mGenericInoutMockName[T]) Times(n uint64) *mGenericInoutMockName[T] {
if n == 0 {
mmName.mock.t.Fatalf("Times of GenericInoutMock.Name mock can not be zero")
}
mm_atomic.StoreUint64(&mmName.expectedInvocations, n)
return mmName
}
func (mmName *mGenericInoutMockName[T]) invocationsDone() bool {
if len(mmName.expectations) == 0 && mmName.defaultExpectation == nil && mmName.mock.funcName == nil {
return true
}
totalInvocations := mm_atomic.LoadUint64(&mmName.mock.afterNameCounter)
expectedInvocations := mm_atomic.LoadUint64(&mmName.expectedInvocations)
return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations)
}
// Name implements genericInout
func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) {
mm_atomic.AddUint64(&mmName.beforeNameCounter, 1)
defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1)
if mmName.inspectFuncName != nil {
mmName.inspectFuncName(t1)
}
mm_params := GenericInoutMockNameParams[T]{t1}
// Record call args
mmName.NameMock.mutex.Lock()
mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params)
mmName.NameMock.mutex.Unlock()
for _, e := range mmName.NameMock.expectations {
if minimock.Equal(*e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.t2
}
}
if mmName.NameMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1)
mm_want := mmName.NameMock.defaultExpectation.params
mm_want_ptrs := mmName.NameMock.defaultExpectation.paramPtrs
mm_got := GenericInoutMockNameParams[T]{t1}
if mm_want_ptrs != nil {
if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) {
mmName.t.Errorf("GenericInoutMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1))
}
} else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmName.t.Errorf("GenericInoutMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmName.NameMock.defaultExpectation.results
if mm_results == nil {
mmName.t.Fatal("No results are set for the GenericInoutMock.Name")
}
return (*mm_results).t2
}
if mmName.funcName != nil {
return mmName.funcName(t1)
}
mmName.t.Fatalf("Unexpected call to GenericInoutMock.Name. %v", t1)
return
}
// NameAfterCounter returns a count of finished GenericInoutMock.Name invocations
func (mmName *GenericInoutMock[T]) NameAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmName.afterNameCounter)
}
// NameBeforeCounter returns a count of GenericInoutMock.Name invocations
func (mmName *GenericInoutMock[T]) NameBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmName.beforeNameCounter)
}
// Calls returns a list of arguments used in each call to GenericInoutMock.Name.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmName *mGenericInoutMockName[T]) Calls() []*GenericInoutMockNameParams[T] {
mmName.mutex.RLock()
argCopy := make([]*GenericInoutMockNameParams[T], len(mmName.callArgs))
copy(argCopy, mmName.callArgs)
mmName.mutex.RUnlock()
return argCopy
}
// MinimockNameDone returns true if the count of the Name invocations corresponds
// the number of defined expectations
func (m *GenericInoutMock[T]) MinimockNameDone() bool {
for _, e := range m.NameMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
return m.NameMock.invocationsDone()
}
// MinimockNameInspect logs each unmet expectation
func (m *GenericInoutMock[T]) MinimockNameInspect() {
for _, e := range m.NameMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *e.params)
}
}
afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter)
// if default expectation was set then invocations count should be greater than zero
if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 {
if m.NameMock.defaultExpectation.params == nil {
m.t.Error("Expected call to GenericInoutMock.Name")
} else {
m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *m.NameMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcName != nil && afterNameCounter < 1 {
m.t.Error("Expected call to GenericInoutMock.Name")
}
if !m.NameMock.invocationsDone() && afterNameCounter > 0 {
m.t.Errorf("Expected %d calls to GenericInoutMock.Name but found %d calls",
mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter)
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *GenericInoutMock[T]) MinimockFinish() {
m.finishOnce.Do(func() {
if !m.minimockDone() {
m.MinimockNameInspect()
m.t.FailNow()
}
})
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *GenericInoutMock[T]) 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 *GenericInoutMock[T]) minimockDone() bool {
done := true
return done &&
m.MinimockNameDone()
}