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

add tests for callSet Add & Remove #108

Merged
merged 2 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (c *Call) Do(f interface{}) *Call {
return c
}

// Return declares the values to be returned by the mocked function call.
func (c *Call) Return(rets ...interface{}) *Call {
mt := c.methodType
if len(rets) != mt.NumOut() {
Expand Down Expand Up @@ -112,6 +113,7 @@ func (c *Call) Return(rets ...interface{}) *Call {
return c
}

// Times declares the exact number of times a function call is expected to be executed.
func (c *Call) Times(n int) *Call {
c.minCalls, c.maxCalls = n, n
return c
Expand Down Expand Up @@ -146,7 +148,7 @@ func (c *Call) SetArg(n int, value interface{}) *Call {
case reflect.Slice:
// nothing to do
default:
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v",
c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface non-slice type %v [%s]",
n, at, c.origin)
}
c.setArgs[n] = reflect.ValueOf(value)
Expand Down Expand Up @@ -204,7 +206,7 @@ func (c *Call) matches(args []interface{}) error {
}
for i, m := range c.args {
if !m.Matches(args[i]) {
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v\n",
return fmt.Errorf("Expected call at %s doesn't match the argument at index %s.\nGot: %v\nWant: %v",
c.origin, strconv.Itoa(i), args[i], m)
}
}
Expand Down
71 changes: 71 additions & 0 deletions gomock/callset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gomock

import "testing"

func TestCallSetAdd(t *testing.T) {
methodVal := "TestMethod"
var receiverVal interface{} = "TestReceiver"
cs := make(callSet)

numCalls := 10
for i := 0; i < numCalls; i++ {
cs.Add(&Call{receiver: receiverVal, method: methodVal})
}

if len(cs) != 1 {
t.Errorf("expected only one reciever in callSet")
}
if numActualMethods := len(cs[receiverVal]); numActualMethods != 1 {
t.Errorf("expected only method on the reciever in callSet, found %d", numActualMethods)
}
if numActualCalls := len(cs[receiverVal][methodVal]); numActualCalls != numCalls {
t.Errorf("expected all %d calls in callSet, found %d", numCalls, numActualCalls)
}
}

func TestCallSetRemove(t *testing.T) {
methodVal := "TestMethod"
var receiverVal interface{} = "TestReceiver"

cs := make(callSet)
ourCalls := []*Call{}

numCalls := 10
for i := 0; i < numCalls; i++ {
// NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls
generatedCall := &Call{receiver: receiverVal, method: methodVal, numCalls: i}
cs.Add(generatedCall)
ourCalls = append(ourCalls, generatedCall)
}

// validateOrder validates that the calls in the array are ordered as they were added
validateOrder := func(calls []*Call) {
// lastNum tracks the last `numCalls` (call order) value seen
lastNum := -1
for _, c := range calls {
if lastNum >= c.numCalls {
t.Errorf("found call %d after call %d", c.numCalls, lastNum)
}
lastNum = c.numCalls
}
}

for _, c := range ourCalls {
validateOrder(cs[receiverVal][methodVal])
cs.Remove(c)
}
}