gmock-function-mocker.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 MOCK_METHOD.
  32. // GOOGLETEST_CM0002 DO NOT DELETE
  33. #ifndef THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_ // NOLINT
  34. #define THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_ // NOLINT
  35. #include "gmock/gmock-generated-function-mockers.h" // NOLINT
  36. #include "gmock/internal/gmock-pp.h"
  37. #define MOCK_METHOD(...) \
  38. GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__)
  39. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_1(...) \
  40. GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__)
  41. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_2(...) \
  42. GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__)
  43. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_3(_Ret, _MethodName, _Args) \
  44. GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, ())
  45. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_4(_Ret, _MethodName, _Args, _Spec) \
  46. GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Args); \
  47. GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Spec); \
  48. GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE( \
  49. GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \
  50. GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \
  51. GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
  52. GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \
  53. GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \
  54. GMOCK_INTERNAL_HAS_NOEXCEPT(_Spec), GMOCK_INTERNAL_GET_CALLTYPE(_Spec), \
  55. (GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)))
  56. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_5(...) \
  57. GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__)
  58. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_6(...) \
  59. GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__)
  60. #define GMOCK_INTERNAL_MOCK_METHOD_ARG_7(...) \
  61. GMOCK_INTERNAL_WRONG_ARITY(__VA_ARGS__)
  62. #define GMOCK_INTERNAL_WRONG_ARITY(...) \
  63. static_assert( \
  64. false, \
  65. "MOCK_METHOD must be called with 3 or 4 arguments. _Ret, " \
  66. "_MethodName, _Args and optionally _Spec. _Args and _Spec must be " \
  67. "enclosed in parentheses. If _Ret is a type with unprotected commas, " \
  68. "it must also be enclosed in parentheses.")
  69. #define GMOCK_INTERNAL_ASSERT_PARENTHESIS(_Tuple) \
  70. static_assert( \
  71. GMOCK_PP_IS_ENCLOSED_PARENS(_Tuple), \
  72. GMOCK_PP_STRINGIZE(_Tuple) " should be enclosed in parentheses.")
  73. #define GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE(_N, ...) \
  74. static_assert( \
  75. std::is_function<__VA_ARGS__>::value, \
  76. "Signature must be a function type, maybe return type contains " \
  77. "unprotected comma."); \
  78. static_assert( \
  79. ::testing::tuple_size<typename ::testing::internal::Function< \
  80. __VA_ARGS__>::ArgumentTuple>::value == _N, \
  81. "This method does not take " GMOCK_PP_STRINGIZE( \
  82. _N) " arguments. Parenthesize all types with unproctected commas.")
  83. #define GMOCK_INTERNAL_ASSERT_VALID_SPEC(_Spec) \
  84. GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_ASSERT_VALID_SPEC_ELEMENT, ~, _Spec)
  85. #define GMOCK_INTERNAL_MOCK_METHOD_IMPL(_N, _MethodName, _Constness, \
  86. _Override, _Final, _Noexcept, \
  87. _CallType, _Signature) \
  88. typename ::testing::internal::Function<GMOCK_PP_REMOVE_PARENS( \
  89. _Signature)>::Result \
  90. GMOCK_INTERNAL_EXPAND(_CallType) \
  91. _MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \
  92. GMOCK_PP_IF(_Constness, const, ) GMOCK_PP_IF(_Noexcept, noexcept, ) \
  93. GMOCK_PP_IF(_Override, override, ) \
  94. GMOCK_PP_IF(_Final, final, ) { \
  95. GMOCK_MOCKER_(_N, _Constness, _MethodName) \
  96. .SetOwnerAndName(this, #_MethodName); \
  97. return GMOCK_MOCKER_(_N, _Constness, _MethodName) \
  98. .Invoke(GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, _Signature, _N)); \
  99. } \
  100. ::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
  101. GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_PARAMETER, _Signature, _N)) \
  102. GMOCK_PP_IF(_Constness, const, ) { \
  103. GMOCK_MOCKER_(_N, _Constness, _MethodName).RegisterOwner(this); \
  104. return GMOCK_MOCKER_(_N, _Constness, _MethodName) \
  105. .With(GMOCK_PP_REPEAT(GMOCK_INTERNAL_MATCHER_ARGUMENT, , _N)); \
  106. } \
  107. ::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
  108. const ::testing::internal::WithoutMatchers&, \
  109. GMOCK_PP_IF(_Constness, const, )::testing::internal::Function< \
  110. GMOCK_PP_REMOVE_PARENS(_Signature)>*) \
  111. const GMOCK_PP_IF(_Noexcept, noexcept, ) { \
  112. return GMOCK_PP_CAT(::testing::internal::AdjustConstness_, \
  113. GMOCK_PP_IF(_Constness, const, ))(this) \
  114. ->gmock_##_MethodName(GMOCK_PP_REPEAT( \
  115. GMOCK_INTERNAL_A_MATCHER_ARGUMENT, _Signature, _N)); \
  116. } \
  117. mutable ::testing::FunctionMocker<GMOCK_PP_REMOVE_PARENS(_Signature)> \
  118. GMOCK_MOCKER_(_N, _Constness, _MethodName)
  119. #define GMOCK_INTERNAL_EXPAND(...) __VA_ARGS__
  120. // Five Valid modifiers.
  121. #define GMOCK_INTERNAL_HAS_CONST(_Tuple) \
  122. GMOCK_PP_HAS_COMMA(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_CONST, ~, _Tuple))
  123. #define GMOCK_INTERNAL_HAS_OVERRIDE(_Tuple) \
  124. GMOCK_PP_HAS_COMMA( \
  125. GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_OVERRIDE, ~, _Tuple))
  126. #define GMOCK_INTERNAL_HAS_FINAL(_Tuple) \
  127. GMOCK_PP_HAS_COMMA(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_FINAL, ~, _Tuple))
  128. #define GMOCK_INTERNAL_HAS_NOEXCEPT(_Tuple) \
  129. GMOCK_PP_HAS_COMMA( \
  130. GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_DETECT_NOEXCEPT, ~, _Tuple))
  131. #define GMOCK_INTERNAL_GET_CALLTYPE(_Tuple) \
  132. GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GET_CALLTYPE_IMPL, ~, _Tuple)
  133. #define GMOCK_INTERNAL_ASSERT_VALID_SPEC_ELEMENT(_i, _, _elem) \
  134. static_assert( \
  135. (GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_CONST(_i, _, _elem)) + \
  136. GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem)) + \
  137. GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem)) + \
  138. GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)) + \
  139. GMOCK_INTERNAL_IS_CALLTYPE(_elem)) == 1, \
  140. GMOCK_PP_STRINGIZE( \
  141. _elem) " cannot be recognized as a valid specification modifier.");
  142. // Modifiers implementation.
  143. #define GMOCK_INTERNAL_DETECT_CONST(_i, _, _elem) \
  144. GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_CONST_I_, _elem)
  145. #define GMOCK_INTERNAL_DETECT_CONST_I_const ,
  146. #define GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem) \
  147. GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_OVERRIDE_I_, _elem)
  148. #define GMOCK_INTERNAL_DETECT_OVERRIDE_I_override ,
  149. #define GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem) \
  150. GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_FINAL_I_, _elem)
  151. #define GMOCK_INTERNAL_DETECT_FINAL_I_final ,
  152. // TODO(iserna): Maybe noexcept should accept an argument here as well.
  153. #define GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem) \
  154. GMOCK_PP_CAT(GMOCK_INTERNAL_DETECT_NOEXCEPT_I_, _elem)
  155. #define GMOCK_INTERNAL_DETECT_NOEXCEPT_I_noexcept ,
  156. #define GMOCK_INTERNAL_GET_CALLTYPE_IMPL(_i, _, _elem) \
  157. GMOCK_PP_IF(GMOCK_INTERNAL_IS_CALLTYPE(_elem), \
  158. GMOCK_INTERNAL_GET_VALUE_CALLTYPE, GMOCK_PP_EMPTY) \
  159. (_elem)
  160. // TODO(iserna): GMOCK_INTERNAL_IS_CALLTYPE and
  161. // GMOCK_INTERNAL_GET_VALUE_CALLTYPE needed more expansions to work on windows
  162. // maybe they can be simplified somehow.
  163. #define GMOCK_INTERNAL_IS_CALLTYPE(_arg) \
  164. GMOCK_INTERNAL_IS_CALLTYPE_I( \
  165. GMOCK_PP_CAT(GMOCK_INTERNAL_IS_CALLTYPE_HELPER_, _arg))
  166. #define GMOCK_INTERNAL_IS_CALLTYPE_I(_arg) GMOCK_PP_IS_ENCLOSED_PARENS(_arg)
  167. #define GMOCK_INTERNAL_GET_VALUE_CALLTYPE(_arg) \
  168. GMOCK_INTERNAL_GET_VALUE_CALLTYPE_I( \
  169. GMOCK_PP_CAT(GMOCK_INTERNAL_IS_CALLTYPE_HELPER_, _arg))
  170. #define GMOCK_INTERNAL_GET_VALUE_CALLTYPE_I(_arg) \
  171. GMOCK_PP_CAT(GMOCK_PP_IDENTITY, _arg)
  172. #define GMOCK_INTERNAL_IS_CALLTYPE_HELPER_Calltype
  173. #define GMOCK_INTERNAL_SIGNATURE(_Ret, _Args) \
  174. GMOCK_PP_IF(GMOCK_PP_IS_BEGIN_PARENS(_Ret), GMOCK_PP_REMOVE_PARENS, \
  175. GMOCK_PP_IDENTITY) \
  176. (_Ret)(GMOCK_PP_FOR_EACH(GMOCK_INTERNAL_GET_TYPE, _, _Args))
  177. #define GMOCK_INTERNAL_GET_TYPE(_i, _, _elem) \
  178. GMOCK_PP_COMMA_IF(_i) \
  179. GMOCK_PP_IF(GMOCK_PP_IS_BEGIN_PARENS(_elem), GMOCK_PP_REMOVE_PARENS, \
  180. GMOCK_PP_IDENTITY) \
  181. (_elem)
  182. #define GMOCK_INTERNAL_PARAMETER(_i, _Signature, _) \
  183. GMOCK_PP_COMMA_IF(_i) \
  184. GMOCK_INTERNAL_ARG_O(typename, GMOCK_PP_INC(_i), \
  185. GMOCK_PP_REMOVE_PARENS(_Signature)) \
  186. gmock_a##_i
  187. #define GMOCK_INTERNAL_FORWARD_ARG(_i, _Signature, _) \
  188. GMOCK_PP_COMMA_IF(_i) \
  189. ::std::forward<GMOCK_INTERNAL_ARG_O(typename, GMOCK_PP_INC(_i), \
  190. GMOCK_PP_REMOVE_PARENS(_Signature))>( \
  191. gmock_a##_i)
  192. #define GMOCK_INTERNAL_MATCHER_PARAMETER(_i, _Signature, _) \
  193. GMOCK_PP_COMMA_IF(_i) \
  194. GMOCK_INTERNAL_MATCHER_O(typename, GMOCK_PP_INC(_i), \
  195. GMOCK_PP_REMOVE_PARENS(_Signature)) \
  196. gmock_a##_i
  197. #define GMOCK_INTERNAL_MATCHER_ARGUMENT(_i, _1, _2) \
  198. GMOCK_PP_COMMA_IF(_i) \
  199. gmock_a##_i
  200. #define GMOCK_INTERNAL_A_MATCHER_ARGUMENT(_i, _Signature, _) \
  201. GMOCK_PP_COMMA_IF(_i) \
  202. ::testing::A<GMOCK_INTERNAL_ARG_O(typename, GMOCK_PP_INC(_i), \
  203. GMOCK_PP_REMOVE_PARENS(_Signature))>()
  204. #define GMOCK_INTERNAL_ARG_O(_tn, _i, ...) GMOCK_ARG_(_tn, _i, __VA_ARGS__)
  205. #define GMOCK_INTERNAL_MATCHER_O(_tn, _i, ...) \
  206. GMOCK_MATCHER_(_tn, _i, __VA_ARGS__)
  207. #endif // THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_FUNCTION_MOCKER_H_