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

Commit e303461

Browse files
authored
add type information to error messages (#559)
- Adds type information to the default got formatter. - Adds type to default want formatter. This happens to be gomock.Eq. Adding more information for debugging seems like a good idea in this case as well. Fixes: #190
1 parent 82ce4a7 commit e303461

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

gomock/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (c *Call) addAction(action func([]interface{}) []interface{}) {
437437
}
438438

439439
func formatGottenArg(m Matcher, arg interface{}) string {
440-
got := fmt.Sprintf("%v", arg)
440+
got := fmt.Sprintf("%v (%T)", arg, arg)
441441
if gs, ok := m.(GotFormatter); ok {
442442
got = gs.Got(arg)
443443
}

gomock/controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,13 @@ func TestUnexpectedArgValue_FirstArg(t *testing.T) {
288288
// the method argument (of TestStruct type) has 1 unexpected value (for the Message field)
289289
ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "no message"}, 15)
290290
}, "Unexpected call to", "doesn't match the argument at index 0",
291-
"Got: {123 no message}\nWant: is equal to {123 hello}")
291+
"Got: {123 no message} (gomock_test.TestStruct)\nWant: is equal to {123 hello} (gomock_test.TestStruct)")
292292

293293
reporter.assertFatal(func() {
294294
// the method argument (of TestStruct type) has 2 unexpected values (for both fields)
295295
ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 11, Message: "no message"}, 15)
296296
}, "Unexpected call to", "doesn't match the argument at index 0",
297-
"Got: {11 no message}\nWant: is equal to {123 hello}")
297+
"Got: {11 no message} (gomock_test.TestStruct)\nWant: is equal to {123 hello} (gomock_test.TestStruct)")
298298

299299
reporter.assertFatal(func() {
300300
// The expected call wasn't made.
@@ -313,7 +313,7 @@ func TestUnexpectedArgValue_SecondArg(t *testing.T) {
313313
reporter.assertFatal(func() {
314314
ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "hello"}, 3)
315315
}, "Unexpected call to", "doesn't match the argument at index 1",
316-
"Got: 3\nWant: is equal to 15")
316+
"Got: 3 (int)\nWant: is equal to 15 (int)")
317317

318318
reporter.assertFatal(func() {
319319
// The expected call wasn't made.
@@ -340,7 +340,7 @@ func TestUnexpectedArgValue_WantFormatter(t *testing.T) {
340340
reporter.assertFatal(func() {
341341
ctrl.Call(subject, "ActOnTestStructMethod", TestStruct{Number: 123, Message: "hello"}, 3)
342342
}, "Unexpected call to", "doesn't match the argument at index 1",
343-
"Got: 3\nWant: is equal to fifteen")
343+
"Got: 3 (int)\nWant: is equal to fifteen")
344344

345345
reporter.assertFatal(func() {
346346
// The expected call wasn't made.
@@ -711,7 +711,7 @@ func TestVariadicNoMatch(t *testing.T) {
711711
rep.assertFatal(func() {
712712
ctrl.Call(s, "VariadicMethod", 1)
713713
}, "expected call at", "doesn't match the argument at index 0",
714-
"Got: 1\nWant: is equal to 0")
714+
"Got: 1 (int)\nWant: is equal to 0 (int)")
715715
ctrl.Call(s, "VariadicMethod", 0)
716716
ctrl.Finish()
717717
}

gomock/matchers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (e eqMatcher) Matches(x interface{}) bool {
120120
}
121121

122122
func (e eqMatcher) String() string {
123-
return fmt.Sprintf("is equal to %v", e.x)
123+
return fmt.Sprintf("is equal to %v (%T)", e.x, e.x)
124124
}
125125

126126
type nilMatcher struct{}

0 commit comments

Comments
 (0)