]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cppunit.h
proper default for iphone
[wxWidgets.git] / include / wx / cppunit.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cppunit.h
3 // Purpose: wrapper header for CppUnit headers
4 // Author: Vadim Zeitlin
5 // Created: 15.02.04
6 // Copyright: (c) 2004 Vadim Zeitlin
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_CPPUNIT_H_
11 #define _WX_CPPUNIT_H_
12
13 ///////////////////////////////////////////////////////////////////////////////
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
27 #ifdef __BORLANDC__
28 #pragma warn -8022
29 #endif
30
31 #ifndef CPPUNIT_STD_NEED_ALLOCATOR
32 #define CPPUNIT_STD_NEED_ALLOCATOR 0
33 #endif
34
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
40 #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
41 #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
42 #endif
43
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // Include all needed cppunit headers.
47 //
48
49 #include "wx/beforestd.h"
50 #ifdef __VISUALC__
51 #pragma warning(push)
52
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)
56
57 // and also 4100 (unreferenced formal parameter) in extensions/
58 // ExceptionTestCaseDecorator.h
59 #pragma warning(disable:4100)
60 #endif
61
62 #include <cppunit/extensions/TestFactoryRegistry.h>
63 #include <cppunit/ui/text/TestRunner.h>
64 #include <cppunit/TestCase.h>
65 #include <cppunit/extensions/HelperMacros.h>
66 #include <cppunit/CompilerOutputter.h>
67
68 #ifdef __VISUALC__
69 #pragma warning(pop)
70 #endif
71 #include "wx/afterstd.h"
72
73 #include "wx/string.h"
74
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
82 // runtime condition is true.
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 \
89 wxLogInfo(wxString::Format(wxT("skipping: %s.%s\n reason: %s equals false\n"), \
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
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
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
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
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
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
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
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
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
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
177
178 #ifdef __VISUALC6__
179 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
180 CPPUNIT_NS_BEGIN \
181 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
182 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
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) \
193 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
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)
199 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned)
200 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long)
201
202 #if defined( __VMS ) && defined( __ia64 )
203 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(std::basic_streambuf<char>::pos_type);
204 #endif
205
206 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
207 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t)
208 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t)
209 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
210
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 }
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
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
237 ///////////////////////////////////////////////////////////////////////////////
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
241
242 #include "wx/string.h"
243
244 #include <iostream>
245
246 inline std::ostream& operator<<(std::ostream& o, const wxString& s)
247 {
248 #if wxUSE_UNICODE
249 return o << (const char *)wxSafeConvertWX2MB(s.wc_str());
250 #else
251 return o << s.c_str();
252 #endif
253 }
254
255 #endif // !wxUSE_STD_IOSTREAM
256
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)
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
277 #endif // VC6 && wxLongLong_t
278
279 ///////////////////////////////////////////////////////////////////////////////
280 // Some more compiler warning tweaking and auto linking.
281 //
282
283 #ifdef __BORLANDC__
284 #pragma warn .8022
285 #endif
286
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