]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/cppunit.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wrapper header for CppUnit headers
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 Vadim Zeitlin
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_CPPUNIT_H_
12 #define _WX_CPPUNIT_H_
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
18 // typedef-name 'foo' used as synonym for class-name 'bar'
19 #pragma warning(disable:4097)
21 // unreachable code: we don't care about warnings in CppUnit headers
22 #pragma warning(disable:4702)
24 // 'id': identifier was truncated to 'num' characters in the debug info
25 #pragma warning(disable:4786)
32 #ifndef CPPUNIT_STD_NEED_ALLOCATOR
33 #define CPPUNIT_STD_NEED_ALLOCATOR 0
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++).
41 #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
42 #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
46 ///////////////////////////////////////////////////////////////////////////////
47 // Include all needed cppunit headers.
50 #include "wx/beforestd.h"
54 // with cppunit 1.12 we get many bogus warnings 4701 (local variable may be
55 // used without having been initialized) in TestAssert.h
56 #pragma warning(disable:4701)
58 // and also 4100 (unreferenced formal parameter) in extensions/
59 // ExceptionTestCaseDecorator.h
60 #pragma warning(disable:4100)
63 #include <cppunit/extensions/TestFactoryRegistry.h>
64 #include <cppunit/ui/text/TestRunner.h>
65 #include <cppunit/TestCase.h>
66 #include <cppunit/extensions/HelperMacros.h>
67 #include <cppunit/CompilerOutputter.h>
72 #include "wx/afterstd.h"
74 #include "wx/string.h"
77 ///////////////////////////////////////////////////////////////////////////////
78 // Set of helpful test macros.
81 // Base macro for wrapping CPPUNIT_TEST macros and so making them conditional
82 // tests, meaning that the test only get registered and thus run when a given
83 // runtime condition is true.
84 // In case the condition is evaluated as false a skip message is logged
85 // (the message will only be shown in verbose mode).
86 #define WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, anyTest) \
90 wxLogInfo(wxString::Format(wxT("skipping: %s.%s\n reason: %s equals false\n"), \
91 wxString(suiteName, wxConvUTF8).c_str(), \
92 wxString(#testMethod, wxConvUTF8).c_str(), \
93 wxString(#Condition, wxConvUTF8).c_str()))
95 // Conditional CPPUNIT_TEST macro.
96 #define WXTEST_WITH_CONDITION(suiteName, Condition, testMethod) \
97 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST(testMethod))
98 // Conditional CPPUNIT_TEST_FAIL macro.
99 #define WXTEST_FAIL_WITH_CONDITION(suiteName, Condition, testMethod) \
100 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST_FAIL(testMethod))
104 // provide an overload of cppunit assertEquals(T, T) which can be used to
105 // compare wxStrings directly with C strings
107 assertEquals(const char *expected
,
109 CppUnit::SourceLine sourceLine
,
110 const std::string
& message
)
112 assertEquals(wxString(expected
), wxString(actual
), sourceLine
, message
);
116 assertEquals(const char *expected
,
117 const wxString
& actual
,
118 CppUnit::SourceLine sourceLine
,
119 const std::string
& message
)
121 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
125 assertEquals(const wxString
& expected
,
127 CppUnit::SourceLine sourceLine
,
128 const std::string
& message
)
130 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
134 assertEquals(const wchar_t *expected
,
135 const wxString
& actual
,
136 CppUnit::SourceLine sourceLine
,
137 const std::string
& message
)
139 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
143 assertEquals(const wxString
& expected
,
144 const wchar_t *actual
,
145 CppUnit::SourceLine sourceLine
,
146 const std::string
& message
)
148 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
153 // define an assertEquals() overload for the given types, this is a helper and
154 // shouldn't be used directly because of VC6 complications, see below
155 #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
157 assertEquals(T1 expected, \
159 CppUnit::SourceLine sourceLine, \
160 const std::string& message) \
162 if ( !assertion_traits<T1>::equal(expected,actual) ) \
164 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
165 assertion_traits<T2>::toString(actual), \
171 // this macro allows us to specify (usually literal) ints as expected values
172 // for functions returning integral types different from "int"
174 // FIXME-VC6: due to incorrect resolution of overloaded/template functions in
175 // this compiler (it basically doesn't use the template version at
176 // all if any overloaded function matches partially even if none of
177 // them matches fully) we also need to provide extra overloads
180 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
182 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
183 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
184 WX_CPPUNIT_ASSERT_EQUALS(T, T) \
188 WX_CPPUNIT_ASSERT_EQUALS(int, int)
191 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
193 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
194 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
198 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long)
199 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short)
200 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned)
201 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long)
203 #if defined( __VMS ) && defined( __ia64 )
204 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(std::basic_streambuf
<char>::pos_type
);
207 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
208 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t
)
209 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t
)
210 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
212 // Use this macro to compare a wxArrayString with the pipe-separated elements
213 // of the given string
215 // NB: it's a macro and not a function to have the correct line numbers in the
216 // test failure messages
217 #define WX_ASSERT_STRARRAY_EQUAL(s, a) \
219 wxArrayString expected(wxSplit(s, '|', '\0')); \
221 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
223 for ( size_t n = 0; n < a.size(); n++ ) \
225 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
229 // Use this macro to assert with the given formatted message (it should contain
230 // the format string and arguments in a separate pair of parentheses)
231 #define WX_ASSERT_MESSAGE(msg, cond) \
232 CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond))
234 #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
235 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
236 (expected), (actual))
238 ///////////////////////////////////////////////////////////////////////////////
239 // define stream inserter for wxString if it's not defined in the main library,
240 // we need it to output the test failures involving wxString
241 #if !wxUSE_STD_IOSTREAM
243 #include "wx/string.h"
247 inline std::ostream
& operator<<(std::ostream
& o
, const wxString
& s
)
250 return o
<< (const char *)wxSafeConvertWX2MB(s
.wc_str());
252 return o
<< s
.c_str();
256 #endif // !wxUSE_STD_IOSTREAM
258 // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes
259 // so do it ourselves
260 #if defined(__VISUALC6__) && defined(wxLongLong_t)
262 #include "wx/longlong.h"
264 inline std::ostream
& operator<<(std::ostream
& ostr
, wxLongLong_t ll
)
266 ostr
<< wxLongLong(ll
).ToString();
271 inline std::ostream
& operator<<(std::ostream
& ostr
, unsigned wxLongLong_t llu
)
273 ostr
<< wxULongLong(llu
).ToString();
278 #endif // VC6 && wxLongLong_t
280 ///////////////////////////////////////////////////////////////////////////////
281 // Some more compiler warning tweaking and auto linking.
289 #pragma warning(default:4702)
292 // for VC++ automatically link in cppunit library
295 #pragma comment(lib, "cppunit.lib")
297 #pragma comment(lib, "cppunitd.lib")
298 #endif // Release/Debug
301 #endif // _WX_CPPUNIT_H_