Path: blob/main/vendor/github.com/golang/mock/gomock/callset.go
2880 views
// Copyright 2011 Google Inc.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package gomock1516import (17"bytes"18"errors"19"fmt"20)2122// callSet represents a set of expected calls, indexed by receiver and method23// name.24type callSet struct {25// Calls that are still expected.26expected map[callSetKey][]*Call27// Calls that have been exhausted.28exhausted map[callSetKey][]*Call29}3031// callSetKey is the key in the maps in callSet32type callSetKey struct {33receiver interface{}34fname string35}3637func newCallSet() *callSet {38return &callSet{make(map[callSetKey][]*Call), make(map[callSetKey][]*Call)}39}4041// Add adds a new expected call.42func (cs callSet) Add(call *Call) {43key := callSetKey{call.receiver, call.method}44m := cs.expected45if call.exhausted() {46m = cs.exhausted47}48m[key] = append(m[key], call)49}5051// Remove removes an expected call.52func (cs callSet) Remove(call *Call) {53key := callSetKey{call.receiver, call.method}54calls := cs.expected[key]55for i, c := range calls {56if c == call {57// maintain order for remaining calls58cs.expected[key] = append(calls[:i], calls[i+1:]...)59cs.exhausted[key] = append(cs.exhausted[key], call)60break61}62}63}6465// FindMatch searches for a matching call. Returns error with explanation message if no call matched.66func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {67key := callSetKey{receiver, method}6869// Search through the expected calls.70expected := cs.expected[key]71var callsErrors bytes.Buffer72for _, call := range expected {73err := call.matches(args)74if err != nil {75_, _ = fmt.Fprintf(&callsErrors, "\n%v", err)76} else {77return call, nil78}79}8081// If we haven't found a match then search through the exhausted calls so we82// get useful error messages.83exhausted := cs.exhausted[key]84for _, call := range exhausted {85if err := call.matches(args); err != nil {86_, _ = fmt.Fprintf(&callsErrors, "\n%v", err)87continue88}89_, _ = fmt.Fprintf(90&callsErrors, "all expected calls for method %q have been exhausted", method,91)92}9394if len(expected)+len(exhausted) == 0 {95_, _ = fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method)96}9798return nil, errors.New(callsErrors.String())99}100101// Failures returns the calls that are not satisfied.102func (cs callSet) Failures() []*Call {103failures := make([]*Call, 0, len(cs.expected))104for _, calls := range cs.expected {105for _, call := range calls {106if !call.satisfied() {107failures = append(failures, call)108}109}110}111return failures112}113114115