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