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

Commit 0f6dc21

Browse files
author
Guido Arnau
authored
format variadic arguments with GotFormatter (#434)
Matchers implementing the GotFormatter interface are no longer ignored. Match failures are formatted correctly. Fixes: #432
1 parent 5b2ea10 commit 0f6dc21

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

gomock/call.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,9 @@ func (c *Call) matches(args []interface{}) error {
304304

305305
for i, m := range c.args {
306306
if !m.Matches(args[i]) {
307-
got := fmt.Sprintf("%v", args[i])
308-
if gs, ok := m.(GotFormatter); ok {
309-
got = gs.Got(args[i])
310-
}
311-
312307
return fmt.Errorf(
313308
"expected call at %s doesn't match the argument at index %d.\nGot: %v\nWant: %v",
314-
c.origin, i, got, m,
309+
c.origin, i, formatGottenArg(m, args[i]), m,
315310
)
316311
}
317312
}
@@ -334,7 +329,7 @@ func (c *Call) matches(args []interface{}) error {
334329
// Non-variadic args
335330
if !m.Matches(args[i]) {
336331
return fmt.Errorf("expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
337-
c.origin, strconv.Itoa(i), args[i], m)
332+
c.origin, strconv.Itoa(i), formatGottenArg(m, args[i]), m)
338333
}
339334
continue
340335
}
@@ -376,9 +371,9 @@ func (c *Call) matches(args []interface{}) error {
376371
// Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD, matcherE)
377372
// Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, matcherC, matcherD)
378373
// Got Foo(a, b, c) want Foo(matcherA, matcherB)
379-
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
380-
c.origin, strconv.Itoa(i), args[i:], c.args[i])
381374

375+
return fmt.Errorf("expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
376+
c.origin, strconv.Itoa(i), formatGottenArg(m, args[i:]), c.args[i])
382377
}
383378
}
384379

@@ -428,3 +423,11 @@ func setSlice(arg interface{}, v reflect.Value) {
428423
func (c *Call) addAction(action func([]interface{}) []interface{}) {
429424
c.actions = append(c.actions, action)
430425
}
426+
427+
func formatGottenArg(m Matcher, arg interface{}) string {
428+
got := fmt.Sprintf("%v", arg)
429+
if gs, ok := m.(GotFormatter); ok {
430+
got = gs.Got(arg)
431+
}
432+
return got
433+
}

gomock/controller_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,55 @@ func TestVariadicMatchingWithSlice(t *testing.T) {
749749
}
750750
}
751751

752+
func TestVariadicArgumentsGotFormatter(t *testing.T) {
753+
rep, ctrl := createFixtures(t)
754+
defer rep.recoverUnexpectedFatal()
755+
756+
s := new(Subject)
757+
ctrl.RecordCall(
758+
s,
759+
"VariadicMethod",
760+
gomock.GotFormatterAdapter(
761+
gomock.GotFormatterFunc(func(i interface{}) string {
762+
return fmt.Sprintf("test{%v}", i)
763+
}),
764+
gomock.Eq(0),
765+
),
766+
)
767+
768+
rep.assertFatal(func() {
769+
ctrl.Call(s, "VariadicMethod", 1)
770+
}, "expected call to", "doesn't match the argument at index 0",
771+
"Got: test{1}\nWant: is equal to 0")
772+
ctrl.Call(s, "VariadicMethod", 0)
773+
ctrl.Finish()
774+
}
775+
776+
func TestVariadicArgumentsGotFormatterTooManyArgsFailure(t *testing.T) {
777+
rep, ctrl := createFixtures(t)
778+
defer rep.recoverUnexpectedFatal()
779+
780+
s := new(Subject)
781+
ctrl.RecordCall(
782+
s,
783+
"VariadicMethod",
784+
0,
785+
gomock.GotFormatterAdapter(
786+
gomock.GotFormatterFunc(func(i interface{}) string {
787+
return fmt.Sprintf("test{%v}", i)
788+
}),
789+
gomock.Eq("1"),
790+
),
791+
)
792+
793+
rep.assertFatal(func() {
794+
ctrl.Call(s, "VariadicMethod", 0, "2", "3")
795+
}, "expected call to", "doesn't match the argument at index 1",
796+
"Got: test{[2 3]}\nWant: is equal to 1")
797+
ctrl.Call(s, "VariadicMethod", 0, "1")
798+
ctrl.Finish()
799+
}
800+
752801
func TestNoHelper(t *testing.T) {
753802
ctrlNoHelper := gomock.NewController(NewErrorReporter(t))
754803

0 commit comments

Comments
 (0)