From f4bbb99c7e306bffffcc3fbdda8328617269f6fc Mon Sep 17 00:00:00 2001 From: Alexander Gehres Date: Thu, 24 May 2018 17:40:17 +0200 Subject: [PATCH 1/5] add fallback function --- gomock/call.go | 21 +++- gomock/callset.go | 16 ++- gomock/controller.go | 2 +- gomock/controller_test.go | 259 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 294 insertions(+), 4 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index a3fa1ae4..f5dd60ce 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -30,6 +30,7 @@ type Call struct { methodType reflect.Type // the type of the method args []Matcher // the args origin string // file and line number of call setup + isFallback bool // true if this is a fallback preReqs []*Call // prerequisite calls @@ -78,6 +79,13 @@ func newCall(t TestReporter, receiver interface{}, method string, methodType ref args: margs, origin: origin, minCalls: 1, maxCalls: 1, actions: actions} } +// Fallback defines this call as fallback (default) if no other call matches. +// The arguments are not checked / matched since this would not be a fallback anymore. +func (c *Call) Fallback() *Call { + c.isFallback = true + return c +} + // AnyTimes allows the expectation to be called 0 or more times func (c *Call) AnyTimes() *Call { c.minCalls, c.maxCalls = 0, 1e8 // close enough to infinity @@ -268,6 +276,16 @@ func (c *Call) After(preReq *Call) *Call { h.Helper() } + // this is more or less a hint, since you could add a call as prerequisite + // and afterwards declare it as Fallback() + if preReq.isFallback { + c.t.Fatalf("Fallback isn't allowed to be a prerequisite") + } + // same here + if c.isFallback { + c.t.Fatalf("Fallback isn't allowed to have prerequisites") + } + if c == preReq { c.t.Fatalf("A call isn't allowed to be its own prerequisite") } @@ -382,7 +400,8 @@ func (c *Call) matches(args []interface{}) error { // Check that all prerequisite calls have been satisfied. for _, preReqCall := range c.preReqs { - if !preReqCall.satisfied() { + // skip fallback since this should not be a prerequisite + if !preReqCall.satisfied() && !preReqCall.isFallback { return fmt.Errorf("Expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", c.origin, preReqCall, c) } diff --git a/gomock/callset.go b/gomock/callset.go index c44a8a58..5e2a5d08 100644 --- a/gomock/callset.go +++ b/gomock/callset.go @@ -53,7 +53,8 @@ func (cs callSet) Remove(call *Call) { key := callSetKey{call.receiver, call.method} calls := cs.expected[key] for i, c := range calls { - if c == call { + // never remove a fallback call + if c == call && !c.isFallback { // maintain order for remaining calls cs.expected[key] = append(calls[:i], calls[i+1:]...) cs.exhausted[key] = append(cs.exhausted[key], call) @@ -69,7 +70,13 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac // Search through the expected calls. expected := cs.expected[key] var callsErrors bytes.Buffer + var fallback *Call for _, call := range expected { + // remember fallback for later use and skip it for now + if call.isFallback { + fallback = call + continue + } err := call.matches(args) if err != nil { fmt.Fprintf(&callsErrors, "\n%v", err) @@ -78,6 +85,11 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac } } + // Nothing found check if we at least hold a callback + if fallback != nil { + return fallback, nil + } + // If we haven't found a match then search through the exhausted calls so we // get useful error messages. exhausted := cs.exhausted[key] @@ -99,7 +111,7 @@ func (cs callSet) Failures() []*Call { failures := make([]*Call, 0, len(cs.expected)) for _, calls := range cs.expected { for _, call := range calls { - if !call.satisfied() { + if !call.satisfied() && !call.isFallback { failures = append(failures, call) } } diff --git a/gomock/controller.go b/gomock/controller.go index a7b79188..ceb63cdf 100644 --- a/gomock/controller.go +++ b/gomock/controller.go @@ -159,7 +159,7 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf } actions := expected.call(args) - if expected.exhausted() { + if expected.exhausted() && !expected.isFallback { ctrl.expectedCalls.Remove(expected) } return actions diff --git a/gomock/controller_test.go b/gomock/controller_test.go index b3bb4de2..69a59974 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -710,3 +710,262 @@ func TestDuplicateFinishCallFails(t *testing.T) { rep.assertFatal(ctrl.Finish, "Controller.Finish was called more than once. It has to be called exactly once.") } + +// Test fallback call that is used to define a default behavior and catches any call for a method that could not be matched + +func TestFallbackOnly(t *testing.T) { + // no call + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(5) + reporter.assertPass("not calling a function with fallback is ok") + ctrl.Finish() + + // multiple arbitrary calls + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + ctrl.RecordCall(subject, "FooMethod", "isIgnored", "for", "fallback").Fallback().Return(5) + + rets := ctrl.Call(subject, "FooMethod", "123") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 5 { + t.Errorf("Return value is %v want 5", ret) + } + + rets = ctrl.Call(subject, "FooMethod", "") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 5 { + t.Errorf("Return value is %v want 5", ret) + } + + reporter.assertPass("calling a function with fallback n times with arbitrary parameters is ok") + ctrl.Finish() +} + +func TestFallbackAndExpectationWithMissingCall(t *testing.T) { + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(5) + ctrl.RecordCall(subject, "FooMethod", "123").Return(123) + + // does fallback but not match expectation + ctrl.Call(subject, "FooMethod", "should fallback") + + // does fallback but not match expectation + ctrl.Call(subject, "FooMethod", "also fallback") + + reporter.assertFatal(func() { + ctrl.Finish() + }, "aborting test due to missing call(s)") +} + +func TestFallbackAndExpectationWithAllExpectationsMet(t *testing.T) { + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + // every expectation should have precedence over fallback + ctrl.RecordCall(subject, "FooMethod", "123").Fallback().Return(5) + ctrl.RecordCall(subject, "FooMethod", "123").Return(123) + ctrl.RecordCall(subject, "FooMethod", "345").Return(345) + + // should not fallback and match expectation + rets := ctrl.Call(subject, "FooMethod", "123") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 123 { + t.Errorf("Return value is %v want 123", ret) + } + + // should fallback + rets = ctrl.Call(subject, "FooMethod", "fallback") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 5 { + t.Errorf("Return value is %v want 5", ret) + } + + // should not fallback and match expectation + rets = ctrl.Call(subject, "FooMethod", "345") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 345 { + t.Errorf("Return value is %v want 345", ret) + } + + // should fallback + rets = ctrl.Call(subject, "FooMethod", "123") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 5 { + t.Errorf("Return value is %v want 5", ret) + } + + reporter.assertPass("expectations should have precedence over fallback") + ctrl.Finish() +} + +func TestOverwriteFallback(t *testing.T) { + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + // first fallback + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(123) + + // uses current fallback + rets := ctrl.Call(subject, "FooMethod", "something") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 123 { + t.Errorf("Return value is %v want 123", ret) + } + + // overwrite fallback + ctrl.RecordCall(subject, "FooMethod", "anotherFallback").Fallback().Return(456) + + // matches new fallback + rets = ctrl.Call(subject, "FooMethod", "arbitrary") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 456 { + t.Errorf("Return value is %v want 456", ret) + } + + reporter.assertPass("should always take the latest fallback definition") + ctrl.Finish() +} + +func TestFallbackWithMissingReturn(t *testing.T) { + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback() + + rets := ctrl.Call(subject, "FooMethod", "something") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 0 { + t.Errorf("Return value is %v want 0", ret) + } + + reporter.assertPass("fallback should return default on missing return definition") + ctrl.Finish() +} + +func TestFallbackIgnoresMinMaxTimes(t *testing.T) { + + // test Min/MaxTimes ignores + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().MinTimes(1).MaxTimes(2).Return(5) + + // call 3 times should work + ctrl.Call(subject, "FooMethod", "something") + ctrl.Call(subject, "FooMethod", "something") + ctrl.Call(subject, "FooMethod", "something") + + reporter.assertPass("fallback should ignore Min/MaxTimes") + ctrl.Finish() +} + +func TestFallbackIgnoresAfterFunction(t *testing.T) { + // fallback should return when trying to add prerequisite + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + someCall := ctrl.RecordCall(subject, "FooMethod", "123").Return(123) + + reporter.assertFatal(func() { + ctrl.RecordCall(subject, "FooMethod", "ignored").Fallback().After(someCall) + }, "Fallback isn't allowed to have prerequisites") + + // fallback should ignore prerequisite when tricked to add one + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + someCall = ctrl.RecordCall(subject, "FooMethod", "123").Return(123) + // trick fallback to hold prerequisite + fallback := ctrl.RecordCall(subject, "FooMethod", "ignored").After(someCall) + fallback.Fallback() + + ctrl.Call(subject, "FooMethod", "ignored") + + reporter.assertPass("fallback should ignore After call") + ctrl.Finish() + + // fallback used as prerequisite should return an error + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + fallback = ctrl.RecordCall(subject, "FooMethod", "ignored").Fallback() + + reporter.assertFatal(func() { + ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(fallback) + }, "Fallback isn't allowed to be a prerequisite") + + // when fallback is tricked to be used as prerequisite it should be ignored + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + fallback = ctrl.RecordCall(subject, "FooMethod", "ignored") + ctrl.RecordCall(subject, "FooMethod", "123").After(fallback) + fallback.Fallback() + + // should be able to call this even if fallback is a prerequisite + ctrl.Call(subject, "FooMethod", "123") + + reporter.assertPass("fallback as prerequisite should be ignored") + ctrl.Finish() +} + +func TestFallbackCallsDoFunc(t *testing.T) { + // After() should be ignored on fallback + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + str := "" + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Do(func(s string) { + str = s + }) + + ctrl.Call(subject, "FooMethod", "something") + + if str != "something" { + t.Errorf("value is %v want 'something'", str) + } + + reporter.assertPass("fallback should ignore After()") + ctrl.Finish() +} + +func TestFallbackCallsDoAndReturnFunc(t *testing.T) { + reporter, ctrl := createFixtures(t) + subject := new(Subject) + + str := "" + + ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().DoAndReturn(func(s string) int { + str = s + return 5 + }) + + rets := ctrl.Call(subject, "FooMethod", "something") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 5 { + t.Errorf("Return value is %v want 5", ret) + } + + if str != "something" { + t.Errorf("value is %v want 'something'", str) + } + + reporter.assertPass("fallback work with DoAndReturn") + ctrl.Finish() +} From 529c9e10c0482c228473d0fad4220753dabdd810 Mon Sep 17 00:00:00 2001 From: Alexander Gehres Date: Fri, 25 May 2018 00:57:27 +0200 Subject: [PATCH 2/5] rename to WillByDefault, remove caveats --- gomock/call.go | 24 ++--- gomock/callset.go | 22 ++--- gomock/controller.go | 2 +- gomock/controller_test.go | 187 ++++++++++++++++++++++++-------------- 4 files changed, 138 insertions(+), 97 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index f5dd60ce..24d36508 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -30,7 +30,7 @@ type Call struct { methodType reflect.Type // the type of the method args []Matcher // the args origin string // file and line number of call setup - isFallback bool // true if this is a fallback + isDefault bool // true if this is a default call preReqs []*Call // prerequisite calls @@ -79,10 +79,12 @@ func newCall(t TestReporter, receiver interface{}, method string, methodType ref args: margs, origin: origin, minCalls: 1, maxCalls: 1, actions: actions} } -// Fallback defines this call as fallback (default) if no other call matches. -// The arguments are not checked / matched since this would not be a fallback anymore. -func (c *Call) Fallback() *Call { - c.isFallback = true +// WillByDefault defines this expectation as a default that is tried to match if no other expectation matches. +// To use this as catch-all you may use Any() matcher for the method parameters. +// WillByDefault expects to be called 0 or more times. This can be overwritten by Min/MaxTimes() methods. +func (c *Call) WillByDefault() *Call { + c.isDefault = true + c.AnyTimes() return c } @@ -276,16 +278,6 @@ func (c *Call) After(preReq *Call) *Call { h.Helper() } - // this is more or less a hint, since you could add a call as prerequisite - // and afterwards declare it as Fallback() - if preReq.isFallback { - c.t.Fatalf("Fallback isn't allowed to be a prerequisite") - } - // same here - if c.isFallback { - c.t.Fatalf("Fallback isn't allowed to have prerequisites") - } - if c == preReq { c.t.Fatalf("A call isn't allowed to be its own prerequisite") } @@ -401,7 +393,7 @@ func (c *Call) matches(args []interface{}) error { // Check that all prerequisite calls have been satisfied. for _, preReqCall := range c.preReqs { // skip fallback since this should not be a prerequisite - if !preReqCall.satisfied() && !preReqCall.isFallback { + if !preReqCall.satisfied() { return fmt.Errorf("Expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", c.origin, preReqCall, c) } diff --git a/gomock/callset.go b/gomock/callset.go index 5e2a5d08..e728dfa0 100644 --- a/gomock/callset.go +++ b/gomock/callset.go @@ -53,8 +53,7 @@ func (cs callSet) Remove(call *Call) { key := callSetKey{call.receiver, call.method} calls := cs.expected[key] for i, c := range calls { - // never remove a fallback call - if c == call && !c.isFallback { + if c == call { // maintain order for remaining calls cs.expected[key] = append(calls[:i], calls[i+1:]...) cs.exhausted[key] = append(cs.exhausted[key], call) @@ -70,24 +69,23 @@ func (cs callSet) FindMatch(receiver interface{}, method string, args []interfac // Search through the expected calls. expected := cs.expected[key] var callsErrors bytes.Buffer - var fallback *Call + var defaultCall *Call for _, call := range expected { - // remember fallback for later use and skip it for now - if call.isFallback { - fallback = call - continue - } err := call.matches(args) if err != nil { fmt.Fprintf(&callsErrors, "\n%v", err) } else { + if call.isDefault { + defaultCall = call + continue + } return call, nil } } - // Nothing found check if we at least hold a callback - if fallback != nil { - return fallback, nil + // Nothing found check if we at least found some default + if defaultCall != nil { + return defaultCall, nil } // If we haven't found a match then search through the exhausted calls so we @@ -111,7 +109,7 @@ func (cs callSet) Failures() []*Call { failures := make([]*Call, 0, len(cs.expected)) for _, calls := range cs.expected { for _, call := range calls { - if !call.satisfied() && !call.isFallback { + if !call.satisfied() { failures = append(failures, call) } } diff --git a/gomock/controller.go b/gomock/controller.go index ceb63cdf..a7b79188 100644 --- a/gomock/controller.go +++ b/gomock/controller.go @@ -159,7 +159,7 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf } actions := expected.call(args) - if expected.exhausted() && !expected.isFallback { + if expected.exhausted() { ctrl.expectedCalls.Remove(expected) } return actions diff --git a/gomock/controller_test.go b/gomock/controller_test.go index 69a59974..4a002c80 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -711,22 +711,23 @@ func TestDuplicateFinishCallFails(t *testing.T) { rep.assertFatal(ctrl.Finish, "Controller.Finish was called more than once. It has to be called exactly once.") } -// Test fallback call that is used to define a default behavior and catches any call for a method that could not be matched +// Test WillByDefault call that is used to define a default behavior -func TestFallbackOnly(t *testing.T) { +func TestWillByDefaultOnly(t *testing.T) { // no call reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(5) - reporter.assertPass("not calling a function with fallback is ok") + ctrl.RecordCall(subject, "FooMethod", "something").WillByDefault().Return(5) ctrl.Finish() + reporter.assertPass("not calling a function with defined default is ok") + // multiple arbitrary calls reporter, ctrl = createFixtures(t) subject = new(Subject) - ctrl.RecordCall(subject, "FooMethod", "isIgnored", "for", "fallback").Fallback().Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) rets := ctrl.Call(subject, "FooMethod", "123") if ret, ok := rets[0].(int); !ok { @@ -742,38 +743,38 @@ func TestFallbackOnly(t *testing.T) { t.Errorf("Return value is %v want 5", ret) } - reporter.assertPass("calling a function with fallback n times with arbitrary parameters is ok") ctrl.Finish() + reporter.assertPass("calling a function with defined default n times with arbitrary parameters is ok") } -func TestFallbackAndExpectationWithMissingCall(t *testing.T) { +func TestWillByDefaultAndExpectationWithMissingCall(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) ctrl.RecordCall(subject, "FooMethod", "123").Return(123) - // does fallback but not match expectation - ctrl.Call(subject, "FooMethod", "should fallback") + // does call default and not match expectation + ctrl.Call(subject, "FooMethod", "should default") - // does fallback but not match expectation - ctrl.Call(subject, "FooMethod", "also fallback") + // does call default and not match expectation + ctrl.Call(subject, "FooMethod", "also default") reporter.assertFatal(func() { ctrl.Finish() }, "aborting test due to missing call(s)") } -func TestFallbackAndExpectationWithAllExpectationsMet(t *testing.T) { +func TestWillByDefaultAndExpectationWithAllExpectationsMet(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - // every expectation should have precedence over fallback - ctrl.RecordCall(subject, "FooMethod", "123").Fallback().Return(5) + // every expectation should have precedence over default call + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) ctrl.RecordCall(subject, "FooMethod", "123").Return(123) ctrl.RecordCall(subject, "FooMethod", "345").Return(345) - // should not fallback and match expectation + // should not use default but match expectation rets := ctrl.Call(subject, "FooMethod", "123") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") @@ -781,15 +782,15 @@ func TestFallbackAndExpectationWithAllExpectationsMet(t *testing.T) { t.Errorf("Return value is %v want 123", ret) } - // should fallback - rets = ctrl.Call(subject, "FooMethod", "fallback") + // should call default since expectation is consumed + rets = ctrl.Call(subject, "FooMethod", "123") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") } else if ret != 5 { t.Errorf("Return value is %v want 5", ret) } - // should not fallback and match expectation + // should not call default but match expectation rets = ctrl.Call(subject, "FooMethod", "345") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") @@ -797,53 +798,64 @@ func TestFallbackAndExpectationWithAllExpectationsMet(t *testing.T) { t.Errorf("Return value is %v want 345", ret) } - // should fallback - rets = ctrl.Call(subject, "FooMethod", "123") + // should call default since expectation is consumed + rets = ctrl.Call(subject, "FooMethod", "345") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") } else if ret != 5 { t.Errorf("Return value is %v want 5", ret) } - reporter.assertPass("expectations should have precedence over fallback") ctrl.Finish() + reporter.assertPass("expectations should have precedence over default call") } -func TestOverwriteFallback(t *testing.T) { +func TestOverwriteWillByDefault(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - // first fallback - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Return(123) + // first defaultCall + ctrl.RecordCall(subject, "FooMethod", "defaultCall").WillByDefault().Return(123) - // uses current fallback - rets := ctrl.Call(subject, "FooMethod", "something") + // uses current default + rets := ctrl.Call(subject, "FooMethod", "defaultCall") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") } else if ret != 123 { t.Errorf("Return value is %v want 123", ret) } - // overwrite fallback - ctrl.RecordCall(subject, "FooMethod", "anotherFallback").Fallback().Return(456) + // overwrite default (when second one matches at least all parameters that first one matched) + ctrl.RecordCall(subject, "FooMethod", "defaultCall").WillByDefault().Return(456) - // matches new fallback - rets = ctrl.Call(subject, "FooMethod", "arbitrary") + // matches new default + rets = ctrl.Call(subject, "FooMethod", "defaultCall") if ret, ok := rets[0].(int); !ok { t.Fatalf("Return value is not an int") } else if ret != 456 { t.Errorf("Return value is %v want 456", ret) } - reporter.assertPass("should always take the latest fallback definition") + // overwrite default with more loose one + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(789) + + // matches new default + rets = ctrl.Call(subject, "FooMethod", "defaultCall") + if ret, ok := rets[0].(int); !ok { + t.Fatalf("Return value is not an int") + } else if ret != 789 { + t.Errorf("Return value is %v want 789", ret) + } + ctrl.Finish() + reporter.assertPass("should always take the latest default definition") } -func TestFallbackWithMissingReturn(t *testing.T) { +func TestWillByDefaultWithMissingReturn(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback() + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() rets := ctrl.Call(subject, "FooMethod", "something") if ret, ok := rets[0].(int); !ok { @@ -852,85 +864,124 @@ func TestFallbackWithMissingReturn(t *testing.T) { t.Errorf("Return value is %v want 0", ret) } - reporter.assertPass("fallback should return default on missing return definition") ctrl.Finish() + reporter.assertPass("should return zero value on missing return definition") } -func TestFallbackIgnoresMinMaxTimes(t *testing.T) { +func TestWillByDefaultMinMaxTimes(t *testing.T) { - // test Min/MaxTimes ignores + // test MinTimes failes reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().MinTimes(1).MaxTimes(2).Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MinTimes(2).Return(5) + + // only one call: should fail expectation + ctrl.Call(subject, "FooMethod", "something") + + reporter.assertFatal(func() { + ctrl.Finish() + }, "aborting test due to missing call(s)") + + // test MinTimes succeeds + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MinTimes(2).Return(5) + + // two calls: should meet expectations + ctrl.Call(subject, "FooMethod", "something") + ctrl.Call(subject, "FooMethod", "something") + + ctrl.Finish() + reporter.assertPass("calling default for at least MinTime should work") + + // test MaxTimes failes + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MaxTimes(2).Return(5) - // call 3 times should work ctrl.Call(subject, "FooMethod", "something") ctrl.Call(subject, "FooMethod", "something") + + reporter.assertFatal(func() { + // call 1 more than MaxTimes should fail + ctrl.Call(subject, "FooMethod", "something") + ctrl.Finish() + }, "Unexpected call to", "has already been called the max number of times.") + + // test MaxTimes succeeds + reporter, ctrl = createFixtures(t) + subject = new(Subject) + + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MaxTimes(2).Return(5) + + // call no more than MaxTimes should work ctrl.Call(subject, "FooMethod", "something") - reporter.assertPass("fallback should ignore Min/MaxTimes") ctrl.Finish() + reporter.assertPass("calling default for at most MaxTimes should work") } -func TestFallbackIgnoresAfterFunction(t *testing.T) { - // fallback should return when trying to add prerequisite +func TestWillByDefaultAfterFunction(t *testing.T) { + // default call should fail if prerequisite not called reporter, ctrl := createFixtures(t) subject := new(Subject) someCall := ctrl.RecordCall(subject, "FooMethod", "123").Return(123) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().After(someCall) reporter.assertFatal(func() { - ctrl.RecordCall(subject, "FooMethod", "ignored").Fallback().After(someCall) - }, "Fallback isn't allowed to have prerequisites") + ctrl.Call(subject, "FooMethod", "something") + }, "") - // fallback should ignore prerequisite when tricked to add one + // should work if prerequisite called reporter, ctrl = createFixtures(t) subject = new(Subject) someCall = ctrl.RecordCall(subject, "FooMethod", "123").Return(123) - // trick fallback to hold prerequisite - fallback := ctrl.RecordCall(subject, "FooMethod", "ignored").After(someCall) - fallback.Fallback() + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().After(someCall) - ctrl.Call(subject, "FooMethod", "ignored") + ctrl.Call(subject, "FooMethod", "123") + ctrl.Call(subject, "FooMethod", "something") - reporter.assertPass("fallback should ignore After call") ctrl.Finish() + reporter.assertPass("should work if prerequisite called before") - // fallback used as prerequisite should return an error + // default call used as prerequisite should work reporter, ctrl = createFixtures(t) subject = new(Subject) - fallback = ctrl.RecordCall(subject, "FooMethod", "ignored").Fallback() + defaultCall := ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() + ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(defaultCall) - reporter.assertFatal(func() { - ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(fallback) - }, "Fallback isn't allowed to be a prerequisite") + ctrl.Call(subject, "FooMethod", "something") + ctrl.Call(subject, "FooMethod", "123") + + ctrl.Finish() + reporter.assertPass("defaultCall called as prerequisite should work") - // when fallback is tricked to be used as prerequisite it should be ignored + // default call used as prerequisite should work even if not called (0 times minimum by default) reporter, ctrl = createFixtures(t) subject = new(Subject) - fallback = ctrl.RecordCall(subject, "FooMethod", "ignored") - ctrl.RecordCall(subject, "FooMethod", "123").After(fallback) - fallback.Fallback() + defaultCall = ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() + ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(defaultCall) - // should be able to call this even if fallback is a prerequisite ctrl.Call(subject, "FooMethod", "123") - reporter.assertPass("fallback as prerequisite should be ignored") ctrl.Finish() + reporter.assertPass("defaultCall called as prerequisite should work") } -func TestFallbackCallsDoFunc(t *testing.T) { - // After() should be ignored on fallback +func TestWillByDefaultCallsDoFunc(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) str := "" - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().Do(func(s string) { + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Do(func(s string) { str = s }) @@ -940,17 +991,17 @@ func TestFallbackCallsDoFunc(t *testing.T) { t.Errorf("value is %v want 'something'", str) } - reporter.assertPass("fallback should ignore After()") ctrl.Finish() + reporter.assertPass("defaultCall should ignore After()") } -func TestFallbackCallsDoAndReturnFunc(t *testing.T) { +func TestWillByDefaultCallsDoAndReturnFunc(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) str := "" - ctrl.RecordCall(subject, "FooMethod", "fallback").Fallback().DoAndReturn(func(s string) int { + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().DoAndReturn(func(s string) int { str = s return 5 }) @@ -966,6 +1017,6 @@ func TestFallbackCallsDoAndReturnFunc(t *testing.T) { t.Errorf("value is %v want 'something'", str) } - reporter.assertPass("fallback work with DoAndReturn") ctrl.Finish() + reporter.assertPass("defaultCall work with DoAndReturn") } From e536f0d350ae08bd973948ac2cd2be49c19d39e4 Mon Sep 17 00:00:00 2001 From: Alexander Gehres Date: Fri, 25 May 2018 01:03:34 +0200 Subject: [PATCH 3/5] Update call.go remove unnecessary comment --- gomock/call.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gomock/call.go b/gomock/call.go index 24d36508..167e690e 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -392,7 +392,6 @@ func (c *Call) matches(args []interface{}) error { // Check that all prerequisite calls have been satisfied. for _, preReqCall := range c.preReqs { - // skip fallback since this should not be a prerequisite if !preReqCall.satisfied() { return fmt.Errorf("Expected call at %s doesn't have a prerequisite call satisfied:\n%v\nshould be called before:\n%v", c.origin, preReqCall, c) From a7f9e1cfb2adb29b9e61023ca5fbcdfdc12a9bfe Mon Sep 17 00:00:00 2001 From: ybbus Date: Mon, 4 Jun 2018 23:37:58 +0200 Subject: [PATCH 4/5] renamed ByDefault() with XTimes() and After() not allowed --- gomock/call.go | 41 ++++++++++-- gomock/controller_test.go | 136 ++++++++++++-------------------------- 2 files changed, 81 insertions(+), 96 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index 167e690e..9167afde 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -79,17 +79,29 @@ func newCall(t TestReporter, receiver interface{}, method string, methodType ref args: margs, origin: origin, minCalls: 1, maxCalls: 1, actions: actions} } -// WillByDefault defines this expectation as a default that is tried to match if no other expectation matches. +// ByDefault defines this expectation as a default that is tried to match if no other expectation matches. // To use this as catch-all you may use Any() matcher for the method parameters. -// WillByDefault expects to be called 0 or more times. This can be overwritten by Min/MaxTimes() methods. -func (c *Call) WillByDefault() *Call { +// ByDefault expects to be called 0 or more times. This can be overwritten by Min/MaxTimes() methods. +func (c *Call) ByDefault() *Call { + if c.minCalls != 1 || c.maxCalls != 1 { + c.t.Fatalf("MinTimes(), MaxTimes(), Times() or AnyTimes() is not allowed when using ByDefault()") + } + + if len(c.preReqs) != 0 { + c.t.Fatalf("After() is not allowed when using ByDefault()") + } + c.isDefault = true - c.AnyTimes() + c.minCalls, c.maxCalls = 0, 1e8 return c } // AnyTimes allows the expectation to be called 0 or more times func (c *Call) AnyTimes() *Call { + if c.isDefault { + c.t.Fatalf("AnyTimes() is not allowed when using ByDefault()") + } + c.minCalls, c.maxCalls = 0, 1e8 // close enough to infinity return c } @@ -97,6 +109,10 @@ func (c *Call) AnyTimes() *Call { // MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called, MinTimes also // sets the maximum number of calls to infinity. func (c *Call) MinTimes(n int) *Call { + if c.isDefault { + c.t.Fatalf("MinTimes() is not allowed when using ByDefault()") + } + c.minCalls = n if c.maxCalls == 1 { c.maxCalls = 1e8 @@ -107,6 +123,10 @@ func (c *Call) MinTimes(n int) *Call { // MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called, MaxTimes also // sets the minimum number of calls to 0. func (c *Call) MaxTimes(n int) *Call { + if c.isDefault { + c.t.Fatalf("MaxTimes() is not allowed when using ByDefault()") + } + c.maxCalls = n if c.minCalls == 1 { c.minCalls = 0 @@ -211,6 +231,10 @@ func (c *Call) Return(rets ...interface{}) *Call { // Times declares the exact number of times a function call is expected to be executed. func (c *Call) Times(n int) *Call { + if c.isDefault { + c.t.Fatalf("Times() is not allowed when using ByDefault()") + } + c.minCalls, c.maxCalls = n, n return c } @@ -278,6 +302,15 @@ func (c *Call) After(preReq *Call) *Call { h.Helper() } + // this is more or less a hint, since you could add a call as prerequisite + // and afterwards use ByDefault() + if preReq.isDefault { + c.t.Fatalf("Default isn't allowed to be a prerequisite") + } + if c.isDefault { + c.t.Fatalf("ByDefault() isn't allowed to have prerequisites") + } + if c == preReq { c.t.Fatalf("A call isn't allowed to be its own prerequisite") } diff --git a/gomock/controller_test.go b/gomock/controller_test.go index 4a002c80..6d63e673 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -711,14 +711,14 @@ func TestDuplicateFinishCallFails(t *testing.T) { rep.assertFatal(ctrl.Finish, "Controller.Finish was called more than once. It has to be called exactly once.") } -// Test WillByDefault call that is used to define a default behavior +// Test ByDefault call that is used to define a default behavior -func TestWillByDefaultOnly(t *testing.T) { +func TestByDefaultOnly(t *testing.T) { // no call reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", "something").WillByDefault().Return(5) + ctrl.RecordCall(subject, "FooMethod", "something").ByDefault().Return(5) ctrl.Finish() reporter.assertPass("not calling a function with defined default is ok") @@ -727,7 +727,7 @@ func TestWillByDefaultOnly(t *testing.T) { reporter, ctrl = createFixtures(t) subject = new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Return(5) rets := ctrl.Call(subject, "FooMethod", "123") if ret, ok := rets[0].(int); !ok { @@ -747,11 +747,11 @@ func TestWillByDefaultOnly(t *testing.T) { reporter.assertPass("calling a function with defined default n times with arbitrary parameters is ok") } -func TestWillByDefaultAndExpectationWithMissingCall(t *testing.T) { +func TestByDefaultAndExpectationWithMissingCall(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Return(5) ctrl.RecordCall(subject, "FooMethod", "123").Return(123) // does call default and not match expectation @@ -765,12 +765,12 @@ func TestWillByDefaultAndExpectationWithMissingCall(t *testing.T) { }, "aborting test due to missing call(s)") } -func TestWillByDefaultAndExpectationWithAllExpectationsMet(t *testing.T) { +func TestByDefaultAndExpectationWithAllExpectationsMet(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) // every expectation should have precedence over default call - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(5) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Return(5) ctrl.RecordCall(subject, "FooMethod", "123").Return(123) ctrl.RecordCall(subject, "FooMethod", "345").Return(345) @@ -810,12 +810,12 @@ func TestWillByDefaultAndExpectationWithAllExpectationsMet(t *testing.T) { reporter.assertPass("expectations should have precedence over default call") } -func TestOverwriteWillByDefault(t *testing.T) { +func TestOverwriteByDefault(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) // first defaultCall - ctrl.RecordCall(subject, "FooMethod", "defaultCall").WillByDefault().Return(123) + ctrl.RecordCall(subject, "FooMethod", "defaultCall").ByDefault().Return(123) // uses current default rets := ctrl.Call(subject, "FooMethod", "defaultCall") @@ -826,7 +826,7 @@ func TestOverwriteWillByDefault(t *testing.T) { } // overwrite default (when second one matches at least all parameters that first one matched) - ctrl.RecordCall(subject, "FooMethod", "defaultCall").WillByDefault().Return(456) + ctrl.RecordCall(subject, "FooMethod", "defaultCall").ByDefault().Return(456) // matches new default rets = ctrl.Call(subject, "FooMethod", "defaultCall") @@ -837,7 +837,7 @@ func TestOverwriteWillByDefault(t *testing.T) { } // overwrite default with more loose one - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Return(789) + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Return(789) // matches new default rets = ctrl.Call(subject, "FooMethod", "defaultCall") @@ -851,11 +851,11 @@ func TestOverwriteWillByDefault(t *testing.T) { reporter.assertPass("should always take the latest default definition") } -func TestWillByDefaultWithMissingReturn(t *testing.T) { +func TestByDefaultWithMissingReturn(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault() rets := ctrl.Call(subject, "FooMethod", "something") if ret, ok := rets[0].(int); !ok { @@ -868,120 +868,72 @@ func TestWillByDefaultWithMissingReturn(t *testing.T) { reporter.assertPass("should return zero value on missing return definition") } -func TestWillByDefaultMinMaxTimes(t *testing.T) { - - // test MinTimes failes +func TestByDefaultMinMaxTimesNotAllowed(t *testing.T) { + // test MinTimes not allowed reporter, ctrl := createFixtures(t) subject := new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MinTimes(2).Return(5) - - // only one call: should fail expectation - ctrl.Call(subject, "FooMethod", "something") - reporter.assertFatal(func() { - ctrl.Finish() - }, "aborting test due to missing call(s)") + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().MinTimes(2).Return(5) + }, "MinTimes() is not allowed when using ByDefault()") - // test MinTimes succeeds + // test MaxTimes not allowed reporter, ctrl = createFixtures(t) subject = new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MinTimes(2).Return(5) - - // two calls: should meet expectations - ctrl.Call(subject, "FooMethod", "something") - ctrl.Call(subject, "FooMethod", "something") - - ctrl.Finish() - reporter.assertPass("calling default for at least MinTime should work") + reporter.assertFatal(func() { + // call 1 more than MaxTimes should fail + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().MaxTimes(2).Return(5) + }, "MaxTimes() is not allowed when using ByDefault()") - // test MaxTimes failes + // test AnyTimes not allowed reporter, ctrl = createFixtures(t) subject = new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MaxTimes(2).Return(5) - - ctrl.Call(subject, "FooMethod", "something") - ctrl.Call(subject, "FooMethod", "something") - reporter.assertFatal(func() { // call 1 more than MaxTimes should fail - ctrl.Call(subject, "FooMethod", "something") - ctrl.Finish() - }, "Unexpected call to", "has already been called the max number of times.") + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().AnyTimes().Return(5) + }, "AnyTimes() is not allowed when using ByDefault()") - // test MaxTimes succeeds + // test Times not allowed reporter, ctrl = createFixtures(t) subject = new(Subject) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().MaxTimes(2).Return(5) - - // call no more than MaxTimes should work - ctrl.Call(subject, "FooMethod", "something") - - ctrl.Finish() - reporter.assertPass("calling default for at most MaxTimes should work") + reporter.assertFatal(func() { + // call 1 more than MaxTimes should fail + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Times(4).Return(5) + }, "Times() is not allowed when using ByDefault()") } -func TestWillByDefaultAfterFunction(t *testing.T) { - // default call should fail if prerequisite not called +func TestByDefaultAfterNotAllowed(t *testing.T) { + // test After not allowed reporter, ctrl := createFixtures(t) subject := new(Subject) someCall := ctrl.RecordCall(subject, "FooMethod", "123").Return(123) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().After(someCall) reporter.assertFatal(func() { - ctrl.Call(subject, "FooMethod", "something") - }, "") + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().After(someCall) + }, "ByDefault() isn't allowed to have prerequisites") - // should work if prerequisite called + // test default call used as prerequisite not allowed reporter, ctrl = createFixtures(t) subject = new(Subject) - someCall = ctrl.RecordCall(subject, "FooMethod", "123").Return(123) - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().After(someCall) - - ctrl.Call(subject, "FooMethod", "123") - ctrl.Call(subject, "FooMethod", "something") - - ctrl.Finish() - reporter.assertPass("should work if prerequisite called before") + defaultCall := ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault() - // default call used as prerequisite should work - reporter, ctrl = createFixtures(t) - subject = new(Subject) - - defaultCall := ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() - ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(defaultCall) - - ctrl.Call(subject, "FooMethod", "something") - ctrl.Call(subject, "FooMethod", "123") - - ctrl.Finish() - reporter.assertPass("defaultCall called as prerequisite should work") - - // default call used as prerequisite should work even if not called (0 times minimum by default) - reporter, ctrl = createFixtures(t) - subject = new(Subject) - - defaultCall = ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault() - ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(defaultCall) - - ctrl.Call(subject, "FooMethod", "123") - - ctrl.Finish() - reporter.assertPass("defaultCall called as prerequisite should work") + reporter.assertFatal(func() { + ctrl.RecordCall(subject, "FooMethod", "123").Return(123).After(defaultCall) + }, "Default isn't allowed to be a prerequisite") } -func TestWillByDefaultCallsDoFunc(t *testing.T) { +func TestByDefaultCallsDoFunc(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) str := "" - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().Do(func(s string) { + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Do(func(s string) { str = s }) @@ -995,13 +947,13 @@ func TestWillByDefaultCallsDoFunc(t *testing.T) { reporter.assertPass("defaultCall should ignore After()") } -func TestWillByDefaultCallsDoAndReturnFunc(t *testing.T) { +func TestByDefaultCallsDoAndReturnFunc(t *testing.T) { reporter, ctrl := createFixtures(t) subject := new(Subject) str := "" - ctrl.RecordCall(subject, "FooMethod", gomock.Any()).WillByDefault().DoAndReturn(func(s string) int { + ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().DoAndReturn(func(s string) int { str = s return 5 }) From 40ef78d356bddc3a4733673506b4fba1c0d37760 Mon Sep 17 00:00:00 2001 From: Alexander Gehres Date: Wed, 6 Jun 2018 18:55:42 +0200 Subject: [PATCH 5/5] removed some wrong comments --- gomock/call.go | 2 +- gomock/controller_test.go | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index 9167afde..7624d50b 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -81,7 +81,7 @@ func newCall(t TestReporter, receiver interface{}, method string, methodType ref // ByDefault defines this expectation as a default that is tried to match if no other expectation matches. // To use this as catch-all you may use Any() matcher for the method parameters. -// ByDefault expects to be called 0 or more times. This can be overwritten by Min/MaxTimes() methods. +// ByDefault expects to be called 0 or more times. func (c *Call) ByDefault() *Call { if c.minCalls != 1 || c.maxCalls != 1 { c.t.Fatalf("MinTimes(), MaxTimes(), Times() or AnyTimes() is not allowed when using ByDefault()") diff --git a/gomock/controller_test.go b/gomock/controller_test.go index 6d63e673..837ff4c9 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -882,7 +882,6 @@ func TestByDefaultMinMaxTimesNotAllowed(t *testing.T) { subject = new(Subject) reporter.assertFatal(func() { - // call 1 more than MaxTimes should fail ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().MaxTimes(2).Return(5) }, "MaxTimes() is not allowed when using ByDefault()") @@ -891,7 +890,6 @@ func TestByDefaultMinMaxTimesNotAllowed(t *testing.T) { subject = new(Subject) reporter.assertFatal(func() { - // call 1 more than MaxTimes should fail ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().AnyTimes().Return(5) }, "AnyTimes() is not allowed when using ByDefault()") @@ -900,7 +898,6 @@ func TestByDefaultMinMaxTimesNotAllowed(t *testing.T) { subject = new(Subject) reporter.assertFatal(func() { - // call 1 more than MaxTimes should fail ctrl.RecordCall(subject, "FooMethod", gomock.Any()).ByDefault().Times(4).Return(5) }, "Times() is not allowed when using ByDefault()") }