call.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2010 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. "fmt"
  17. "reflect"
  18. "strings"
  19. )
  20. // Call represents an expected call to a mock.
  21. type Call struct {
  22. t TestReporter // for triggering test failures on invalid call setup
  23. receiver interface{} // the receiver of the method call
  24. method string // the name of the method
  25. methodType reflect.Type // the type of the method
  26. args []Matcher // the args
  27. rets []interface{} // the return values (if any)
  28. preReqs []*Call // prerequisite calls
  29. // Expectations
  30. minCalls, maxCalls int
  31. numCalls int // actual number made
  32. // Actions
  33. doFunc reflect.Value
  34. setArgs map[int]reflect.Value
  35. }
  36. // AnyTimes allows the expectation to be called 0 or more times
  37. func (c *Call) AnyTimes() *Call {
  38. c.minCalls, c.maxCalls = 0, 1e8 // close enough to infinity
  39. return c
  40. }
  41. // MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called, MinTimes also
  42. // sets the maximum number of calls to infinity.
  43. func (c *Call) MinTimes(n int) *Call {
  44. c.minCalls = n
  45. if c.maxCalls == 1 {
  46. c.maxCalls = 1e8
  47. }
  48. return c
  49. }
  50. // MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called, MaxTimes also
  51. // sets the minimum number of calls to 0.
  52. func (c *Call) MaxTimes(n int) *Call {
  53. c.maxCalls = n
  54. if c.minCalls == 1 {
  55. c.minCalls = 0
  56. }
  57. return c
  58. }
  59. // Do declares the action to run when the call is matched.
  60. // It takes an interface{} argument to support n-arity functions.
  61. func (c *Call) Do(f interface{}) *Call {
  62. // TODO: Check arity and types here, rather than dying badly elsewhere.
  63. c.doFunc = reflect.ValueOf(f)
  64. return c
  65. }
  66. func (c *Call) Return(rets ...interface{}) *Call {
  67. mt := c.methodType
  68. if len(rets) != mt.NumOut() {
  69. c.t.Fatalf("wrong number of arguments to Return for %T.%v: got %d, want %d",
  70. c.receiver, c.method, len(rets), mt.NumOut())
  71. }
  72. for i, ret := range rets {
  73. if got, want := reflect.TypeOf(ret), mt.Out(i); got == want {
  74. // Identical types; nothing to do.
  75. } else if got == nil {
  76. // Nil needs special handling.
  77. switch want.Kind() {
  78. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  79. // ok
  80. default:
  81. c.t.Fatalf("argument %d to Return for %T.%v is nil, but %v is not nillable",
  82. i, c.receiver, c.method, want)
  83. }
  84. } else if got.AssignableTo(want) {
  85. // Assignable type relation. Make the assignment now so that the generated code
  86. // can return the values with a type assertion.
  87. v := reflect.New(want).Elem()
  88. v.Set(reflect.ValueOf(ret))
  89. rets[i] = v.Interface()
  90. } else {
  91. c.t.Fatalf("wrong type of argument %d to Return for %T.%v: %v is not assignable to %v",
  92. i, c.receiver, c.method, got, want)
  93. }
  94. }
  95. c.rets = rets
  96. return c
  97. }
  98. func (c *Call) Times(n int) *Call {
  99. c.minCalls, c.maxCalls = n, n
  100. return c
  101. }
  102. // SetArg declares an action that will set the nth argument's value,
  103. // indirected through a pointer.
  104. func (c *Call) SetArg(n int, value interface{}) *Call {
  105. if c.setArgs == nil {
  106. c.setArgs = make(map[int]reflect.Value)
  107. }
  108. mt := c.methodType
  109. // TODO: This will break on variadic methods.
  110. // We will need to check those at invocation time.
  111. if n < 0 || n >= mt.NumIn() {
  112. c.t.Fatalf("SetArg(%d, ...) called for a method with %d args", n, mt.NumIn())
  113. }
  114. // Permit setting argument through an interface.
  115. // In the interface case, we don't (nay, can't) check the type here.
  116. at := mt.In(n)
  117. switch at.Kind() {
  118. case reflect.Ptr:
  119. dt := at.Elem()
  120. if vt := reflect.TypeOf(value); !vt.AssignableTo(dt) {
  121. c.t.Fatalf("SetArg(%d, ...) argument is a %v, not assignable to %v", n, vt, dt)
  122. }
  123. case reflect.Interface:
  124. // nothing to do
  125. default:
  126. c.t.Fatalf("SetArg(%d, ...) referring to argument of non-pointer non-interface type %v", n, at)
  127. }
  128. c.setArgs[n] = reflect.ValueOf(value)
  129. return c
  130. }
  131. // isPreReq returns true if other is a direct or indirect prerequisite to c.
  132. func (c *Call) isPreReq(other *Call) bool {
  133. for _, preReq := range c.preReqs {
  134. if other == preReq || preReq.isPreReq(other) {
  135. return true
  136. }
  137. }
  138. return false
  139. }
  140. // After declares that the call may only match after preReq has been exhausted.
  141. func (c *Call) After(preReq *Call) *Call {
  142. if c == preReq {
  143. c.t.Fatalf("A call isn't allowed to be it's own prerequisite")
  144. }
  145. if preReq.isPreReq(c) {
  146. c.t.Fatalf("Loop in call order: %v is a prerequisite to %v (possibly indirectly).", c, preReq)
  147. }
  148. c.preReqs = append(c.preReqs, preReq)
  149. return c
  150. }
  151. // Returns true iff the minimum number of calls have been made.
  152. func (c *Call) satisfied() bool {
  153. return c.numCalls >= c.minCalls
  154. }
  155. // Returns true iff the maximum number of calls have been made.
  156. func (c *Call) exhausted() bool {
  157. return c.numCalls >= c.maxCalls
  158. }
  159. func (c *Call) String() string {
  160. args := make([]string, len(c.args))
  161. for i, arg := range c.args {
  162. args[i] = arg.String()
  163. }
  164. arguments := strings.Join(args, ", ")
  165. return fmt.Sprintf("%T.%v(%s)", c.receiver, c.method, arguments)
  166. }
  167. // Tests if the given call matches the expected call.
  168. func (c *Call) matches(args []interface{}) bool {
  169. if len(args) != len(c.args) {
  170. return false
  171. }
  172. for i, m := range c.args {
  173. if !m.Matches(args[i]) {
  174. return false
  175. }
  176. }
  177. // Check that all prerequisite calls have been satisfied.
  178. for _, preReqCall := range c.preReqs {
  179. if !preReqCall.satisfied() {
  180. return false
  181. }
  182. }
  183. return true
  184. }
  185. // dropPrereqs tells the expected Call to not re-check prerequite calls any
  186. // longer, and to return its current set.
  187. func (c *Call) dropPrereqs() (preReqs []*Call) {
  188. preReqs = c.preReqs
  189. c.preReqs = nil
  190. return
  191. }
  192. func (c *Call) call(args []interface{}) (rets []interface{}, action func()) {
  193. c.numCalls++
  194. // Actions
  195. if c.doFunc.IsValid() {
  196. doArgs := make([]reflect.Value, len(args))
  197. ft := c.doFunc.Type()
  198. for i := 0; i < len(args); i++ {
  199. if args[i] != nil {
  200. doArgs[i] = reflect.ValueOf(args[i])
  201. } else {
  202. // Use the zero value for the arg.
  203. doArgs[i] = reflect.Zero(ft.In(i))
  204. }
  205. }
  206. action = func() { c.doFunc.Call(doArgs) }
  207. }
  208. for n, v := range c.setArgs {
  209. reflect.ValueOf(args[n]).Elem().Set(v)
  210. }
  211. rets = c.rets
  212. if rets == nil {
  213. // Synthesize the zero value for each of the return args' types.
  214. mt := c.methodType
  215. rets = make([]interface{}, mt.NumOut())
  216. for i := 0; i < mt.NumOut(); i++ {
  217. rets[i] = reflect.Zero(mt.Out(i)).Interface()
  218. }
  219. }
  220. return
  221. }
  222. // InOrder declares that the given calls should occur in order.
  223. func InOrder(calls ...*Call) {
  224. for i := 1; i < len(calls); i++ {
  225. calls[i].After(calls[i-1])
  226. }
  227. }