Debian Codegraph @

gomock/callset_test.go

raw
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 (
18 "reflect"
19 "testing"
20 )
21
22 type receiverType struct{}
23
24 func (receiverType) Func() {}
25
26 func TestCallSetAdd(t *testing.T) {
27 method := "TestMethod"
28 var receiver interface{} = "TestReceiver"
29 cs := newCallSet()
30
31 numCalls := 10
32 for i := 0; i < numCalls; i++ {
33 cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func)))
34 }
35
36 call, err := cs.FindMatch(receiver, method, []interface{}{})
37 if err != nil {
38 t.Fatalf("FindMatch: %v", err)
39 }
40 if call == nil {
41 t.Fatalf("FindMatch: Got nil, want non-nil *Call")
42 }
43 }
44
45 func TestCallSetRemove(t *testing.T) {
46 method := "TestMethod"
47 var receiver interface{} = "TestReceiver"
48
49 cs := newCallSet()
50 ourCalls := []*Call{}
51
52 numCalls := 10
53 for i := 0; i < numCalls; i++ {
54 // NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls
55 generatedCall := &Call{receiver: receiver, method: method, numCalls: i}
56 cs.Add(generatedCall)
57 ourCalls = append(ourCalls, generatedCall)
58 }
59
60 // validateOrder validates that the calls in the array are ordered as they were added
61 validateOrder := func(calls []*Call) {
62 // lastNum tracks the last `numCalls` (call order) value seen
63 lastNum := -1
64 for _, c := range calls {
65 if lastNum >= c.numCalls {
66 t.Errorf("found call %d after call %d", c.numCalls, lastNum)
67 }
68 lastNum = c.numCalls
69 }
70 }
71
72 for _, c := range ourCalls {
73 validateOrder(cs.expected[callSetKey{receiver, method}])
74 cs.Remove(c)
75 }
76 }
77
78 func TestCallSetFindMatch(t *testing.T) {
79 t.Run("call is exhausted", func(t *testing.T) {
80 cs := callSet{}
81 var receiver interface{} = "TestReceiver"
82 method := "TestMethod"
83 args := []interface{}{}
84
85 c1 := newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func))
86 cs.exhausted = map[callSetKey][]*Call{
87 {receiver: receiver, fname: method}: {c1},
88 }
89
90 _, err := cs.FindMatch(receiver, method, args)
91 if err == nil {
92 t.Fatal("expected error, but was nil")
93 }
94
95 if err.Error() == "" {
96 t.Fatal("expected error to have message, but was empty")
97 }
98 })
99 }