-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoer_mock.go
More file actions
282 lines (234 loc) · 7.82 KB
/
doer_mock.go
File metadata and controls
282 lines (234 loc) · 7.82 KB
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
package httpclient
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
import (
"net/http"
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
)
// DoerMock implements Doer
type DoerMock struct {
t minimock.Tester
funcDo func(req *http.Request) (rp1 *http.Response, err error)
inspectFuncDo func(req *http.Request)
afterDoCounter uint64
beforeDoCounter uint64
DoMock mDoerMockDo
}
// NewDoerMock returns a mock for Doer
func NewDoerMock(t minimock.Tester) *DoerMock {
m := &DoerMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.DoMock = mDoerMockDo{mock: m}
m.DoMock.callArgs = []*DoerMockDoParams{}
return m
}
type mDoerMockDo struct {
mock *DoerMock
defaultExpectation *DoerMockDoExpectation
expectations []*DoerMockDoExpectation
callArgs []*DoerMockDoParams
mutex sync.RWMutex
}
// DoerMockDoExpectation specifies expectation struct of the Doer.Do
type DoerMockDoExpectation struct {
mock *DoerMock
params *DoerMockDoParams
results *DoerMockDoResults
Counter uint64
}
// DoerMockDoParams contains parameters of the Doer.Do
type DoerMockDoParams struct {
req *http.Request
}
// DoerMockDoResults contains results of the Doer.Do
type DoerMockDoResults struct {
rp1 *http.Response
err error
}
// Expect sets up expected params for Doer.Do
func (mmDo *mDoerMockDo) Expect(req *http.Request) *mDoerMockDo {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("DoerMock.Do mock is already set by Set")
}
if mmDo.defaultExpectation == nil {
mmDo.defaultExpectation = &DoerMockDoExpectation{}
}
mmDo.defaultExpectation.params = &DoerMockDoParams{req}
for _, e := range mmDo.expectations {
if minimock.Equal(e.params, mmDo.defaultExpectation.params) {
mmDo.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmDo.defaultExpectation.params)
}
}
return mmDo
}
// Inspect accepts an inspector function that has same arguments as the Doer.Do
func (mmDo *mDoerMockDo) Inspect(f func(req *http.Request)) *mDoerMockDo {
if mmDo.mock.inspectFuncDo != nil {
mmDo.mock.t.Fatalf("Inspect function is already set for DoerMock.Do")
}
mmDo.mock.inspectFuncDo = f
return mmDo
}
// Return sets up results that will be returned by Doer.Do
func (mmDo *mDoerMockDo) Return(rp1 *http.Response, err error) *DoerMock {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("DoerMock.Do mock is already set by Set")
}
if mmDo.defaultExpectation == nil {
mmDo.defaultExpectation = &DoerMockDoExpectation{mock: mmDo.mock}
}
mmDo.defaultExpectation.results = &DoerMockDoResults{rp1, err}
return mmDo.mock
}
//Set uses given function f to mock the Doer.Do method
func (mmDo *mDoerMockDo) Set(f func(req *http.Request) (rp1 *http.Response, err error)) *DoerMock {
if mmDo.defaultExpectation != nil {
mmDo.mock.t.Fatalf("Default expectation is already set for the Doer.Do method")
}
if len(mmDo.expectations) > 0 {
mmDo.mock.t.Fatalf("Some expectations are already set for the Doer.Do method")
}
mmDo.mock.funcDo = f
return mmDo.mock
}
// When sets expectation for the Doer.Do which will trigger the result defined by the following
// Then helper
func (mmDo *mDoerMockDo) When(req *http.Request) *DoerMockDoExpectation {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("DoerMock.Do mock is already set by Set")
}
expectation := &DoerMockDoExpectation{
mock: mmDo.mock,
params: &DoerMockDoParams{req},
}
mmDo.expectations = append(mmDo.expectations, expectation)
return expectation
}
// Then sets up Doer.Do return parameters for the expectation previously defined by the When method
func (e *DoerMockDoExpectation) Then(rp1 *http.Response, err error) *DoerMock {
e.results = &DoerMockDoResults{rp1, err}
return e.mock
}
// Do implements Doer
func (mmDo *DoerMock) Do(req *http.Request) (rp1 *http.Response, err error) {
mm_atomic.AddUint64(&mmDo.beforeDoCounter, 1)
defer mm_atomic.AddUint64(&mmDo.afterDoCounter, 1)
if mmDo.inspectFuncDo != nil {
mmDo.inspectFuncDo(req)
}
mm_params := &DoerMockDoParams{req}
// Record call args
mmDo.DoMock.mutex.Lock()
mmDo.DoMock.callArgs = append(mmDo.DoMock.callArgs, mm_params)
mmDo.DoMock.mutex.Unlock()
for _, e := range mmDo.DoMock.expectations {
if minimock.Equal(e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.rp1, e.results.err
}
}
if mmDo.DoMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmDo.DoMock.defaultExpectation.Counter, 1)
mm_want := mmDo.DoMock.defaultExpectation.params
mm_got := DoerMockDoParams{req}
if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmDo.t.Errorf("DoerMock.Do got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmDo.DoMock.defaultExpectation.results
if mm_results == nil {
mmDo.t.Fatal("No results are set for the DoerMock.Do")
}
return (*mm_results).rp1, (*mm_results).err
}
if mmDo.funcDo != nil {
return mmDo.funcDo(req)
}
mmDo.t.Fatalf("Unexpected call to DoerMock.Do. %v", req)
return
}
// DoAfterCounter returns a count of finished DoerMock.Do invocations
func (mmDo *DoerMock) DoAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmDo.afterDoCounter)
}
// DoBeforeCounter returns a count of DoerMock.Do invocations
func (mmDo *DoerMock) DoBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmDo.beforeDoCounter)
}
// Calls returns a list of arguments used in each call to DoerMock.Do.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmDo *mDoerMockDo) Calls() []*DoerMockDoParams {
mmDo.mutex.RLock()
argCopy := make([]*DoerMockDoParams, len(mmDo.callArgs))
copy(argCopy, mmDo.callArgs)
mmDo.mutex.RUnlock()
return argCopy
}
// MinimockDoDone returns true if the count of the Do invocations corresponds
// the number of defined expectations
func (m *DoerMock) MinimockDoDone() bool {
for _, e := range m.DoMock.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.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
return false
}
return true
}
// MinimockDoInspect logs each unmet expectation
func (m *DoerMock) MinimockDoInspect() {
for _, e := range m.DoMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to DoerMock.Do with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
if m.DoMock.defaultExpectation.params == nil {
m.t.Error("Expected call to DoerMock.Do")
} else {
m.t.Errorf("Expected call to DoerMock.Do with params: %#v", *m.DoMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 {
m.t.Error("Expected call to DoerMock.Do")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *DoerMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockDoInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *DoerMock) 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 *DoerMock) minimockDone() bool {
done := true
return done &&
m.MinimockDoDone()
}