]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/cppunit.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wrapper header for CppUnit headers
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2004 Vadim Zeitlin
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_CPPUNIT_H_
11 #define _WX_CPPUNIT_H_
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
17 // typedef-name 'foo' used as synonym for class-name 'bar'
18 #pragma warning(disable:4097)
20 // unreachable code: we don't care about warnings in CppUnit headers
21 #pragma warning(disable:4702)
23 // 'id': identifier was truncated to 'num' characters in the debug info
24 #pragma warning(disable:4786)
31 #ifndef CPPUNIT_STD_NEED_ALLOCATOR
32 #define CPPUNIT_STD_NEED_ALLOCATOR 0
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++).
40 #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
41 #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
45 ///////////////////////////////////////////////////////////////////////////////
46 // Include all needed cppunit headers.
49 #include "wx/beforestd.h"
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)
57 // and also 4100 (unreferenced formal parameter) in extensions/
58 // ExceptionTestCaseDecorator.h
59 #pragma warning(disable:4100)
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>
71 #include "wx/afterstd.h"
73 #include "wx/string.h"
76 ///////////////////////////////////////////////////////////////////////////////
77 // Set of helpful test macros.
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) \
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()))
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))
103 // provide an overload of cppunit assertEquals(T, T) which can be used to
104 // compare wxStrings directly with C strings
106 assertEquals(const char *expected
,
108 CppUnit::SourceLine sourceLine
,
109 const std::string
& message
)
111 assertEquals(wxString(expected
), wxString(actual
), sourceLine
, message
);
115 assertEquals(const char *expected
,
116 const wxString
& actual
,
117 CppUnit::SourceLine sourceLine
,
118 const std::string
& message
)
120 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
124 assertEquals(const wxString
& expected
,
126 CppUnit::SourceLine sourceLine
,
127 const std::string
& message
)
129 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
133 assertEquals(const wchar_t *expected
,
134 const wxString
& actual
,
135 CppUnit::SourceLine sourceLine
,
136 const std::string
& message
)
138 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
142 assertEquals(const wxString
& expected
,
143 const wchar_t *actual
,
144 CppUnit::SourceLine sourceLine
,
145 const std::string
& message
)
147 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
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) \
156 assertEquals(T1 expected, \
158 CppUnit::SourceLine sourceLine, \
159 const std::string& message) \
161 if ( !assertion_traits<T1>::equal(expected,actual) ) \
163 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
164 assertion_traits<T2>::toString(actual), \
170 // this macro allows us to specify (usually literal) ints as expected values
171 // for functions returning integral types different from "int"
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
179 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
181 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
182 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
183 WX_CPPUNIT_ASSERT_EQUALS(T, T) \
187 WX_CPPUNIT_ASSERT_EQUALS(int, int)
190 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
192 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
193 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
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)
202 #if defined( __VMS ) && defined( __ia64 )
203 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(std::basic_streambuf
<char>::pos_type
);
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
211 // Use this macro to compare a wxArrayString with the pipe-separated elements
212 // of the given string
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) \
218 wxArrayString expected(wxSplit(s, '|', '\0')); \
220 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
222 for ( size_t n = 0; n < a.size(); n++ ) \
224 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
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))
233 #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
234 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
235 (expected), (actual))
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
242 #include "wx/string.h"
246 inline std::ostream
& operator<<(std::ostream
& o
, const wxString
& s
)
249 return o
<< (const char *)wxSafeConvertWX2MB(s
.wc_str());
251 return o
<< s
.c_str();
255 #endif // !wxUSE_STD_IOSTREAM
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)
261 #include "wx/longlong.h"
263 inline std::ostream
& operator<<(std::ostream
& ostr
, wxLongLong_t ll
)
265 ostr
<< wxLongLong(ll
).ToString();
270 inline std::ostream
& operator<<(std::ostream
& ostr
, unsigned wxLongLong_t llu
)
272 ostr
<< wxULongLong(llu
).ToString();
277 #endif // VC6 && wxLongLong_t
279 ///////////////////////////////////////////////////////////////////////////////
280 // Some more compiler warning tweaking and auto linking.
288 #pragma warning(default:4702)
291 // for VC++ automatically link in cppunit library
294 #pragma comment(lib, "cppunit.lib")
296 #pragma comment(lib, "cppunitd.lib")
297 #endif // Release/Debug
300 #endif // _WX_CPPUNIT_H_