gmock-generated-function-mockers.h.pump 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert
  3. $$ it to gmock-generated-function-mockers.h.
  4. $$
  5. $var n = 10 $$ The maximum arity we support.
  6. // Copyright 2007, Google Inc.
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions are
  11. // met:
  12. //
  13. // * Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. // * Redistributions in binary form must reproduce the above
  16. // copyright notice, this list of conditions and the following disclaimer
  17. // in the documentation and/or other materials provided with the
  18. // distribution.
  19. // * Neither the name of Google Inc. nor the names of its
  20. // contributors may be used to endorse or promote products derived from
  21. // this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. // Google Mock - a framework for writing C++ mock classes.
  35. //
  36. // This file implements function mockers of various arities.
  37. // GOOGLETEST_CM0002 DO NOT DELETE
  38. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
  39. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
  40. #include <functional>
  41. #include <utility>
  42. #include "gmock/gmock-spec-builders.h"
  43. #include "gmock/internal/gmock-internal-utils.h"
  44. namespace testing {
  45. namespace internal {
  46. $range i 0..n
  47. // Removes the given pointer; this is a helper for the expectation setter method
  48. // for parameterless matchers.
  49. //
  50. // We want to make sure that the user cannot set a parameterless expectation on
  51. // overloaded methods, including methods which are overloaded on const. Example:
  52. //
  53. // class MockClass {
  54. // MOCK_METHOD0(GetName, string&());
  55. // MOCK_CONST_METHOD0(GetName, const string&());
  56. // };
  57. //
  58. // TEST() {
  59. // // This should be an error, as it's not clear which overload is expected.
  60. // EXPECT_CALL(mock, GetName).WillOnce(ReturnRef(value));
  61. // }
  62. //
  63. // Here are the generated expectation-setter methods:
  64. //
  65. // class MockClass {
  66. // // Overload 1
  67. // MockSpec<string&()> gmock_GetName() { ... }
  68. // // Overload 2. Declared const so that the compiler will generate an
  69. // // error when trying to resolve between this and overload 4 in
  70. // // 'gmock_GetName(WithoutMatchers(), nullptr)'.
  71. // MockSpec<string&()> gmock_GetName(
  72. // const WithoutMatchers&, const Function<string&()>*) const {
  73. // // Removes const from this, calls overload 1
  74. // return AdjustConstness_(this)->gmock_GetName();
  75. // }
  76. //
  77. // // Overload 3
  78. // const string& gmock_GetName() const { ... }
  79. // // Overload 4
  80. // MockSpec<const string&()> gmock_GetName(
  81. // const WithoutMatchers&, const Function<const string&()>*) const {
  82. // // Does not remove const, calls overload 3
  83. // return AdjustConstness_const(this)->gmock_GetName();
  84. // }
  85. // }
  86. //
  87. template <typename MockType>
  88. const MockType* AdjustConstness_const(const MockType* mock) {
  89. return mock;
  90. }
  91. // Removes const from and returns the given pointer; this is a helper for the
  92. // expectation setter method for parameterless matchers.
  93. template <typename MockType>
  94. MockType* AdjustConstness_(const MockType* mock) {
  95. return const_cast<MockType*>(mock);
  96. }
  97. } // namespace internal
  98. // The style guide prohibits "using" statements in a namespace scope
  99. // inside a header file. However, the FunctionMocker class template
  100. // is meant to be defined in the ::testing namespace. The following
  101. // line is just a trick for working around a bug in MSVC 8.0, which
  102. // cannot handle it if we define FunctionMocker in ::testing.
  103. using internal::FunctionMocker;
  104. // GMOCK_RESULT_(tn, F) expands to the result type of function type F.
  105. // We define this as a variadic macro in case F contains unprotected
  106. // commas (the same reason that we use variadic macros in other places
  107. // in this file).
  108. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  109. #define GMOCK_RESULT_(tn, ...) \
  110. tn ::testing::internal::Function<__VA_ARGS__>::Result
  111. // The type of argument N of the given function type.
  112. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  113. #define GMOCK_ARG_(tn, N, ...) \
  114. tn ::testing::internal::Function<__VA_ARGS__>::template Arg<N-1>::type
  115. // The matcher type for argument N of the given function type.
  116. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  117. #define GMOCK_MATCHER_(tn, N, ...) \
  118. const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
  119. // The variable for mocking the given method.
  120. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  121. #define GMOCK_MOCKER_(arity, constness, Method) \
  122. GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__)
  123. $for i [[
  124. $range j 1..i
  125. $var arg_as = [[$for j, [[GMOCK_ARG_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  126. $var as = [[$for j, \
  127. [[::std::forward<GMOCK_ARG_(tn, $j, __VA_ARGS__)>(gmock_a$j)]]]]
  128. $var matcher_arg_as = [[$for j, \
  129. [[GMOCK_MATCHER_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  130. $var matcher_as = [[$for j, [[gmock_a$j]]]]
  131. $var anything_matchers = [[$for j, \
  132. [[::testing::A<GMOCK_ARG_(tn, $j, __VA_ARGS__)>()]]]]
  133. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  134. #define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
  135. static_assert($i == ::testing::internal::Function<__VA_ARGS__>::ArgumentCount, "MOCK_METHOD<N> must match argument count.");\
  136. GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
  137. $arg_as) constness { \
  138. GMOCK_MOCKER_($i, constness, Method).SetOwnerAndName(this, #Method); \
  139. return GMOCK_MOCKER_($i, constness, Method).Invoke($as); \
  140. } \
  141. ::testing::MockSpec<__VA_ARGS__> \
  142. gmock_##Method($matcher_arg_as) constness { \
  143. GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
  144. return GMOCK_MOCKER_($i, constness, Method).With($matcher_as); \
  145. } \
  146. ::testing::MockSpec<__VA_ARGS__> gmock_##Method( \
  147. const ::testing::internal::WithoutMatchers&, \
  148. constness ::testing::internal::Function<__VA_ARGS__>* ) const { \
  149. return ::testing::internal::AdjustConstness_##constness(this)-> \
  150. gmock_##Method($anything_matchers); \
  151. } \
  152. mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
  153. ]]
  154. $for i [[
  155. #define MOCK_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, , , m, __VA_ARGS__)
  156. ]]
  157. $for i [[
  158. #define MOCK_CONST_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, const, , m, __VA_ARGS__)
  159. ]]
  160. $for i [[
  161. #define MOCK_METHOD$i[[]]_T(m, ...) GMOCK_METHOD$i[[]]_(typename, , , m, __VA_ARGS__)
  162. ]]
  163. $for i [[
  164. #define MOCK_CONST_METHOD$i[[]]_T(m, ...) \
  165. GMOCK_METHOD$i[[]]_(typename, const, , m, __VA_ARGS__)
  166. ]]
  167. $for i [[
  168. #define MOCK_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  169. GMOCK_METHOD$i[[]]_(, , ct, m, __VA_ARGS__)
  170. ]]
  171. $for i [[
  172. #define MOCK_CONST_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  173. GMOCK_METHOD$i[[]]_(, const, ct, m, __VA_ARGS__)
  174. ]]
  175. $for i [[
  176. #define MOCK_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  177. GMOCK_METHOD$i[[]]_(typename, , ct, m, __VA_ARGS__)
  178. ]]
  179. $for i [[
  180. #define MOCK_CONST_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  181. GMOCK_METHOD$i[[]]_(typename, const, ct, m, __VA_ARGS__)
  182. ]]
  183. } // namespace testing
  184. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_