2 $$ This is a Pump source file. Please use Pump to convert it to
3 $$ cmock-function-mockers.h. Pump (pump.py) can be found in
6 $var n = 10 $$ The maximum arity we support.
7 // Copyright 2021, Hubert Jagodziński
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
14 // * Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 // * Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following disclaimer
18 // in the documentation and/or other materials provided with the
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Author: hubert.jagodzinski@gmail.com (Hubert Jagodziński)
35 // C Mock - Google Mock's extension allowing a function mocking.
37 // This file implements DECLARE_FUNCTION_MOCKn() and IMPLEMENT_FUNCTION_MOCKn() macros of various arities.
39 #ifndef CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_
40 #define CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_
51 $var call_args = [[$for j, [[cmock_a$j]]]]
52 $var declare_args = [[$for j, [[GMOCK_ARG_(, $j, F) cmock_a$j]]]]
53 $var matcher_args = [[$for j, [[GMOCK_MATCHER_(, $j, F) cmock_a$j]]]]
55 #define DECLARE_FUNCTION_MOCK$i(c, n, F) \
58 typedef GMOCK_RESULT_(, F) (*func_type)($declare_args); \
61 static func_type real; \
66 GMOCK_RESULT_(, F) operator()($declare_args); \
67 ::testing::MockSpec<F> cmock_func($matcher_args); \
70 static func_type lookup(); \
71 static GMOCK_RESULT_(, F) call($declare_args); \
75 ::testing::FunctionMocker<F> mocker; \
77 friend GMOCK_RESULT_(, F) n($declare_args); \
80 #define IMPLEMENT_FUNCTION_MOCK$i(c, n, F) \
89 GMOCK_RESULT_(, F) c::operator()($declare_args) { \
90 GTEST_COMPILE_ASSERT_(::std::tuple_size< \
91 ::testing::internal::Function<F>::ArgumentTuple>::value == $i, \
92 this_method_does_not_take_$i[[]]_argument[[$if i != 1 [[s]]]]); \
93 mocker.SetOwnerAndName(this, #n); \
94 return mocker.Invoke($call_args); \
97 ::testing::MockSpec<F> c::cmock_func($matcher_args) { \
98 mocker.RegisterOwner(this); \
99 return mocker.With($call_args); \
102 c::func_type c::lookup() { \
103 return (c::func_type)dlsym(RTLD_NEXT, #n); \
106 GMOCK_RESULT_(, F) c::call($declare_args) { \
107 if (instance != NULL) { \
108 return (*instance)($call_args); \
111 if (real == NULL) { \
112 std::ostringstream msg; \
113 msg << "Error: Function " << #n; \
114 msg << " not found. Neither mock nor real function is present."; \
115 throw std::logic_error(msg.str()); \
117 return real($call_args); \
120 c *c::instance = NULL; \
121 c::func_type c::real = lookup(); \
123 GMOCK_RESULT_(, F) n($declare_args) { \
124 return c::call($call_args); \
129 #endif // CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_