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

Commit c7d0ee7

Browse files
authored
Merge pull request #108 from Clever/test-callset-remove-preserve-ordering
add tests for callSet Add & Remove
2 parents cd1f5ca + 6e23395 commit c7d0ee7

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

gomock/call.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (c *Call) Do(f interface{}) *Call {
7878
return c
7979
}
8080

81+
// Return declares the values to be returned by the mocked function call.
8182
func (c *Call) Return(rets ...interface{}) *Call {
8283
mt := c.methodType
8384
if len(rets) != mt.NumOut() {
@@ -112,6 +113,7 @@ func (c *Call) Return(rets ...interface{}) *Call {
112113
return c
113114
}
114115

116+
// Times declares the exact number of times a function call is expected to be executed.
115117
func (c *Call) Times(n int) *Call {
116118
c.minCalls, c.maxCalls = n, n
117119
return c
@@ -146,7 +148,7 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
146148
case reflect.Slice:
147149
// nothing to do
148150
default:
149-
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v",
151+
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v [%s]",
150152
n, at, c.origin)
151153
}
152154
c.setArgs[n] = reflect.ValueOf(value)
@@ -204,7 +206,7 @@ func (c *Call) matches(args []interface{}) error {
204206
}
205207
for i, m := range c.args {
206208
if !m.Matches(args[i]) {
207-
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v\n",
209+
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
208210
c.origin, strconv.Itoa(i), args[i], m)
209211
}
210212
}

gomock/callset_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2011 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gomock
16+
17+
import "testing"
18+
19+
func TestCallSetAdd(t *testing.T) {
20+
methodVal := "TestMethod"
21+
var receiverVal interface{} = "TestReceiver"
22+
cs := make(callSet)
23+
24+
numCalls := 10
25+
for i := 0; i < numCalls; i++ {
26+
cs.Add(&Call{receiver: receiverVal, method: methodVal})
27+
}
28+
29+
if len(cs) != 1 {
30+
t.Errorf("expected only one reciever in callSet")
31+
}
32+
if numActualMethods := len(cs[receiverVal]); numActualMethods != 1 {
33+
t.Errorf("expected only method on the reciever in callSet, found %d", numActualMethods)
34+
}
35+
if numActualCalls := len(cs[receiverVal][methodVal]); numActualCalls != numCalls {
36+
t.Errorf("expected all %d calls in callSet, found %d", numCalls, numActualCalls)
37+
}
38+
}
39+
40+
func TestCallSetRemove(t *testing.T) {
41+
methodVal := "TestMethod"
42+
var receiverVal interface{} = "TestReceiver"
43+
44+
cs := make(callSet)
45+
ourCalls := []*Call{}
46+
47+
numCalls := 10
48+
for i := 0; i < numCalls; i++ {
49+
// NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls
50+
generatedCall := &Call{receiver: receiverVal, method: methodVal, numCalls: i}
51+
cs.Add(generatedCall)
52+
ourCalls = append(ourCalls, generatedCall)
53+
}
54+
55+
// validateOrder validates that the calls in the array are ordered as they were added
56+
validateOrder := func(calls []*Call) {
57+
// lastNum tracks the last `numCalls` (call order) value seen
58+
lastNum := -1
59+
for _, c := range calls {
60+
if lastNum >= c.numCalls {
61+
t.Errorf("found call %d after call %d", c.numCalls, lastNum)
62+
}
63+
lastNum = c.numCalls
64+
}
65+
}
66+
67+
for _, c := range ourCalls {
68+
validateOrder(cs[receiverVal][methodVal])
69+
cs.Remove(c)
70+
}
71+
}

0 commit comments

Comments
 (0)