]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cppunit.h
Forgot to commit the header change too.
[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 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin
8 // Licence: wxWindows Licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_CPPUNIT_H_
12 #define _WX_CPPUNIT_H_
13
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
17 #ifdef __VISUALC__
18 // typedef-name 'foo' used as synonym for class-name 'bar'
19 #pragma warning(disable:4097)
20
21 // unreachable code: we don't care about warnings in CppUnit headers
22 #pragma warning(disable:4702)
23
24 // 'id': identifier was truncated to 'num' characters in the debug info
25 #pragma warning(disable:4786)
26 #endif // __VISUALC__
27
28 #ifdef __BORLANDC__
29 #pragma warn -8022
30 #endif
31
32 #ifndef CPPUNIT_STD_NEED_ALLOCATOR
33 #define CPPUNIT_STD_NEED_ALLOCATOR 0
34 #endif
35
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++).
40
41 #ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
42 #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
43 #endif
44
45
46 ///////////////////////////////////////////////////////////////////////////////
47 // Include all needed cppunit headers.
48 //
49
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"
57
58 #include "wx/string.h"
59
60
61 ///////////////////////////////////////////////////////////////////////////////
62 // Set of helpful test macros.
63 //
64
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) \
71 if (Condition) \
72 { anyTest; } \
73 else \
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()))
78
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))
85
86 CPPUNIT_NS_BEGIN
87
88 // provide an overload of cppunit assertEquals(T, T) which can be used to
89 // compare wxStrings directly with C strings
90 inline void
91 assertEquals(const char *expected,
92 const wxString& actual,
93 CppUnit::SourceLine sourceLine,
94 const std::string& message)
95 {
96 assertEquals(wxString(expected), actual, sourceLine, message);
97 }
98
99 inline void
100 assertEquals(const wchar_t *expected,
101 const wxString& actual,
102 CppUnit::SourceLine sourceLine,
103 const std::string& message)
104 {
105 assertEquals(wxString(expected), actual, sourceLine, message);
106 }
107
108 CPPUNIT_NS_END
109
110 // define an assertEquals() overload for the given types, this is a helper and
111 // shouldn't be used directly because of VC6 complications, see below
112 #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
113 inline void \
114 assertEquals(T1 expected, \
115 T2 actual, \
116 CppUnit::SourceLine sourceLine, \
117 const std::string& message) \
118 { \
119 if ( !assertion_traits<T1>::equal(expected,actual) ) \
120 { \
121 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
122 assertion_traits<T2>::toString(actual), \
123 sourceLine, \
124 message ); \
125 } \
126 }
127
128 // this macro allows us to specify (usually literal) ints as expected values
129 // for functions returning integral types different from "int"
130 //
131 // FIXME-VC6: due to incorrect resolution of overloaded/template functions in
132 // this compiler (it basically doesn't use the template version at
133 // all if any overloaded function matches partially even if none of
134 // them matches fully) we also need to provide extra overloads
135
136 #ifdef __VISUALC6__
137 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
138 CPPUNIT_NS_BEGIN \
139 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
140 WX_CPPUNIT_ASSERT_EQUALS(T, T) \
141 CPPUNIT_NS_END
142
143 CPPUNIT_NS_BEGIN
144 WX_CPPUNIT_ASSERT_EQUALS(int, int)
145 CPPUNIT_NS_END
146 #else // !VC6
147 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
148 CPPUNIT_NS_BEGIN \
149 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
150 CPPUNIT_NS_END
151 #endif // VC6/!VC6
152
153 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long)
154 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short)
155 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(size_t)
156
157 // Use this macro to compare a wxArrayString with the pipe-separated elements
158 // of the given string
159 //
160 // NB: it's a macro and not a function to have the correct line numbers in the
161 // test failure messages
162 #define WX_ASSERT_STRARRAY_EQUAL(s, a) \
163 { \
164 wxArrayString expected(wxSplit(s, '|', '\0')); \
165 \
166 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
167 \
168 for ( size_t n = 0; n < a.size(); n++ ) \
169 { \
170 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
171 } \
172 }
173
174 // Use this macro to assert with the given formatted message (it should contain
175 // the format string and arguments in a separate pair of parentheses)
176 #define WX_ASSERT_MESSAGE(msg, cond) \
177 CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond))
178
179 #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
180 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
181 (expected), (actual))
182
183 ///////////////////////////////////////////////////////////////////////////////
184 // define stream inserter for wxString if it's not defined in the main library,
185 // we need it to output the test failures involving wxString
186 #if !wxUSE_STD_IOSTREAM
187
188 #include "wx/string.h"
189
190 #include <iostream>
191
192 inline std::ostream& operator<<(std::ostream& o, const wxString& s)
193 {
194 #if wxUSE_UNICODE
195 return o << (const char *)wxSafeConvertWX2MB(s.wc_str());
196 #else
197 return o << s.c_str();
198 #endif
199 }
200
201 #endif // !wxUSE_STD_IOSTREAM
202
203 // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes
204 // so do it ourselves
205 #if defined(__VISUALC6__) && defined(wxLongLong_t)
206
207 #include "wx/longlong.h"
208
209 inline std::ostream& operator<<(std::ostream& ostr, wxLongLong_t ll)
210 {
211 ostr << wxLongLong(ll).ToString();
212
213 return ostr;
214 }
215
216 inline std::ostream& operator<<(std::ostream& ostr, unsigned wxLongLong_t llu)
217 {
218 ostr << wxULongLong(llu).ToString();
219
220 return ostr;
221 }
222
223 #endif // VC6 && wxLongLong_t
224
225 ///////////////////////////////////////////////////////////////////////////////
226 // Some more compiler warning tweaking and auto linking.
227 //
228
229 #ifdef __BORLANDC__
230 #pragma warn .8022
231 #endif
232
233 #ifdef _MSC_VER
234 #pragma warning(default:4702)
235 #endif // _MSC_VER
236
237 // for VC++ automatically link in cppunit library
238 #ifdef __VISUALC__
239 #ifdef NDEBUG
240 #pragma comment(lib, "cppunit.lib")
241 #else // Debug
242 #pragma comment(lib, "cppunitd.lib")
243 #endif // Release/Debug
244 #endif
245
246 #endif // _WX_CPPUNIT_H_
247