Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

77 řádky
2.0 KiB

  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. package gomock
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. type receiverType struct{}
  20. func (receiverType) Func() {}
  21. func TestCallSetAdd(t *testing.T) {
  22. method := "TestMethod"
  23. var receiver interface{} = "TestReceiver"
  24. cs := newCallSet()
  25. numCalls := 10
  26. for i := 0; i < numCalls; i++ {
  27. cs.Add(newCall(t, receiver, method, reflect.TypeOf(receiverType{}.Func)))
  28. }
  29. call, err := cs.FindMatch(receiver, method, []interface{}{})
  30. if err != nil {
  31. t.Fatalf("FindMatch: %v", err)
  32. }
  33. if call == nil {
  34. t.Fatalf("FindMatch: Got nil, want non-nil *Call")
  35. }
  36. }
  37. func TestCallSetRemove(t *testing.T) {
  38. method := "TestMethod"
  39. var receiver interface{} = "TestReceiver"
  40. cs := newCallSet()
  41. ourCalls := []*Call{}
  42. numCalls := 10
  43. for i := 0; i < numCalls; i++ {
  44. // NOTE: abuse the `numCalls` value to convey initial ordering of mocked calls
  45. generatedCall := &Call{receiver: receiver, method: method, numCalls: i}
  46. cs.Add(generatedCall)
  47. ourCalls = append(ourCalls, generatedCall)
  48. }
  49. // validateOrder validates that the calls in the array are ordered as they were added
  50. validateOrder := func(calls []*Call) {
  51. // lastNum tracks the last `numCalls` (call order) value seen
  52. lastNum := -1
  53. for _, c := range calls {
  54. if lastNum >= c.numCalls {
  55. t.Errorf("found call %d after call %d", c.numCalls, lastNum)
  56. }
  57. lastNum = c.numCalls
  58. }
  59. }
  60. for _, c := range ourCalls {
  61. validateOrder(cs.expected[callSetKey{receiver, method}])
  62. cs.Remove(c)
  63. }
  64. }