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