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 | |
| 44 | #include <gmock/gmock-spec-builders.h> |
| 45 | #include <gmock/internal/gmock-internal-utils.h> |
| 46 | |
| 47 | namespace testing { |
| 48 | |
| 49 | template <typename F> |
| 50 | class MockSpec; |
| 51 | |
| 52 | namespace internal { |
| 53 | |
| 54 | template <typename F> |
| 55 | class FunctionMockerBase; |
| 56 | |
| 57 | // Note: class FunctionMocker really belongs to the ::testing |
| 58 | // namespace. However if we define it in ::testing, MSVC will |
| 59 | // complain when classes in ::testing::internal declare it as a |
| 60 | // friend class template. To workaround this compiler bug, we define |
| 61 | // FunctionMocker in ::testing::internal and import it into ::testing. |
| 62 | template <typename F> |
| 63 | class FunctionMocker; |
| 64 | |
| 65 | |
| 66 | $range i 0..n |
| 67 | $for i [[ |
| 68 | $range j 1..i |
| 69 | $var typename_As = [[$for j [[, typename A$j]]]] |
| 70 | $var As = [[$for j, [[A$j]]]] |
| 71 | $var as = [[$for j, [[a$j]]]] |
| 72 | $var Aas = [[$for j, [[A$j a$j]]]] |
| 73 | $var ms = [[$for j, [[m$j]]]] |
| 74 | $var matchers = [[$for j, [[const Matcher<A$j>& m$j]]]] |
| 75 | template <typename R$typename_As> |
| 76 | class FunctionMocker<R($As)> : public |
| 77 | internal::FunctionMockerBase<R($As)> { |
| 78 | public: |
| 79 | typedef R F($As); |
| 80 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 81 | |
| 82 | MockSpec<F>& With($matchers) { |
| 83 | |
| 84 | $if i >= 1 [[ |
| 85 | this->current_spec().SetMatchers(::std::tr1::make_tuple($ms)); |
| 86 | |
| 87 | ]] |
| 88 | return this->current_spec(); |
| 89 | } |
| 90 | |
| 91 | R Invoke($Aas) { |
| 92 | return InvokeWith(ArgumentTuple($as)); |
| 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 | |
| 107 | // The result type of function type F. |
| 108 | // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 109 | #define GMOCK_RESULT_(tn, F) tn ::testing::internal::Function<F>::Result |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 110 | |
| 111 | // The type of argument N of function type F. |
| 112 | // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 113 | #define GMOCK_ARG_(tn, F, N) tn ::testing::internal::Function<F>::Argument##N |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 114 | |
| 115 | // The matcher type for argument N of function type F. |
| 116 | // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 117 | #define GMOCK_MATCHER_(tn, F, N) const ::testing::Matcher<GMOCK_ARG_(tn, F, N)>& |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 118 | |
| 119 | // The variable for mocking the given method. |
| 120 | // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! |
zhanyong.wan | 68be111 | 2009-03-25 03:56:48 +0000 | [diff] [blame] | 121 | #define GMOCK_MOCKER_(arity, constness, Method) \ |
| 122 | GMOCK_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | $for i [[ |
| 126 | $range j 1..i |
| 127 | $var arg_as = [[$for j, \ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 128 | [[GMOCK_ARG_(tn, F, $j) gmock_a$j]]]] |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 129 | $var as = [[$for j, [[gmock_a$j]]]] |
| 130 | $var matcher_as = [[$for j, \ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 131 | [[GMOCK_MATCHER_(tn, F, $j) gmock_a$j]]]] |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 132 | // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 133 | #define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, F) \ |
| 134 | GMOCK_RESULT_(tn, F) ct Method($arg_as) constness { \ |
| 135 | GMOCK_COMPILE_ASSERT_(::std::tr1::tuple_size< \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 136 | tn ::testing::internal::Function<F>::ArgumentTuple>::value == $i, \ |
| 137 | this_method_does_not_take_$i[[]]_argument[[$if i != 1 [[s]]]]); \ |
zhanyong.wan | 68be111 | 2009-03-25 03:56:48 +0000 | [diff] [blame] | 138 | GMOCK_MOCKER_($i, constness, Method).SetOwnerAndName(this, #Method); \ |
| 139 | return GMOCK_MOCKER_($i, constness, Method).Invoke($as); \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 140 | } \ |
| 141 | ::testing::MockSpec<F>& \ |
| 142 | gmock_##Method($matcher_as) constness { \ |
zhanyong.wan | 68be111 | 2009-03-25 03:56:48 +0000 | [diff] [blame] | 143 | return GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this).With($as); \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 144 | } \ |
zhanyong.wan | 68be111 | 2009-03-25 03:56:48 +0000 | [diff] [blame] | 145 | mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_($i, constness, Method) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | ]] |
| 149 | $for i [[ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 150 | #define MOCK_METHOD$i(m, F) GMOCK_METHOD$i[[]]_(, , , m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 151 | |
| 152 | ]] |
| 153 | |
| 154 | |
| 155 | $for i [[ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 156 | #define MOCK_CONST_METHOD$i(m, F) GMOCK_METHOD$i[[]]_(, const, , m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 157 | |
| 158 | ]] |
| 159 | |
| 160 | |
| 161 | $for i [[ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 162 | #define MOCK_METHOD$i[[]]_T(m, F) GMOCK_METHOD$i[[]]_(typename, , , m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 163 | |
| 164 | ]] |
| 165 | |
| 166 | |
| 167 | $for i [[ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 168 | #define MOCK_CONST_METHOD$i[[]]_T(m, F) [[]] |
| 169 | GMOCK_METHOD$i[[]]_(typename, const, , m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 170 | |
| 171 | ]] |
| 172 | |
| 173 | |
| 174 | $for i [[ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 175 | #define MOCK_METHOD$i[[]]_WITH_CALLTYPE(ct, m, F) [[]] |
| 176 | GMOCK_METHOD$i[[]]_(, , ct, m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 177 | |
| 178 | ]] |
| 179 | |
| 180 | |
| 181 | $for i [[ |
| 182 | #define MOCK_CONST_METHOD$i[[]]_WITH_CALLTYPE(ct, m, F) \ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 183 | GMOCK_METHOD$i[[]]_(, const, ct, m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 184 | |
| 185 | ]] |
| 186 | |
| 187 | |
| 188 | $for i [[ |
| 189 | #define MOCK_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, F) \ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 190 | GMOCK_METHOD$i[[]]_(typename, , ct, m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 191 | |
| 192 | ]] |
| 193 | |
| 194 | |
| 195 | $for i [[ |
| 196 | #define MOCK_CONST_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, F) \ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 197 | GMOCK_METHOD$i[[]]_(typename, const, ct, m, F) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 198 | |
| 199 | ]] |
| 200 | |
zhanyong.wan | f3aa4d2 | 2009-09-25 22:34:47 +0000 | [diff] [blame^] | 201 | // A MockFunction<F> class has one mock method whose type is F. It is |
| 202 | // useful when you just want your test code to emit some messages and |
| 203 | // have Google Mock verify the right messages are sent (and perhaps at |
| 204 | // the right times). For example, if you are exercising code: |
| 205 | // |
| 206 | // Foo(1); |
| 207 | // Foo(2); |
| 208 | // Foo(3); |
| 209 | // |
| 210 | // and want to verify that Foo(1) and Foo(3) both invoke |
| 211 | // mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: |
| 212 | // |
| 213 | // TEST(FooTest, InvokesBarCorrectly) { |
| 214 | // MyMock mock; |
| 215 | // MockFunction<void(string check_point_name)> check; |
| 216 | // { |
| 217 | // InSequence s; |
| 218 | // |
| 219 | // EXPECT_CALL(mock, Bar("a")); |
| 220 | // EXPECT_CALL(check, Call("1")); |
| 221 | // EXPECT_CALL(check, Call("2")); |
| 222 | // EXPECT_CALL(mock, Bar("a")); |
| 223 | // } |
| 224 | // Foo(1); |
| 225 | // check.Call("1"); |
| 226 | // Foo(2); |
| 227 | // check.Call("2"); |
| 228 | // Foo(3); |
| 229 | // } |
| 230 | // |
| 231 | // The expectation spec says that the first Bar("a") must happen |
| 232 | // before check point "1", the second Bar("a") must happen after check |
| 233 | // point "2", and nothing should happen between the two check |
| 234 | // points. The explicit check points make it easy to tell which |
| 235 | // Bar("a") is called by which call to Foo(). |
| 236 | template <typename F> |
| 237 | class MockFunction; |
| 238 | |
| 239 | |
| 240 | $for i [[ |
| 241 | $range j 0..i-1 |
| 242 | template <typename R$for j [[, typename A$j]]> |
| 243 | class MockFunction<R($for j, [[A$j]])> { |
| 244 | public: |
| 245 | MOCK_METHOD$i[[]]_T(Call, R($for j, [[A$j]])); |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | ]] |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 250 | } // namespace testing |
| 251 | |
| 252 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_ |