Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 13f3609

Browse files
authored
Merge pull request #93 from golang/restore_backwards_compatibility
Restore backwards compatibility for controller.RecordCall.
2 parents 6244772 + dea6b4f commit 13f3609

File tree

6 files changed

+74
-66
lines changed

6 files changed

+74
-66
lines changed

gomock/controller.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
package gomock
5757

5858
import (
59+
"fmt"
5960
"reflect"
6061
"sync"
6162
)
@@ -83,7 +84,19 @@ func NewController(t TestReporter) *Controller {
8384
}
8485
}
8586

86-
func (ctrl *Controller) RecordCall(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
87+
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
88+
recv := reflect.ValueOf(receiver)
89+
for i := 0; i < recv.Type().NumMethod(); i++ {
90+
if recv.Type().Method(i).Name == method {
91+
return ctrl.RecordCallWithMethodType(receiver, method, recv.Method(i).Type(), args...)
92+
}
93+
}
94+
ctrl.t.Fatalf("gomock: failed finding method %s on %T", method, receiver)
95+
// In case t.Fatalf does not panic.
96+
panic(fmt.Sprintf("gomock: failed finding method %s on %T", method, receiver))
97+
}
98+
99+
func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
87100
// TODO: check arity, types.
88101
margs := make([]Matcher, len(args))
89102
for i, arg := range args {

gomock/controller_test.go

+29-34
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ func (s *Subject) BarMethod(arg string) int {
119119
return 0
120120
}
121121

122-
var (
123-
FooMethodType = reflect.TypeOf((*Subject).FooMethod)
124-
BarMethodType = reflect.TypeOf((*Subject).BarMethod)
125-
)
126-
127122
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
128123
if !reflect.DeepEqual(expected, actual) {
129124
t.Errorf("Expected %+v, but got %+v", expected, actual)
@@ -149,7 +144,7 @@ func TestExpectedMethodCall(t *testing.T) {
149144
reporter, ctrl := createFixtures(t)
150145
subject := new(Subject)
151146

152-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument")
147+
ctrl.RecordCall(subject, "FooMethod", "argument")
153148
ctrl.Call(subject, "FooMethod", "argument")
154149
ctrl.Finish()
155150

@@ -171,7 +166,7 @@ func TestRepeatedCall(t *testing.T) {
171166
reporter, ctrl := createFixtures(t)
172167
subject := new(Subject)
173168

174-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").Times(3)
169+
ctrl.RecordCall(subject, "FooMethod", "argument").Times(3)
175170
ctrl.Call(subject, "FooMethod", "argument")
176171
ctrl.Call(subject, "FooMethod", "argument")
177172
ctrl.Call(subject, "FooMethod", "argument")
@@ -188,7 +183,7 @@ func TestUnexpectedArgCount(t *testing.T) {
188183
defer reporter.recoverUnexpectedFatal()
189184
subject := new(Subject)
190185

191-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument")
186+
ctrl.RecordCall(subject, "FooMethod", "argument")
192187
reporter.assertFatal(func() {
193188
// This call is made with the wrong number of arguments...
194189
ctrl.Call(subject, "FooMethod", "argument", "extra_argument")
@@ -207,7 +202,7 @@ func TestAnyTimes(t *testing.T) {
207202
reporter, ctrl := createFixtures(t)
208203
subject := new(Subject)
209204

210-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").AnyTimes()
205+
ctrl.RecordCall(subject, "FooMethod", "argument").AnyTimes()
211206
for i := 0; i < 100; i++ {
212207
ctrl.Call(subject, "FooMethod", "argument")
213208
}
@@ -219,22 +214,22 @@ func TestMinTimes1(t *testing.T) {
219214
// It fails if there are no calls
220215
reporter, ctrl := createFixtures(t)
221216
subject := new(Subject)
222-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MinTimes(1)
217+
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
223218
reporter.assertFatal(func() {
224219
ctrl.Finish()
225220
})
226221

227222
// It succeeds if there is one call
228223
reporter, ctrl = createFixtures(t)
229224
subject = new(Subject)
230-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MinTimes(1)
225+
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
231226
ctrl.Call(subject, "FooMethod", "argument")
232227
ctrl.Finish()
233228

234229
// It succeeds if there are many calls
235230
reporter, ctrl = createFixtures(t)
236231
subject = new(Subject)
237-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MinTimes(1)
232+
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
238233
for i := 0; i < 100; i++ {
239234
ctrl.Call(subject, "FooMethod", "argument")
240235
}
@@ -245,20 +240,20 @@ func TestMaxTimes1(t *testing.T) {
245240
// It succeeds if there are no calls
246241
_, ctrl := createFixtures(t)
247242
subject := new(Subject)
248-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MaxTimes(1)
243+
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
249244
ctrl.Finish()
250245

251246
// It succeeds if there is one call
252247
_, ctrl = createFixtures(t)
253248
subject = new(Subject)
254-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MaxTimes(1)
249+
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
255250
ctrl.Call(subject, "FooMethod", "argument")
256251
ctrl.Finish()
257252

258253
//It fails if there are more
259254
reporter, ctrl := createFixtures(t)
260255
subject = new(Subject)
261-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MaxTimes(1)
256+
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
262257
ctrl.Call(subject, "FooMethod", "argument")
263258
reporter.assertFatal(func() {
264259
ctrl.Call(subject, "FooMethod", "argument")
@@ -270,7 +265,7 @@ func TestMinMaxTimes(t *testing.T) {
270265
// It fails if there are less calls than specified
271266
reporter, ctrl := createFixtures(t)
272267
subject := new(Subject)
273-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MinTimes(2).MaxTimes(2)
268+
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
274269
ctrl.Call(subject, "FooMethod", "argument")
275270
reporter.assertFatal(func() {
276271
ctrl.Finish()
@@ -279,7 +274,7 @@ func TestMinMaxTimes(t *testing.T) {
279274
// It fails if there are more calls than specified
280275
reporter, ctrl = createFixtures(t)
281276
subject = new(Subject)
282-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MinTimes(2).MaxTimes(2)
277+
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
283278
ctrl.Call(subject, "FooMethod", "argument")
284279
ctrl.Call(subject, "FooMethod", "argument")
285280
reporter.assertFatal(func() {
@@ -289,7 +284,7 @@ func TestMinMaxTimes(t *testing.T) {
289284
// It succeeds if there is just the right number of calls
290285
reporter, ctrl = createFixtures(t)
291286
subject = new(Subject)
292-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").MaxTimes(2).MinTimes(2)
287+
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(2).MinTimes(2)
293288
ctrl.Call(subject, "FooMethod", "argument")
294289
ctrl.Call(subject, "FooMethod", "argument")
295290
ctrl.Finish()
@@ -301,7 +296,7 @@ func TestDo(t *testing.T) {
301296

302297
doCalled := false
303298
var argument string
304-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "argument").Do(
299+
ctrl.RecordCall(subject, "FooMethod", "argument").Do(
305300
func(arg string) {
306301
doCalled = true
307302
argument = arg
@@ -327,8 +322,8 @@ func TestReturn(t *testing.T) {
327322
subject := new(Subject)
328323

329324
// Unspecified return should produce "zero" result.
330-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "zero")
331-
ctrl.RecordCall(subject, "FooMethod", FooMethodType, "five").Return(5)
325+
ctrl.RecordCall(subject, "FooMethod", "zero")
326+
ctrl.RecordCall(subject, "FooMethod", "five").Return(5)
332327

333328
assertEqual(
334329
t,
@@ -348,10 +343,10 @@ func TestUnorderedCalls(t *testing.T) {
348343
subjectTwo := new(Subject)
349344
subjectOne := new(Subject)
350345

351-
ctrl.RecordCall(subjectOne, "FooMethod", FooMethodType, "1")
352-
ctrl.RecordCall(subjectOne, "BarMethod", BarMethodType, "2")
353-
ctrl.RecordCall(subjectTwo, "FooMethod", FooMethodType, "3")
354-
ctrl.RecordCall(subjectTwo, "BarMethod", BarMethodType, "4")
346+
ctrl.RecordCall(subjectOne, "FooMethod", "1")
347+
ctrl.RecordCall(subjectOne, "BarMethod", "2")
348+
ctrl.RecordCall(subjectTwo, "FooMethod", "3")
349+
ctrl.RecordCall(subjectTwo, "BarMethod", "4")
355350

356351
// Make the calls in a different order, which should be fine.
357352
ctrl.Call(subjectOne, "BarMethod", "2")
@@ -373,9 +368,9 @@ func commonTestOrderedCalls(t *testing.T) (reporter *ErrorReporter, ctrl *gomock
373368
subjectTwo = new(Subject)
374369

375370
gomock.InOrder(
376-
ctrl.RecordCall(subjectOne, "FooMethod", FooMethodType, "1").AnyTimes(),
377-
ctrl.RecordCall(subjectTwo, "FooMethod", FooMethodType, "2"),
378-
ctrl.RecordCall(subjectTwo, "BarMethod", BarMethodType, "3"),
371+
ctrl.RecordCall(subjectOne, "FooMethod", "1").AnyTimes(),
372+
ctrl.RecordCall(subjectTwo, "FooMethod", "2"),
373+
ctrl.RecordCall(subjectTwo, "BarMethod", "3"),
379374
)
380375

381376
return
@@ -428,9 +423,9 @@ func TestCallAfterLoopPanic(t *testing.T) {
428423

429424
subject := new(Subject)
430425

431-
firstCall := ctrl.RecordCall(subject, "FooMethod", FooMethodType, "1")
432-
secondCall := ctrl.RecordCall(subject, "FooMethod", FooMethodType, "2")
433-
thirdCall := ctrl.RecordCall(subject, "FooMethod", FooMethodType, "3")
426+
firstCall := ctrl.RecordCall(subject, "FooMethod", "1")
427+
secondCall := ctrl.RecordCall(subject, "FooMethod", "2")
428+
thirdCall := ctrl.RecordCall(subject, "FooMethod", "3")
434429

435430
gomock.InOrder(firstCall, secondCall, thirdCall)
436431

@@ -450,7 +445,7 @@ func TestPanicOverridesExpectationChecks(t *testing.T) {
450445
reporter := NewErrorReporter(t)
451446

452447
reporter.assertFatal(func() {
453-
ctrl.RecordCall(new(Subject), "FooMethod", FooMethodType, "1")
448+
ctrl.RecordCall(new(Subject), "FooMethod", "1")
454449
defer ctrl.Finish()
455450
reporter.Fatalf("Intentional panic")
456451
})
@@ -463,7 +458,7 @@ func TestSetArgWithBadType(t *testing.T) {
463458
s := new(Subject)
464459
// This should catch a type error:
465460
rep.assertFatal(func() {
466-
ctrl.RecordCall(s, "FooMethod", FooMethodType, "1").SetArg(0, "blah")
461+
ctrl.RecordCall(s, "FooMethod", "1").SetArg(0, "blah")
467462
})
468463
ctrl.Call(s, "FooMethod", "1")
469464
}
@@ -473,7 +468,7 @@ func TestTimes0(t *testing.T) {
473468
defer ctrl.Finish()
474469

475470
s := new(Subject)
476-
ctrl.RecordCall(s, "FooMethod", FooMethodType, "arg").Times(0)
471+
ctrl.RecordCall(s, "FooMethod", "arg").Times(0)
477472
rep.assertFatal(func() {
478473
ctrl.Call(s, "FooMethod", "arg")
479474
})

gomock/mock_matcher/mock_matcher.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/mockgen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method)
425425
callArgs = ", _s..."
426426
}
427427
}
428-
g.p(`return _mr.mock.ctrl.RecordCall(_mr.mock, "%s", reflect.TypeOf((*%s)(nil).%s)%s)`, m.Name, mockType, m.Name, callArgs)
428+
g.p(`return _mr.mock.ctrl.RecordCallWithMethodType(_mr.mock, "%s", reflect.TypeOf((*%s)(nil).%s)%s)`, m.Name, mockType, m.Name, callArgs)
429429

430430
g.out()
431431
g.p("}")

mockgen/tests/unexported_method/bugreport_mock.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)