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

Commit 2ee3d4d

Browse files
author
Nate Brennand
committed
add tests for callSet Add & Remove
Adds tests to complement the changes introduced in #107. I validated that these tests would have caught the ordering bug fixed by PR #107 by reverting the change locally: revert diff: ```diff diff --git a/gomock/callset.go b/gomock/callset.go index 05d6fa2..c652f42 100644 --- a/gomock/callset.go +++ b/gomock/callset.go @@ -44,7 +44,11 @@ func (cs callSet) Remove(call *Call) { for i, c := range sl { if c == call { // maintain order for remaining calls - methodMap[call.method] = append(sl[:i], sl[i+1:]...) + // methodMap[call.method] = append(sl[:i], sl[i+1:]...) + if len(sl) > 1 { + sl[i] = sl[len(sl)-1] + } + methodMap[call.method] = sl[:len(sl)-1] break } } ``` test output: ``` === RUN TestCallSetAdd --- PASS: TestCallSetAdd (0.00s) === RUN TestCallSetRemove --- FAIL: TestCallSetRemove (0.00s) callset_test.go:61: found call 1 after call 9 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 2 after call 8 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 7 after call 8 callset_test.go:61: found call 3 after call 7 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 7 after call 8 callset_test.go:61: found call 6 after call 7 callset_test.go:61: found call 4 after call 6 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 7 after call 8 callset_test.go:61: found call 6 after call 7 callset_test.go:61: found call 5 after call 6 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 7 after call 8 callset_test.go:61: found call 6 after call 7 callset_test.go:61: found call 8 after call 9 callset_test.go:61: found call 7 after call 8 callset_test.go:61: found call 8 after call 9 ```
1 parent cd1f5ca commit 2ee3d4d

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

gomock/callset_test.go

Lines changed: 71 additions & 0 deletions
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)