]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
fix tests for the release builds
[wxWidgets.git] / tests / test.cpp
CommitLineData
670ec357
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: test.cpp
3// Purpose: Test program for wxWidgets
4// Author: Mike Wetherell
5// RCS-ID: $Id$
6// Copyright: (c) 2004 Mike Wetherell
7// Licence: wxWidgets licence
8///////////////////////////////////////////////////////////////////////////////
9
8899b155
RN
10// For compilers that support precompilation, includes "wx/wx.h"
11// and "wx/cppunit.h"
12#include "testprec.h"
670ec357
VS
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18// for all others, include the necessary headers
19#ifndef WX_PRECOMP
20 #include "wx/wx.h"
21#endif
22
afd90468 23#include "wx/beforestd.h"
6d9407fe
VZ
24#ifdef __VISUALC__
25 #pragma warning(disable:4100)
26#endif
afd90468 27#include <cppunit/TestListener.h>
6d9407fe
VZ
28#ifdef __VISUALC__
29 #pragma warning(default:4100)
30#endif
b33e98f0 31#include <cppunit/Protector.h>
afd90468
VZ
32#include <cppunit/Test.h>
33#include <cppunit/TestResult.h>
34#include "wx/afterstd.h"
35
670ec357 36#include "wx/cmdline.h"
670ec357
VS
37#include <iostream>
38
b33e98f0
VZ
39#ifdef __WXMSW__
40 #include "wx/msw/msvcrt.h"
41#endif
42
43using namespace std;
44
3e5f6c1c
WS
45using CppUnit::Test;
46using CppUnit::TestSuite;
47using CppUnit::TestFactoryRegistry;
3e5f6c1c 48
b33e98f0
VZ
49// exception class for MSVC debug CRT assertion failures
50#ifdef wxUSE_VC_CRTDBG
51
52struct CrtAssertFailure
53{
54 CrtAssertFailure(const char *message) : m_msg(message) { }
55
56 const wxString m_msg;
57
58 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
59};
60
61#endif // wxUSE_VC_CRTDBG
62
63// this function should only be called from a catch clause
64static string GetExceptionMessage()
65{
66 wxString msg;
67
68 try
69 {
70 throw;
71 }
72#if wxDEBUG_LEVEL
73 catch ( TestAssertFailure& e )
74 {
75 msg << "wxWidgets assert: " << e.m_cond << " failed "
76 "at " << e.m_file << ":" << e.m_line << " in " << e.m_func
77 << " with message " << e.m_msg;
78 }
79#endif // wxDEBUG_LEVEL
80#ifdef wxUSE_VC_CRTDBG
81 catch ( CrtAssertFailure& e )
82 {
83 msg << "CRT assert failure: " << e.m_msg;
84 }
85#endif // wxUSE_VC_CRTDBG
86 catch ( std::exception& e )
87 {
88 msg << "std::exception: " << e.what();
89 }
90 catch ( ... )
91 {
92 msg = "Unknown exception caught.";
93 }
94
95 return string(msg.mb_str());
96}
97
98// Protector adding handling of wx-specific (this includes MSVC debug CRT in
99// this context) exceptions
100class wxUnitTestProtector : public CppUnit::Protector
101{
102public:
103 virtual bool protect(const CppUnit::Functor &functor,
104 const CppUnit::ProtectorContext& context)
105 {
106 try
107 {
108 return functor();
109 }
9912799c
VZ
110 catch ( std::exception& e )
111 {
112 // cppunit deals with the standard exceptions itself, let it do as
113 // it output more details (especially for std::exception-derived
114 // CppUnit::Exception) than we do
115 throw;
116 }
b33e98f0
VZ
117 catch ( ... )
118 {
119 reportError(context, CppUnit::Message("Uncaught exception",
120 GetExceptionMessage()));
121 }
122
123 return false;
124 }
125};
afd90468 126
6d9407fe
VZ
127// Displays the test name before starting to execute it: this helps with
128// diagnosing where exactly does a test crash or hang when/if it does.
afd90468
VZ
129class DetailListener : public CppUnit::TestListener
130{
131public:
132 DetailListener(bool doTiming = false):
133 CppUnit::TestListener(),
134 m_timing(doTiming)
135 {
136 }
137
138 virtual void startTest(CppUnit::Test *test)
139 {
6d9407fe 140 std::cout << test->getName () << " ";
afd90468
VZ
141 m_watch.Start();
142 }
143
144 virtual void endTest(CppUnit::Test * WXUNUSED(test))
145 {
146 m_watch.Pause();
147 if ( m_timing )
6d9407fe
VZ
148 std::cout << " (in "<< m_watch.Time() << " ms )";
149 std::cout << "\n";
afd90468
VZ
150 }
151
152protected :
153 bool m_timing;
154 wxStopWatch m_watch;
155};
156
c0d9b217
VZ
157#if wxUSE_GUI
158 typedef wxApp TestAppBase;
159#else
160 typedef wxAppConsole TestAppBase;
161#endif
162
670ec357
VS
163// The application class
164//
c0d9b217 165class TestApp : public TestAppBase
670ec357
VS
166{
167public:
168 TestApp();
169
170 // standard overrides
c0d9b217
VZ
171 virtual void OnInitCmdLine(wxCmdLineParser& parser);
172 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
173 virtual bool OnInit();
174 virtual int OnRun();
175 virtual int OnExit();
670ec357 176
1649d288
VZ
177 // used by events propagation test
178 virtual int FilterEvent(wxEvent& event);
179 virtual bool ProcessEvent(wxEvent& event);
180
181 void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
182 void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
183
670ec357 184private:
8dae9169 185 void List(Test *test, const string& parent = "") const;
670ec357
VS
186
187 // command lines options/parameters
188 bool m_list;
a81f3066 189 bool m_longlist;
afd90468
VZ
190 bool m_detail;
191 bool m_timing;
6582f592 192 wxArrayString m_registries;
1649d288
VZ
193
194 // event handling hooks
195 FilterEventFunc m_filterEventFunc;
196 ProcessEventFunc m_processEventFunc;
670ec357
VS
197};
198
b33e98f0
VZ
199IMPLEMENT_APP_NO_MAIN(TestApp)
200
201#ifdef wxUSE_VC_CRTDBG
202
203static int TestCrtReportHook(int reportType, char *message, int *)
204{
205 if ( reportType != _CRT_ASSERT )
206 return FALSE;
207
208 throw CrtAssertFailure(message);
209}
210
211#endif // wxUSE_VC_CRTDBG
212
213#if wxDEBUG_LEVEL
214
215static void TestAssertHandler(const wxString& file,
216 int line,
217 const wxString& func,
218 const wxString& cond,
219 const wxString& msg)
220{
221 throw TestAssertFailure(file, line, func, cond, msg);
222}
223
224#endif // wxDEBUG_LEVEL
225
226int main(int argc, char **argv)
227{
228 // tests can be ran non-interactively so make sure we don't show any assert
229 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
230 // prevent them from completing
231
232#if wxDEBUG_LEVEL
233 wxSetAssertHandler(TestAssertHandler);
234#endif // wxDEBUG_LEVEL
235
236#ifdef wxUSE_VC_CRTDBG
237 _CrtSetReportHook(TestCrtReportHook);
238#endif // wxUSE_VC_CRTDBG
239
240 try
241 {
242 return wxEntry(argc, argv);
243 }
244 catch ( ... )
245 {
246 cerr << "\n" << GetExceptionMessage() << endl;
247 }
248
249 return -1;
250}
670ec357
VS
251
252TestApp::TestApp()
8dae9169
VS
253 : m_list(false),
254 m_longlist(false)
670ec357 255{
1649d288
VZ
256 m_filterEventFunc = NULL;
257 m_processEventFunc = NULL;
670ec357
VS
258}
259
260// Init
261//
262bool TestApp::OnInit()
263{
c0d9b217
VZ
264 if ( !TestAppBase::OnInit() )
265 return false;
266
670ec357 267 cout << "Test program for wxWidgets\n"
3e5f6c1c 268 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 269
c0d9b217
VZ
270#if wxUSE_GUI
271 // create a hidden parent window to be used as parent for the GUI controls
272 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
273#endif // wxUSE_GUI
274
275 return true;
9f10e7c7 276}
670ec357
VS
277
278// The table of command line options
279//
280void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
281{
c0d9b217 282 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
283
284 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
285 { wxCMD_LINE_SWITCH, "l", "list",
286 "list the test suites, do not run them",
a81f3066 287 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
288 { wxCMD_LINE_SWITCH, "L", "longlist",
289 "list the test cases, do not run them",
670ec357 290 wxCMD_LINE_VAL_NONE, 0 },
afd90468
VZ
291 { wxCMD_LINE_SWITCH, "d", "detail",
292 "print the test case names, run them",
293 wxCMD_LINE_VAL_NONE, 0 },
294 { wxCMD_LINE_SWITCH, "t", "timing",
295 "print names and mesure running time of individual test, run them",
296 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e 297 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 298 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 299 wxCMD_LINE_DESC_END
670ec357
VS
300 };
301
302 parser.SetDesc(cmdLineDesc);
303}
304
305// Handle command line options
306//
307bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
308{
a81f3066
VS
309 if (parser.GetParamCount())
310 for (size_t i = 0; i < parser.GetParamCount(); i++)
6582f592 311 m_registries.push_back(parser.GetParam(i));
a81f3066
VS
312 else
313 m_registries.push_back("");
3e5f6c1c 314
a81f3066
VS
315 m_longlist = parser.Found(_T("longlist"));
316 m_list = m_longlist || parser.Found(_T("list"));
afd90468
VZ
317 m_timing = parser.Found(_T("timing"));
318 m_detail = !m_timing && parser.Found(_T("detail"));
670ec357 319
c0d9b217 320 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
321}
322
1649d288
VZ
323// Event handling
324int TestApp::FilterEvent(wxEvent& event)
325{
326 if ( m_filterEventFunc )
327 return (*m_filterEventFunc)(event);
328
329 return TestAppBase::FilterEvent(event);
330}
331
332bool TestApp::ProcessEvent(wxEvent& event)
333{
334 if ( m_processEventFunc )
335 return (*m_processEventFunc)(event);
336
337 return TestAppBase::ProcessEvent(event);
338}
339
340extern void SetFilterEventFunc(FilterEventFunc func)
341{
342 wxGetApp().SetFilterEventFunc(func);
343}
344
345extern void SetProcessEventFunc(ProcessEventFunc func)
346{
347 wxGetApp().SetProcessEventFunc(func);
348}
349
670ec357
VS
350// Run
351//
352int TestApp::OnRun()
353{
2976d6cb 354 CppUnit::TextTestRunner runner;
a81f3066 355
6d423df0
FM
356 for (size_t i = 0; i < m_registries.size(); i++)
357 {
6582f592
VZ
358 wxString reg = m_registries[i];
359 if (!reg.empty() && !reg.EndsWith("TestCase"))
6d423df0
FM
360 reg += "TestCase";
361 // allow the user to specify the name of the testcase "in short form"
362 // (all wx test cases end with TestCase postfix)
363
364 auto_ptr<Test> test(reg.empty() ?
a81f3066 365 TestFactoryRegistry::getRegistry().makeTest() :
6582f592 366 TestFactoryRegistry::getRegistry(string(reg.mb_str())).makeTest());
a81f3066
VS
367
368 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
369
370 if (suite && suite->countTestCases() == 0)
6582f592 371 wxLogError(_T("No such test suite: %s"), reg);
a81f3066
VS
372 else if (m_list)
373 List(test.get());
374 else
375 runner.addTest(test.release());
670ec357 376 }
a81f3066 377
2976d6cb 378 if ( m_list )
1dd8319a 379 return EXIT_SUCCESS;
2976d6cb
VZ
380
381 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 382
86ca2b4c 383#if wxUSE_LOG
a81f3066 384 // Switch off logging unless --verbose
3e5f6c1c
WS
385 bool verbose = wxLog::GetVerbose();
386 wxLog::EnableLogging(verbose);
86ca2b4c 387#else
3e5f6c1c
WS
388 bool verbose = false;
389#endif
390
2976d6cb
VZ
391 // there is a bug
392 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
393 // in some versions of cppunit: they write progress dots to cout (and not
394 // cerr) and don't flush it so all the dots appear at once at the end which
395 // is not very useful so unbuffer cout to work around this
396 cout.setf(ios::unitbuf);
397
afd90468
VZ
398 // add detail listener if needed
399 DetailListener detailListener(m_timing);
400 if ( m_detail || m_timing )
401 runner.eventManager().addListener(&detailListener);
402
b33e98f0
VZ
403 // finally ensure that we report our own exceptions nicely instead of
404 // giving "uncaught exception of unknown type" messages
405 runner.eventManager().pushProtector(new wxUnitTestProtector);
406
1dd8319a 407 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
408}
409
c0d9b217
VZ
410int TestApp::OnExit()
411{
412#if wxUSE_GUI
413 delete GetTopWindow();
414#endif // wxUSE_GUI
415
416 return 0;
417}
418
670ec357
VS
419// List the tests
420//
8dae9169 421void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 422{
670ec357 423 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
424 string name;
425
bc10103e 426 if (suite) {
8dae9169
VS
427 // take the last component of the name and append to the parent
428 name = test->getName();
429 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
430 if (i != string::npos)
431 name = name.substr(i + 1);
432 name = parent + "." + name;
8dae9169
VS
433
434 // drop the 1st component from the display and indent
435 if (parent != "") {
436 string::size_type j = i = name.find('.', 1);
437 while ((j = name.find('.', j + 1)) != string::npos)
438 cout << " ";
439 cout << " " << name.substr(i + 1) << "\n";
440 }
a81f3066 441
f69577be 442 typedef vector<Test*> Tests;
670ec357
VS
443 typedef Tests::const_iterator Iter;
444
f69577be 445 const Tests& tests = suite->getTests();
670ec357
VS
446
447 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 448 List(*it, name);
670ec357 449 }
bc10103e
VS
450 else if (m_longlist) {
451 string::size_type i = 0;
452 while ((i = parent.find('.', i + 1)) != string::npos)
453 cout << " ";
454 cout << " " << test->getName() << "\n";
455 }
670ec357 456}