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

Commit 4887ba1

Browse files
authored
Merge pull request #101 from hori-ryota/feature/fix-variadic-mock
Fix variadic mock for gomock.Any() . fix #68
2 parents 2b473a1 + df8ec80 commit 4887ba1

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

gomock/call.go

+62-8
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,68 @@ func (c *Call) String() string {
212212
// Tests if the given call matches the expected call.
213213
// If yes, returns nil. If no, returns error with message explaining why it does not match.
214214
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.\nGot: %v\nWant: %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.\nGot: %v\nWant: %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.\nGot: %v\nWant: %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.\nGot: %v\nWant: %v",
275+
c.origin, strconv.Itoa(i), args[i], m)
276+
}
223277
}
224278
}
225279

sample/user_test.go

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

7070
mockIndex := mock_user.NewMockIndex(ctrl)
71-
m := mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3)
72-
m.Do(func(format string, nums ...int) {
71+
mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3).Do(func(format string, nums ...int) {
7372
sum := 0
7473
for _, value := range nums {
7574
sum += value
@@ -78,8 +77,48 @@ func TestVariadicFunction(t *testing.T) {
7877
t.Errorf("Expected 7, got %d", sum)
7978
}
8079
})
80+
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
81+
sum := 0
82+
for _, value := range nums {
83+
sum += value
84+
}
85+
if sum != 7 {
86+
t.Errorf("Expected 7, got %d", sum)
87+
}
88+
})
89+
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
90+
sum := 0
91+
for _, value := range nums {
92+
sum += value
93+
}
94+
if sum != 0 {
95+
t.Errorf("Expected 0, got %d", sum)
96+
}
97+
})
98+
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
99+
sum := 0
100+
for _, value := range nums {
101+
sum += value
102+
}
103+
if sum != 0 {
104+
t.Errorf("Expected 0, got %d", sum)
105+
}
106+
})
107+
mockIndex.EXPECT().Ellip("%d").Do(func(format string, nums ...int) {
108+
sum := 0
109+
for _, value := range nums {
110+
sum += value
111+
}
112+
if sum != 0 {
113+
t.Errorf("Expected 0, got %d", sum)
114+
}
115+
})
81116

82117
mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
118+
mockIndex.Ellip("%d", 0, 1, 1, 2, 3)
119+
mockIndex.Ellip("%d", 0)
120+
mockIndex.Ellip("%d")
121+
mockIndex.Ellip("%d")
83122
}
84123

85124
func TestGrabPointer(t *testing.T) {

0 commit comments

Comments
 (0)