gmock-more-actions.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file implements some actions that depend on gmock-generated-actions.h.
  34. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  35. #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  36. #include <algorithm>
  37. #include "gmock/gmock-generated-actions.h"
  38. namespace testing {
  39. namespace internal {
  40. // Implements the Invoke(f) action. The template argument
  41. // FunctionImpl is the implementation type of f, which can be either a
  42. // function pointer or a functor. Invoke(f) can be used as an
  43. // Action<F> as long as f's type is compatible with F (i.e. f can be
  44. // assigned to a tr1::function<F>).
  45. template <typename FunctionImpl>
  46. class InvokeAction {
  47. public:
  48. // The c'tor makes a copy of function_impl (either a function
  49. // pointer or a functor).
  50. explicit InvokeAction(FunctionImpl function_impl)
  51. : function_impl_(function_impl) {}
  52. template <typename Result, typename ArgumentTuple>
  53. Result Perform(const ArgumentTuple& args) {
  54. return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
  55. }
  56. private:
  57. FunctionImpl function_impl_;
  58. GTEST_DISALLOW_ASSIGN_(InvokeAction);
  59. };
  60. // Implements the Invoke(object_ptr, &Class::Method) action.
  61. template <class Class, typename MethodPtr>
  62. class InvokeMethodAction {
  63. public:
  64. InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
  65. : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
  66. template <typename Result, typename ArgumentTuple>
  67. Result Perform(const ArgumentTuple& args) const {
  68. return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
  69. obj_ptr_, method_ptr_, args);
  70. }
  71. private:
  72. // The order of these members matters. Reversing the order can trigger
  73. // warning C4121 in MSVC (see
  74. // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ).
  75. const MethodPtr method_ptr_;
  76. Class* const obj_ptr_;
  77. GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
  78. };
  79. // An internal replacement for std::copy which mimics its behavior. This is
  80. // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
  81. // However Visual Studio 2010 and later do not honor #pragmas which disable that
  82. // warning.
  83. template<typename InputIterator, typename OutputIterator>
  84. inline OutputIterator CopyElements(InputIterator first,
  85. InputIterator last,
  86. OutputIterator output) {
  87. for (; first != last; ++first, ++output) {
  88. *output = *first;
  89. }
  90. return output;
  91. }
  92. } // namespace internal
  93. // Various overloads for Invoke().
  94. // Creates an action that invokes 'function_impl' with the mock
  95. // function's arguments.
  96. template <typename FunctionImpl>
  97. PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
  98. FunctionImpl function_impl) {
  99. return MakePolymorphicAction(
  100. internal::InvokeAction<FunctionImpl>(function_impl));
  101. }
  102. // Creates an action that invokes the given method on the given object
  103. // with the mock function's arguments.
  104. template <class Class, typename MethodPtr>
  105. PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
  106. Class* obj_ptr, MethodPtr method_ptr) {
  107. return MakePolymorphicAction(
  108. internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
  109. }
  110. // WithoutArgs(inner_action) can be used in a mock function with a
  111. // non-empty argument list to perform inner_action, which takes no
  112. // argument. In other words, it adapts an action accepting no
  113. // argument to one that accepts (and ignores) arguments.
  114. template <typename InnerAction>
  115. inline internal::WithArgsAction<InnerAction>
  116. WithoutArgs(const InnerAction& action) {
  117. return internal::WithArgsAction<InnerAction>(action);
  118. }
  119. // WithArg<k>(an_action) creates an action that passes the k-th
  120. // (0-based) argument of the mock function to an_action and performs
  121. // it. It adapts an action accepting one argument to one that accepts
  122. // multiple arguments. For convenience, we also provide
  123. // WithArgs<k>(an_action) (defined below) as a synonym.
  124. template <int k, typename InnerAction>
  125. inline internal::WithArgsAction<InnerAction, k>
  126. WithArg(const InnerAction& action) {
  127. return internal::WithArgsAction<InnerAction, k>(action);
  128. }
  129. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  130. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  131. // the macro definition, as the warnings are generated when the macro
  132. // is expanded and macro expansion cannot contain #pragma. Therefore
  133. // we suppress them here.
  134. #ifdef _MSC_VER
  135. # pragma warning(push)
  136. # pragma warning(disable:4100)
  137. #endif
  138. // Action ReturnArg<k>() returns the k-th argument of the mock function.
  139. ACTION_TEMPLATE(ReturnArg,
  140. HAS_1_TEMPLATE_PARAMS(int, k),
  141. AND_0_VALUE_PARAMS()) {
  142. return ::testing::get<k>(args);
  143. }
  144. // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
  145. // mock function to *pointer.
  146. ACTION_TEMPLATE(SaveArg,
  147. HAS_1_TEMPLATE_PARAMS(int, k),
  148. AND_1_VALUE_PARAMS(pointer)) {
  149. *pointer = ::testing::get<k>(args);
  150. }
  151. // Action SaveArgPointee<k>(pointer) saves the value pointed to
  152. // by the k-th (0-based) argument of the mock function to *pointer.
  153. ACTION_TEMPLATE(SaveArgPointee,
  154. HAS_1_TEMPLATE_PARAMS(int, k),
  155. AND_1_VALUE_PARAMS(pointer)) {
  156. *pointer = *::testing::get<k>(args);
  157. }
  158. // Action SetArgReferee<k>(value) assigns 'value' to the variable
  159. // referenced by the k-th (0-based) argument of the mock function.
  160. ACTION_TEMPLATE(SetArgReferee,
  161. HAS_1_TEMPLATE_PARAMS(int, k),
  162. AND_1_VALUE_PARAMS(value)) {
  163. typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
  164. // Ensures that argument #k is a reference. If you get a compiler
  165. // error on the next line, you are using SetArgReferee<k>(value) in
  166. // a mock function whose k-th (0-based) argument is not a reference.
  167. GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
  168. SetArgReferee_must_be_used_with_a_reference_argument);
  169. ::testing::get<k>(args) = value;
  170. }
  171. // Action SetArrayArgument<k>(first, last) copies the elements in
  172. // source range [first, last) to the array pointed to by the k-th
  173. // (0-based) argument, which can be either a pointer or an
  174. // iterator. The action does not take ownership of the elements in the
  175. // source range.
  176. ACTION_TEMPLATE(SetArrayArgument,
  177. HAS_1_TEMPLATE_PARAMS(int, k),
  178. AND_2_VALUE_PARAMS(first, last)) {
  179. // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
  180. #ifdef _MSC_VER
  181. internal::CopyElements(first, last, ::testing::get<k>(args));
  182. #else
  183. ::std::copy(first, last, ::testing::get<k>(args));
  184. #endif
  185. }
  186. // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
  187. // function.
  188. ACTION_TEMPLATE(DeleteArg,
  189. HAS_1_TEMPLATE_PARAMS(int, k),
  190. AND_0_VALUE_PARAMS()) {
  191. delete ::testing::get<k>(args);
  192. }
  193. // This action returns the value pointed to by 'pointer'.
  194. ACTION_P(ReturnPointee, pointer) { return *pointer; }
  195. // Action Throw(exception) can be used in a mock function of any type
  196. // to throw the given exception. Any copyable value can be thrown.
  197. #if GTEST_HAS_EXCEPTIONS
  198. // Suppresses the 'unreachable code' warning that VC generates in opt modes.
  199. # ifdef _MSC_VER
  200. # pragma warning(push) // Saves the current warning state.
  201. # pragma warning(disable:4702) // Temporarily disables warning 4702.
  202. # endif
  203. ACTION_P(Throw, exception) { throw exception; }
  204. # ifdef _MSC_VER
  205. # pragma warning(pop) // Restores the warning state.
  206. # endif
  207. #endif // GTEST_HAS_EXCEPTIONS
  208. #ifdef _MSC_VER
  209. # pragma warning(pop)
  210. #endif
  211. } // namespace testing
  212. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_