| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 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 | "bytes" |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame] | 19 | "errors" |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 20 | "fmt" |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 21 | "sync" |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // callSet represents a set of expected calls, indexed by receiver and method |
| 25 | // name. |
| 26 | type callSet struct { |
| 27 | // Calls that are still expected. |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 28 | expected map[callSetKey][]*Call |
| 29 | expectedMu *sync.Mutex |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 30 | // Calls that have been exhausted. |
| 31 | exhausted map[callSetKey][]*Call |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 32 | // when set to true, existing call expectations are overridden when new call expectations are made |
| 33 | allowOverride bool |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // callSetKey is the key in the maps in callSet |
| 37 | type callSetKey struct { |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 38 | receiver any |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 39 | fname string |
| 40 | } |
| 41 | |
| 42 | func newCallSet() *callSet { |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 43 | return &callSet{ |
| 44 | expected: make(map[callSetKey][]*Call), |
| 45 | expectedMu: &sync.Mutex{}, |
| 46 | exhausted: make(map[callSetKey][]*Call), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func newOverridableCallSet() *callSet { |
| 51 | return &callSet{ |
| 52 | expected: make(map[callSetKey][]*Call), |
| 53 | expectedMu: &sync.Mutex{}, |
| 54 | exhausted: make(map[callSetKey][]*Call), |
| 55 | allowOverride: true, |
| 56 | } |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Add adds a new expected call. |
| 60 | func (cs callSet) Add(call *Call) { |
| 61 | key := callSetKey{call.receiver, call.method} |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 62 | |
| 63 | cs.expectedMu.Lock() |
| 64 | defer cs.expectedMu.Unlock() |
| 65 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 66 | m := cs.expected |
| 67 | if call.exhausted() { |
| 68 | m = cs.exhausted |
| 69 | } |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 70 | if cs.allowOverride { |
| 71 | m[key] = make([]*Call, 0) |
| 72 | } |
| 73 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 74 | m[key] = append(m[key], call) |
| 75 | } |
| 76 | |
| 77 | // Remove removes an expected call. |
| 78 | func (cs callSet) Remove(call *Call) { |
| 79 | key := callSetKey{call.receiver, call.method} |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 80 | |
| 81 | cs.expectedMu.Lock() |
| 82 | defer cs.expectedMu.Unlock() |
| 83 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 84 | calls := cs.expected[key] |
| 85 | for i, c := range calls { |
| 86 | if c == call { |
| 87 | // maintain order for remaining calls |
| 88 | cs.expected[key] = append(calls[:i], calls[i+1:]...) |
| 89 | cs.exhausted[key] = append(cs.exhausted[key], call) |
| 90 | break |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // FindMatch searches for a matching call. Returns error with explanation message if no call matched. |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 96 | func (cs callSet) FindMatch(receiver any, method string, args []any) (*Call, error) { |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 97 | key := callSetKey{receiver, method} |
| 98 | |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 99 | cs.expectedMu.Lock() |
| 100 | defer cs.expectedMu.Unlock() |
| 101 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 102 | // Search through the expected calls. |
| 103 | expected := cs.expected[key] |
| 104 | var callsErrors bytes.Buffer |
| 105 | for _, call := range expected { |
| 106 | err := call.matches(args) |
| 107 | if err != nil { |
| 108 | _, _ = fmt.Fprintf(&callsErrors, "\n%v", err) |
| 109 | } else { |
| 110 | return call, nil |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // If we haven't found a match then search through the exhausted calls so we |
| 115 | // get useful error messages. |
| 116 | exhausted := cs.exhausted[key] |
| 117 | for _, call := range exhausted { |
| 118 | if err := call.matches(args); err != nil { |
| 119 | _, _ = fmt.Fprintf(&callsErrors, "\n%v", err) |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame] | 120 | continue |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 121 | } |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame] | 122 | _, _ = fmt.Fprintf( |
| 123 | &callsErrors, "all expected calls for method %q have been exhausted", method, |
| 124 | ) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if len(expected)+len(exhausted) == 0 { |
| 128 | _, _ = fmt.Fprintf(&callsErrors, "there are no expected calls of the method %q for that receiver", method) |
| 129 | } |
| 130 | |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame] | 131 | return nil, errors.New(callsErrors.String()) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Failures returns the calls that are not satisfied. |
| 135 | func (cs callSet) Failures() []*Call { |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 136 | cs.expectedMu.Lock() |
| 137 | defer cs.expectedMu.Unlock() |
| 138 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 139 | failures := make([]*Call, 0, len(cs.expected)) |
| 140 | for _, calls := range cs.expected { |
| 141 | for _, call := range calls { |
| 142 | if !call.satisfied() { |
| 143 | failures = append(failures, call) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return failures |
| 148 | } |
| bseeniva | dd66c36 | 2026-02-12 19:13:26 +0530 | [diff] [blame^] | 149 | |
| 150 | // Satisfied returns true in case all expected calls in this callSet are satisfied. |
| 151 | func (cs callSet) Satisfied() bool { |
| 152 | cs.expectedMu.Lock() |
| 153 | defer cs.expectedMu.Unlock() |
| 154 | |
| 155 | for _, calls := range cs.expected { |
| 156 | for _, call := range calls { |
| 157 | if !call.satisfied() { |
| 158 | return false |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return true |
| 164 | } |