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

Commit e86b1bf

Browse files
authored
Allow checking whether all expectations have been met (#21)
1 parent c1ed26f commit e86b1bf

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

gomock/callset.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import (
1818
"bytes"
1919
"errors"
2020
"fmt"
21+
"sync"
2122
)
2223

2324
// callSet represents a set of expected calls, indexed by receiver and method
2425
// name.
2526
type callSet struct {
2627
// Calls that are still expected.
27-
expected map[callSetKey][]*Call
28+
expected map[callSetKey][]*Call
29+
expectedMu *sync.Mutex
2830
// Calls that have been exhausted.
2931
exhausted map[callSetKey][]*Call
3032
}
@@ -36,12 +38,20 @@ type callSetKey struct {
3638
}
3739

3840
func newCallSet() *callSet {
39-
return &callSet{make(map[callSetKey][]*Call), make(map[callSetKey][]*Call)}
41+
return &callSet{
42+
expected: make(map[callSetKey][]*Call),
43+
expectedMu: &sync.Mutex{},
44+
exhausted: make(map[callSetKey][]*Call),
45+
}
4046
}
4147

4248
// Add adds a new expected call.
4349
func (cs callSet) Add(call *Call) {
4450
key := callSetKey{call.receiver, call.method}
51+
52+
cs.expectedMu.Lock()
53+
defer cs.expectedMu.Unlock()
54+
4555
m := cs.expected
4656
if call.exhausted() {
4757
m = cs.exhausted
@@ -52,6 +62,10 @@ func (cs callSet) Add(call *Call) {
5262
// Remove removes an expected call.
5363
func (cs callSet) Remove(call *Call) {
5464
key := callSetKey{call.receiver, call.method}
65+
66+
cs.expectedMu.Lock()
67+
defer cs.expectedMu.Unlock()
68+
5569
calls := cs.expected[key]
5670
for i, c := range calls {
5771
if c == call {
@@ -67,6 +81,9 @@ func (cs callSet) Remove(call *Call) {
6781
func (cs callSet) FindMatch(receiver interface{}, method string, args []interface{}) (*Call, error) {
6882
key := callSetKey{receiver, method}
6983

84+
cs.expectedMu.Lock()
85+
defer cs.expectedMu.Unlock()
86+
7087
// Search through the expected calls.
7188
expected := cs.expected[key]
7289
var callsErrors bytes.Buffer
@@ -101,6 +118,9 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac
101118

102119
// Failures returns the calls that are not satisfied.
103120
func (cs callSet) Failures() []*Call {
121+
cs.expectedMu.Lock()
122+
defer cs.expectedMu.Unlock()
123+
104124
failures := make([]*Call, 0, len(cs.expected))
105125
for _, calls := range cs.expected {
106126
for _, call := range calls {
@@ -111,3 +131,19 @@ func (cs callSet) Failures() []*Call {
111131
}
112132
return failures
113133
}
134+
135+
// Satisfied returns true in case all expected calls in this callSet are satisfied.
136+
func (cs callSet) Satisfied() bool {
137+
cs.expectedMu.Lock()
138+
defer cs.expectedMu.Unlock()
139+
140+
for _, calls := range cs.expected {
141+
for _, call := range calls {
142+
if !call.satisfied() {
143+
return false
144+
}
145+
}
146+
}
147+
148+
return true
149+
}

gomock/callset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestCallSetRemove(t *testing.T) {
7777

7878
func TestCallSetFindMatch(t *testing.T) {
7979
t.Run("call is exhausted", func(t *testing.T) {
80-
cs := callSet{}
80+
cs := newCallSet()
8181
var receiver interface{} = "TestReceiver"
8282
method := "TestMethod"
8383
args := []interface{}{}

gomock/controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ func (ctrl *Controller) Finish() {
231231
ctrl.finish(false, err)
232232
}
233233

234+
// Satisfied returns whether all expected calls bound to this Controller have been satisfied.
235+
// Calling Finish is then guaranteed to not fail due to missing calls.
236+
func (ctrl *Controller) Satisfied() bool {
237+
return ctrl.expectedCalls.Satisfied()
238+
}
239+
234240
func (ctrl *Controller) finish(cleanup bool, panicErr interface{}) {
235241
ctrl.T.Helper()
236242

0 commit comments

Comments
 (0)