]>
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"
51 #include <cppunit/extensions/TestFactoryRegistry.h>
52 #include <cppunit/ui/text/TestRunner.h>
53 #include <cppunit/TestCase.h>
54 #include <cppunit/extensions/HelperMacros.h>
55 #include <cppunit/CompilerOutputter.h>
56 #include "wx/afterstd.h"
58 #include "wx/string.h"
61 ///////////////////////////////////////////////////////////////////////////////
62 // Set of helpful test macros.
65 // Base macro for wrapping CPPUNIT_TEST macros and so making them conditional
66 // tests, meaning that the test only get registered and thus run when a given
67 // runtime condition is true.
68 // In case the condition is evaluated as false a skip message is logged
69 // (the message will only be shown in verbose mode).
70 #define WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, anyTest) \
74 wxLogInfo(wxString::Format(_T("skipping: %s.%s\n reason: %s equals false\n"), \
75 wxString(suiteName, wxConvUTF8).c_str(), \
76 wxString(#testMethod, wxConvUTF8).c_str(), \
77 wxString(#Condition, wxConvUTF8).c_str()))
79 // Conditional CPPUNIT_TEST macro.
80 #define WXTEST_WITH_CONDITION(suiteName, Condition, testMethod) \
81 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST(testMethod))
82 // Conditional CPPUNIT_TEST_FAIL macro.
83 #define WXTEST_FAIL_WITH_CONDITION(suiteName, Condition, testMethod) \
84 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST_FAIL(testMethod))
88 // provide an overload of cppunit assertEquals(T, T) which can be used to
89 // compare wxStrings directly with C strings
91 assertEquals(const char *expected
,
93 CppUnit::SourceLine sourceLine
,
94 const std::string
& message
)
96 assertEquals(wxString(expected
), wxString(actual
), sourceLine
, message
);
100 assertEquals(const char *expected
,
101 const wxString
& actual
,
102 CppUnit::SourceLine sourceLine
,
103 const std::string
& message
)
105 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
109 assertEquals(const wxString
& expected
,
111 CppUnit::SourceLine sourceLine
,
112 const std::string
& message
)
114 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
118 assertEquals(const wchar_t *expected
,
119 const wxString
& actual
,
120 CppUnit::SourceLine sourceLine
,
121 const std::string
& message
)
123 assertEquals(wxString(expected
), actual
, sourceLine
, message
);
127 assertEquals(const wxString
& expected
,
128 const wchar_t *actual
,
129 CppUnit::SourceLine sourceLine
,
130 const std::string
& message
)
132 assertEquals(expected
, wxString(actual
), sourceLine
, message
);
137 // define an assertEquals() overload for the given types, this is a helper and
138 // shouldn't be used directly because of VC6 complications, see below
139 #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
141 assertEquals(T1 expected, \
143 CppUnit::SourceLine sourceLine, \
144 const std::string& message) \
146 if ( !assertion_traits<T1>::equal(expected,actual) ) \
148 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
149 assertion_traits<T2>::toString(actual), \
155 // this macro allows us to specify (usually literal) ints as expected values
156 // for functions returning integral types different from "int"
158 // FIXME-VC6: due to incorrect resolution of overloaded/template functions in
159 // this compiler (it basically doesn't use the template version at
160 // all if any overloaded function matches partially even if none of
161 // them matches fully) we also need to provide extra overloads
164 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
166 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
167 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
168 WX_CPPUNIT_ASSERT_EQUALS(T, T) \
172 WX_CPPUNIT_ASSERT_EQUALS(int, int)
175 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
177 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
178 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
182 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long)
183 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short)
184 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned)
185 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long)
187 #if defined(wxLongLong_t) && !defined(wxLongLongIsLong)
188 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t
)
189 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t
)
192 // Use this macro to compare a wxArrayString with the pipe-separated elements
193 // of the given string
195 // NB: it's a macro and not a function to have the correct line numbers in the
196 // test failure messages
197 #define WX_ASSERT_STRARRAY_EQUAL(s, a) \
199 wxArrayString expected(wxSplit(s, '|', '\0')); \
201 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
203 for ( size_t n = 0; n < a.size(); n++ ) \
205 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
209 // Use this macro to assert with the given formatted message (it should contain
210 // the format string and arguments in a separate pair of parentheses)
211 #define WX_ASSERT_MESSAGE(msg, cond) \
212 CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond))
214 #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
215 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
216 (expected), (actual))
218 ///////////////////////////////////////////////////////////////////////////////
219 // define stream inserter for wxString if it's not defined in the main library,
220 // we need it to output the test failures involving wxString
221 #if !wxUSE_STD_IOSTREAM
223 #include "wx/string.h"
227 inline std::ostream
& operator<<(std::ostream
& o
, const wxString
& s
)
230 return o
<< (const char *)wxSafeConvertWX2MB(s
.wc_str());
232 return o
<< s
.c_str();
236 #endif // !wxUSE_STD_IOSTREAM
238 // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes
239 // so do it ourselves
240 #if defined(__VISUALC6__) && defined(wxLongLong_t)
242 #include "wx/longlong.h"
244 inline std::ostream
& operator<<(std::ostream
& ostr
, wxLongLong_t ll
)
246 ostr
<< wxLongLong(ll
).ToString();
251 inline std::ostream
& operator<<(std::ostream
& ostr
, unsigned wxLongLong_t llu
)
253 ostr
<< wxULongLong(llu
).ToString();
258 #endif // VC6 && wxLongLong_t
260 ///////////////////////////////////////////////////////////////////////////////
261 // Some more compiler warning tweaking and auto linking.
269 #pragma warning(default:4702)
272 // for VC++ automatically link in cppunit library
275 #pragma comment(lib, "cppunit.lib")
277 #pragma comment(lib, "cppunitd.lib")
278 #endif // Release/Debug
281 #endif // _WX_CPPUNIT_H_