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

Commit a473650

Browse files
committed
Use overridableExpectationsOption instead of introducing new constructor
1 parent 5a60090 commit a473650

File tree

4 files changed

+15
-25
lines changed

4 files changed

+15
-25
lines changed

gomock/callset.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type callSet struct {
2929
expectedMu *sync.Mutex
3030
// Calls that have been exhausted.
3131
exhausted map[callSetKey][]*Call
32-
// when set to true,
32+
// when set to true, existing call expectations are overridden when new call expectations are made
3333
allowOverride bool
3434
}
3535

@@ -68,7 +68,6 @@ func (cs callSet) Add(call *Call) {
6868
m = cs.exhausted
6969
}
7070
if cs.allowOverride {
71-
delete(m, key)
7271
m[key] = make([]*Call, 0)
7372
}
7473

gomock/controller.go

+10-19
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,21 @@ func NewController(t TestReporter, opts ...ControllerOption) *Controller {
108108
return ctrl
109109
}
110110

111-
// ControllerOption configures how a Controller should behave. Currently
112-
// there are no implementations of it.
111+
// ControllerOption configures how a Controller should behave.
113112
type ControllerOption interface {
114113
apply(*Controller)
115114
}
116115

117-
// NewOverridableController returns a new Controller that allows for overridable call expectations
118-
func NewOverridableController(t TestReporter) *Controller {
119-
h, ok := t.(TestHelper)
120-
if !ok {
121-
h = &nopTestHelper{t}
122-
}
123-
ctrl := &Controller{
124-
T: h,
125-
expectedCalls: newOverridableCallSet(),
126-
}
127-
if c, ok := isCleanuper(ctrl.T); ok {
128-
c.Cleanup(func() {
129-
ctrl.T.Helper()
130-
ctrl.finish(true, nil)
131-
})
132-
}
116+
type overridableExpectationsOption struct{}
133117

134-
return ctrl
118+
// WithOverridableExpectations allows for overridable call expectations
119+
// i.e., subsequent call expectations override existing call expectations
120+
func WithOverridableExpectations() overridableExpectationsOption {
121+
return overridableExpectationsOption{}
122+
}
123+
124+
func (o overridableExpectationsOption) apply(ctrl *Controller) {
125+
ctrl.expectedCalls = newOverridableCallSet()
135126
}
136127

137128
type cancelReporter struct {

gomock/example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func ExampleCall_DoAndReturn_captureArguments() {
4949
// Output: I'm sleepy foo
5050
}
5151

52-
func ExampleCall_OverridableController_HappyPath() {
52+
func ExampleCall_WithOverridableExpectations() {
5353
t := &testing.T{} // provided by test
54-
ctrl := gomock.NewOverridableController(t)
54+
ctrl := gomock.NewController(t, gomock.WithOverridableExpectations())
5555
mockIndex := NewMockFoo(ctrl)
5656
var s string
5757

gomock/overridable_controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestEcho_NoOverride(t *testing.T) {
10-
ctrl := gomock.NewOverridableController(t)
10+
ctrl := gomock.NewController(t, gomock.WithOverridableExpectations())
1111
mockIndex := NewMockFoo(ctrl)
1212

1313
mockIndex.EXPECT().Bar(gomock.Any()).Return("foo")
@@ -19,7 +19,7 @@ func TestEcho_NoOverride(t *testing.T) {
1919
}
2020

2121
func TestEcho_WithOverride_BaseCase(t *testing.T) {
22-
ctrl := gomock.NewOverridableController(t)
22+
ctrl := gomock.NewController(t, gomock.WithOverridableExpectations())
2323
mockIndex := NewMockFoo(ctrl)
2424

2525
// initial expectation set

0 commit comments

Comments
 (0)