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