My Project
Loading...
Searching...
No Matches
Mock.h
Go to the documentation of this file.
1#ifndef __cxxtest__Mock_h__
2#define __cxxtest__Mock_h__
3
4//
5// The default namespace is T::
6//
7#ifndef CXXTEST_MOCK_NAMESPACE
8# define CXXTEST_MOCK_NAMESPACE T
9#endif // CXXTEST_MOCK_NAMESPACE
10
11//
12// MockTraits: What to return when no mock object has been created
13//
14#define __CXXTEST_MOCK__TRAITS \
15 namespace CXXTEST_MOCK_NAMESPACE \
16 { \
17 template<class T> \
18 class MockTraits \
19 { \
20 public: \
21 static T defaultValue() { return 0; } \
22 }; \
23 };
24
25//
26// extern "C" when needed
27//
28#ifdef __cplusplus
29# define CXXTEST_EXTERN_C extern "C"
30#else
31# define CXXTEST_EXTERN_C
32#endif // __cplusplus
33
34//
35// Prototypes: For "normal" headers
36//
37#define __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
38 namespace CXXTEST_MOCK_NAMESPACE { TYPE NAME ARGS; }
39
40#define __CXXTEST_MOCK_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
41 __CXXTEST_MOCK__PROTOTYPE( MOCK, void, NAME, ARGS, REAL, CALL )
42
43#define __CXXTEST_SUPPLY__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
44 TYPE REAL ARGS;
45
46#define __CXXTEST_SUPPLY_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
47 __CXXTEST_SUPPLY__PROTOTYPE( MOCK, void, NAME, ARGS, REAL, CALL )
48
49//
50// Class declarations: For test files
51//
52#define __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
53 namespace CXXTEST_MOCK_NAMESPACE { \
54 class Base_##MOCK : public CxxTest::Link \
55 { \
56 public: \
57 Base_##MOCK(); \
58 ~Base_##MOCK(); \
59 bool setUp(); \
60 bool tearDown(); \
61 \
62 static Base_##MOCK &current(); \
63 \
64 virtual TYPE NAME ARGS = 0; \
65 \
66 private: \
67 static CxxTest::List _list; \
68 }; \
69 \
70 class Real_##MOCK : public Base_##MOCK \
71 { \
72 public: \
73 TYPE NAME ARGS; \
74 }; \
75 \
76 class _Unimplemented_##MOCK : public Base_##MOCK \
77 { \
78 public: \
79 TYPE NAME ARGS; \
80 }; \
81 }
82
83#define __CXXTEST_MOCK_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL ) \
84 __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, void, NAME, ARGS, REAL, CALL )
85
86#define __CXXTEST_SUPPLY__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
87 namespace CXXTEST_MOCK_NAMESPACE { \
88 class Base_##MOCK : public CxxTest::Link \
89 { \
90 public: \
91 Base_##MOCK(); \
92 ~Base_##MOCK(); \
93 bool setUp(); \
94 bool tearDown(); \
95 \
96 static Base_##MOCK &current(); \
97 \
98 virtual TYPE NAME ARGS = 0; \
99 \
100 private: \
101 static CxxTest::List _list; \
102 }; \
103 \
104 class _Unimplemented_##MOCK : public Base_##MOCK \
105 { \
106 public: \
107 TYPE NAME ARGS; \
108 }; \
109 }
110
111#define __CXXTEST_SUPPLY_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL ) \
112 __CXXTEST_SUPPLY__CLASS_DECLARATION( MOCK, void, NAME, ARGS, REAL, CALL )
113
114//
115// Class implementation: For test source files
116//
117#define __CXXTEST_MOCK__COMMON_CLASS_IMPLEMENTATION( MOCK, NAME ) \
118 namespace CXXTEST_MOCK_NAMESPACE { \
119 \
120 CxxTest::List Base_##MOCK::_list = { 0, 0 }; \
121 \
122 Base_##MOCK::Base_##MOCK() { attach( _list ); } \
123 Base_##MOCK::~Base_##MOCK() { detach( _list ); } \
124 bool Base_##MOCK::setUp() { return true; } \
125 bool Base_##MOCK::tearDown() { return true; } \
126 \
127 Base_##MOCK &Base_##MOCK::current() \
128 { \
129 if ( _list.empty() ) \
130 static _Unimplemented_##MOCK unimplemented; \
131 return *(Base_##MOCK *)_list.tail(); \
132 } \
133 }
134
135#define __CXXTEST_MOCK__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
136 __CXXTEST_MOCK__COMMON_CLASS_IMPLEMENTATION( MOCK, NAME ) \
137 namespace CXXTEST_MOCK_NAMESPACE { \
138 TYPE Real_##MOCK::NAME ARGS \
139 { \
140 return REAL CALL; \
141 } \
142 \
143 TYPE _Unimplemented_##MOCK::NAME ARGS \
144 { \
145 while ( false ) \
146 return NAME CALL; \
147 __CXXTEST_MOCK_UNIMPLEMENTED( NAME, ARGS ); \
148 return MockTraits<TYPE>::defaultValue(); \
149 } \
150 \
151 TYPE NAME ARGS \
152 { \
153 return Base_##MOCK::current().NAME CALL; \
154 } \
155 }
156
157#define __CXXTEST_MOCK_VOID__CLASS_IMPLEMENTATION( MOCK, NAME, ARGS, REAL, CALL ) \
158 __CXXTEST_MOCK__COMMON_CLASS_IMPLEMENTATION( MOCK, NAME ) \
159 namespace CXXTEST_MOCK_NAMESPACE { \
160 void Real_##MOCK::NAME ARGS \
161 { \
162 REAL CALL; \
163 } \
164 \
165 void _Unimplemented_##MOCK::NAME ARGS \
166 { \
167 while ( false ) \
168 NAME CALL; \
169 __CXXTEST_MOCK_UNIMPLEMENTED( NAME, ARGS ); \
170 } \
171 \
172 void NAME ARGS \
173 { \
174 Base_##MOCK::current().NAME CALL; \
175 } \
176 }
177
178#define __CXXTEST_SUPPLY__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
179 __CXXTEST_MOCK__COMMON_CLASS_IMPLEMENTATION( MOCK, NAME ) \
180 namespace CXXTEST_MOCK_NAMESPACE { \
181 TYPE _Unimplemented_##MOCK::NAME ARGS \
182 { \
183 while ( false ) \
184 return NAME CALL; \
185 __CXXTEST_MOCK_UNIMPLEMENTED( NAME, ARGS ); \
186 return MockTraits<TYPE>::defaultValue(); \
187 } \
188 } \
189 \
190 TYPE REAL ARGS \
191 { \
192 return CXXTEST_MOCK_NAMESPACE::Base_##MOCK::current().NAME CALL; \
193 }
194
195#define __CXXTEST_SUPPLY_VOID__CLASS_IMPLEMENTATION( MOCK, NAME, ARGS, REAL, CALL ) \
196 __CXXTEST_MOCK__COMMON_CLASS_IMPLEMENTATION( MOCK, NAME ) \
197 namespace CXXTEST_MOCK_NAMESPACE { \
198 void _Unimplemented_##MOCK::NAME ARGS \
199 { \
200 while ( false ) \
201 NAME CALL; \
202 __CXXTEST_MOCK_UNIMPLEMENTED( NAME, ARGS ); \
203 } \
204 } \
205 \
206 void REAL ARGS \
207 { \
208 CXXTEST_MOCK_NAMESPACE::Base_##MOCK::current().NAME CALL; \
209 } \
210
211//
212// Error for calling mock function w/o object
213//
214#define __CXXTEST_MOCK_UNIMPLEMENTED( NAME, ARGS ) \
215 TS_FAIL( CXXTEST_MOCK_NAMESPACE_STR #NAME #ARGS " called with no " \
216 CXXTEST_MOCK_NAMESPACE_STR "Base_" #NAME " object" ); \
217
218#define CXXTEST_MOCK_NAMESPACE_STR __CXXTEST_STR(CXXTEST_MOCK_NAMESPACE) "::"
219#define __CXXTEST_STR(X) __CXXTEST_XSTR(X)
220#define __CXXTEST_XSTR(X) #X
221
222#if defined(CXXTEST_MOCK_TEST_SOURCE_FILE)
223//
224// Test source file: Prototypes, class declarations and implementation
225//
226#include <cxxtest/TestSuite.h>
227
229
230#define CXXTEST_MOCK( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
231 __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
232 __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
233 __CXXTEST_MOCK__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS, REAL, CALL )
234
235#define CXXTEST_MOCK_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
236 __CXXTEST_MOCK_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
237 __CXXTEST_MOCK_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL ) \
238 __CXXTEST_MOCK_VOID__CLASS_IMPLEMENTATION( MOCK, NAME, ARGS, REAL, CALL )
239
240#define CXXTEST_SUPPLY( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
241 __CXXTEST_SUPPLY__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
242 __CXXTEST_SUPPLY__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
243 __CXXTEST_SUPPLY__CLASS_IMPLEMENTATION( MOCK, TYPE, NAME, ARGS, REAL, CALL )
244
245#define CXXTEST_SUPPLY_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
246 __CXXTEST_SUPPLY_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
247 __CXXTEST_SUPPLY_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL ) \
248 __CXXTEST_SUPPLY_VOID__CLASS_IMPLEMENTATION( MOCK, NAME, ARGS, REAL, CALL )
249
250#elif defined(CXXTEST_FLAGS) || defined(CXXTEST_RUNNING)
251//
252// Test file other than source: Prototypes and class declarations
253//
254#include <cxxtest/TestSuite.h>
255
257
258#define CXXTEST_MOCK( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
259 __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
260 __CXXTEST_MOCK__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL )
261
262#define CXXTEST_MOCK_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
263 __CXXTEST_MOCK_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
264 __CXXTEST_MOCK_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL )
265
266#define CXXTEST_SUPPLY( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
267 __CXXTEST_SUPPLY__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
268 __CXXTEST_SUPPLY__CLASS_DECLARATION( MOCK, TYPE, NAME, ARGS, REAL, CALL )
269
270#define CXXTEST_SUPPLY_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
271 __CXXTEST_SUPPLY_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
272 __CXXTEST_SUPPLY_VOID__CLASS_DECLARATION( MOCK, NAME, ARGS, REAL, CALL )
273
274#elif defined(CXXTEST_MOCK_REAL_SOURCE_FILE)
275//
276// Real source file: "Real" implementations
277//
278#define CXXTEST_MOCK( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
279 namespace CXXTEST_MOCK_NAMESPACE { TYPE NAME ARGS { return REAL CALL; } }
280
281#define CXXTEST_MOCK_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
282 namespace CXXTEST_MOCK_NAMESPACE { void NAME ARGS { REAL CALL; } }
283
284#else
285//
286// Ordinary header file: Just prototypes
287//
288
289#define CXXTEST_MOCK( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
290 __CXXTEST_MOCK__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL )
291
292#define CXXTEST_MOCK_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
293 __CXXTEST_MOCK_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL )
294
295#define CXXTEST_SUPPLY( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
296 __CXXTEST_SUPPLY__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL )
297
298#define CXXTEST_SUPPLY_VOID( MOCK, NAME, ARGS, REAL, CALL ) \
299 __CXXTEST_SUPPLY_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL )
300
301#endif // Ordinary header file
302
303//
304// How to supply extern "C" functions
305//
306#define CXXTEST_SUPPLY_C( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
307 CXXTEST_EXTERN_C __CXXTEST_SUPPLY__PROTOTYPE( MOCK, TYPE, NAME, ARGS, REAL, CALL ) \
308 CXXTEST_SUPPLY( MOCK, TYPE, NAME, ARGS, REAL, CALL )
309
310#define CXXTEST_SUPPLY_VOID_C( MOCK, NAME, ARGS, REAL, CALL ) \
311 CXXTEST_EXTERN_C __CXXTEST_SUPPLY_VOID__PROTOTYPE( MOCK, NAME, ARGS, REAL, CALL ) \
312 CXXTEST_SUPPLY_VOID( MOCK, NAME, ARGS, REAL, CALL )
313
314//
315// Usually we mean the global namespace
316//
317#define CXXTEST_MOCK_GLOBAL( TYPE, NAME, ARGS, CALL ) \
318 CXXTEST_MOCK( NAME, TYPE, NAME, ARGS, ::NAME, CALL )
319
320#define CXXTEST_MOCK_VOID_GLOBAL( NAME, ARGS, CALL ) \
321 CXXTEST_MOCK_VOID( NAME, NAME, ARGS, ::NAME, CALL )
322
323#define CXXTEST_SUPPLY_GLOBAL( TYPE, NAME, ARGS, CALL ) \
324 CXXTEST_SUPPLY( NAME, TYPE, NAME, ARGS, NAME, CALL )
325
326#define CXXTEST_SUPPLY_VOID_GLOBAL( NAME, ARGS, CALL ) \
327 CXXTEST_SUPPLY_VOID( NAME, NAME, ARGS, NAME, CALL )
328
329#define CXXTEST_SUPPLY_GLOBAL_C( TYPE, NAME, ARGS, CALL ) \
330 CXXTEST_SUPPLY_C( NAME, TYPE, NAME, ARGS, NAME, CALL )
331
332#define CXXTEST_SUPPLY_VOID_GLOBAL_C( NAME, ARGS, CALL ) \
333 CXXTEST_SUPPLY_VOID_C( NAME, NAME, ARGS, NAME, CALL )
334
335//
336// What to return when no mock object has been created.
337// The default value of 0 usually works, but some cases may need this.
338//
339#define CXXTEST_MOCK_DEFAULT_VALUE( TYPE, VALUE ) \
340 namespace CXXTEST_MOCK_NAMESPACE \
341 { \
342 template<> \
343 class MockTraits<TYPE> \
344 { \
345 public: \
346 static TYPE defaultValue() { return VALUE; } \
347 }; \
348 }
349
350#endif // __cxxtest__Mock_h__
#define __CXXTEST_MOCK__TRAITS
Definition: Mock.h:14