Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wrong print msg when comparing initialized and uninitialized slice #79

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
21 changes: 14 additions & 7 deletions gomock/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion gomock/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down