Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Add extra methods to control the number of times a call is expected.
Browse files Browse the repository at this point in the history
The following methods are added:

* MinTimes(int) *Call
* MaxTimes(int) *Call

These additions a user full control over the number of times an
expectation is called.

Signed-off-by: David Symonds <dsymonds@golang.org>
  • Loading branch information
Daniel Zepeda authored and dsymonds committed Jan 27, 2016
1 parent 58a501f commit bd3c8e8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,32 @@ type Call struct {
setArgs map[int]reflect.Value
}

// 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
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.
func (c *Call) MinTimes(n int) *Call {
c.minCalls = n
if c.maxCalls == 1 {
c.maxCalls = 1e8
}
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.
func (c *Call) MaxTimes(n int) *Call {
c.maxCalls = n
if c.minCalls == 1 {
c.minCalls = 0
}
return c
}

// Do declares the action to run when the call is matched.
// It takes an interface{} argument to support n-arity functions.
func (c *Call) Do(f interface{}) *Call {
Expand Down
80 changes: 80 additions & 0 deletions gomock/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,86 @@ func TestAnyTimes(t *testing.T) {
ctrl.Finish()
}

func TestMinTimes1(t *testing.T) {
// It fails if there are no calls
reporter, ctrl := createFixtures(t)
subject := new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
reporter.assertFatal(func() {
ctrl.Finish()
})

// It succeeds if there is one call
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Finish()

// It succeeds if there are many calls
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(1)
for i := 0; i < 100; i++ {
ctrl.Call(subject, "FooMethod", "argument")
}
ctrl.Finish()
}

func TestMaxTimes1(t *testing.T) {
// It succeeds if there are no calls
_, ctrl := createFixtures(t)
subject := new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
ctrl.Finish()

// It succeeds if there is one call
_, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Finish()

//It fails if there are more
reporter, ctrl := createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(1)
ctrl.Call(subject, "FooMethod", "argument")
reporter.assertFatal(func() {
ctrl.Call(subject, "FooMethod", "argument")
})
ctrl.Finish()
}

func TestMinMaxTimes(t *testing.T) {
// It fails if there are less calls than specified
reporter, ctrl := createFixtures(t)
subject := new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
ctrl.Call(subject, "FooMethod", "argument")
reporter.assertFatal(func() {
ctrl.Finish()
})

// It fails if there are more calls than specified
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MinTimes(2).MaxTimes(2)
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Call(subject, "FooMethod", "argument")
reporter.assertFatal(func() {
ctrl.Call(subject, "FooMethod", "argument")
})

// It succeeds if there is just the right number of calls
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").MaxTimes(2).MinTimes(2)
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Finish()
}

func TestDo(t *testing.T) {
_, ctrl := createFixtures(t)
subject := new(Subject)
Expand Down

0 comments on commit bd3c8e8

Please sign in to comment.