]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/cppunit.h | |
3 | // Purpose: wrapper header for CppUnit headers | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 15.02.04 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin | |
8 | // Licence: wxWindows Licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_CPPUNIT_H_ | |
12 | #define _WX_CPPUNIT_H_ | |
13 | ||
14 | /////////////////////////////////////////////////////////////////////////////// | |
15 | // using CPPUNIT_TEST() macro results in this warning, disable it as there is | |
16 | // no other way to get rid of it and it's not very useful anyhow | |
17 | #ifdef __VISUALC__ | |
18 | // typedef-name 'foo' used as synonym for class-name 'bar' | |
19 | #pragma warning(disable:4097) | |
20 | ||
21 | // unreachable code: we don't care about warnings in CppUnit headers | |
22 | #pragma warning(disable:4702) | |
23 | ||
24 | // 'id': identifier was truncated to 'num' characters in the debug info | |
25 | #pragma warning(disable:4786) | |
26 | #endif // __VISUALC__ | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma warn -8022 | |
30 | #endif | |
31 | ||
32 | #ifndef CPPUNIT_STD_NEED_ALLOCATOR | |
33 | #define CPPUNIT_STD_NEED_ALLOCATOR 0 | |
34 | #endif | |
35 | ||
36 | /////////////////////////////////////////////////////////////////////////////// | |
37 | // Set the default format for the errors, which can be used by an IDE to jump | |
38 | // to the error location. This default gets overridden by the cppunit headers | |
39 | // for some compilers (e.g. VC++). | |
40 | ||
41 | #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT | |
42 | #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:" | |
43 | #endif | |
44 | ||
45 | ||
46 | /////////////////////////////////////////////////////////////////////////////// | |
47 | // Include all needed cppunit headers. | |
48 | // | |
49 | ||
50 | #include "wx/beforestd.h" | |
51 | #ifdef __VISUALC__ | |
52 | // with cppunit 1.12 we get many bogus warnings 4701 (local variable may be | |
53 | // used without having been initialized) in TestAssert.h | |
54 | #pragma warning(disable:4701) | |
55 | #endif | |
56 | ||
57 | #include <cppunit/extensions/TestFactoryRegistry.h> | |
58 | #include <cppunit/ui/text/TestRunner.h> | |
59 | #include <cppunit/TestCase.h> | |
60 | #include <cppunit/extensions/HelperMacros.h> | |
61 | #include <cppunit/CompilerOutputter.h> | |
62 | ||
63 | #ifdef __VISUALC__ | |
64 | #pragma warning(default:4701) | |
65 | #endif | |
66 | #include "wx/afterstd.h" | |
67 | ||
68 | #include "wx/string.h" | |
69 | ||
70 | ||
71 | /////////////////////////////////////////////////////////////////////////////// | |
72 | // Set of helpful test macros. | |
73 | // | |
74 | ||
75 | // Base macro for wrapping CPPUNIT_TEST macros and so making them conditional | |
76 | // tests, meaning that the test only get registered and thus run when a given | |
77 | // runtime condition is true. | |
78 | // In case the condition is evaluated as false a skip message is logged | |
79 | // (the message will only be shown in verbose mode). | |
80 | #define WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, anyTest) \ | |
81 | if (Condition) \ | |
82 | { anyTest; } \ | |
83 | else \ | |
84 | wxLogInfo(wxString::Format(wxT("skipping: %s.%s\n reason: %s equals false\n"), \ | |
85 | wxString(suiteName, wxConvUTF8).c_str(), \ | |
86 | wxString(#testMethod, wxConvUTF8).c_str(), \ | |
87 | wxString(#Condition, wxConvUTF8).c_str())) | |
88 | ||
89 | // Conditional CPPUNIT_TEST macro. | |
90 | #define WXTEST_WITH_CONDITION(suiteName, Condition, testMethod) \ | |
91 | WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST(testMethod)) | |
92 | // Conditional CPPUNIT_TEST_FAIL macro. | |
93 | #define WXTEST_FAIL_WITH_CONDITION(suiteName, Condition, testMethod) \ | |
94 | WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST_FAIL(testMethod)) | |
95 | ||
96 | CPPUNIT_NS_BEGIN | |
97 | ||
98 | // provide an overload of cppunit assertEquals(T, T) which can be used to | |
99 | // compare wxStrings directly with C strings | |
100 | inline void | |
101 | assertEquals(const char *expected, | |
102 | const char *actual, | |
103 | CppUnit::SourceLine sourceLine, | |
104 | const std::string& message) | |
105 | { | |
106 | assertEquals(wxString(expected), wxString(actual), sourceLine, message); | |
107 | } | |
108 | ||
109 | inline void | |
110 | assertEquals(const char *expected, | |
111 | const wxString& actual, | |
112 | CppUnit::SourceLine sourceLine, | |
113 | const std::string& message) | |
114 | { | |
115 | assertEquals(wxString(expected), actual, sourceLine, message); | |
116 | } | |
117 | ||
118 | inline void | |
119 | assertEquals(const wxString& expected, | |
120 | const char *actual, | |
121 | CppUnit::SourceLine sourceLine, | |
122 | const std::string& message) | |
123 | { | |
124 | assertEquals(expected, wxString(actual), sourceLine, message); | |
125 | } | |
126 | ||
127 | inline void | |
128 | assertEquals(const wchar_t *expected, | |
129 | const wxString& actual, | |
130 | CppUnit::SourceLine sourceLine, | |
131 | const std::string& message) | |
132 | { | |
133 | assertEquals(wxString(expected), actual, sourceLine, message); | |
134 | } | |
135 | ||
136 | inline void | |
137 | assertEquals(const wxString& expected, | |
138 | const wchar_t *actual, | |
139 | CppUnit::SourceLine sourceLine, | |
140 | const std::string& message) | |
141 | { | |
142 | assertEquals(expected, wxString(actual), sourceLine, message); | |
143 | } | |
144 | ||
145 | CPPUNIT_NS_END | |
146 | ||
147 | // define an assertEquals() overload for the given types, this is a helper and | |
148 | // shouldn't be used directly because of VC6 complications, see below | |
149 | #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \ | |
150 | inline void \ | |
151 | assertEquals(T1 expected, \ | |
152 | T2 actual, \ | |
153 | CppUnit::SourceLine sourceLine, \ | |
154 | const std::string& message) \ | |
155 | { \ | |
156 | if ( !assertion_traits<T1>::equal(expected,actual) ) \ | |
157 | { \ | |
158 | Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \ | |
159 | assertion_traits<T2>::toString(actual), \ | |
160 | sourceLine, \ | |
161 | message ); \ | |
162 | } \ | |
163 | } | |
164 | ||
165 | // this macro allows us to specify (usually literal) ints as expected values | |
166 | // for functions returning integral types different from "int" | |
167 | // | |
168 | // FIXME-VC6: due to incorrect resolution of overloaded/template functions in | |
169 | // this compiler (it basically doesn't use the template version at | |
170 | // all if any overloaded function matches partially even if none of | |
171 | // them matches fully) we also need to provide extra overloads | |
172 | ||
173 | #ifdef __VISUALC6__ | |
174 | #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \ | |
175 | CPPUNIT_NS_BEGIN \ | |
176 | WX_CPPUNIT_ASSERT_EQUALS(int, T) \ | |
177 | WX_CPPUNIT_ASSERT_EQUALS(T, int) \ | |
178 | WX_CPPUNIT_ASSERT_EQUALS(T, T) \ | |
179 | CPPUNIT_NS_END | |
180 | ||
181 | CPPUNIT_NS_BEGIN | |
182 | WX_CPPUNIT_ASSERT_EQUALS(int, int) | |
183 | CPPUNIT_NS_END | |
184 | #else // !VC6 | |
185 | #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \ | |
186 | CPPUNIT_NS_BEGIN \ | |
187 | WX_CPPUNIT_ASSERT_EQUALS(int, T) \ | |
188 | WX_CPPUNIT_ASSERT_EQUALS(T, int) \ | |
189 | CPPUNIT_NS_END | |
190 | #endif // VC6/!VC6 | |
191 | ||
192 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long) | |
193 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short) | |
194 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned) | |
195 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long) | |
196 | ||
197 | #if defined(wxLongLong_t) && !defined(wxLongLongIsLong) | |
198 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t) | |
199 | WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t) | |
200 | #endif | |
201 | ||
202 | // Use this macro to compare a wxArrayString with the pipe-separated elements | |
203 | // of the given string | |
204 | // | |
205 | // NB: it's a macro and not a function to have the correct line numbers in the | |
206 | // test failure messages | |
207 | #define WX_ASSERT_STRARRAY_EQUAL(s, a) \ | |
208 | { \ | |
209 | wxArrayString expected(wxSplit(s, '|', '\0')); \ | |
210 | \ | |
211 | CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \ | |
212 | \ | |
213 | for ( size_t n = 0; n < a.size(); n++ ) \ | |
214 | { \ | |
215 | CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \ | |
216 | } \ | |
217 | } | |
218 | ||
219 | // Use this macro to assert with the given formatted message (it should contain | |
220 | // the format string and arguments in a separate pair of parentheses) | |
221 | #define WX_ASSERT_MESSAGE(msg, cond) \ | |
222 | CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond)) | |
223 | ||
224 | #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \ | |
225 | CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \ | |
226 | (expected), (actual)) | |
227 | ||
228 | /////////////////////////////////////////////////////////////////////////////// | |
229 | // define stream inserter for wxString if it's not defined in the main library, | |
230 | // we need it to output the test failures involving wxString | |
231 | #if !wxUSE_STD_IOSTREAM | |
232 | ||
233 | #include "wx/string.h" | |
234 | ||
235 | #include <iostream> | |
236 | ||
237 | inline std::ostream& operator<<(std::ostream& o, const wxString& s) | |
238 | { | |
239 | #if wxUSE_UNICODE | |
240 | return o << (const char *)wxSafeConvertWX2MB(s.wc_str()); | |
241 | #else | |
242 | return o << s.c_str(); | |
243 | #endif | |
244 | } | |
245 | ||
246 | #endif // !wxUSE_STD_IOSTREAM | |
247 | ||
248 | // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes | |
249 | // so do it ourselves | |
250 | #if defined(__VISUALC6__) && defined(wxLongLong_t) | |
251 | ||
252 | #include "wx/longlong.h" | |
253 | ||
254 | inline std::ostream& operator<<(std::ostream& ostr, wxLongLong_t ll) | |
255 | { | |
256 | ostr << wxLongLong(ll).ToString(); | |
257 | ||
258 | return ostr; | |
259 | } | |
260 | ||
261 | inline std::ostream& operator<<(std::ostream& ostr, unsigned wxLongLong_t llu) | |
262 | { | |
263 | ostr << wxULongLong(llu).ToString(); | |
264 | ||
265 | return ostr; | |
266 | } | |
267 | ||
268 | #endif // VC6 && wxLongLong_t | |
269 | ||
270 | /////////////////////////////////////////////////////////////////////////////// | |
271 | // Some more compiler warning tweaking and auto linking. | |
272 | // | |
273 | ||
274 | #ifdef __BORLANDC__ | |
275 | #pragma warn .8022 | |
276 | #endif | |
277 | ||
278 | #ifdef _MSC_VER | |
279 | #pragma warning(default:4702) | |
280 | #endif // _MSC_VER | |
281 | ||
282 | // for VC++ automatically link in cppunit library | |
283 | #ifdef __VISUALC__ | |
284 | #ifdef NDEBUG | |
285 | #pragma comment(lib, "cppunit.lib") | |
286 | #else // Debug | |
287 | #pragma comment(lib, "cppunitd.lib") | |
288 | #endif // Release/Debug | |
289 | #endif | |
290 | ||
291 | #endif // _WX_CPPUNIT_H_ | |
292 |