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

Commit 5234544

Browse files
author
tocrafty
committed
When searches for a matching call, use most recently expected call.
1 parent 22bbf0d commit 5234544

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

gomock/callset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac
6969
// Search through the expected calls.
7070
expected := cs.expected[key]
7171
var callsErrors bytes.Buffer
72-
for _, call := range expected {
73-
err := call.matches(args)
72+
for i := len(expected) - 1; i >= 0; i-- {
73+
err := expected[i].matches(args)
7474
if err != nil {
7575
fmt.Fprintf(&callsErrors, "\n%v", err)
7676
} else {
77-
return call, nil
77+
return expected[i], nil
7878
}
7979
}
8080

sample/user_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func TestRemember(t *testing.T) {
3535

3636
user.Remember(mockIndex, []string{"a", "b"}, []interface{}{1, 2})
3737
// Check the ConcreteRet calls.
38-
if c := mockIndex.ConcreteRet(); c != boolc {
39-
t.Errorf("ConcreteRet: got %v, want %v", c, boolc)
40-
}
4138
if c := mockIndex.ConcreteRet(); c != nil {
4239
t.Errorf("ConcreteRet: got %v, want nil", c)
4340
}
41+
if c := mockIndex.ConcreteRet(); c != boolc {
42+
t.Errorf("ConcreteRet: got %v, want %v", c, boolc)
43+
}
4444

4545
// Try one with an action.
4646
calledString := ""
@@ -68,22 +68,22 @@ func TestVariadicFunction(t *testing.T) {
6868
defer ctrl.Finish()
6969

7070
mockIndex := mock_user.NewMockIndex(ctrl)
71-
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
71+
mockIndex.EXPECT().Ellip("%d").Do(func(format string, nums ...int) {
7272
sum := 0
7373
for _, value := range nums {
7474
sum += value
7575
}
76-
if sum != 26 {
77-
t.Errorf("Expected 7, got %d", sum)
76+
if sum != 0 {
77+
t.Errorf("Expected 0, got %d", sum)
7878
}
7979
})
8080
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
8181
sum := 0
8282
for _, value := range nums {
8383
sum += value
8484
}
85-
if sum != 10 {
86-
t.Errorf("Expected 7, got %d", sum)
85+
if sum != 0 {
86+
t.Errorf("Expected 0, got %d", sum)
8787
}
8888
})
8989
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
@@ -100,22 +100,22 @@ func TestVariadicFunction(t *testing.T) {
100100
for _, value := range nums {
101101
sum += value
102102
}
103-
if sum != 0 {
104-
t.Errorf("Expected 0, got %d", sum)
103+
if sum != 10 {
104+
t.Errorf("Expected 7, got %d", sum)
105105
}
106106
})
107-
mockIndex.EXPECT().Ellip("%d").Do(func(format string, nums ...int) {
107+
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
108108
sum := 0
109109
for _, value := range nums {
110110
sum += value
111111
}
112-
if sum != 0 {
113-
t.Errorf("Expected 0, got %d", sum)
112+
if sum != 26 {
113+
t.Errorf("Expected 7, got %d", sum)
114114
}
115115
})
116116

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

0 commit comments

Comments
 (0)