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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "gmock/gmock-spec-builders.h"
  41. #include "gmock/internal/gmock-internal-utils.h"
  42. #if GTEST_HAS_STD_FUNCTION_
  43. # include <functional>
  44. #endif
  45. namespace testing {
  46. namespace internal {
  47. template <typename F>
  48. class FunctionMockerBase;
  49. // Note: class FunctionMocker really belongs to the ::testing
  50. // namespace. However if we define it in ::testing, MSVC will
  51. // complain when classes in ::testing::internal declare it as a
  52. // friend class template. To workaround this compiler bug, we define
  53. // FunctionMocker in ::testing::internal and import it into ::testing.
  54. template <typename F>
  55. class FunctionMocker;
  56. $range i 0..n
  57. $for i [[
  58. $range j 1..i
  59. $var typename_As = [[$for j [[, typename A$j]]]]
  60. $var As = [[$for j, [[A$j]]]]
  61. $var as = [[$for j, [[internal::forward<A$j>(a$j)]]]]
  62. $var Aas = [[$for j, [[A$j a$j]]]]
  63. $var ms = [[$for j, [[m$j]]]]
  64. $var matchers = [[$for j, [[const Matcher<A$j>& m$j]]]]
  65. template <typename R$typename_As>
  66. class FunctionMocker<R($As)> : public
  67. internal::FunctionMockerBase<R($As)> {
  68. public:
  69. typedef R F($As);
  70. typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
  71. MockSpec<F> With($matchers) {
  72. return MockSpec<F>(this, ::testing::make_tuple($ms));
  73. }
  74. R Invoke($Aas) {
  75. // Even though gcc and MSVC don't enforce it, 'this->' is required
  76. // by the C++ standard [14.6.4] here, as the base class type is
  77. // dependent on the template argument (and thus shouldn't be
  78. // looked into when resolving InvokeWith).
  79. return this->InvokeWith(ArgumentTuple($as));
  80. }
  81. };
  82. ]]
  83. // Removes the given pointer; this is a helper for the expectation setter method
  84. // for parameterless matchers.
  85. //
  86. // We want to make sure that the user cannot set a parameterless expectation on
  87. // overloaded methods, including methods which are overloaded on const. Example:
  88. //
  89. // class MockClass {
  90. // MOCK_METHOD0(GetName, string&());
  91. // MOCK_CONST_METHOD0(GetName, const string&());
  92. // };
  93. //
  94. // TEST() {
  95. // // This should be an error, as it's not clear which overload is expected.
  96. // EXPECT_CALL(mock, GetName).WillOnce(ReturnRef(value));
  97. // }
  98. //
  99. // Here are the generated expectation-setter methods:
  100. //
  101. // class MockClass {
  102. // // Overload 1
  103. // MockSpec<string&()> gmock_GetName() { ... }
  104. // // Overload 2. Declared const so that the compiler will generate an
  105. // // error when trying to resolve between this and overload 4 in
  106. // // 'gmock_GetName(WithoutMatchers(), nullptr)'.
  107. // MockSpec<string&()> gmock_GetName(
  108. // const WithoutMatchers&, const Function<string&()>*) const {
  109. // // Removes const from this, calls overload 1
  110. // return AdjustConstness_(this)->gmock_GetName();
  111. // }
  112. //
  113. // // Overload 3
  114. // const string& gmock_GetName() const { ... }
  115. // // Overload 4
  116. // MockSpec<const string&()> gmock_GetName(
  117. // const WithoutMatchers&, const Function<const string&()>*) const {
  118. // // Does not remove const, calls overload 3
  119. // return AdjustConstness_const(this)->gmock_GetName();
  120. // }
  121. // }
  122. //
  123. template <typename MockType>
  124. const MockType* AdjustConstness_const(const MockType* mock) {
  125. return mock;
  126. }
  127. // Removes const from and returns the given pointer; this is a helper for the
  128. // expectation setter method for parameterless matchers.
  129. template <typename MockType>
  130. MockType* AdjustConstness_(const MockType* mock) {
  131. return const_cast<MockType*>(mock);
  132. }
  133. } // namespace internal
  134. // The style guide prohibits "using" statements in a namespace scope
  135. // inside a header file. However, the FunctionMocker class template
  136. // is meant to be defined in the ::testing namespace. The following
  137. // line is just a trick for working around a bug in MSVC 8.0, which
  138. // cannot handle it if we define FunctionMocker in ::testing.
  139. using internal::FunctionMocker;
  140. // GMOCK_RESULT_(tn, F) expands to the result type of function type F.
  141. // We define this as a variadic macro in case F contains unprotected
  142. // commas (the same reason that we use variadic macros in other places
  143. // in this file).
  144. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  145. #define GMOCK_RESULT_(tn, ...) \
  146. tn ::testing::internal::Function<__VA_ARGS__>::Result
  147. // The type of argument N of the given function type.
  148. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  149. #define GMOCK_ARG_(tn, N, ...) \
  150. tn ::testing::internal::Function<__VA_ARGS__>::Argument##N
  151. // The matcher type for argument N of the given function type.
  152. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  153. #define GMOCK_MATCHER_(tn, N, ...) \
  154. const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
  155. // The variable for mocking the given method.
  156. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  157. #define GMOCK_MOCKER_(arity, constness, Method) \
  158. GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__)
  159. $for i [[
  160. $range j 1..i
  161. $var arg_as = [[$for j, [[GMOCK_ARG_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  162. $var as = [[$for j, \
  163. [[::testing::internal::forward<GMOCK_ARG_(tn, $j, __VA_ARGS__)>(gmock_a$j)]]]]
  164. $var matcher_arg_as = [[$for j, \
  165. [[GMOCK_MATCHER_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
  166. $var matcher_as = [[$for j, [[gmock_a$j]]]]
  167. $var anything_matchers = [[$for j, \
  168. [[::testing::A<GMOCK_ARG_(tn, $j, __VA_ARGS__)>()]]]]
  169. // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
  170. #define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
  171. GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
  172. $arg_as) constness { \
  173. GTEST_COMPILE_ASSERT_((::testing::tuple_size< \
  174. tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value == $i), \
  175. this_method_does_not_take_$i[[]]_argument[[$if i != 1 [[s]]]]); \
  176. GMOCK_MOCKER_($i, constness, Method).SetOwnerAndName(this, #Method); \
  177. return GMOCK_MOCKER_($i, constness, Method).Invoke($as); \
  178. } \
  179. ::testing::MockSpec<__VA_ARGS__> \
  180. gmock_##Method($matcher_arg_as) constness { \
  181. GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
  182. return GMOCK_MOCKER_($i, constness, Method).With($matcher_as); \
  183. } \
  184. ::testing::MockSpec<__VA_ARGS__> gmock_##Method( \
  185. const ::testing::internal::WithoutMatchers&, \
  186. constness ::testing::internal::Function<__VA_ARGS__>* ) const { \
  187. return ::testing::internal::AdjustConstness_##constness(this)-> \
  188. gmock_##Method($anything_matchers); \
  189. } \
  190. mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
  191. ]]
  192. $for i [[
  193. #define MOCK_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, , , m, __VA_ARGS__)
  194. ]]
  195. $for i [[
  196. #define MOCK_CONST_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, const, , m, __VA_ARGS__)
  197. ]]
  198. $for i [[
  199. #define MOCK_METHOD$i[[]]_T(m, ...) GMOCK_METHOD$i[[]]_(typename, , , m, __VA_ARGS__)
  200. ]]
  201. $for i [[
  202. #define MOCK_CONST_METHOD$i[[]]_T(m, ...) \
  203. GMOCK_METHOD$i[[]]_(typename, const, , m, __VA_ARGS__)
  204. ]]
  205. $for i [[
  206. #define MOCK_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  207. GMOCK_METHOD$i[[]]_(, , ct, m, __VA_ARGS__)
  208. ]]
  209. $for i [[
  210. #define MOCK_CONST_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
  211. GMOCK_METHOD$i[[]]_(, const, ct, m, __VA_ARGS__)
  212. ]]
  213. $for i [[
  214. #define MOCK_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  215. GMOCK_METHOD$i[[]]_(typename, , ct, m, __VA_ARGS__)
  216. ]]
  217. $for i [[
  218. #define MOCK_CONST_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
  219. GMOCK_METHOD$i[[]]_(typename, const, ct, m, __VA_ARGS__)
  220. ]]
  221. // A MockFunction<F> class has one mock method whose type is F. It is
  222. // useful when you just want your test code to emit some messages and
  223. // have Google Mock verify the right messages are sent (and perhaps at
  224. // the right times). For example, if you are exercising code:
  225. //
  226. // Foo(1);
  227. // Foo(2);
  228. // Foo(3);
  229. //
  230. // and want to verify that Foo(1) and Foo(3) both invoke
  231. // mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write:
  232. //
  233. // TEST(FooTest, InvokesBarCorrectly) {
  234. // MyMock mock;
  235. // MockFunction<void(string check_point_name)> check;
  236. // {
  237. // InSequence s;
  238. //
  239. // EXPECT_CALL(mock, Bar("a"));
  240. // EXPECT_CALL(check, Call("1"));
  241. // EXPECT_CALL(check, Call("2"));
  242. // EXPECT_CALL(mock, Bar("a"));
  243. // }
  244. // Foo(1);
  245. // check.Call("1");
  246. // Foo(2);
  247. // check.Call("2");
  248. // Foo(3);
  249. // }
  250. //
  251. // The expectation spec says that the first Bar("a") must happen
  252. // before check point "1", the second Bar("a") must happen after check
  253. // point "2", and nothing should happen between the two check
  254. // points. The explicit check points make it easy to tell which
  255. // Bar("a") is called by which call to Foo().
  256. //
  257. // MockFunction<F> can also be used to exercise code that accepts
  258. // std::function<F> callbacks. To do so, use AsStdFunction() method
  259. // to create std::function proxy forwarding to original object's Call.
  260. // Example:
  261. //
  262. // TEST(FooTest, RunsCallbackWithBarArgument) {
  263. // MockFunction<int(string)> callback;
  264. // EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));
  265. // Foo(callback.AsStdFunction());
  266. // }
  267. template <typename F>
  268. class MockFunction;
  269. $for i [[
  270. $range j 0..i-1
  271. $var ArgTypes = [[$for j, [[A$j]]]]
  272. $var ArgValues = [[$for j, [[::std::move(a$j)]]]]
  273. $var ArgDecls = [[$for j, [[A$j a$j]]]]
  274. template <typename R$for j [[, typename A$j]]>
  275. class MockFunction<R($ArgTypes)> {
  276. public:
  277. MockFunction() {}
  278. MOCK_METHOD$i[[]]_T(Call, R($ArgTypes));
  279. #if GTEST_HAS_STD_FUNCTION_
  280. ::std::function<R($ArgTypes)> AsStdFunction() {
  281. return [this]($ArgDecls) -> R {
  282. return this->Call($ArgValues);
  283. };
  284. }
  285. #endif // GTEST_HAS_STD_FUNCTION_
  286. private:
  287. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFunction);
  288. };
  289. ]]
  290. } // namespace testing
  291. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_