diff --git a/gomock/call.go b/gomock/call.go index b8a06ac..fdd7086 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -463,7 +463,7 @@ func (c *Call) addAction(action func([]any) []any) { } func formatGottenArg(m Matcher, arg any) string { - got := fmt.Sprintf("%v (%T)", arg, arg) + got := fmt.Sprintf("%#v (%T)", arg, arg) if gs, ok := m.(GotFormatter); ok { got = gs.Got(arg) } diff --git a/gomock/controller_test.go b/gomock/controller_test.go index 0c2e297..80a85e0 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -152,6 +152,7 @@ func (s *Subject) VariadicMethod(arg int, vararg ...string) {} type TestStruct struct { Number int Message string + Slice []int } func (s *Subject) ActOnTestStructMethod(arg TestStruct, arg1 int) int { @@ -282,20 +283,26 @@ func TestUnexpectedArgValue_FirstArg(t *testing.T) { defer reporter.recoverUnexpectedFatal() subject := new(Subject) - expectedArg0 := TestStruct{Number: 123, Message: "hello %s"} + expectedArg0 := TestStruct{Number: 123, Message: "hello %s", Slice: []int{1, 2}} ctrl.RecordCall(subject, "ActOnTestStructMethod", expectedArg0, 15) reporter.assertFatal(func() { // the method argument (of TestStruct type) has 1 unexpected value (for the Message field) - ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "no message"}, 15) + ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "no message", Slice: []int{1, 2}}, 15) }, "Unexpected call to", "doesn't match the argument at index 0", - "Got: {123 no message} (gomock_test.TestStruct)\nWant: is equal to {123 hello %s} (gomock_test.TestStruct)") + "Got: gomock_test.TestStruct{Number:123, Message:\"no message\", Slice:[]int{1, 2}} (gomock_test.TestStruct)\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}} (gomock_test.TestStruct)") reporter.assertFatal(func() { - // the method argument (of TestStruct type) has 2 unexpected values (for both fields) - ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 11, Message: "no message"}, 15) + // the method argument (of TestStruct type) has 3 unexpected values (for all fields) + ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 11, Message: "no message", Slice: []int{}}, 15) }, "Unexpected call to", "doesn't match the argument at index 0", - "Got: {11 no message} (gomock_test.TestStruct)\nWant: is equal to {123 hello %s} (gomock_test.TestStruct)") + "Got: gomock_test.TestStruct{Number:11, Message:\"no message\", Slice:[]int{}} (gomock_test.TestStruct)\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}} (gomock_test.TestStruct)") + + reporter.assertFatal(func() { + // the method argument (of TestStruct type) has 1 unexpected values (for slice field) + ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "hello %s", Slice: nil}, 15) + }, "Unexpected call to", "doesn't match the argument at index 0", + "Got: gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int(nil)} (gomock_test.TestStruct)\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}} (gomock_test.TestStruct)") reporter.assertFatal(func() { // The expected call wasn't made. @@ -821,7 +828,7 @@ func TestVariadicArgumentsGotFormatterTooManyArgsFailure(t *testing.T) { 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") + "Got: test{[2 3]}\nWant: is equal to \"1\" (string)") ctrl.Call(s, "VariadicMethod", 0, "1") ctrl.Finish() } diff --git a/gomock/matchers.go b/gomock/matchers.go index bac4623..66e404f 100644 --- a/gomock/matchers.go +++ b/gomock/matchers.go @@ -132,7 +132,7 @@ func (e eqMatcher) Matches(x any) bool { } func (e eqMatcher) String() string { - return fmt.Sprintf("is equal to %v (%T)", e.x, e.x) + return fmt.Sprintf("is equal to %#v (%T)", e.x, e.x) } type nilMatcher struct{}