From 5fc2922c09dc5576da9a4ddcd74bd2fa7eff609d Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 18 Oct 2019 16:50:52 -0500 Subject: [PATCH] Update documentation on MinTimes and MaxTimes. Closes #331 --- gomock/call.go | 8 ++++---- gomock/controller_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/gomock/call.go b/gomock/call.go index d3d195c5..be60e413 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -82,8 +82,8 @@ func (c *Call) AnyTimes() *Call { return c } -// 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. +// MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called or if MaxTimes +// was previously called with 1, MinTimes also sets the maximum number of calls to infinity. func (c *Call) MinTimes(n int) *Call { c.minCalls = n if c.maxCalls == 1 { @@ -92,8 +92,8 @@ func (c *Call) MinTimes(n int) *Call { return c } -// 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. +// MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called or if MinTimes was +// previously called with 1, MaxTimes also sets the minimum number of calls to 0. func (c *Call) MaxTimes(n int) *Call { c.maxCalls = n if c.minCalls == 1 { diff --git a/gomock/controller_test.go b/gomock/controller_test.go index d85cf9de..0f1eb01f 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -407,6 +407,25 @@ func TestMinMaxTimes(t *testing.T) { ctrl.Call(subject, "FooMethod", "argument") ctrl.Call(subject, "FooMethod", "argument") ctrl.Finish() + + // If MaxTimes is called after MinTimes is called with 1, MaxTimes takes precedence. + reporter, ctrl = createFixtures(t) + subject = new(Subject) + ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1).MaxTimes(2) + ctrl.Call(subject, "FooMethod", "argument") + ctrl.Call(subject, "FooMethod", "argument") + reporter.assertFatal(func() { + ctrl.Call(subject, "FooMethod", "argument") + }) + + // If MinTimes is called after MaxTimes is called with 1, MinTimes takes precedence. + reporter, ctrl = createFixtures(t) + subject = new(Subject) + ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1).MinTimes(2) + for i := 0; i < 100; i++ { + ctrl.Call(subject, "FooMethod", "argument") + } + ctrl.Finish() } func TestDo(t *testing.T) {