Skip to content

Commit

Permalink
Add ControllerOption (#24)
Browse files Browse the repository at this point in the history
Originally discussed in golang/mock#238.

This adds a functional option parameter to NewController to allow adding
future configurations to control the behavior of Controller.

This will come in handy for implementing features like the one in #22.
  • Loading branch information
sywhang authored Jul 1, 2023
1 parent e86b1bf commit fcef047
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion gomock/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Controller struct {
//
// New in go1.14+, if you are passing a *testing.T into this function you no
// longer need to call ctrl.Finish() in your test methods.
func NewController(t TestReporter) *Controller {
func NewController(t TestReporter, opts ...ControllerOption) *Controller {
h, ok := t.(TestHelper)
if !ok {
h = &nopTestHelper{t}
Expand All @@ -95,6 +95,9 @@ func NewController(t TestReporter) *Controller {
T: h,
expectedCalls: newCallSet(),
}
for _, opt := range opts {
opt.apply(ctrl)
}
if c, ok := isCleanuper(ctrl.T); ok {
c.Cleanup(func() {
ctrl.T.Helper()
Expand All @@ -105,6 +108,12 @@ func NewController(t TestReporter) *Controller {
return ctrl
}

// ControllerOption configures how a Controller should behave. Currently
// there are no implementations of it.
type ControllerOption interface {
apply(*Controller)
}

type cancelReporter struct {
t TestHelper
cancel func()
Expand Down

0 comments on commit fcef047

Please sign in to comment.