@@ -212,14 +212,68 @@ func (c *Call) String() string {
212
212
// Tests if the given call matches the expected call.
213
213
// If yes, returns nil. If no, returns error with message explaining why it does not match.
214
214
func (c * Call ) matches (args []interface {}) error {
215
- if len (args ) != len (c .args ) {
216
- return fmt .Errorf ("Expected call at %s has the wrong number of arguments. Got: %s, want: %s" ,
217
- c .origin , strconv .Itoa (len (args )), strconv .Itoa (len (c .args )))
218
- }
219
- for i , m := range c .args {
220
- if ! m .Matches (args [i ]) {
221
- return fmt .Errorf ("Expected call at %s doesn't match the argument at index %s.\n Got: %v\n Want: %v" ,
222
- c .origin , strconv .Itoa (i ), args [i ], m )
215
+ if ! c .methodType .IsVariadic () {
216
+ if len (args ) != len (c .args ) {
217
+ return fmt .Errorf ("Expected call at %s has the wrong number of arguments. Got: %d, want: %d" ,
218
+ c .origin , len (args ), len (c .args ))
219
+ }
220
+
221
+ for i , m := range c .args {
222
+ if ! m .Matches (args [i ]) {
223
+ return fmt .Errorf ("Expected call at %s doesn't match the argument at index %s.\n Got: %v\n Want: %v" ,
224
+ c .origin , strconv .Itoa (i ), args [i ], m )
225
+ }
226
+ }
227
+ } else {
228
+ if len (c .args ) < c .methodType .NumIn ()- 1 {
229
+ return fmt .Errorf ("Expected call at %s has the wrong number of matchers. Got: %d, want: %d" ,
230
+ c .origin , len (c .args ), c .methodType .NumIn ()- 1 )
231
+ }
232
+ if len (c .args ) != c .methodType .NumIn () && len (args ) != len (c .args ) {
233
+ return fmt .Errorf ("Expected call at %s has the wrong number of arguments. Got: %d, want: %d" ,
234
+ c .origin , len (args ), len (c .args ))
235
+ }
236
+ if len (args ) < len (c .args )- 1 {
237
+ return fmt .Errorf ("Expected call at %s has the wrong number of arguments. Got: %d, want: greater than or equal to %d" ,
238
+ c .origin , len (args ), len (c .args )- 1 )
239
+ }
240
+
241
+ for i , m := range c .args {
242
+ if i == len (c .args )- 1 {
243
+ // The last arg has a possibility of a variadic argument, so let it branch
244
+
245
+ // sample: Foo(a int, b int, c ...int)
246
+
247
+ if len (c .args ) == len (args ) {
248
+ if c .args [i ].Matches (args [i ]) {
249
+ // Got Foo(a, b, c) want Foo(matcherA, matcherB, gomock.Any())
250
+ // Got Foo(a, b, c) want Foo(matcherA, matcherB, someSliceMatcher)
251
+ // Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC)
252
+ // Got Foo(a, b) want Foo(matcherA, matcherB)
253
+ // Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD)
254
+ break
255
+ }
256
+ } else if c .args [i ].Matches (args [i :]) {
257
+ // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, gomock.Any())
258
+ // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, someSliceMatcher)
259
+ // Got Foo(a, b) want Foo(matcherA, matcherB, gomock.Any())
260
+ // Got Foo(a, b) want Foo(matcherA, matcherB, someEmptySliceMatcher)
261
+ break
262
+ }
263
+ // Wrong number of matchers or not match. Fail.
264
+ // Got Foo(a, b) want Foo(matcherA, matcherB, matcherC, matcherD)
265
+ // Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC, matcherD)
266
+ // Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD, matcherE)
267
+ // Got Foo(a, b, c, d, e) want Foo(matcherA, matcherB, matcherC, matcherD)
268
+ // Got Foo(a, b, c) want Foo(matcherA, matcherB)
269
+ return fmt .Errorf ("Expected call at %s doesn't match the argument at index %s.\n Got: %v\n Want: %v" ,
270
+ c .origin , strconv .Itoa (i ), args [i :], c .args [i ])
271
+ }
272
+
273
+ if ! m .Matches (args [i ]) {
274
+ return fmt .Errorf ("Expected call at %s doesn't match the argument at index %s.\n Got: %v\n Want: %v" ,
275
+ c .origin , strconv .Itoa (i ), args [i ], m )
276
+ }
223
277
}
224
278
}
225
279
0 commit comments