652e4c204c5f4089fc108a1bf1d6e3fb1dcd7a18
[platform/core/ml/aitt.git] / external / C-Mock / v0.3.1 / cmock / cmock-function-mockers.h.pump
1 $$ -*- mode: c++; -*-
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
4 $$ googletest/scripts.
5 $$
6 $var n = 10  $$ The maximum arity we support.
7 // Copyright 2021, Hubert Jagodziński
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
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
19 // distribution.
20 //
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.
32 //
33 // Author: hubert.jagodzinski@gmail.com (Hubert Jagodziński)
34
35 // C Mock - Google Mock's extension allowing a function mocking.
36 //
37 // This file implements DECLARE_FUNCTION_MOCKn() and IMPLEMENT_FUNCTION_MOCKn() macros of various arities.
38
39 #ifndef CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_
40 #define CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_
41
42 #include <dlfcn.h>
43
44 #include <sstream>
45 #include <stdexcept>
46
47 $range i 0..n
48
49 $for i [[
50 $range j 1..i
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]]]]
54
55 #define DECLARE_FUNCTION_MOCK$i(c, n, F) \
56 class c \
57 { \
58     typedef GMOCK_RESULT_(, F) (*func_type)($declare_args); \
59 \
60     public: \
61         static func_type real; \
62 \
63         c(); \
64         ~c(); \
65 \
66         GMOCK_RESULT_(, F) operator()($declare_args); \
67         ::testing::MockSpec<F> cmock_func($matcher_args); \
68 \
69     private: \
70         static func_type lookup(); \
71         static GMOCK_RESULT_(, F) call($declare_args); \
72 \
73         static c *instance; \
74 \
75         ::testing::FunctionMocker<F> mocker; \
76 \
77         friend GMOCK_RESULT_(, F) n($declare_args); \
78 };
79
80 #define IMPLEMENT_FUNCTION_MOCK$i(c, n, F) \
81 c::c() { \
82     instance = this; \
83 } \
84 \
85 c::~c() { \
86     instance = NULL; \
87 } \
88 \
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); \
95 } \
96 \
97 ::testing::MockSpec<F> c::cmock_func($matcher_args) { \
98     mocker.RegisterOwner(this); \
99     return mocker.With($call_args); \
100 } \
101 \
102 c::func_type c::lookup() { \
103     return (c::func_type)dlsym(RTLD_NEXT, #n); \
104 } \
105 \
106 GMOCK_RESULT_(, F) c::call($declare_args) { \
107     if (instance != NULL) { \
108         return (*instance)($call_args); \
109     } \
110 \
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()); \
116     } \
117     return real($call_args); \
118 } \
119 \
120 c *c::instance = NULL; \
121 c::func_type c::real = lookup(); \
122 \
123 GMOCK_RESULT_(, F) n($declare_args) { \
124     return c::call($call_args); \
125 }
126
127 ]]
128
129 #endif // CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_MOCKERS_H_