|
| 1 | +// Copyright 2011 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gomock |
| 16 | + |
| 17 | +import "testing" |
| 18 | + |
| 19 | +func TestCallSetAdd(t *testing.T) { |
| 20 | + methodVal := "TestMethod" |
| 21 | + var receiverVal interface{} = "TestReceiver" |
| 22 | + cs := make(callSet) |
| 23 | + |
| 24 | + numCalls := 10 |
| 25 | + for i := 0; i < numCalls; i++ { |
| 26 | + cs.Add(&Call{receiver: receiverVal, method: methodVal}) |
| 27 | + } |
| 28 | + |
| 29 | + if len(cs) != 1 { |
| 30 | + t.Errorf("expected only one reciever in callSet") |
| 31 | + } |
| 32 | + if numActualMethods := len(cs[receiverVal]); numActualMethods != 1 { |
| 33 | + t.Errorf("expected only method on the reciever in callSet, found %d", numActualMethods) |
| 34 | + } |
| 35 | + if numActualCalls := len(cs[receiverVal][methodVal]); numActualCalls != numCalls { |
| 36 | + t.Errorf("expected all %d calls in callSet, found %d", numCalls, numActualCalls) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestCallSetRemove(t *testing.T) { |
| 41 | + methodVal := "TestMethod" |
| 42 | + var receiverVal interface{} = "TestReceiver" |
| 43 | + |
| 44 | + cs := make(callSet) |
| 45 | + ourCalls := []*Call{} |
| 46 | + |
| 47 | + numCalls := 10 |
| 48 | + for i := 0; i < numCalls; i++ { |
| 49 | + // NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls |
| 50 | + generatedCall := &Call{receiver: receiverVal, method: methodVal, numCalls: i} |
| 51 | + cs.Add(generatedCall) |
| 52 | + ourCalls = append(ourCalls, generatedCall) |
| 53 | + } |
| 54 | + |
| 55 | + // validateOrder validates that the calls in the array are ordered as they were added |
| 56 | + validateOrder := func(calls []*Call) { |
| 57 | + // lastNum tracks the last `numCalls` (call order) value seen |
| 58 | + lastNum := -1 |
| 59 | + for _, c := range calls { |
| 60 | + if lastNum >= c.numCalls { |
| 61 | + t.Errorf("found call %d after call %d", c.numCalls, lastNum) |
| 62 | + } |
| 63 | + lastNum = c.numCalls |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + for _, c := range ourCalls { |
| 68 | + validateOrder(cs[receiverVal][methodVal]) |
| 69 | + cs.Remove(c) |
| 70 | + } |
| 71 | +} |
0 commit comments