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

Commit

Permalink
Merge pull request #165 from golang/variadic
Browse files Browse the repository at this point in the history
Fix variadic argument matching
  • Loading branch information
balshetzer authored Apr 3, 2018
2 parents 960a6ff + 0bea072 commit 69521b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ func (c *Call) matches(args []interface{}) error {
// The last arg has a possibility of a variadic argument, so let it branch

// sample: Foo(a int, b int, c ...int)
if len(c.args) == len(args) {
if i < len(c.args) && i < len(args) {
if m.Matches(args[i]) {
// Got Foo(a, b, c) want Foo(matcherA, matcherB, gomock.Any())
// Got Foo(a, b, c) want Foo(matcherA, matcherB, someSliceMatcher)
// Got Foo(a, b, c) want Foo(matcherA, matcherB, matcherC)
// Got Foo(a, b) want Foo(matcherA, matcherB)
// Got Foo(a, b, c, d) want Foo(matcherA, matcherB, matcherC, matcherD)
break
continue
}
}

Expand Down
10 changes: 5 additions & 5 deletions sample/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func TestVariadicFunction(t *testing.T) {
defer ctrl.Finish()

mockIndex := mock_user.NewMockIndex(ctrl)
mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3).Do(func(format string, nums ...int) {
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 7 {
if sum != 26 {
t.Errorf("Expected 7, got %d", sum)
}
})
Expand All @@ -82,7 +82,7 @@ func TestVariadicFunction(t *testing.T) {
for _, value := range nums {
sum += value
}
if sum != 7 {
if sum != 10 {
t.Errorf("Expected 7, got %d", sum)
}
})
Expand Down Expand Up @@ -114,8 +114,8 @@ func TestVariadicFunction(t *testing.T) {
}
})

mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
mockIndex.Ellip("%d", 1, 2, 3, 4) // Match second matcher.
mockIndex.Ellip("%d", 5, 6, 7, 8) // Match first matcher.
mockIndex.Ellip("%d", 0)
mockIndex.Ellip("%d")
mockIndex.Ellip("%d")
Expand Down

0 comments on commit 69521b3

Please sign in to comment.