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

Format variadic arguments with GotFormatter #434

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Format variadic arguments with GotFormatter
  • Loading branch information
guiarn committed May 19, 2020
commit 3bc6cb03d89418b570a5626382612b7bf254f4cf
21 changes: 12 additions & 9 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,9 @@ func (c *Call) matches(args []interface{}) error {

for i, m := range c.args {
if !m.Matches(args[i]) {
got := fmt.Sprintf("%v", args[i])
if gs, ok := m.(GotFormatter); ok {
got = gs.Got(args[i])
}

return fmt.Errorf(
"expected call at %s doesn't match the argument at index %d.\nGot: %v\nWant: %v",
c.origin, i, got, m,
c.origin, i, formatGottenArg(m, args[i]), m,
)
}
}
Expand All @@ -331,7 +326,7 @@ func (c *Call) matches(args []interface{}) error {
// Non-variadic args
if !m.Matches(args[i]) {
return fmt.Errorf("expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
c.origin, strconv.Itoa(i), args[i], m)
c.origin, strconv.Itoa(i), formatGottenArg(m, args[i]), m)
}
continue
}
Expand Down Expand Up @@ -373,9 +368,9 @@ func (c *Call) matches(args []interface{}) error {
// Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD, matcherE)
// Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, matcherC, matcherD)
// Got Foo(a, b, c) want Foo(matcherA, matcherB)
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
c.origin, strconv.Itoa(i), args[i:], c.args[i])

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

Expand Down Expand Up @@ -425,3 +420,11 @@ func setSlice(arg interface{}, v reflect.Value) {
func (c *Call) addAction(action func([]interface{}) []interface{}) {
c.actions = append(c.actions, action)
}

func formatGottenArg(m Matcher, arg interface{}) string {
got := fmt.Sprintf("%v", arg)
if gs, ok := m.(GotFormatter); ok {
got = gs.Got(arg)
}
return got
}
49 changes: 49 additions & 0 deletions gomock/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,55 @@ func TestVariadicMatchingWithSlice(t *testing.T) {
}
}

func TestVariadicArgumentsGotFormatter(t *testing.T) {
rep, ctrl := createFixtures(t)
defer rep.recoverUnexpectedFatal()

s := new(Subject)
ctrl.RecordCall(
s,
"VariadicMethod",
gomock.GotFormatterAdapter(
gomock.GotFormatterFunc(func(i interface{}) string {
return fmt.Sprintf("test{%v}", i)
}),
gomock.Eq(0),
),
)

rep.assertFatal(func() {
ctrl.Call(s, "VariadicMethod", 1)
}, "expected call to", "doesn't match the argument at index 0",
"Got: test{1}\nWant: is equal to 0")
ctrl.Call(s, "VariadicMethod", 0)
ctrl.Finish()
}

func TestVariadicArgumentsGotFormatterTooManyArgsFailure(t *testing.T) {
rep, ctrl := createFixtures(t)
defer rep.recoverUnexpectedFatal()

s := new(Subject)
ctrl.RecordCall(
s,
"VariadicMethod",
0,
gomock.GotFormatterAdapter(
gomock.GotFormatterFunc(func(i interface{}) string {
return fmt.Sprintf("test{%v}", i)
}),
gomock.Eq("1"),
),
)

rep.assertFatal(func() {
ctrl.Call(s, "VariadicMethod", 0, "2", "3")
}, "expected call to", "doesn't match the argument at index 1",
"Got: test{[2 3]}\nWant: is equal to 1")
ctrl.Call(s, "VariadicMethod", 0, "1")
ctrl.Finish()
}

func TestNoHelper(t *testing.T) {
ctrlNoHelper := gomock.NewController(NewErrorReporter(t))

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy