@@ -20,6 +20,8 @@ import (
20
20
"strings"
21
21
"testing"
22
22
23
+ "github.com/google/go-cmp/cmp/cmpopts"
24
+
23
25
"go.uber.org/mock/gomock"
24
26
)
25
27
@@ -74,8 +76,11 @@ func (e *ErrorReporter) assertFatal(fn func(), expectedErrMsgs ...string) {
74
76
// check the last actualErrMsg, because the previous messages come from previous errors
75
77
actualErrMsg := e .log [len (e .log )- 1 ]
76
78
for _ , expectedErrMsg := range expectedErrMsgs {
77
- if ! strings .Contains (actualErrMsg , expectedErrMsg ) {
79
+ i := strings .Index (actualErrMsg , expectedErrMsg )
80
+ if i == - 1 {
78
81
e .t .Errorf ("Error message:\n got: %q\n want to contain: %q\n " , actualErrMsg , expectedErrMsg )
82
+ } else {
83
+ actualErrMsg = actualErrMsg [i + len (expectedErrMsg ):]
79
84
}
80
85
}
81
86
}
@@ -149,8 +154,9 @@ func (s *Subject) VariadicMethod(arg int, vararg ...string) {}
149
154
150
155
// A type purely for ActOnTestStructMethod
151
156
type TestStruct struct {
152
- Number int
153
- Message string
157
+ Number int
158
+ Message string
159
+ secretMessage string
154
160
}
155
161
156
162
func (s * Subject ) ActOnTestStructMethod (arg TestStruct , arg1 int ) int {
@@ -171,7 +177,9 @@ func createFixtures(t *testing.T) (reporter *ErrorReporter, ctrl *gomock.Control
171
177
// Controller. We use it to test that the mock considered tests
172
178
// successful or failed.
173
179
reporter = NewErrorReporter (t )
174
- ctrl = gomock .NewController (reporter )
180
+ ctrl = gomock .NewController (
181
+ reporter , gomock .WithCmpOpts (cmpopts .IgnoreUnexported (TestStruct {})),
182
+ )
175
183
return
176
184
}
177
185
@@ -298,13 +306,13 @@ func TestUnexpectedArgValue_FirstArg(t *testing.T) {
298
306
// the method argument (of TestStruct type) has 1 unexpected value (for the Message field)
299
307
ctrl .Call (subject , "ActOnTestStructMethod" , TestStruct {Number : 123 , Message : "no message" }, 15 )
300
308
}, "Unexpected call to" , "doesn't match the argument at index 0" ,
301
- "Got: {123 no message} ( gomock_test.TestStruct) \n Want: is equal to {123 hello %s} (gomock_test.TestStruct) " )
309
+ "Diff (-want +got):" , " gomock_test.TestStruct{" , "Number: 123" , "-" , "Message: \" hello %s\" ," , "+" , "Message: \" no message \" ," , "} " )
302
310
303
311
reporter .assertFatal (func () {
304
312
// the method argument (of TestStruct type) has 2 unexpected values (for both fields)
305
313
ctrl .Call (subject , "ActOnTestStructMethod" , TestStruct {Number : 11 , Message : "no message" }, 15 )
306
314
}, "Unexpected call to" , "doesn't match the argument at index 0" ,
307
- "Got: {11 no message} ( gomock_test.TestStruct) \n Want: is equal to { 123 hello %s} (gomock_test.TestStruct) " )
315
+ "Diff (-want +got):" , " gomock_test.TestStruct{" , "-" , "Number: 123," , "+" , "Number: 11," , "-" , "Message: \" hello %s\" ," , "+" , "Message: \" no message \" ," , "} " )
308
316
309
317
reporter .assertFatal (func () {
310
318
// The expected call wasn't made.
@@ -323,7 +331,7 @@ func TestUnexpectedArgValue_SecondArg(t *testing.T) {
323
331
reporter .assertFatal (func () {
324
332
ctrl .Call (subject , "ActOnTestStructMethod" , TestStruct {Number : 123 , Message : "hello" }, 3 )
325
333
}, "Unexpected call to" , "doesn't match the argument at index 1" ,
326
- "Got: 3 (int) \n Want: is equal to 15 (int )" )
334
+ "Diff (-want +got):" , "int(" , "-" , "15," , "+" , "3," , " )" )
327
335
328
336
reporter .assertFatal (func () {
329
337
// The expected call wasn't made.
@@ -742,8 +750,8 @@ func TestVariadicNoMatch(t *testing.T) {
742
750
ctrl .RecordCall (s , "VariadicMethod" , 0 )
743
751
rep .assertFatal (func () {
744
752
ctrl .Call (s , "VariadicMethod" , 1 )
745
- }, "expected call at" , "doesn't match the argument at index 0" ,
746
- "Got: 1 (int) \n Want: is equal to 0 (int )" )
753
+ }, "expected call at" , "doesn't match the argument at index 0: " ,
754
+ "Diff (-want +got):" , "int(" , "-" , "0," , "+" , "1," , " )" )
747
755
ctrl .Call (s , "VariadicMethod" , 0 )
748
756
}
749
757
0 commit comments