From c6389b3659f3f1bb9a78ab243f0e7c32d24045c3 Mon Sep 17 00:00:00 2001 From: Kanata Miyahana Date: Mon, 11 Sep 2023 00:15:25 +0900 Subject: [PATCH 1/5] change message formts for want and got --- gomock/call.go | 2 +- gomock/matchers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index b8a06ac..5923eb6 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", arg) if gs, ok := m.(GotFormatter); ok { got = gs.Got(arg) } diff --git a/gomock/matchers.go b/gomock/matchers.go index bac4623..35c904f 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", e.x) } type nilMatcher struct{} From a3168ec0c9859c2bc227d866ca5b57d3ad972408 Mon Sep 17 00:00:00 2001 From: Kanata Miyahana Date: Mon, 11 Sep 2023 00:15:32 +0900 Subject: [PATCH 2/5] add test --- gomock/controller_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/gomock/controller_test.go b/gomock/controller_test.go index 0c2e297..d7b2746 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}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") 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) + ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 11, Message: "no message", Slice: []int{1, 2}}, 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{1, 2}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") + + reporter.assertFatal(func() { + // the method argument (of TestStruct type) has 1 unexpected values (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)}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") reporter.assertFatal(func() { // The expected call wasn't made. From 02116f3df713d6daf7442fbd8d2eeaf7c3288729 Mon Sep 17 00:00:00 2001 From: Kanata Miyahana Date: Mon, 11 Sep 2023 00:21:24 +0900 Subject: [PATCH 3/5] update test case --- gomock/controller_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gomock/controller_test.go b/gomock/controller_test.go index d7b2746..b3140cd 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -293,13 +293,13 @@ func TestUnexpectedArgValue_FirstArg(t *testing.T) { "Got: gomock_test.TestStruct{Number:123, Message:\"no message\", Slice:[]int{1, 2}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") 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", Slice: []int{1, 2}}, 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: gomock_test.TestStruct{Number:11, Message:\"no message\", Slice:[]int{1, 2}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") + "Got: gomock_test.TestStruct{Number:11, Message:\"no message\", Slice:[]int{}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") reporter.assertFatal(func() { - // the method argument (of TestStruct type) has 1 unexpected values (slice field) + // 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)}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") From 0d68c12a3b00c1aa5e7c8e59394d8ef23057838f Mon Sep 17 00:00:00 2001 From: Kanata Miyahana Date: Sat, 30 Sep 2023 14:43:42 +0900 Subject: [PATCH 4/5] add type info for printing message --- gomock/call.go | 2 +- gomock/matchers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index 5923eb6..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", arg) + got := fmt.Sprintf("%#v (%T)", arg, arg) if gs, ok := m.(GotFormatter); ok { got = gs.Got(arg) } diff --git a/gomock/matchers.go b/gomock/matchers.go index 35c904f..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", e.x) + return fmt.Sprintf("is equal to %#v (%T)", e.x, e.x) } type nilMatcher struct{} From 73c1f4de1f651417c1cac471bd5a417d6cc7c57b Mon Sep 17 00:00:00 2001 From: Kanata Miyahana Date: Sat, 30 Sep 2023 14:43:45 +0900 Subject: [PATCH 5/5] fix tests --- gomock/controller_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gomock/controller_test.go b/gomock/controller_test.go index b3140cd..80a85e0 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -290,19 +290,19 @@ func TestUnexpectedArgValue_FirstArg(t *testing.T) { // the method argument (of TestStruct type) has 1 unexpected value (for the Message field) 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: gomock_test.TestStruct{Number:123, Message:\"no message\", Slice:[]int{1, 2}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") + "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 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: gomock_test.TestStruct{Number:11, Message:\"no message\", Slice:[]int{}}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") + "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)}\nWant: is equal to gomock_test.TestStruct{Number:123, Message:\"hello %s\", Slice:[]int{1, 2}}") + "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. @@ -828,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() }