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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert it to
  3. $$ 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. //
  35. // Author: wan@google.com (Zhanyong Wan)
  36. // Google Mock - a framework for writing C++ mock classes.
  37. //
  38. // This file implements function mockers of various arities.
  39. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
  40. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
  41. #include "gmock/gmock-spec-builders.h"
  42. #include "gmock/internal/gmock-internal-utils.h"
  43. #if GTEST_HAS_STD_FUNCTION_
  44. # include <functional>
  45. #endif
  46. namespace testing {
  47. namespace internal {
  48. template <typename F>
  49. class FunctionMockerBase;
  50. // Note: class FunctionMocker really belongs to the ::testing
  51. // namespace. However if we define it in ::testing, MSVC will
  52. // complain when classes in ::testing::internal declare it as a
  53. // friend class template. To workaround this compiler bug, we define
  54. // FunctionMocker in ::testing::internal and import it into ::testing.
  55. template <typename F>
  56. class FunctionMocker;
  57. $range i 0..n
  58. $for i [[
  59. $range j 1..i
  60. $var typename_As = [[$for j [[, typename A$j]]]]
  61. $var As = [[$for j, [[A$j]]]]
  62. $var as = [[$for j, [[a$j]]]]
  63. $var Aas = [[$for j, [[A$j a$j]]]]
  64. $var ms = [[$for j, [[m$j]]]]
  65. $var matchers = [[$for j, [[const Matcher<A$j>& m$j]]]]
  66. template <typename R$typename_As>
  67. class FunctionMocker<R($As)> : public
  68. internal::FunctionMockerBase<R($As)> {
  69. public:
  70. typedef R F($As);
  71. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  72. MockSpec<F>& With($matchers) {
  73. $if i >= 1 [[
  74. this->current_spec().SetMatchers(::testing::make_tuple($ms));
  75. ]]
  76. return this->current_spec();
  77. }
  78. R Invoke($Aas) {
  79. // Even though gcc and MSVC don't enforce it, 'this->' is required
  80. // by the C++ standard [14.6.4] here, as the base class type is
  81. // dependent on the template argument (and thus shouldn't be
  82. // looked into when resolving InvokeWith).
  83. return this->InvokeWith(ArgumentTuple($as));
  84. }
  85. };
  86. ]]
  87. } // namespace internal
  88. // The style guide prohibits "using" statements in a namespace scope
  89. // inside a header file. However, the FunctionMocker class template
  90. // is meant to be defined in the ::testing namespace. The following
  91. // line is just a trick for working around a bug in MSVC 8.0, which
  92. // cannot handle it if we define FunctionMocker in ::testing.
  93. using internal::FunctionMocker;
  94. // GMOCK_RESULT_(tn, F) expands to the result type of function type F.
  95. // We define this as a variadic macro in case F contains unprotected
  96. // commas (the same reason that we use variadic macros in other places
  97. // in this file).
  98. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  99. #define GMOCK_RESULT_(tn, ...) \
  100. tn ::testing::internal::Function<__VA_ARGS__>::Result
  101. // The type of argument N of the given function type.
  102. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  103. #define GMOCK_ARG_(tn, N, ...) \
  104. tn ::testing::internal::Function<__VA_ARGS__>::Argument##N
  105. // The matcher type for argument N of the given function type.
  106. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  107. #define GMOCK_MATCHER_(tn, N, ...) \
  108. const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
  109. // The variable for mocking the given method.
  110. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  111. #define GMOCK_MOCKER_(arity, constness, Method) \
  112. GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__)
  113. $for i [[
  114. $range j 1..i
  115. $var arg_as = [[$for j, \
  116. [[GMOCK_ARG_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  117. $var as = [[$for j, [[gmock_a$j]]]]
  118. $var matcher_as = [[$for j, \
  119. [[GMOCK_MATCHER_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  120. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  121. #define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
  122. GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
  123. $arg_as) constness { \
  124. GTEST_COMPILE_ASSERT_((::testing::tuple_size< \
  125. tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value == $i), \
  126. this_method_does_not_take_$i[[]]_argument[[$if i != 1 [[s]]]]); \
  127. GMOCK_MOCKER_($i, constness, Method).SetOwnerAndName(this, #Method); \
  128. return GMOCK_MOCKER_($i, constness, Method).Invoke($as); \
  129. } \
  130. ::testing::MockSpec<__VA_ARGS__>& \
  131. gmock_##Method($matcher_as) constness { \
  132. GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
  133. return GMOCK_MOCKER_($i, constness, Method).With($as); \
  134. } \
  135. mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
  136. ]]
  137. $for i [[
  138. #define MOCK_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, , , m, __VA_ARGS__)
  139. ]]
  140. $for i [[
  141. #define MOCK_CONST_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, const, , m, __VA_ARGS__)
  142. ]]
  143. $for i [[
  144. #define MOCK_METHOD$i[[]]_T(m, ...) GMOCK_METHOD$i[[]]_(typename, , , m, __VA_ARGS__)
  145. ]]
  146. $for i [[
  147. #define MOCK_CONST_METHOD$i[[]]_T(m, ...) \
  148. GMOCK_METHOD$i[[]]_(typename, const, , m, __VA_ARGS__)
  149. ]]
  150. $for i [[
  151. #define MOCK_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  152. GMOCK_METHOD$i[[]]_(, , ct, m, __VA_ARGS__)
  153. ]]
  154. $for i [[
  155. #define MOCK_CONST_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  156. GMOCK_METHOD$i[[]]_(, const, ct, m, __VA_ARGS__)
  157. ]]
  158. $for i [[
  159. #define MOCK_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  160. GMOCK_METHOD$i[[]]_(typename, , ct, m, __VA_ARGS__)
  161. ]]
  162. $for i [[
  163. #define MOCK_CONST_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  164. GMOCK_METHOD$i[[]]_(typename, const, ct, m, __VA_ARGS__)
  165. ]]
  166. // A MockFunction<F> class has one mock method whose type is F. It is
  167. // useful when you just want your test code to emit some messages and
  168. // have Google Mock verify the right messages are sent (and perhaps at
  169. // the right times). For example, if you are exercising code:
  170. //
  171. // Foo(1);
  172. // Foo(2);
  173. // Foo(3);
  174. //
  175. // and want to verify that Foo(1) and Foo(3) both invoke
  176. // mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write:
  177. //
  178. // TEST(FooTest, InvokesBarCorrectly) {
  179. // MyMock mock;
  180. // MockFunction<void(string check_point_name)> check;
  181. // {
  182. // InSequence s;
  183. //
  184. // EXPECT_CALL(mock, Bar("a"));
  185. // EXPECT_CALL(check, Call("1"));
  186. // EXPECT_CALL(check, Call("2"));
  187. // EXPECT_CALL(mock, Bar("a"));
  188. // }
  189. // Foo(1);
  190. // check.Call("1");
  191. // Foo(2);
  192. // check.Call("2");
  193. // Foo(3);
  194. // }
  195. //
  196. // The expectation spec says that the first Bar("a") must happen
  197. // before check point "1", the second Bar("a") must happen after check
  198. // point "2", and nothing should happen between the two check
  199. // points. The explicit check points make it easy to tell which
  200. // Bar("a") is called by which call to Foo().
  201. //
  202. // MockFunction<F> can also be used to exercise code that accepts
  203. // std::function<F> callbacks. To do so, use AsStdFunction() method
  204. // to create std::function proxy forwarding to original object's Call.
  205. // Example:
  206. //
  207. // TEST(FooTest, RunsCallbackWithBarArgument) {
  208. // MockFunction<int(string)> callback;
  209. // EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));
  210. // Foo(callback.AsStdFunction());
  211. // }
  212. template <typename F>
  213. class MockFunction;
  214. $for i [[
  215. $range j 0..i-1
  216. $var ArgTypes = [[$for j, [[A$j]]]]
  217. $var ArgNames = [[$for j, [[a$j]]]]
  218. $var ArgDecls = [[$for j, [[A$j a$j]]]]
  219. template <typename R$for j [[, typename A$j]]>
  220. class MockFunction<R($ArgTypes)> {
  221. public:
  222. MockFunction() {}
  223. MOCK_METHOD$i[[]]_T(Call, R($ArgTypes));
  224. #if GTEST_HAS_STD_FUNCTION_
  225. std::function<R($ArgTypes)> AsStdFunction() {
  226. return [this]($ArgDecls) -> R {
  227. return this->Call($ArgNames);
  228. };
  229. }
  230. #endif // GTEST_HAS_STD_FUNCTION_
  231. private:
  232. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFunction);
  233. };
  234. ]]
  235. } // namespace testing
  236. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_