gmock-more-actions.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file implements some actions that depend on gmock-generated-actions.h.
  32. // GOOGLETEST_CM0002 DO NOT DELETE
  33. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  34. #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
  35. #include <algorithm>
  36. #include "gmock/gmock-generated-actions.h"
  37. namespace testing {
  38. namespace internal {
  39. // Implements the Invoke(f) action. The template argument
  40. // FunctionImpl is the implementation type of f, which can be either a
  41. // function pointer or a functor. Invoke(f) can be used as an
  42. // Action<F> as long as f's type is compatible with F (i.e. f can be
  43. // assigned to a tr1::function<F>).
  44. template <typename FunctionImpl>
  45. class InvokeAction {
  46. public:
  47. // The c'tor makes a copy of function_impl (either a function
  48. // pointer or a functor).
  49. explicit InvokeAction(FunctionImpl function_impl)
  50. : function_impl_(function_impl) {}
  51. template <typename Result, typename ArgumentTuple>
  52. Result Perform(const ArgumentTuple& args) {
  53. return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
  54. }
  55. private:
  56. FunctionImpl function_impl_;
  57. GTEST_DISALLOW_ASSIGN_(InvokeAction);
  58. };
  59. // Implements the Invoke(object_ptr, &Class::Method) action.
  60. template <class Class, typename MethodPtr>
  61. class InvokeMethodAction {
  62. public:
  63. InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
  64. : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
  65. template <typename Result, typename ArgumentTuple>
  66. Result Perform(const ArgumentTuple& args) const {
  67. return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
  68. obj_ptr_, method_ptr_, args);
  69. }
  70. private:
  71. // The order of these members matters. Reversing the order can trigger
  72. // warning C4121 in MSVC (see
  73. // http://computer-programming-forum.com/7-vc.net/6fbc30265f860ad1.htm ).
  74. const MethodPtr method_ptr_;
  75. Class* const obj_ptr_;
  76. GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
  77. };
  78. // An internal replacement for std::copy which mimics its behavior. This is
  79. // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
  80. // However Visual Studio 2010 and later do not honor #pragmas which disable that
  81. // warning.
  82. template<typename InputIterator, typename OutputIterator>
  83. inline OutputIterator CopyElements(InputIterator first,
  84. InputIterator last,
  85. OutputIterator output) {
  86. for (; first != last; ++first, ++output) {
  87. *output = *first;
  88. }
  89. return output;
  90. }
  91. } // namespace internal
  92. // Various overloads for Invoke().
  93. // Creates an action that invokes 'function_impl' with the mock
  94. // function's arguments.
  95. template <typename FunctionImpl>
  96. PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
  97. FunctionImpl function_impl) {
  98. return MakePolymorphicAction(
  99. internal::InvokeAction<FunctionImpl>(function_impl));
  100. }
  101. // Creates an action that invokes the given method on the given object
  102. // with the mock function's arguments.
  103. template <class Class, typename MethodPtr>
  104. PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
  105. Class* obj_ptr, MethodPtr method_ptr) {
  106. return MakePolymorphicAction(
  107. internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
  108. }
  109. // WithoutArgs(inner_action) can be used in a mock function with a
  110. // non-empty argument list to perform inner_action, which takes no
  111. // argument. In other words, it adapts an action accepting no
  112. // argument to one that accepts (and ignores) arguments.
  113. template <typename InnerAction>
  114. inline internal::WithArgsAction<InnerAction>
  115. WithoutArgs(const InnerAction& action) {
  116. return internal::WithArgsAction<InnerAction>(action);
  117. }
  118. // WithArg<k>(an_action) creates an action that passes the k-th
  119. // (0-based) argument of the mock function to an_action and performs
  120. // it. It adapts an action accepting one argument to one that accepts
  121. // multiple arguments. For convenience, we also provide
  122. // WithArgs<k>(an_action) (defined below) as a synonym.
  123. template <int k, typename InnerAction>
  124. inline internal::WithArgsAction<InnerAction, k>
  125. WithArg(const InnerAction& action) {
  126. return internal::WithArgsAction<InnerAction, k>(action);
  127. }
  128. // The ACTION*() macros trigger warning C4100 (unreferenced formal
  129. // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
  130. // the macro definition, as the warnings are generated when the macro
  131. // is expanded and macro expansion cannot contain #pragma. Therefore
  132. // we suppress them here.
  133. #ifdef _MSC_VER
  134. # pragma warning(push)
  135. # pragma warning(disable:4100)
  136. #endif
  137. // Action ReturnArg<k>() returns the k-th argument of the mock function.
  138. ACTION_TEMPLATE(ReturnArg,
  139. HAS_1_TEMPLATE_PARAMS(int, k),
  140. AND_0_VALUE_PARAMS()) {
  141. return ::testing::get<k>(args);
  142. }
  143. // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
  144. // mock function to *pointer.
  145. ACTION_TEMPLATE(SaveArg,
  146. HAS_1_TEMPLATE_PARAMS(int, k),
  147. AND_1_VALUE_PARAMS(pointer)) {
  148. *pointer = ::testing::get<k>(args);
  149. }
  150. // Action SaveArgPointee<k>(pointer) saves the value pointed to
  151. // by the k-th (0-based) argument of the mock function to *pointer.
  152. ACTION_TEMPLATE(SaveArgPointee,
  153. HAS_1_TEMPLATE_PARAMS(int, k),
  154. AND_1_VALUE_PARAMS(pointer)) {
  155. *pointer = *::testing::get<k>(args);
  156. }
  157. // Action SetArgReferee<k>(value) assigns 'value' to the variable
  158. // referenced by the k-th (0-based) argument of the mock function.
  159. ACTION_TEMPLATE(SetArgReferee,
  160. HAS_1_TEMPLATE_PARAMS(int, k),
  161. AND_1_VALUE_PARAMS(value)) {
  162. typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
  163. // Ensures that argument #k is a reference. If you get a compiler
  164. // error on the next line, you are using SetArgReferee<k>(value) in
  165. // a mock function whose k-th (0-based) argument is not a reference.
  166. GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
  167. SetArgReferee_must_be_used_with_a_reference_argument);
  168. ::testing::get<k>(args) = value;
  169. }
  170. // Action SetArrayArgument<k>(first, last) copies the elements in
  171. // source range [first, last) to the array pointed to by the k-th
  172. // (0-based) argument, which can be either a pointer or an
  173. // iterator. The action does not take ownership of the elements in the
  174. // source range.
  175. ACTION_TEMPLATE(SetArrayArgument,
  176. HAS_1_TEMPLATE_PARAMS(int, k),
  177. AND_2_VALUE_PARAMS(first, last)) {
  178. // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
  179. #ifdef _MSC_VER
  180. internal::CopyElements(first, last, ::testing::get<k>(args));
  181. #else
  182. ::std::copy(first, last, ::testing::get<k>(args));
  183. #endif
  184. }
  185. // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
  186. // function.
  187. ACTION_TEMPLATE(DeleteArg,
  188. HAS_1_TEMPLATE_PARAMS(int, k),
  189. AND_0_VALUE_PARAMS()) {
  190. delete ::testing::get<k>(args);
  191. }
  192. // This action returns the value pointed to by 'pointer'.
  193. ACTION_P(ReturnPointee, pointer) { return *pointer; }
  194. // Action Throw(exception) can be used in a mock function of any type
  195. // to throw the given exception. Any copyable value can be thrown.
  196. #if GTEST_HAS_EXCEPTIONS
  197. // Suppresses the 'unreachable code' warning that VC generates in opt modes.
  198. # ifdef _MSC_VER
  199. # pragma warning(push) // Saves the current warning state.
  200. # pragma warning(disable:4702) // Temporarily disables warning 4702.
  201. # endif
  202. ACTION_P(Throw, exception) { throw exception; }
  203. # ifdef _MSC_VER
  204. # pragma warning(pop) // Restores the warning state.
  205. # endif
  206. #endif // GTEST_HAS_EXCEPTIONS
  207. #ifdef _MSC_VER
  208. # pragma warning(pop)
  209. #endif
  210. } // namespace testing
  211. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_