From 6fb19fede01af1e774848d3b983c9cb452da0843 Mon Sep 17 00:00:00 2001 From: Conor Horan-Kates Date: Thu, 3 Oct 2024 16:31:10 -0600 Subject: [PATCH 1/2] AtLeast(times int) implementation --- mock/mock.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mock/mock.go b/mock/mock.go index 41257fbeb..a2703fd83 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -57,6 +57,9 @@ type Call struct { // Amount of times this call has been called totalCalls int + // Minimum number of times this call should be called + minimumCalls int + // Call to this method can be optional optional bool @@ -151,6 +154,19 @@ func (c *Call) Times(i int) *Call { return c } +// AtLeast indicates that the mock should return at least the indicated number +// of times. The mock will always return, but the count will be verified +// when AssertExpectations() is called. +// +// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).AtLeast(2) +func (c *Call) AtLeast(i int) *Call { + c.lock() + defer c.unlock() + c.Repeatability = 0 + c.minimumCalls = i + return c +} + // WaitUntil sets the channel that will block the mock's return until its closed // or a message is received. // @@ -644,6 +660,9 @@ func (m *Mock) checkExpectation(call *Call) (bool, string) { if call.Repeatability > 0 { return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) } + if call.minimumCalls >= call.totalCalls { + return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) + } return true, fmt.Sprintf("PASS:\t%s(%s)", call.Method, call.Arguments.String()) } From 54181502f27f71dc0881df575e9374e67d072a7f Mon Sep 17 00:00:00 2001 From: Conor Horan-Kates Date: Thu, 3 Oct 2024 19:19:53 -0600 Subject: [PATCH 2/2] init and boundary cases --- mock/mock.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mock/mock.go b/mock/mock.go index a2703fd83..4c9365955 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -91,6 +91,7 @@ func newCall(parent *Mock, methodName string, callerInfo []string, methodArgumen ReturnArguments: make([]interface{}, 0), callerInfo: callerInfo, Repeatability: 0, + minimumCalls: 0, WaitFor: nil, RunFn: nil, PanicMsg: nil, @@ -660,7 +661,7 @@ func (m *Mock) checkExpectation(call *Call) (bool, string) { if call.Repeatability > 0 { return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) } - if call.minimumCalls >= call.totalCalls { + if call.minimumCalls > 0 && call.minimumCalls >= call.totalCalls { return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo) } return true, fmt.Sprintf("PASS:\t%s(%s)", call.Method, call.Arguments.String())