Warning, /JETSCAPE/external_packages/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h.pump is written in an unsupported language. File is not indexed.
0001 $$ -*- mode: c++; -*-
0002 $$ This is a Pump source file. Please use Pump to convert it to
0003 $$ gmock-generated-function-mockers.h.
0004 $$
0005 $var n = 10 $$ The maximum arity we support.
0006 // Copyright 2007, Google Inc.
0007 // All rights reserved.
0008 //
0009 // Redistribution and use in source and binary forms, with or without
0010 // modification, are permitted provided that the following conditions are
0011 // met:
0012 //
0013 // * Redistributions of source code must retain the above copyright
0014 // notice, this list of conditions and the following disclaimer.
0015 // * Redistributions in binary form must reproduce the above
0016 // copyright notice, this list of conditions and the following disclaimer
0017 // in the documentation and/or other materials provided with the
0018 // distribution.
0019 // * Neither the name of Google Inc. nor the names of its
0020 // contributors may be used to endorse or promote products derived from
0021 // this software without specific prior written permission.
0022 //
0023 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0024 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0025 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0026 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0027 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0028 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0029 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0030 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0031 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0033 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0034 //
0035 // Author: wan@google.com (Zhanyong Wan)
0036
0037 // Google Mock - a framework for writing C++ mock classes.
0038 //
0039 // This file implements function mockers of various arities.
0040
0041 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
0042 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_
0043
0044 #include "gmock/gmock-spec-builders.h"
0045 #include "gmock/internal/gmock-internal-utils.h"
0046
0047 #if GTEST_HAS_STD_FUNCTION_
0048 # include <functional>
0049 #endif
0050
0051 namespace testing {
0052 namespace internal {
0053
0054 template <typename F>
0055 class FunctionMockerBase;
0056
0057 // Note: class FunctionMocker really belongs to the ::testing
0058 // namespace. However if we define it in ::testing, MSVC will
0059 // complain when classes in ::testing::internal declare it as a
0060 // friend class template. To workaround this compiler bug, we define
0061 // FunctionMocker in ::testing::internal and import it into ::testing.
0062 template <typename F>
0063 class FunctionMocker;
0064
0065
0066 $range i 0..n
0067 $for i [[
0068 $range j 1..i
0069 $var typename_As = [[$for j [[, typename A$j]]]]
0070 $var As = [[$for j, [[A$j]]]]
0071 $var as = [[$for j, [[a$j]]]]
0072 $var Aas = [[$for j, [[A$j a$j]]]]
0073 $var ms = [[$for j, [[m$j]]]]
0074 $var matchers = [[$for j, [[const Matcher<A$j>& m$j]]]]
0075 template <typename R$typename_As>
0076 class FunctionMocker<R($As)> : public
0077 internal::FunctionMockerBase<R($As)> {
0078 public:
0079 typedef R F($As);
0080 typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
0081
0082 MockSpec<F>& With($matchers) {
0083
0084 $if i >= 1 [[
0085 this->current_spec().SetMatchers(::testing::make_tuple($ms));
0086
0087 ]]
0088 return this->current_spec();
0089 }
0090
0091 R Invoke($Aas) {
0092 // Even though gcc and MSVC don't enforce it, 'this->' is required
0093 // by the C++ standard [14.6.4] here, as the base class type is
0094 // dependent on the template argument (and thus shouldn't be
0095 // looked into when resolving InvokeWith).
0096 return this->InvokeWith(ArgumentTuple($as));
0097 }
0098 };
0099
0100
0101 ]]
0102 } // namespace internal
0103
0104 // The style guide prohibits "using" statements in a namespace scope
0105 // inside a header file. However, the FunctionMocker class template
0106 // is meant to be defined in the ::testing namespace. The following
0107 // line is just a trick for working around a bug in MSVC 8.0, which
0108 // cannot handle it if we define FunctionMocker in ::testing.
0109 using internal::FunctionMocker;
0110
0111 // GMOCK_RESULT_(tn, F) expands to the result type of function type F.
0112 // We define this as a variadic macro in case F contains unprotected
0113 // commas (the same reason that we use variadic macros in other places
0114 // in this file).
0115 // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
0116 #define GMOCK_RESULT_(tn, ...) \
0117 tn ::testing::internal::Function<__VA_ARGS__>::Result
0118
0119 // The type of argument N of the given function type.
0120 // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
0121 #define GMOCK_ARG_(tn, N, ...) \
0122 tn ::testing::internal::Function<__VA_ARGS__>::Argument##N
0123
0124 // The matcher type for argument N of the given function type.
0125 // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
0126 #define GMOCK_MATCHER_(tn, N, ...) \
0127 const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
0128
0129 // The variable for mocking the given method.
0130 // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
0131 #define GMOCK_MOCKER_(arity, constness, Method) \
0132 GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__)
0133
0134
0135 $for i [[
0136 $range j 1..i
0137 $var arg_as = [[$for j, \
0138 [[GMOCK_ARG_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
0139 $var as = [[$for j, [[gmock_a$j]]]]
0140 $var matcher_as = [[$for j, \
0141 [[GMOCK_MATCHER_(tn, $j, __VA_ARGS__) gmock_a$j]]]]
0142 // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
0143 #define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
0144 GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
0145 $arg_as) constness { \
0146 GTEST_COMPILE_ASSERT_((::testing::tuple_size< \
0147 tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value == $i), \
0148 this_method_does_not_take_$i[[]]_argument[[$if i != 1 [[s]]]]); \
0149 GMOCK_MOCKER_($i, constness, Method).SetOwnerAndName(this, #Method); \
0150 return GMOCK_MOCKER_($i, constness, Method).Invoke($as); \
0151 } \
0152 ::testing::MockSpec<__VA_ARGS__>& \
0153 gmock_##Method($matcher_as) constness { \
0154 GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
0155 return GMOCK_MOCKER_($i, constness, Method).With($as); \
0156 } \
0157 mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
0158
0159
0160 ]]
0161 $for i [[
0162 #define MOCK_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, , , m, __VA_ARGS__)
0163
0164 ]]
0165
0166
0167 $for i [[
0168 #define MOCK_CONST_METHOD$i(m, ...) GMOCK_METHOD$i[[]]_(, const, , m, __VA_ARGS__)
0169
0170 ]]
0171
0172
0173 $for i [[
0174 #define MOCK_METHOD$i[[]]_T(m, ...) GMOCK_METHOD$i[[]]_(typename, , , m, __VA_ARGS__)
0175
0176 ]]
0177
0178
0179 $for i [[
0180 #define MOCK_CONST_METHOD$i[[]]_T(m, ...) \
0181 GMOCK_METHOD$i[[]]_(typename, const, , m, __VA_ARGS__)
0182
0183 ]]
0184
0185
0186 $for i [[
0187 #define MOCK_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
0188 GMOCK_METHOD$i[[]]_(, , ct, m, __VA_ARGS__)
0189
0190 ]]
0191
0192
0193 $for i [[
0194 #define MOCK_CONST_METHOD$i[[]]_WITH_CALLTYPE(ct, m, ...) \
0195 GMOCK_METHOD$i[[]]_(, const, ct, m, __VA_ARGS__)
0196
0197 ]]
0198
0199
0200 $for i [[
0201 #define MOCK_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
0202 GMOCK_METHOD$i[[]]_(typename, , ct, m, __VA_ARGS__)
0203
0204 ]]
0205
0206
0207 $for i [[
0208 #define MOCK_CONST_METHOD$i[[]]_T_WITH_CALLTYPE(ct, m, ...) \
0209 GMOCK_METHOD$i[[]]_(typename, const, ct, m, __VA_ARGS__)
0210
0211 ]]
0212
0213 // A MockFunction<F> class has one mock method whose type is F. It is
0214 // useful when you just want your test code to emit some messages and
0215 // have Google Mock verify the right messages are sent (and perhaps at
0216 // the right times). For example, if you are exercising code:
0217 //
0218 // Foo(1);
0219 // Foo(2);
0220 // Foo(3);
0221 //
0222 // and want to verify that Foo(1) and Foo(3) both invoke
0223 // mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write:
0224 //
0225 // TEST(FooTest, InvokesBarCorrectly) {
0226 // MyMock mock;
0227 // MockFunction<void(string check_point_name)> check;
0228 // {
0229 // InSequence s;
0230 //
0231 // EXPECT_CALL(mock, Bar("a"));
0232 // EXPECT_CALL(check, Call("1"));
0233 // EXPECT_CALL(check, Call("2"));
0234 // EXPECT_CALL(mock, Bar("a"));
0235 // }
0236 // Foo(1);
0237 // check.Call("1");
0238 // Foo(2);
0239 // check.Call("2");
0240 // Foo(3);
0241 // }
0242 //
0243 // The expectation spec says that the first Bar("a") must happen
0244 // before check point "1", the second Bar("a") must happen after check
0245 // point "2", and nothing should happen between the two check
0246 // points. The explicit check points make it easy to tell which
0247 // Bar("a") is called by which call to Foo().
0248 //
0249 // MockFunction<F> can also be used to exercise code that accepts
0250 // std::function<F> callbacks. To do so, use AsStdFunction() method
0251 // to create std::function proxy forwarding to original object's Call.
0252 // Example:
0253 //
0254 // TEST(FooTest, RunsCallbackWithBarArgument) {
0255 // MockFunction<int(string)> callback;
0256 // EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));
0257 // Foo(callback.AsStdFunction());
0258 // }
0259 template <typename F>
0260 class MockFunction;
0261
0262
0263 $for i [[
0264 $range j 0..i-1
0265 $var ArgTypes = [[$for j, [[A$j]]]]
0266 $var ArgNames = [[$for j, [[a$j]]]]
0267 $var ArgDecls = [[$for j, [[A$j a$j]]]]
0268 template <typename R$for j [[, typename A$j]]>
0269 class MockFunction<R($ArgTypes)> {
0270 public:
0271 MockFunction() {}
0272
0273 MOCK_METHOD$i[[]]_T(Call, R($ArgTypes));
0274
0275 #if GTEST_HAS_STD_FUNCTION_
0276 std::function<R($ArgTypes)> AsStdFunction() {
0277 return [this]($ArgDecls) -> R {
0278 return this->Call($ArgNames);
0279 };
0280 }
0281 #endif // GTEST_HAS_STD_FUNCTION_
0282
0283 private:
0284 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFunction);
0285 };
0286
0287
0288 ]]
0289 } // namespace testing
0290
0291 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_