]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cppunit.h
b11bebf5a907144bc0736abf609347c66291a995
[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 #include "wx/filefn.h" // for wxFileOffset
60
61
62 ///////////////////////////////////////////////////////////////////////////////
63 // Set of helpful test macros.
64 //
65
66 // Base macro for wrapping CPPUNIT_TEST macros and so making them conditional
67 // tests, meaning that the test only get registered and thus run when a given
68 // runtime condition is true.
69 // In case the condition is evaluated as false a skip message is logged
70 // (the message will only be shown in verbose mode).
71 #define WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, anyTest) \
72 if (Condition) \
73 { anyTest; } \
74 else \
75 wxLogInfo(wxString::Format(_T("skipping: %s.%s\n reason: %s equals false\n"), \
76 wxString(suiteName, wxConvUTF8).c_str(), \
77 wxString(#testMethod, wxConvUTF8).c_str(), \
78 wxString(#Condition, wxConvUTF8).c_str()))
79
80 // Conditional CPPUNIT_TEST macro.
81 #define WXTEST_WITH_CONDITION(suiteName, Condition, testMethod) \
82 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST(testMethod))
83 // Conditional CPPUNIT_TEST_FAIL macro.
84 #define WXTEST_FAIL_WITH_CONDITION(suiteName, Condition, testMethod) \
85 WXTEST_ANY_WITH_CONDITION(suiteName, Condition, testMethod, CPPUNIT_TEST_FAIL(testMethod))
86
87 CPPUNIT_NS_BEGIN
88
89 // provide an overload of cppunit assertEquals(T, T) which can be used to
90 // compare wxStrings directly with C strings
91 inline void
92 assertEquals(const char *expected,
93 const wxString& actual,
94 CppUnit::SourceLine sourceLine,
95 const std::string& message)
96 {
97 assertEquals(wxString(expected), actual, sourceLine, message);
98 }
99
100 inline void
101 assertEquals(const wchar_t *expected,
102 const wxString& actual,
103 CppUnit::SourceLine sourceLine,
104 const std::string& message)
105 {
106 assertEquals(wxString(expected), actual, sourceLine, message);
107 }
108
109 #define WX_CPPUNIT_ASSERT_EQUALS(T1, T2) \
110 inline void \
111 assertEquals(T1 expected, \
112 T2 actual, \
113 CppUnit::SourceLine sourceLine, \
114 const std::string& message) \
115 { \
116 if ( !assertion_traits<T1>::equal(expected,actual) ) \
117 { \
118 Asserter::failNotEqual( assertion_traits<T1>::toString(expected), \
119 assertion_traits<T2>::toString(actual), \
120 sourceLine, \
121 message ); \
122 } \
123 }
124
125 // and another to be able to specify (usually literal) ints as expected values
126 // for functions returning size_t/short/long/wxFileOffset
127 WX_CPPUNIT_ASSERT_EQUALS(int, long)
128 WX_CPPUNIT_ASSERT_EQUALS(int, short)
129 WX_CPPUNIT_ASSERT_EQUALS(int, size_t)
130 WX_CPPUNIT_ASSERT_EQUALS(int, wxFileOffset)
131
132 // special section with VC6 workarounds: due to incorrect resolution of
133 // overloaded/template functions in this compiler (it basically doesn't use the
134 // template version at all if any overloaded function matches partially even if
135 // none of them matches fully) we also need
136 #ifdef __VISUALC6__
137
138 WX_CPPUNIT_ASSERT_EQUALS(int, int)
139 WX_CPPUNIT_ASSERT_EQUALS(long, long)
140 WX_CPPUNIT_ASSERT_EQUALS(short, short)
141 WX_CPPUNIT_ASSERT_EQUALS(size_t, size_t)
142 WX_CPPUNIT_ASSERT_EQUALS(wxFileOffset, wxFileOffset)
143
144 #endif // VC6
145
146 CPPUNIT_NS_END
147
148 // Use this macro to compare a wxArrayString with the pipe-separated elements
149 // of the given string
150 //
151 // NB: it's a macro and not a function to have the correct line numbers in the
152 // test failure messages
153 #define WX_ASSERT_STRARRAY_EQUAL(s, a) \
154 { \
155 wxArrayString expected(wxSplit(s, '|', '\0')); \
156 \
157 CPPUNIT_ASSERT_EQUAL( expected.size(), a.size() ); \
158 \
159 for ( size_t n = 0; n < a.size(); n++ ) \
160 { \
161 CPPUNIT_ASSERT_EQUAL( expected[n], a[n] ); \
162 } \
163 }
164
165 // Use this macro to assert with the given formatted message (it should contain
166 // the format string and arguments in a separate pair of parentheses)
167 #define WX_ASSERT_MESSAGE(msg, cond) \
168 CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str()), (cond))
169
170 #define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
171 CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
172 (expected), (actual))
173
174 ///////////////////////////////////////////////////////////////////////////////
175 // define stream inserter for wxString if it's not defined in the main library,
176 // we need it to output the test failures involving wxString
177 #if !wxUSE_STD_IOSTREAM
178
179 #include "wx/string.h"
180
181 #include <iostream>
182
183 inline std::ostream& operator<<(std::ostream& o, const wxString& s)
184 {
185 #if wxUSE_UNICODE
186 return o << (const char *)wxSafeConvertWX2MB(s.wc_str());
187 #else
188 return o << s.c_str();
189 #endif
190 }
191
192 #endif // !wxUSE_STD_IOSTREAM
193
194 // VC6 doesn't provide overloads for operator<<(__int64) in its stream classes
195 // so do it ourselves
196 #if defined(__VISUALC6__) && defined(wxLongLong_t)
197
198 #include "wx/longlong.h"
199
200 inline std::ostream& operator<<(std::ostream& ostr, wxLongLong_t ll)
201 {
202 ostr << wxLongLong(ll).ToString();
203
204 return ostr;
205 }
206
207 inline std::ostream& operator<<(std::ostream& ostr, unsigned wxLongLong_t llu)
208 {
209 ostr << wxULongLong(llu).ToString();
210
211 return ostr;
212 }
213
214 #endif // VC6 && wxLongLong_t
215
216 ///////////////////////////////////////////////////////////////////////////////
217 // Some more compiler warning tweaking and auto linking.
218 //
219
220 #ifdef __BORLANDC__
221 #pragma warn .8022
222 #endif
223
224 #ifdef _MSC_VER
225 #pragma warning(default:4702)
226 #endif // _MSC_VER
227
228 // for VC++ automatically link in cppunit library
229 #ifdef __VISUALC__
230 #ifdef NDEBUG
231 #pragma comment(lib, "cppunit.lib")
232 #else // Debug
233 #pragma comment(lib, "cppunitd.lib")
234 #endif // Release/Debug
235 #endif
236
237 #endif // _WX_CPPUNIT_H_
238