summaryrefslogtreecommitdiff
path: root/vendor/github.com/golang/mock/gomock/callset.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/golang/mock/gomock/callset.go')
-rw-r--r--vendor/github.com/golang/mock/gomock/callset.go108
1 files changed, 70 insertions, 38 deletions
diff --git a/vendor/github.com/golang/mock/gomock/callset.go b/vendor/github.com/golang/mock/gomock/callset.go
index 1b7de4c..c44a8a5 100644
--- a/vendor/github.com/golang/mock/gomock/callset.go
+++ b/vendor/github.com/golang/mock/gomock/callset.go
@@ -14,63 +14,95 @@
package gomock
+import (
+ "bytes"
+ "fmt"
+)
+
// callSet represents a set of expected calls, indexed by receiver and method
// name.
-type callSet map[interface{}]map[string][]*Call
+type callSet struct {
+ // Calls that are still expected.
+ expected map[callSetKey][]*Call
+ // Calls that have been exhausted.
+ exhausted map[callSetKey][]*Call
+}
+
+// callSetKey is the key in the maps in callSet
+type callSetKey struct {
+ receiver interface{}
+ fname string
+}
+
+func newCallSet() *callSet {
+ return &callSet{make(map[callSetKey][]*Call), make(map[callSetKey][]*Call)}
+}
// Add adds a new expected call.
func (cs callSet) Add(call *Call) {
- methodMap, ok := cs[call.receiver]
- if !ok {
- methodMap = make(map[string][]*Call)
- cs[call.receiver] = methodMap
+ key := callSetKey{call.receiver, call.method}
+ m := cs.expected
+ if call.exhausted() {
+ m = cs.exhausted
}
- methodMap[call.method] = append(methodMap[call.method], call)
+ m[key] = append(m[key], call)
}
// Remove removes an expected call.
func (cs callSet) Remove(call *Call) {
- methodMap, ok := cs[call.receiver]
- if !ok {
- return
- }
- sl := methodMap[call.method]
- for i, c := range sl {
+ key := callSetKey{call.receiver, call.method}
+ calls := cs.expected[key]
+ for i, c := range calls {
if c == call {
- // quick removal; we don't need to maintain call order
- if len(sl) > 1 {
- sl[i] = sl[len(sl)-1]
- }
- methodMap[call.method] = sl[:len(sl)-1]
+ // maintain order for remaining calls
+ cs.expected[key] = append(calls[:i], calls[i+1:]...)
+ cs.exhausted[key] = append(cs.exhausted[key], call)
break
}
}
}
-// FindMatch searches for a matching call. Returns nil if no call matched.
-func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) *Call {
- methodMap, ok := cs[receiver]
- if !ok {
- return nil
- }
- calls, ok := methodMap[method]
- if !ok {
- return nil
- }
+// FindMatch searches for a matching call. Returns error with explanation message if no call matched.
+func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {
+ key := callSetKey{receiver, method}
- // Search through the unordered set of calls expected on a method on a
- // receiver.
- for _, call := range calls {
- // A call should not normally still be here if exhausted,
- // but it can happen if, for instance, .Times(0) was used.
- // Pretend the call doesn't match.
- if call.exhausted() {
- continue
+ // Search through the expected calls.
+ expected := cs.expected[key]
+ var callsErrors bytes.Buffer
+ for _, call := range expected {
+ err := call.matches(args)
+ if err != nil {
+ fmt.Fprintf(&callsErrors, "\n%v", err)
+ } else {
+ return call, nil
}
- if call.matches(args) {
- return call
+ }
+
+ // If we haven't found a match then search through the exhausted calls so we
+ // get useful error messages.
+ exhausted := cs.exhausted[key]
+ for _, call := range exhausted {
+ if err := call.matches(args); err != nil {
+ fmt.Fprintf(&callsErrors, "\n%v", err)
}
}
- return nil
+ if len(expected)+len(exhausted) == 0 {
+ fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method)
+ }
+
+ return nil, fmt.Errorf(callsErrors.String())
+}
+
+// Failures returns the calls that are not satisfied.
+func (cs callSet) Failures() []*Call {
+ failures := make([]*Call, 0, len(cs.expected))
+ for _, calls := range cs.expected {
+ for _, call := range calls {
+ if !call.satisfied() {
+ failures = append(failures, call)
+ }
+ }
+ }
+ return failures
}