Skip to content

Commit

Permalink
Add ControllerOption (uber-go#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 uber-go#22.
  • Loading branch information
sywhang committed Jul 5, 2023
1 parent 673e300 commit fbce5e7
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 fbce5e7

Please sign in to comment.