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

Commit 69521b3

Browse files
authored
Merge pull request #165 from golang/variadic
Fix variadic argument matching
2 parents 960a6ff + 0bea072 commit 69521b3

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

gomock/call.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,14 @@ func (c *Call) matches(args []interface{}) error {
339339
// The last arg has a possibility of a variadic argument, so let it branch
340340

341341
// sample: Foo(a int, b int, c ...int)
342-
if len(c.args) == len(args) {
342+
if i < len(c.args) && i < len(args) {
343343
if m.Matches(args[i]) {
344344
// Got Foo(a, b, c) want Foo(matcherA, matcherB, gomock.Any())
345345
// Got Foo(a, b, c) want Foo(matcherA, matcherB, someSliceMatcher)
346346
// Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC)
347347
// Got Foo(a, b) want Foo(matcherA, matcherB)
348348
// Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD)
349-
break
349+
continue
350350
}
351351
}
352352

sample/user_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ func TestVariadicFunction(t *testing.T) {
6868
defer ctrl.Finish()
6969

7070
mockIndex := mock_user.NewMockIndex(ctrl)
71-
mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3).Do(func(format string, nums ...int) {
71+
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
7272
sum := 0
7373
for _, value := range nums {
7474
sum += value
7575
}
76-
if sum != 7 {
76+
if sum != 26 {
7777
t.Errorf("Expected 7, got %d", sum)
7878
}
7979
})
@@ -82,7 +82,7 @@ func TestVariadicFunction(t *testing.T) {
8282
for _, value := range nums {
8383
sum += value
8484
}
85-
if sum != 7 {
85+
if sum != 10 {
8686
t.Errorf("Expected 7, got %d", sum)
8787
}
8888
})
@@ -114,8 +114,8 @@ func TestVariadicFunction(t *testing.T) {
114114
}
115115
})
116116

117-
mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
118-
mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
117+
mockIndex.Ellip("%d", 1, 2, 3, 4) // Match second matcher.
118+
mockIndex.Ellip("%d", 5, 6, 7, 8) // Match first matcher.
119119
mockIndex.Ellip("%d", 0)
120120
mockIndex.Ellip("%d")
121121
mockIndex.Ellip("%d")

0 commit comments

Comments
 (0)