]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cppunit.h
Define WXBUILDING in Xcode projects.
[wxWidgets.git] / include / wx / cppunit.h
CommitLineData
86132a69
VZ
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
99d80019 8// Licence: wxWindows Licence
86132a69
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_CPPUNIT_H_
12#define _WX_CPPUNIT_H_
13
044a69a4 14///////////////////////////////////////////////////////////////////////////////
86132a69
VZ
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
20f46e8d
VS
28#ifdef __BORLANDC__
29 #pragma warn -8022
30#endif
d2001a56 31
53e372c2
MW
32#ifndef CPPUNIT_STD_NEED_ALLOCATOR
33 #define CPPUNIT_STD_NEED_ALLOCATOR 0
34#endif
35
14dc53b2
MW
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
d2001a56 41#ifndef CPPUNIT_COMPILER_LOCATION_FORMAT
14dc53b2
MW
42 #define CPPUNIT_COMPILER_LOCATION_FORMAT "%p:%l:"
43#endif
20f46e8d 44
044a69a4
VS
45
46///////////////////////////////////////////////////////////////////////////////
47// Include all needed cppunit headers.
48//
49
86132a69 50#include "wx/beforestd.h"
895cae46 51#ifdef __VISUALC__
17e48bce
VZ
52 #pragma warning(push)
53
895cae46
VZ
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)
17e48bce
VZ
57
58 // and also 4100 (unreferenced formal parameter) in extensions/
59 // ExceptionTestCaseDecorator.h
60 #pragma warning(disable:4100)
895cae46
VZ
61#endif
62
86132a69
VZ
63#include <cppunit/extensions/TestFactoryRegistry.h>
64#include <cppunit/ui/text/TestRunner.h>
65#include <cppunit/TestCase.h>
66#include <cppunit/extensions/HelperMacros.h>
14dc53b2 67#include <cppunit/CompilerOutputter.h>
895cae46
VZ
68
69#ifdef __VISUALC__
17e48bce 70 #pragma warning(pop)
895cae46 71#endif
86132a69
VZ
72#include "wx/afterstd.h"
73
3d9b0b53
VZ
74#include "wx/string.h"
75
044a69a4
VS
76
77///////////////////////////////////////////////////////////////////////////////
78// Set of helpful test macros.
79//
80
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
68379eaf 83// runtime condition is true.
044a69a4
VS
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) \
87 if (Condition) \
88 { anyTest; } \
89 else \
9a83f860 90 wxLogInfo(wxString::Format(wxT("skipping: %s.%s\n reason: %s equals false\n"), \
044a69a4
VS
91 wxString(suiteName, wxConvUTF8).c_str(), \
92 wxString(#testMethod, wxConvUTF8).c_str(), \
93 wxString(#Condition, wxConvUTF8).c_str()))
94
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))
101
1de532f5
VZ
102CPPUNIT_NS_BEGIN
103
104// provide an overload of cppunit assertEquals(T, T) which can be used to
105// compare wxStrings directly with C strings
390b8241
VZ
106inline void
107assertEquals(const char *expected,
108 const char *actual,
109 CppUnit::SourceLine sourceLine,
110 const std::string& message)
111{
112 assertEquals(wxString(expected), wxString(actual), sourceLine, message);
113}
114
1de532f5
VZ
115inline void
116assertEquals(const char *expected,
117 const wxString& actual,
118 CppUnit::SourceLine sourceLine,
119 const std::string& message)
120{
121 assertEquals(wxString(expected), actual, sourceLine, message);
122}
123
599d1291
VZ
124inline void
125assertEquals(const wxString& expected,
126 const char *actual,
127 CppUnit::SourceLine sourceLine,
128 const std::string& message)
129{
130 assertEquals(expected, wxString(actual), sourceLine, message);
131}
132
1de532f5
VZ
133inline void
134assertEquals(const wchar_t *expected,
135 const wxString& actual,
136 CppUnit::SourceLine sourceLine,
137 const std::string& message)
138{
139 assertEquals(wxString(expected), actual, sourceLine, message);
140}
141
599d1291
VZ
142inline void
143assertEquals(const wxString& expected,
144 const wchar_t *actual,
145 CppUnit::SourceLine sourceLine,
146 const std::string& message)
147{
148 assertEquals(expected, wxString(actual), sourceLine, message);
149}
150
b2eabfe8
VZ
151CPPUNIT_NS_END
152
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
527587d3
VZ
155#define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
156 inline void \
157 assertEquals(T1 expected, \
158 T2 actual, \
159 CppUnit::SourceLine sourceLine, \
160 const std::string& message) \
161 { \
162 if ( !assertion_traits<T1>::equal(expected,actual) ) \
163 { \
164 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
165 assertion_traits<T2>::toString(actual), \
166 sourceLine, \
167 message ); \
168 } \
169 }
170
b2eabfe8
VZ
171// this macro allows us to specify (usually literal) ints as expected values
172// for functions returning integral types different from "int"
173//
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
6fc905e0 178
b2eabfe8
VZ
179#ifdef __VISUALC6__
180 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
181 CPPUNIT_NS_BEGIN \
182 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
599d1291 183 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
b2eabfe8
VZ
184 WX_CPPUNIT_ASSERT_EQUALS(T, T) \
185 CPPUNIT_NS_END
186
187 CPPUNIT_NS_BEGIN
188 WX_CPPUNIT_ASSERT_EQUALS(int, int)
189 CPPUNIT_NS_END
190#else // !VC6
191 #define WX_CPPUNIT_ALLOW_EQUALS_TO_INT(T) \
192 CPPUNIT_NS_BEGIN \
193 WX_CPPUNIT_ASSERT_EQUALS(int, T) \
599d1291 194 WX_CPPUNIT_ASSERT_EQUALS(T, int) \
b2eabfe8
VZ
195 CPPUNIT_NS_END
196#endif // VC6/!VC6
197
198WX_CPPUNIT_ALLOW_EQUALS_TO_INT(long)
199WX_CPPUNIT_ALLOW_EQUALS_TO_INT(short)
306f34a3
VZ
200WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned)
201WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned long)
044a69a4 202
df016fc8 203#if defined( __VMS ) && defined( __ia64 )
e26aff56
JJ
204WX_CPPUNIT_ALLOW_EQUALS_TO_INT(std::basic_streambuf<char>::pos_type);
205#endif
206
066e5e3f 207#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
61b1a51b
VZ
208WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxLongLong_t)
209WX_CPPUNIT_ALLOW_EQUALS_TO_INT(unsigned wxLongLong_t)
066e5e3f 210#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
61b1a51b 211
5769cf0f
VZ
212// Use this macro to compare a wxArrayString with the pipe-separated elements
213// of the given string
214//
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) \
218 { \
219 wxArrayString expected(wxSplit(s, '|', '\0')); \
220 \
221 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
222 \
223 for ( size_t n = 0; n < a.size(); n++ ) \
224 { \
225 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
226 } \
227 }
b7c746d0
VZ
228
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))
233
e18865ec
VZ
234#define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
235 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
236 (expected), (actual))
237
2c940453 238///////////////////////////////////////////////////////////////////////////////
6919e3bb
VZ
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
2c940453 242
ef0aeb72
MW
243#include "wx/string.h"
244
5098c258
VZ
245#include <iostream>
246
2c940453
MW
247inline std::ostream& operator<<(std::ostream& o, const wxString& s)
248{
dad49cc7
VZ
249#if wxUSE_UNICODE
250 return o << (const char *)wxSafeConvertWX2MB(s.wc_str());
251#else
252 return o << s.c_str();
253#endif
2c940453
MW
254}
255
55d9f029
VZ
256#endif // !wxUSE_STD_IOSTREAM
257
aa709c26
VZ
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)
5098c258
VZ
261
262#include "wx/longlong.h"
263
264inline std::ostream& operator<<(std::ostream& ostr, wxLongLong_t ll)
265{
266 ostr << wxLongLong(ll).ToString();
267
268 return ostr;
269}
270
271inline std::ostream& operator<<(std::ostream& ostr, unsigned wxLongLong_t llu)
272{
273 ostr << wxULongLong(llu).ToString();
274
275 return ostr;
276}
277
aa709c26 278#endif // VC6 && wxLongLong_t
5098c258 279
044a69a4
VS
280///////////////////////////////////////////////////////////////////////////////
281// Some more compiler warning tweaking and auto linking.
282//
283
20f46e8d
VS
284#ifdef __BORLANDC__
285 #pragma warn .8022
286#endif
287
86132a69
VZ
288#ifdef _MSC_VER
289 #pragma warning(default:4702)
290#endif // _MSC_VER
291
292// for VC++ automatically link in cppunit library
293#ifdef __VISUALC__
294 #ifdef NDEBUG
295 #pragma comment(lib, "cppunit.lib")
296 #else // Debug
297 #pragma comment(lib, "cppunitd.lib")
298 #endif // Release/Debug
299#endif
300
301#endif // _WX_CPPUNIT_H_
302