OLD | NEW |
(Empty) | |
| 1 $$ -*- mode: c++; -*- |
| 2 $$ This is a Pump source file. Please use Pump to convert it to |
| 3 $$ gmock-generated-variadic-actions.h. |
| 4 $$ |
| 5 $var n = 10 $$ The maximum arity we support. |
| 6 $$}} This meta comment fixes auto-indentation in editors. |
| 7 // Copyright 2007, Google Inc. |
| 8 // All rights reserved. |
| 9 // |
| 10 // Redistribution and use in source and binary forms, with or without |
| 11 // modification, are permitted provided that the following conditions are |
| 12 // met: |
| 13 // |
| 14 // * Redistributions of source code must retain the above copyright |
| 15 // notice, this list of conditions and the following disclaimer. |
| 16 // * Redistributions in binary form must reproduce the above |
| 17 // copyright notice, this list of conditions and the following disclaimer |
| 18 // in the documentation and/or other materials provided with the |
| 19 // distribution. |
| 20 // * Neither the name of Google Inc. nor the names of its |
| 21 // contributors may be used to endorse or promote products derived from |
| 22 // this software without specific prior written permission. |
| 23 // |
| 24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 // |
| 36 // Author: wan@google.com (Zhanyong Wan) |
| 37 |
| 38 // Google Mock - a framework for writing C++ mock classes. |
| 39 // |
| 40 // This file implements some commonly used variadic actions. |
| 41 |
| 42 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |
| 43 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |
| 44 |
| 45 #include <gmock/gmock-actions.h> |
| 46 #include <gmock/internal/gmock-port.h> |
| 47 |
| 48 namespace testing { |
| 49 namespace internal { |
| 50 |
| 51 // InvokeHelper<F> knows how to unpack an N-tuple and invoke an N-ary |
| 52 // function or method with the unpacked values, where F is a function |
| 53 // type that takes N arguments. |
| 54 template <typename Result, typename ArgumentTuple> |
| 55 class InvokeHelper; |
| 56 |
| 57 |
| 58 $range i 0..n |
| 59 $for i [[ |
| 60 $range j 1..i |
| 61 $var types = [[$for j [[, typename A$j]]]] |
| 62 $var as = [[$for j, [[A$j]]]] |
| 63 $var args = [[$if i==0 [[]] $else [[ args]]]] |
| 64 $var import = [[$if i==0 [[]] $else [[ |
| 65 using ::std::tr1::get; |
| 66 |
| 67 ]]]] |
| 68 $var gets = [[$for j, [[get<$(j - 1)>(args)]]]] |
| 69 template <typename R$types> |
| 70 class InvokeHelper<R, ::std::tr1::tuple<$as> > { |
| 71 public: |
| 72 template <typename Function> |
| 73 static R Invoke(Function function, const ::std::tr1::tuple<$as>&$args) { |
| 74 $import return function($gets); |
| 75 } |
| 76 |
| 77 template <class Class, typename MethodPtr> |
| 78 static R InvokeMethod(Class* obj_ptr, |
| 79 MethodPtr method_ptr, |
| 80 const ::std::tr1::tuple<$as>&$args) { |
| 81 $import return (obj_ptr->*method_ptr)($gets); |
| 82 } |
| 83 }; |
| 84 |
| 85 |
| 86 ]] |
| 87 |
| 88 // Implements the Invoke(f) action. The template argument |
| 89 // FunctionImpl is the implementation type of f, which can be either a |
| 90 // function pointer or a functor. Invoke(f) can be used as an |
| 91 // Action<F> as long as f's type is compatible with F (i.e. f can be |
| 92 // assigned to a tr1::function<F>). |
| 93 template <typename FunctionImpl> |
| 94 class InvokeAction { |
| 95 public: |
| 96 // The c'tor makes a copy of function_impl (either a function |
| 97 // pointer or a functor). |
| 98 explicit InvokeAction(FunctionImpl function_impl) |
| 99 : function_impl_(function_impl) {} |
| 100 |
| 101 template <typename Result, typename ArgumentTuple> |
| 102 Result Perform(const ArgumentTuple& args) { |
| 103 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); |
| 104 } |
| 105 private: |
| 106 FunctionImpl function_impl_; |
| 107 }; |
| 108 |
| 109 // Implements the Invoke(object_ptr, &Class::Method) action. |
| 110 template <class Class, typename MethodPtr> |
| 111 class InvokeMethodAction { |
| 112 public: |
| 113 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) |
| 114 : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
| 115 |
| 116 template <typename Result, typename ArgumentTuple> |
| 117 Result Perform(const ArgumentTuple& args) const { |
| 118 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( |
| 119 obj_ptr_, method_ptr_, args); |
| 120 } |
| 121 private: |
| 122 Class* const obj_ptr_; |
| 123 const MethodPtr method_ptr_; |
| 124 }; |
| 125 |
| 126 // A ReferenceWrapper<T> object represents a reference to type T, |
| 127 // which can be either const or not. It can be explicitly converted |
| 128 // from, and implicitly converted to, a T&. Unlike a reference, |
| 129 // ReferenceWrapper<T> can be copied and can survive template type |
| 130 // inference. This is used to support by-reference arguments in the |
| 131 // InvokeArgument<N>(...) action. The idea was from "reference |
| 132 // wrappers" in tr1, which we don't have in our source tree yet. |
| 133 template <typename T> |
| 134 class ReferenceWrapper { |
| 135 public: |
| 136 // Constructs a ReferenceWrapper<T> object from a T&. |
| 137 explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT |
| 138 |
| 139 // Allows a ReferenceWrapper<T> object to be implicitly converted to |
| 140 // a T&. |
| 141 operator T&() const { return *pointer_; } |
| 142 private: |
| 143 T* pointer_; |
| 144 }; |
| 145 |
| 146 // CallableHelper has static methods for invoking "callables", |
| 147 // i.e. function pointers and functors. It uses overloading to |
| 148 // provide a uniform interface for invoking different kinds of |
| 149 // callables. In particular, you can use: |
| 150 // |
| 151 // CallableHelper<R>::Call(callable, a1, a2, ..., an) |
| 152 // |
| 153 // to invoke an n-ary callable, where R is its return type. If an |
| 154 // argument, say a2, needs to be passed by reference, you should write |
| 155 // ByRef(a2) instead of a2 in the above expression. |
| 156 template <typename R> |
| 157 class CallableHelper { |
| 158 public: |
| 159 // Calls a nullary callable. |
| 160 template <typename Function> |
| 161 static R Call(Function function) { return function(); } |
| 162 |
| 163 // Calls a unary callable. |
| 164 |
| 165 // We deliberately pass a1 by value instead of const reference here |
| 166 // in case it is a C-string literal. If we had declared the |
| 167 // parameter as 'const A1& a1' and write Call(function, "Hi"), the |
| 168 // compiler would've thought A1 is 'char[3]', which causes trouble |
| 169 // when you need to copy a value of type A1. By declaring the |
| 170 // parameter as 'A1 a1', the compiler will correctly infer that A1 |
| 171 // is 'const char*' when it sees Call(function, "Hi"). |
| 172 // |
| 173 // Since this function is defined inline, the compiler can get rid |
| 174 // of the copying of the arguments. Therefore the performance won't |
| 175 // be hurt. |
| 176 template <typename Function, typename A1> |
| 177 static R Call(Function function, A1 a1) { return function(a1); } |
| 178 |
| 179 $range i 2..n |
| 180 $for i |
| 181 [[ |
| 182 $var arity = [[$if i==2 [[binary]] $elif i==3 [[ternary]] $else [[$i-ary]]]] |
| 183 |
| 184 // Calls a $arity callable. |
| 185 |
| 186 $range j 1..i |
| 187 $var typename_As = [[$for j, [[typename A$j]]]] |
| 188 $var Aas = [[$for j, [[A$j a$j]]]] |
| 189 $var as = [[$for j, [[a$j]]]] |
| 190 $var typename_Ts = [[$for j, [[typename T$j]]]] |
| 191 $var Ts = [[$for j, [[T$j]]]] |
| 192 template <typename Function, $typename_As> |
| 193 static R Call(Function function, $Aas) { |
| 194 return function($as); |
| 195 } |
| 196 |
| 197 ]] |
| 198 |
| 199 }; // class CallableHelper |
| 200 |
| 201 // An INTERNAL macro for extracting the type of a tuple field. It's |
| 202 // subject to change without notice - DO NOT USE IN USER CODE! |
| 203 #define GMOCK_FIELD_(Tuple, N) \ |
| 204 typename ::std::tr1::tuple_element<N, Tuple>::type |
| 205 |
| 206 $range i 1..n |
| 207 |
| 208 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::type is the |
| 209 // type of an n-ary function whose i-th (1-based) argument type is the |
| 210 // k{i}-th (0-based) field of ArgumentTuple, which must be a tuple |
| 211 // type, and whose return type is Result. For example, |
| 212 // SelectArgs<int, ::std::tr1::tuple<bool, char, double, long>, 0, 3>::type |
| 213 // is int(bool, long). |
| 214 // |
| 215 // SelectArgs<Result, ArgumentTuple, k1, k2, ..., k_n>::Select(args) |
| 216 // returns the selected fields (k1, k2, ..., k_n) of args as a tuple. |
| 217 // For example, |
| 218 // SelectArgs<int, ::std::tr1::tuple<bool, char, double>, 2, 0>::Select( |
| 219 // ::std::tr1::make_tuple(true, 'a', 2.5)) |
| 220 // returns ::std::tr1::tuple (2.5, true). |
| 221 // |
| 222 // The numbers in list k1, k2, ..., k_n must be >= 0, where n can be |
| 223 // in the range [0, $n]. Duplicates are allowed and they don't have |
| 224 // to be in an ascending or descending order. |
| 225 |
| 226 template <typename Result, typename ArgumentTuple, $for i, [[int k$i]]> |
| 227 class SelectArgs { |
| 228 public: |
| 229 typedef Result type($for i, [[GMOCK_FIELD_(ArgumentTuple, k$i)]]); |
| 230 typedef typename Function<type>::ArgumentTuple SelectedArgs; |
| 231 static SelectedArgs Select(const ArgumentTuple& args) { |
| 232 using ::std::tr1::get; |
| 233 return SelectedArgs($for i, [[get<k$i>(args)]]); |
| 234 } |
| 235 }; |
| 236 |
| 237 |
| 238 $for i [[ |
| 239 $range j 1..n |
| 240 $range j1 1..i-1 |
| 241 template <typename Result, typename ArgumentTuple$for j1[[, int k$j1]]> |
| 242 class SelectArgs<Result, ArgumentTuple, |
| 243 $for j, [[$if j <= i-1 [[k$j]] $else [[-1]]]]> { |
| 244 public: |
| 245 typedef Result type($for j1, [[GMOCK_FIELD_(ArgumentTuple, k$j1)]]); |
| 246 typedef typename Function<type>::ArgumentTuple SelectedArgs; |
| 247 static SelectedArgs Select(const ArgumentTuple& [[]] |
| 248 $if i == 1 [[/* args */]] $else [[args]]) { |
| 249 using ::std::tr1::get; |
| 250 return SelectedArgs($for j1, [[get<k$j1>(args)]]); |
| 251 } |
| 252 }; |
| 253 |
| 254 |
| 255 ]] |
| 256 #undef GMOCK_FIELD_ |
| 257 |
| 258 $var ks = [[$for i, [[k$i]]]] |
| 259 |
| 260 // Implements the WithArgs action. |
| 261 template <typename InnerAction, $for i, [[int k$i = -1]]> |
| 262 class WithArgsAction { |
| 263 public: |
| 264 explicit WithArgsAction(const InnerAction& action) : action_(action) {} |
| 265 |
| 266 template <typename F> |
| 267 operator Action<F>() const { return MakeAction(new Impl<F>(action_)); } |
| 268 |
| 269 private: |
| 270 template <typename F> |
| 271 class Impl : public ActionInterface<F> { |
| 272 public: |
| 273 typedef typename Function<F>::Result Result; |
| 274 typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 275 |
| 276 explicit Impl(const InnerAction& action) : action_(action) {} |
| 277 |
| 278 virtual Result Perform(const ArgumentTuple& args) { |
| 279 return action_.Perform(SelectArgs<Result, ArgumentTuple, $ks>::Select(args
)); |
| 280 } |
| 281 |
| 282 private: |
| 283 typedef typename SelectArgs<Result, ArgumentTuple, |
| 284 $ks>::type InnerFunctionType; |
| 285 |
| 286 Action<InnerFunctionType> action_; |
| 287 }; |
| 288 |
| 289 const InnerAction action_; |
| 290 }; |
| 291 |
| 292 // Does two actions sequentially. Used for implementing the DoAll(a1, |
| 293 // a2, ...) action. |
| 294 template <typename Action1, typename Action2> |
| 295 class DoBothAction { |
| 296 public: |
| 297 DoBothAction(Action1 action1, Action2 action2) |
| 298 : action1_(action1), action2_(action2) {} |
| 299 |
| 300 // This template type conversion operator allows DoAll(a1, ..., a_n) |
| 301 // to be used in ANY function of compatible type. |
| 302 template <typename F> |
| 303 operator Action<F>() const { |
| 304 return Action<F>(new Impl<F>(action1_, action2_)); |
| 305 } |
| 306 |
| 307 private: |
| 308 // Implements the DoAll(...) action for a particular function type F. |
| 309 template <typename F> |
| 310 class Impl : public ActionInterface<F> { |
| 311 public: |
| 312 typedef typename Function<F>::Result Result; |
| 313 typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 314 typedef typename Function<F>::MakeResultVoid VoidResult; |
| 315 |
| 316 Impl(const Action<VoidResult>& action1, const Action<F>& action2) |
| 317 : action1_(action1), action2_(action2) {} |
| 318 |
| 319 virtual Result Perform(const ArgumentTuple& args) { |
| 320 action1_.Perform(args); |
| 321 return action2_.Perform(args); |
| 322 } |
| 323 |
| 324 private: |
| 325 const Action<VoidResult> action1_; |
| 326 const Action<F> action2_; |
| 327 }; |
| 328 |
| 329 Action1 action1_; |
| 330 Action2 action2_; |
| 331 }; |
| 332 |
| 333 // A macro from the ACTION* family (defined later in this file) |
| 334 // defines an action that can be used in a mock function. Typically, |
| 335 // these actions only care about a subset of the arguments of the mock |
| 336 // function. For example, if such an action only uses the second |
| 337 // argument, it can be used in any mock function that takes >= 2 |
| 338 // arguments where the type of the second argument is compatible. |
| 339 // |
| 340 // Therefore, the action implementation must be prepared to take more |
| 341 // arguments than it needs. The ExcessiveArg type is used to |
| 342 // represent those excessive arguments. In order to keep the compiler |
| 343 // error messages tractable, we define it in the testing namespace |
| 344 // instead of testing::internal. However, this is an INTERNAL TYPE |
| 345 // and subject to change without notice, so a user MUST NOT USE THIS |
| 346 // TYPE DIRECTLY. |
| 347 struct ExcessiveArg {}; |
| 348 |
| 349 // A helper class needed for implementing the ACTION* macros. |
| 350 template <typename Result, class Impl> |
| 351 class ActionHelper { |
| 352 public: |
| 353 $range i 0..n |
| 354 $for i |
| 355 |
| 356 [[ |
| 357 $var template = [[$if i==0 [[]] $else [[ |
| 358 $range j 0..i-1 |
| 359 template <$for j, [[typename A$j]]> |
| 360 ]]]] |
| 361 $range j 0..i-1 |
| 362 $var As = [[$for j, [[A$j]]]] |
| 363 $var as = [[$for j, [[get<$j>(args)]]]] |
| 364 $range k 1..n-i |
| 365 $var eas = [[$for k, [[ExcessiveArg()]]]] |
| 366 $var arg_list = [[$if (i==0) | (i==n) [[$as$eas]] $else [[$as, $eas]]]] |
| 367 $template |
| 368 static Result Perform(Impl* impl, const ::std::tr1::tuple<$As>& args) { |
| 369 using ::std::tr1::get; |
| 370 return impl->template gmock_PerformImpl<$As>(args, $arg_list); |
| 371 } |
| 372 |
| 373 ]] |
| 374 }; |
| 375 |
| 376 } // namespace internal |
| 377 |
| 378 // Various overloads for Invoke(). |
| 379 |
| 380 // Creates an action that invokes 'function_impl' with the mock |
| 381 // function's arguments. |
| 382 template <typename FunctionImpl> |
| 383 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( |
| 384 FunctionImpl function_impl) { |
| 385 return MakePolymorphicAction( |
| 386 internal::InvokeAction<FunctionImpl>(function_impl)); |
| 387 } |
| 388 |
| 389 // Creates an action that invokes the given method on the given object |
| 390 // with the mock function's arguments. |
| 391 template <class Class, typename MethodPtr> |
| 392 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( |
| 393 Class* obj_ptr, MethodPtr method_ptr) { |
| 394 return MakePolymorphicAction( |
| 395 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); |
| 396 } |
| 397 |
| 398 // Creates a reference wrapper for the given L-value. If necessary, |
| 399 // you can explicitly specify the type of the reference. For example, |
| 400 // suppose 'derived' is an object of type Derived, ByRef(derived) |
| 401 // would wrap a Derived&. If you want to wrap a const Base& instead, |
| 402 // where Base is a base class of Derived, just write: |
| 403 // |
| 404 // ByRef<const Base>(derived) |
| 405 template <typename T> |
| 406 inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT |
| 407 return internal::ReferenceWrapper<T>(l_value); |
| 408 } |
| 409 |
| 410 // WithoutArgs(inner_action) can be used in a mock function with a |
| 411 // non-empty argument list to perform inner_action, which takes no |
| 412 // argument. In other words, it adapts an action accepting no |
| 413 // argument to one that accepts (and ignores) arguments. |
| 414 template <typename InnerAction> |
| 415 inline internal::WithArgsAction<InnerAction> |
| 416 WithoutArgs(const InnerAction& action) { |
| 417 return internal::WithArgsAction<InnerAction>(action); |
| 418 } |
| 419 |
| 420 // WithArg<k>(an_action) creates an action that passes the k-th |
| 421 // (0-based) argument of the mock function to an_action and performs |
| 422 // it. It adapts an action accepting one argument to one that accepts |
| 423 // multiple arguments. For convenience, we also provide |
| 424 // WithArgs<k>(an_action) (defined below) as a synonym. |
| 425 template <int k, typename InnerAction> |
| 426 inline internal::WithArgsAction<InnerAction, k> |
| 427 WithArg(const InnerAction& action) { |
| 428 return internal::WithArgsAction<InnerAction, k>(action); |
| 429 } |
| 430 |
| 431 // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes |
| 432 // the selected arguments of the mock function to an_action and |
| 433 // performs it. It serves as an adaptor between actions with |
| 434 // different argument lists. C++ doesn't support default arguments for |
| 435 // function templates, so we have to overload it. |
| 436 |
| 437 $range i 1..n |
| 438 $for i [[ |
| 439 $range j 1..i |
| 440 template <$for j [[int k$j, ]]typename InnerAction> |
| 441 inline internal::WithArgsAction<InnerAction$for j [[, k$j]]> |
| 442 WithArgs(const InnerAction& action) { |
| 443 return internal::WithArgsAction<InnerAction$for j [[, k$j]]>(action); |
| 444 } |
| 445 |
| 446 |
| 447 ]] |
| 448 // Creates an action that does actions a1, a2, ..., sequentially in |
| 449 // each invocation. |
| 450 $range i 2..n |
| 451 $for i [[ |
| 452 $range j 2..i |
| 453 $var types = [[$for j, [[typename Action$j]]]] |
| 454 $var Aas = [[$for j [[, Action$j a$j]]]] |
| 455 |
| 456 template <typename Action1, $types> |
| 457 $range k 1..i-1 |
| 458 |
| 459 inline $for k [[internal::DoBothAction<Action$k, ]]Action$i$for k [[>]] |
| 460 |
| 461 DoAll(Action1 a1$Aas) { |
| 462 $if i==2 [[ |
| 463 |
| 464 return internal::DoBothAction<Action1, Action2>(a1, a2); |
| 465 ]] $else [[ |
| 466 $range j2 2..i |
| 467 |
| 468 return DoAll(a1, DoAll($for j2, [[a$j2]])); |
| 469 ]] |
| 470 |
| 471 } |
| 472 |
| 473 ]] |
| 474 |
| 475 } // namespace testing |
| 476 |
| 477 // The ACTION* family of macros can be used in a namespace scope to |
| 478 // define custom actions easily. The syntax: |
| 479 // |
| 480 // ACTION(name) { statements; } |
| 481 // |
| 482 // will define an action with the given name that executes the |
| 483 // statements. The value returned by the statements will be used as |
| 484 // the return value of the action. Inside the statements, you can |
| 485 // refer to the K-th (0-based) argument of the mock function by |
| 486 // 'argK', and refer to its type by 'argK_type'. For example: |
| 487 // |
| 488 // ACTION(IncrementArg1) { |
| 489 // arg1_type temp = arg1; |
| 490 // return ++(*temp); |
| 491 // } |
| 492 // |
| 493 // allows you to write |
| 494 // |
| 495 // ...WillOnce(IncrementArg1()); |
| 496 // |
| 497 // You can also refer to the entire argument tuple and its type by |
| 498 // 'args' and 'args_type', and refer to the mock function type and its |
| 499 // return type by 'function_type' and 'return_type'. |
| 500 // |
| 501 // Note that you don't need to specify the types of the mock function |
| 502 // arguments. However rest assured that your code is still type-safe: |
| 503 // you'll get a compiler error if *arg1 doesn't support the ++ |
| 504 // operator, or if the type of ++(*arg1) isn't compatible with the |
| 505 // mock function's return type, for example. |
| 506 // |
| 507 // Sometimes you'll want to parameterize the action. For that you can use |
| 508 // another macro: |
| 509 // |
| 510 // ACTION_P(name, param_name) { statements; } |
| 511 // |
| 512 // For example: |
| 513 // |
| 514 // ACTION_P(Add, n) { return arg0 + n; } |
| 515 // |
| 516 // will allow you to write: |
| 517 // |
| 518 // ...WillOnce(Add(5)); |
| 519 // |
| 520 // Note that you don't need to provide the type of the parameter |
| 521 // either. If you need to reference the type of a parameter named |
| 522 // 'foo', you can write 'foo_type'. For example, in the body of |
| 523 // ACTION_P(Add, n) above, you can write 'n_type' to refer to the type |
| 524 // of 'n'. |
| 525 // |
| 526 // We also provide ACTION_P2, ACTION_P3, ..., up to ACTION_P$n to support |
| 527 // multi-parameter actions. |
| 528 // |
| 529 // For the purpose of typing, you can view |
| 530 // |
| 531 // ACTION_Pk(Foo, p1, ..., pk) { ... } |
| 532 // |
| 533 // as shorthand for |
| 534 // |
| 535 // template <typename p1_type, ..., typename pk_type> |
| 536 // FooActionPk<p1_type, ..., pk_type> Foo(p1_type p1, ..., pk_type pk) { ... } |
| 537 // |
| 538 // In particular, you can provide the template type arguments |
| 539 // explicitly when invoking Foo(), as in Foo<long, bool>(5, false); |
| 540 // although usually you can rely on the compiler to infer the types |
| 541 // for you automatically. You can assign the result of expression |
| 542 // Foo(p1, ..., pk) to a variable of type FooActionPk<p1_type, ..., |
| 543 // pk_type>. This can be useful when composing actions. |
| 544 // |
| 545 // You can also overload actions with different numbers of parameters: |
| 546 // |
| 547 // ACTION_P(Plus, a) { ... } |
| 548 // ACTION_P2(Plus, a, b) { ... } |
| 549 // |
| 550 // While it's tempting to always use the ACTION* macros when defining |
| 551 // a new action, you should also consider implementing ActionInterface |
| 552 // or using MakePolymorphicAction() instead, especially if you need to |
| 553 // use the action a lot. While these approaches require more work, |
| 554 // they give you more control on the types of the mock function |
| 555 // arguments and the action parameters, which in general leads to |
| 556 // better compiler error messages that pay off in the long run. They |
| 557 // also allow overloading actions based on parameter types (as opposed |
| 558 // to just based on the number of parameters). |
| 559 // |
| 560 // CAVEAT: |
| 561 // |
| 562 // ACTION*() can only be used in a namespace scope. The reason is |
| 563 // that C++ doesn't yet allow function-local types to be used to |
| 564 // instantiate templates. The up-coming C++0x standard will fix this. |
| 565 // Once that's done, we'll consider supporting using ACTION*() inside |
| 566 // a function. |
| 567 // |
| 568 // MORE INFORMATION: |
| 569 // |
| 570 // To learn more about using these macros, please search for 'ACTION' |
| 571 // on http://code.google.com/p/googlemock/wiki/CookBook. |
| 572 |
| 573 $range i 0..n |
| 574 $range k 0..n-1 |
| 575 |
| 576 // An internal macro needed for implementing ACTION*(). |
| 577 #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_\ |
| 578 const args_type& args GTEST_ATTRIBUTE_UNUSED_ |
| 579 $for k [[,\ |
| 580 arg$k[[]]_type arg$k GTEST_ATTRIBUTE_UNUSED_]] |
| 581 |
| 582 |
| 583 // Sometimes you want to give an action explicit template parameters |
| 584 // that cannot be inferred from its value parameters. ACTION() and |
| 585 // ACTION_P*() don't support that. ACTION_TEMPLATE() remedies that |
| 586 // and can be viewed as an extension to ACTION() and ACTION_P*(). |
| 587 // |
| 588 // The syntax: |
| 589 // |
| 590 // ACTION_TEMPLATE(ActionName, |
| 591 // HAS_m_TEMPLATE_PARAMS(kind1, name1, ..., kind_m, name_m), |
| 592 // AND_n_VALUE_PARAMS(p1, ..., p_n)) { statements; } |
| 593 // |
| 594 // defines an action template that takes m explicit template |
| 595 // parameters and n value parameters. name_i is the name of the i-th |
| 596 // template parameter, and kind_i specifies whether it's a typename, |
| 597 // an integral constant, or a template. p_i is the name of the i-th |
| 598 // value parameter. |
| 599 // |
| 600 // Example: |
| 601 // |
| 602 // // DuplicateArg<k, T>(output) converts the k-th argument of the mock |
| 603 // // function to type T and copies it to *output. |
| 604 // ACTION_TEMPLATE(DuplicateArg, |
| 605 // HAS_2_TEMPLATE_PARAMS(int, k, typename, T), |
| 606 // AND_1_VALUE_PARAMS(output)) { |
| 607 // *output = T(std::tr1::get<k>(args)); |
| 608 // } |
| 609 // ... |
| 610 // int n; |
| 611 // EXPECT_CALL(mock, Foo(_, _)) |
| 612 // .WillOnce(DuplicateArg<1, unsigned char>(&n)); |
| 613 // |
| 614 // To create an instance of an action template, write: |
| 615 // |
| 616 // ActionName<t1, ..., t_m>(v1, ..., v_n) |
| 617 // |
| 618 // where the ts are the template arguments and the vs are the value |
| 619 // arguments. The value argument types are inferred by the compiler. |
| 620 // If you want to explicitly specify the value argument types, you can |
| 621 // provide additional template arguments: |
| 622 // |
| 623 // ActionName<t1, ..., t_m, u1, ..., u_k>(v1, ..., v_n) |
| 624 // |
| 625 // where u_i is the desired type of v_i. |
| 626 // |
| 627 // ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded on the |
| 628 // number of value parameters, but not on the number of template |
| 629 // parameters. Without the restriction, the meaning of the following |
| 630 // is unclear: |
| 631 // |
| 632 // OverloadedAction<int, bool>(x); |
| 633 // |
| 634 // Are we using a single-template-parameter action where 'bool' refers |
| 635 // to the type of x, or are we using a two-template-parameter action |
| 636 // where the compiler is asked to infer the type of x? |
| 637 // |
| 638 // Implementation notes: |
| 639 // |
| 640 // GMOCK_INTERNAL_*_HAS_m_TEMPLATE_PARAMS and |
| 641 // GMOCK_INTERNAL_*_AND_n_VALUE_PARAMS are internal macros for |
| 642 // implementing ACTION_TEMPLATE. The main trick we use is to create |
| 643 // new macro invocations when expanding a macro. For example, we have |
| 644 // |
| 645 // #define ACTION_TEMPLATE(name, template_params, value_params) |
| 646 // ... GMOCK_INTERNAL_DECL_##template_params ... |
| 647 // |
| 648 // which causes ACTION_TEMPLATE(..., HAS_1_TEMPLATE_PARAMS(typename, T), ...) |
| 649 // to expand to |
| 650 // |
| 651 // ... GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS(typename, T) ... |
| 652 // |
| 653 // Since GMOCK_INTERNAL_DECL_HAS_1_TEMPLATE_PARAMS is a macro, the |
| 654 // preprocessor will continue to expand it to |
| 655 // |
| 656 // ... typename T ... |
| 657 // |
| 658 // This technique conforms to the C++ standard and is portable. It |
| 659 // allows us to implement action templates using O(N) code, where N is |
| 660 // the maximum number of template/value parameters supported. Without |
| 661 // using it, we'd have to devote O(N^2) amount of code to implement all |
| 662 // combinations of m and n. |
| 663 |
| 664 // Declares the template parameters. |
| 665 |
| 666 $range j 1..n |
| 667 $for j [[ |
| 668 $range m 0..j-1 |
| 669 #define GMOCK_INTERNAL_DECL_HAS_$j[[]] |
| 670 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[kind$m name$m]] |
| 671 |
| 672 |
| 673 ]] |
| 674 |
| 675 // Lists the template parameters. |
| 676 |
| 677 $for j [[ |
| 678 $range m 0..j-1 |
| 679 #define GMOCK_INTERNAL_LIST_HAS_$j[[]] |
| 680 _TEMPLATE_PARAMS($for m, [[kind$m, name$m]]) $for m, [[name$m]] |
| 681 |
| 682 |
| 683 ]] |
| 684 |
| 685 // Declares the types of value parameters. |
| 686 |
| 687 $for i [[ |
| 688 $range j 0..i-1 |
| 689 #define GMOCK_INTERNAL_DECL_TYPE_AND_$i[[]] |
| 690 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, typename p$j##_type]] |
| 691 |
| 692 |
| 693 ]] |
| 694 |
| 695 // Initializes the value parameters. |
| 696 |
| 697 $for i [[ |
| 698 $range j 0..i-1 |
| 699 #define GMOCK_INTERNAL_INIT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])\ |
| 700 ($for j, [[p$j##_type gmock_p$j]])$if i>0 [[ : ]]$for j, [[p$j(gmock_p$j)]] |
| 701 |
| 702 |
| 703 ]] |
| 704 |
| 705 // Declares the fields for storing the value parameters. |
| 706 |
| 707 $for i [[ |
| 708 $range j 0..i-1 |
| 709 #define GMOCK_INTERNAL_DEFN_AND_$i[[]] |
| 710 _VALUE_PARAMS($for j, [[p$j]]) $for j [[p$j##_type p$j; ]] |
| 711 |
| 712 |
| 713 ]] |
| 714 |
| 715 // Lists the value parameters. |
| 716 |
| 717 $for i [[ |
| 718 $range j 0..i-1 |
| 719 #define GMOCK_INTERNAL_LIST_AND_$i[[]] |
| 720 _VALUE_PARAMS($for j, [[p$j]]) $for j, [[p$j]] |
| 721 |
| 722 |
| 723 ]] |
| 724 |
| 725 // Lists the value parameter types. |
| 726 |
| 727 $for i [[ |
| 728 $range j 0..i-1 |
| 729 #define GMOCK_INTERNAL_LIST_TYPE_AND_$i[[]] |
| 730 _VALUE_PARAMS($for j, [[p$j]]) $for j [[, p$j##_type]] |
| 731 |
| 732 |
| 733 ]] |
| 734 |
| 735 // Declares the value parameters. |
| 736 |
| 737 $for i [[ |
| 738 $range j 0..i-1 |
| 739 #define GMOCK_INTERNAL_DECL_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]] |
| 740 $for j, [[p$j##_type p$j]] |
| 741 |
| 742 |
| 743 ]] |
| 744 |
| 745 // The suffix of the class template implementing the action template. |
| 746 $for i [[ |
| 747 |
| 748 |
| 749 $range j 0..i-1 |
| 750 #define GMOCK_INTERNAL_COUNT_AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]]) [[]] |
| 751 $if i==1 [[P]] $elif i>=2 [[P$i]] |
| 752 ]] |
| 753 |
| 754 |
| 755 // The name of the class template implementing the action template. |
| 756 #define GMOCK_ACTION_CLASS_(name, value_params)\ |
| 757 GMOCK_CONCAT_TOKEN_(name##Action, GMOCK_INTERNAL_COUNT_##value_params) |
| 758 |
| 759 $range k 0..n-1 |
| 760 |
| 761 #define ACTION_TEMPLATE(name, template_params, value_params)\ |
| 762 template <GMOCK_INTERNAL_DECL_##template_params\ |
| 763 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ |
| 764 class GMOCK_ACTION_CLASS_(name, value_params) {\ |
| 765 public:\ |
| 766 GMOCK_ACTION_CLASS_(name, value_params)\ |
| 767 GMOCK_INTERNAL_INIT_##value_params {}\ |
| 768 template <typename F>\ |
| 769 class gmock_Impl : public ::testing::ActionInterface<F> {\ |
| 770 public:\ |
| 771 typedef F function_type;\ |
| 772 typedef typename ::testing::internal::Function<F>::Result return_type;\ |
| 773 typedef typename ::testing::internal::Function<F>::ArgumentTuple\ |
| 774 args_type;\ |
| 775 explicit gmock_Impl GMOCK_INTERNAL_INIT_##value_params {}\ |
| 776 virtual return_type Perform(const args_type& args) {\ |
| 777 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\ |
| 778 Perform(this, args);\ |
| 779 }\ |
| 780 template <$for k, [[typename arg$k[[]]_type]]>\ |
| 781 return_type gmock_PerformImpl(const args_type& args[[]] |
| 782 $for k [[, arg$k[[]]_type arg$k]]) const;\ |
| 783 GMOCK_INTERNAL_DEFN_##value_params\ |
| 784 };\ |
| 785 template <typename F> operator ::testing::Action<F>() const {\ |
| 786 return ::testing::Action<F>(\ |
| 787 new gmock_Impl<F>(GMOCK_INTERNAL_LIST_##value_params));\ |
| 788 }\ |
| 789 GMOCK_INTERNAL_DEFN_##value_params\ |
| 790 };\ |
| 791 template <GMOCK_INTERNAL_DECL_##template_params\ |
| 792 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ |
| 793 inline GMOCK_ACTION_CLASS_(name, value_params)<\ |
| 794 GMOCK_INTERNAL_LIST_##template_params\ |
| 795 GMOCK_INTERNAL_LIST_TYPE_##value_params> name(\ |
| 796 GMOCK_INTERNAL_DECL_##value_params) {\ |
| 797 return GMOCK_ACTION_CLASS_(name, value_params)<\ |
| 798 GMOCK_INTERNAL_LIST_##template_params\ |
| 799 GMOCK_INTERNAL_LIST_TYPE_##value_params>(\ |
| 800 GMOCK_INTERNAL_LIST_##value_params);\ |
| 801 }\ |
| 802 template <GMOCK_INTERNAL_DECL_##template_params\ |
| 803 GMOCK_INTERNAL_DECL_TYPE_##value_params>\ |
| 804 template <typename F>\ |
| 805 template <typename arg0_type, typename arg1_type, typename arg2_type,\ |
| 806 typename arg3_type, typename arg4_type, typename arg5_type,\ |
| 807 typename arg6_type, typename arg7_type, typename arg8_type,\ |
| 808 typename arg9_type>\ |
| 809 typename ::testing::internal::Function<F>::Result\ |
| 810 GMOCK_ACTION_CLASS_(name, value_params)<\ |
| 811 GMOCK_INTERNAL_LIST_##template_params\ |
| 812 GMOCK_INTERNAL_LIST_TYPE_##value_params>::gmock_Impl<F>::\ |
| 813 gmock_PerformImpl(\ |
| 814 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const |
| 815 |
| 816 $for i |
| 817 |
| 818 [[ |
| 819 $var template = [[$if i==0 [[]] $else [[ |
| 820 $range j 0..i-1 |
| 821 |
| 822 template <$for j, [[typename p$j##_type]]>\ |
| 823 ]]]] |
| 824 $var class_name = [[name##Action[[$if i==0 [[]] $elif i==1 [[P]] |
| 825 $else [[P$i]]]]]] |
| 826 $range j 0..i-1 |
| 827 $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]] |
| 828 $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]] |
| 829 $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]] |
| 830 $var param_field_decls = [[$for j |
| 831 [[ |
| 832 |
| 833 p$j##_type p$j;\ |
| 834 ]]]] |
| 835 $var param_field_decls2 = [[$for j |
| 836 [[ |
| 837 |
| 838 p$j##_type p$j;\ |
| 839 ]]]] |
| 840 $var params = [[$for j, [[p$j]]]] |
| 841 $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]] |
| 842 $var typename_arg_types = [[$for k, [[typename arg$k[[]]_type]]]] |
| 843 $var arg_types_and_names = [[$for k, [[arg$k[[]]_type arg$k]]]] |
| 844 $var macro_name = [[$if i==0 [[ACTION]] $elif i==1 [[ACTION_P]] |
| 845 $else [[ACTION_P$i]]]] |
| 846 |
| 847 #define $macro_name(name$for j [[, p$j]])\$template |
| 848 class $class_name {\ |
| 849 public:\ |
| 850 $class_name($ctor_param_list)$inits {}\ |
| 851 template <typename F>\ |
| 852 class gmock_Impl : public ::testing::ActionInterface<F> {\ |
| 853 public:\ |
| 854 typedef F function_type;\ |
| 855 typedef typename ::testing::internal::Function<F>::Result return_type;\ |
| 856 typedef typename ::testing::internal::Function<F>::ArgumentTuple\ |
| 857 args_type;\ |
| 858 [[$if i==1 [[explicit ]]]]gmock_Impl($ctor_param_list)$inits {}\ |
| 859 virtual return_type Perform(const args_type& args) {\ |
| 860 return ::testing::internal::ActionHelper<return_type, gmock_Impl>::\ |
| 861 Perform(this, args);\ |
| 862 }\ |
| 863 template <$typename_arg_types>\ |
| 864 return_type gmock_PerformImpl(const args_type& args, [[]] |
| 865 $arg_types_and_names) const;\$param_field_decls |
| 866 };\ |
| 867 template <typename F> operator ::testing::Action<F>() const {\ |
| 868 return ::testing::Action<F>(new gmock_Impl<F>($params));\ |
| 869 }\$param_field_decls2 |
| 870 };\$template |
| 871 inline $class_name$param_types name($param_types_and_names) {\ |
| 872 return $class_name$param_types($params);\ |
| 873 }\$template |
| 874 template <typename F>\ |
| 875 template <$typename_arg_types>\ |
| 876 typename ::testing::internal::Function<F>::Result\ |
| 877 $class_name$param_types::gmock_Impl<F>::gmock_PerformImpl(\ |
| 878 GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_) const |
| 879 ]] |
| 880 $$ } // This meta comment fixes auto-indentation in Emacs. It won't |
| 881 $$ // show up in the generated code. |
| 882 |
| 883 |
| 884 // TODO(wan@google.com): move the following to a different .h file |
| 885 // such that we don't have to run 'pump' every time the code is |
| 886 // updated. |
| 887 namespace testing { |
| 888 |
| 889 // Various overloads for InvokeArgument<N>(). |
| 890 // |
| 891 // The InvokeArgument<N>(a1, a2, ..., a_k) action invokes the N-th |
| 892 // (0-based) argument, which must be a k-ary callable, of the mock |
| 893 // function, with arguments a1, a2, ..., a_k. |
| 894 // |
| 895 // Notes: |
| 896 // |
| 897 // 1. The arguments are passed by value by default. If you need to |
| 898 // pass an argument by reference, wrap it inside ByRef(). For |
| 899 // example, |
| 900 // |
| 901 // InvokeArgument<1>(5, string("Hello"), ByRef(foo)) |
| 902 // |
| 903 // passes 5 and string("Hello") by value, and passes foo by |
| 904 // reference. |
| 905 // |
| 906 // 2. If the callable takes an argument by reference but ByRef() is |
| 907 // not used, it will receive the reference to a copy of the value, |
| 908 // instead of the original value. For example, when the 0-th |
| 909 // argument of the mock function takes a const string&, the action |
| 910 // |
| 911 // InvokeArgument<0>(string("Hello")) |
| 912 // |
| 913 // makes a copy of the temporary string("Hello") object and passes a |
| 914 // reference of the copy, instead of the original temporary object, |
| 915 // to the callable. This makes it easy for a user to define an |
| 916 // InvokeArgument action from temporary values and have it performed |
| 917 // later. |
| 918 |
| 919 $range i 0..n |
| 920 $for i [[ |
| 921 $range j 0..i-1 |
| 922 |
| 923 ACTION_TEMPLATE(InvokeArgument, |
| 924 HAS_1_TEMPLATE_PARAMS(int, k), |
| 925 AND_$i[[]]_VALUE_PARAMS($for j, [[p$j]])) { |
| 926 return internal::CallableHelper<return_type>::Call( |
| 927 ::std::tr1::get<k>(args)$for j [[, p$j]]); |
| 928 } |
| 929 |
| 930 ]] |
| 931 |
| 932 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the |
| 933 // mock function to *pointer. |
| 934 ACTION_TEMPLATE(SaveArg, |
| 935 HAS_1_TEMPLATE_PARAMS(int, k), |
| 936 AND_1_VALUE_PARAMS(pointer)) { |
| 937 *pointer = ::std::tr1::get<k>(args); |
| 938 } |
| 939 |
| 940 // Action SetArgReferee<k>(value) assigns 'value' to the variable |
| 941 // referenced by the k-th (0-based) argument of the mock function. |
| 942 ACTION_TEMPLATE(SetArgReferee, |
| 943 HAS_1_TEMPLATE_PARAMS(int, k), |
| 944 AND_1_VALUE_PARAMS(value)) { |
| 945 typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type; |
| 946 // Ensures that argument #k is a reference. If you get a compiler |
| 947 // error on the next line, you are using SetArgReferee<k>(value) in |
| 948 // a mock function whose k-th (0-based) argument is not a reference. |
| 949 GMOCK_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, |
| 950 SetArgReferee_must_be_used_with_a_reference_argument); |
| 951 ::std::tr1::get<k>(args) = value; |
| 952 } |
| 953 |
| 954 // Action SetArrayArgument<k>(first, last) copies the elements in |
| 955 // source range [first, last) to the array pointed to by the k-th |
| 956 // (0-based) argument, which can be either a pointer or an |
| 957 // iterator. The action does not take ownership of the elements in the |
| 958 // source range. |
| 959 ACTION_TEMPLATE(SetArrayArgument, |
| 960 HAS_1_TEMPLATE_PARAMS(int, k), |
| 961 AND_2_VALUE_PARAMS(first, last)) { |
| 962 // Microsoft compiler deprecates ::std::copy, so we want to suppress warning |
| 963 // 4996 (Function call with parameters that may be unsafe) there. |
| 964 #ifdef _MSC_VER |
| 965 #pragma warning(push) // Saves the current warning state. |
| 966 #pragma warning(disable:4996) // Temporarily disables warning 4996. |
| 967 #endif |
| 968 ::std::copy(first, last, ::std::tr1::get<k>(args)); |
| 969 #ifdef _MSC_VER |
| 970 #pragma warning(pop) // Restores the warning state. |
| 971 #endif |
| 972 } |
| 973 |
| 974 // Various overloads for ReturnNew<T>(). |
| 975 // |
| 976 // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new |
| 977 // instance of type T, constructed on the heap with constructor arguments |
| 978 // a1, a2, ..., and a_k. The caller assumes ownership of the returned value. |
| 979 $range i 0..n |
| 980 $for i [[ |
| 981 $range j 0..i-1 |
| 982 $var ps = [[$for j, [[p$j]]]] |
| 983 |
| 984 ACTION_TEMPLATE(ReturnNew, |
| 985 HAS_1_TEMPLATE_PARAMS(typename, T), |
| 986 AND_$i[[]]_VALUE_PARAMS($ps)) { |
| 987 return new T($ps); |
| 988 } |
| 989 |
| 990 ]] |
| 991 |
| 992 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock |
| 993 // function. |
| 994 ACTION_TEMPLATE(DeleteArg, |
| 995 HAS_1_TEMPLATE_PARAMS(int, k), |
| 996 AND_0_VALUE_PARAMS()) { |
| 997 delete ::std::tr1::get<k>(args); |
| 998 } |
| 999 |
| 1000 // Action Throw(exception) can be used in a mock function of any type |
| 1001 // to throw the given exception. Any copyable value can be thrown. |
| 1002 #if GTEST_HAS_EXCEPTIONS |
| 1003 ACTION_P(Throw, exception) { throw exception; } |
| 1004 #endif // GTEST_HAS_EXCEPTIONS |
| 1005 |
| 1006 } // namespace testing |
| 1007 |
| 1008 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ |
OLD | NEW |