From fbce5e7b815d7df710f56be08e5042ad84c89f1a Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 30 Jun 2023 17:15:25 -0700 Subject: [PATCH] Add ControllerOption (#24) Originally discussed in https://github.com/golang/mock/issues/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. --- gomock/controller.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/gomock/controller.go b/gomock/controller.go index ee7e4cd..db03522 100644 --- a/gomock/controller.go +++ b/gomock/controller.go @@ -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} @@ -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() @@ -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()