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

Commit f159d62

Browse files
author
hori-ryota
committed
Fix variadic mock for gomock.Any() . fix #68
1 parent 1df903b commit f159d62

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

gomock/call.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,37 @@ func (c *Call) String() string {
198198
// Tests if the given call matches the expected call.
199199
// If yes, returns nil. If no, returns error with message explaining why it does not match.
200200
func (c *Call) matches(args []interface{}) error {
201-
if len(args) != len(c.args) {
202-
return fmt.Errorf("Expected call at %s has the wrong number of arguments. Got: %s, want: %s",
203-
c.origin, strconv.Itoa(len(args)), strconv.Itoa(len(c.args)))
201+
if c.methodType.IsVariadic() {
202+
if len(c.args) < c.methodType.NumIn()-1 {
203+
return fmt.Errorf("matcher count is not enough at %s. Got: %d, want: greater than or equal to %d",
204+
c.origin, len(c.args), c.methodType.NumIn()-1)
205+
}
206+
if len(c.args) != c.methodType.NumIn() && len(args) != len(c.args) {
207+
return fmt.Errorf("Expected call at %s has the wrong number of arguments. Got: %d, want: %d",
208+
c.origin, len(args), len(c.args))
209+
}
210+
if len(args) < len(c.args)-1 {
211+
return fmt.Errorf("Expected call at %s has the wrong number of arguments. Got: %d, want: greater than or equal to %d",
212+
c.origin, len(args), len(c.args)-1)
213+
}
214+
} else {
215+
if len(args) != len(c.args) {
216+
return fmt.Errorf("Expected call at %s has the wrong number of arguments. Got: %d, want %d",
217+
c.origin, len(args), len(c.args))
218+
}
219+
}
220+
221+
if len(args) < len(c.args) {
222+
args = append(args, nil)
204223
}
205224
for i, m := range c.args {
225+
if i == len(c.args) && c.methodType.IsVariadic() {
226+
if !c.args[i].Matches(args[i:]) && !(len(c.args) == len(args) && c.args[i].Matches(args[i])) {
227+
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v\n",
228+
c.origin, strconv.Itoa(i), args[i:], c.args[i])
229+
}
230+
break
231+
}
206232
if !m.Matches(args[i]) {
207233
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v\n",
208234
c.origin, strconv.Itoa(i), args[i], m)

sample/user_test.go

Lines changed: 41 additions & 2 deletions
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)