]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
Fix Cygwin 1.7 build.
[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
526954c5 7// Licence: wxWindows licence
670ec357
VS
8///////////////////////////////////////////////////////////////////////////////
9
1f51673b
FM
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
8899b155
RN
14// For compilers that support precompilation, includes "wx/wx.h"
15// and "wx/cppunit.h"
16#include "testprec.h"
670ec357
VS
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22// for all others, include the necessary headers
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
afd90468 27#include "wx/beforestd.h"
6d9407fe
VZ
28#ifdef __VISUALC__
29 #pragma warning(disable:4100)
30#endif
de830910 31
afd90468 32#include <cppunit/TestListener.h>
b33e98f0 33#include <cppunit/Protector.h>
afd90468
VZ
34#include <cppunit/Test.h>
35#include <cppunit/TestResult.h>
29e0c68e 36#include <cppunit/TestFailure.h>
de830910
VZ
37
38#ifdef __VISUALC__
39 #pragma warning(default:4100)
40#endif
afd90468
VZ
41#include "wx/afterstd.h"
42
670ec357 43#include "wx/cmdline.h"
670ec357
VS
44#include <iostream>
45
b33e98f0
VZ
46#ifdef __WXMSW__
47 #include "wx/msw/msvcrt.h"
48#endif
49
8b7b1a57
SC
50#ifdef __WXOSX__
51 #include "wx/osx/private.h"
52#endif
53
1f51673b
FM
54#include "wx/socket.h"
55
b33e98f0
VZ
56using namespace std;
57
3e5f6c1c
WS
58using CppUnit::Test;
59using CppUnit::TestSuite;
60using CppUnit::TestFactoryRegistry;
3e5f6c1c 61
1f51673b
FM
62
63// ----------------------------------------------------------------------------
64// helper classes
65// ----------------------------------------------------------------------------
66
b33e98f0
VZ
67// exception class for MSVC debug CRT assertion failures
68#ifdef wxUSE_VC_CRTDBG
69
70struct CrtAssertFailure
71{
72 CrtAssertFailure(const char *message) : m_msg(message) { }
73
74 const wxString m_msg;
75
76 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
77};
78
79#endif // wxUSE_VC_CRTDBG
80
884ca4e4
VZ
81#if wxDEBUG_LEVEL
82
83static wxString FormatAssertMessage(const wxString& file,
84 int line,
85 const wxString& func,
86 const wxString& cond,
87 const wxString& msg)
88{
89 wxString str;
90 str << "wxWidgets assert: " << cond << " failed "
91 "at " << file << ":" << line << " in " << func
92 << " with message '" << msg << "'";
93 return str;
94}
95
96static void TestAssertHandler(const wxString& file,
97 int line,
98 const wxString& func,
99 const wxString& cond,
100 const wxString& msg)
101{
102 // can't throw from other threads, die immediately
103 if ( !wxIsMainThread() )
104 {
105 wxPrintf("%s in a worker thread -- aborting.",
106 FormatAssertMessage(file, line, func, cond, msg));
107 fflush(stdout);
108 _exit(-1);
109 }
110
111 throw TestAssertFailure(file, line, func, cond, msg);
112}
113
114#endif // wxDEBUG_LEVEL
115
b33e98f0
VZ
116// this function should only be called from a catch clause
117static string GetExceptionMessage()
118{
119 wxString msg;
120
121 try
122 {
123 throw;
124 }
125#if wxDEBUG_LEVEL
126 catch ( TestAssertFailure& e )
127 {
884ca4e4
VZ
128 msg << FormatAssertMessage(e.m_file, e.m_line, e.m_func,
129 e.m_cond, e.m_msg);
b33e98f0
VZ
130 }
131#endif // wxDEBUG_LEVEL
132#ifdef wxUSE_VC_CRTDBG
133 catch ( CrtAssertFailure& e )
134 {
135 msg << "CRT assert failure: " << e.m_msg;
136 }
137#endif // wxUSE_VC_CRTDBG
138 catch ( std::exception& e )
139 {
140 msg << "std::exception: " << e.what();
141 }
142 catch ( ... )
143 {
144 msg = "Unknown exception caught.";
145 }
146
147 return string(msg.mb_str());
148}
149
150// Protector adding handling of wx-specific (this includes MSVC debug CRT in
151// this context) exceptions
152class wxUnitTestProtector : public CppUnit::Protector
153{
154public:
155 virtual bool protect(const CppUnit::Functor &functor,
156 const CppUnit::ProtectorContext& context)
157 {
158 try
159 {
160 return functor();
161 }
92f8206f 162 catch ( std::exception& )
9912799c
VZ
163 {
164 // cppunit deals with the standard exceptions itself, let it do as
165 // it output more details (especially for std::exception-derived
166 // CppUnit::Exception) than we do
167 throw;
168 }
b33e98f0
VZ
169 catch ( ... )
170 {
171 reportError(context, CppUnit::Message("Uncaught exception",
172 GetExceptionMessage()));
173 }
174
175 return false;
176 }
177};
afd90468 178
6d9407fe
VZ
179// Displays the test name before starting to execute it: this helps with
180// diagnosing where exactly does a test crash or hang when/if it does.
afd90468
VZ
181class DetailListener : public CppUnit::TestListener
182{
183public:
184 DetailListener(bool doTiming = false):
185 CppUnit::TestListener(),
186 m_timing(doTiming)
187 {
188 }
189
190 virtual void startTest(CppUnit::Test *test)
191 {
29e0c68e
VZ
192 wxPrintf(" %-60s ", test->getName());
193 m_result = RESULT_OK;
afd90468
VZ
194 m_watch.Start();
195 }
196
62714742
VZ
197 virtual void addFailure(const CppUnit::TestFailure& failure)
198 {
29e0c68e
VZ
199 m_result = failure.isError() ? RESULT_ERROR : RESULT_FAIL;
200 }
201
afd90468
VZ
202 virtual void endTest(CppUnit::Test * WXUNUSED(test))
203 {
204 m_watch.Pause();
29e0c68e
VZ
205 wxPrintf(GetResultStr(m_result));
206 if (m_timing)
207 wxPrintf(" %6d ms", m_watch.Time());
208 wxPrintf("\n");
afd90468
VZ
209 }
210
211protected :
62714742
VZ
212 enum ResultType
213 {
29e0c68e
VZ
214 RESULT_OK = 0,
215 RESULT_FAIL,
62714742
VZ
216 RESULT_ERROR,
217 RESULT_MAX
29e0c68e
VZ
218 };
219
62714742
VZ
220 wxString GetResultStr(ResultType type) const
221 {
222 static const char *resultTypeNames[] =
223 {
224 " OK",
225 "FAIL",
226 " ERR"
29e0c68e 227 };
62714742
VZ
228
229 wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames) == RESULT_MAX,
230 ResultTypeNamesMismatch );
231
232 return resultTypeNames[type];
29e0c68e
VZ
233 }
234
afd90468
VZ
235 bool m_timing;
236 wxStopWatch m_watch;
29e0c68e 237 ResultType m_result;
afd90468
VZ
238};
239
c0d9b217
VZ
240#if wxUSE_GUI
241 typedef wxApp TestAppBase;
242#else
243 typedef wxAppConsole TestAppBase;
244#endif
245
670ec357
VS
246// The application class
247//
c0d9b217 248class TestApp : public TestAppBase
670ec357
VS
249{
250public:
251 TestApp();
252
253 // standard overrides
c0d9b217
VZ
254 virtual void OnInitCmdLine(wxCmdLineParser& parser);
255 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
256 virtual bool OnInit();
257 virtual int OnRun();
258 virtual int OnExit();
670ec357 259
1649d288
VZ
260 // used by events propagation test
261 virtual int FilterEvent(wxEvent& event);
262 virtual bool ProcessEvent(wxEvent& event);
263
264 void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
265 void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
266
670ec357 267private:
8dae9169 268 void List(Test *test, const string& parent = "") const;
670ec357 269
e992b97f
VZ
270 // call List() if m_list or runner.addTest() otherwise
271 void AddTest(CppUnit::TestRunner& runner, Test *test)
272 {
273 if (m_list)
274 List(test);
275 else
276 runner.addTest(test);
277 }
278
670ec357
VS
279 // command lines options/parameters
280 bool m_list;
a81f3066 281 bool m_longlist;
afd90468
VZ
282 bool m_detail;
283 bool m_timing;
6582f592 284 wxArrayString m_registries;
ad16130f 285 wxLocale *m_locale;
1649d288
VZ
286
287 // event handling hooks
288 FilterEventFunc m_filterEventFunc;
289 ProcessEventFunc m_processEventFunc;
670ec357
VS
290};
291
b33e98f0
VZ
292IMPLEMENT_APP_NO_MAIN(TestApp)
293
1f51673b
FM
294
295// ----------------------------------------------------------------------------
296// global functions
297// ----------------------------------------------------------------------------
298
b33e98f0
VZ
299#ifdef wxUSE_VC_CRTDBG
300
301static int TestCrtReportHook(int reportType, char *message, int *)
302{
303 if ( reportType != _CRT_ASSERT )
304 return FALSE;
305
306 throw CrtAssertFailure(message);
307}
308
309#endif // wxUSE_VC_CRTDBG
310
b33e98f0
VZ
311int main(int argc, char **argv)
312{
313 // tests can be ran non-interactively so make sure we don't show any assert
314 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
315 // prevent them from completing
316
317#if wxDEBUG_LEVEL
318 wxSetAssertHandler(TestAssertHandler);
319#endif // wxDEBUG_LEVEL
320
321#ifdef wxUSE_VC_CRTDBG
322 _CrtSetReportHook(TestCrtReportHook);
323#endif // wxUSE_VC_CRTDBG
324
325 try
326 {
327 return wxEntry(argc, argv);
328 }
329 catch ( ... )
330 {
331 cerr << "\n" << GetExceptionMessage() << endl;
332 }
333
334 return -1;
335}
670ec357 336
1f51673b
FM
337extern void SetFilterEventFunc(FilterEventFunc func)
338{
339 wxGetApp().SetFilterEventFunc(func);
340}
341
342extern void SetProcessEventFunc(ProcessEventFunc func)
343{
344 wxGetApp().SetProcessEventFunc(func);
345}
346
347extern bool IsNetworkAvailable()
348{
349 // NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in
350 // wxCore we use a simple rough test:
351
352 wxSocketBase::Initialize();
353
354 wxIPV4address addr;
355 if (!addr.Hostname("www.google.com") || !addr.Service("www"))
356 {
357 wxSocketBase::Shutdown();
358 return false;
359 }
360
361 wxSocketClient sock;
5b105c6f 362 sock.SetTimeout(10); // 10 secs
1f51673b
FM
363 bool online = sock.Connect(addr);
364
365 wxSocketBase::Shutdown();
366
367 return online;
368}
369
370// helper of OnRun(): gets the test with the given name, returning NULL (and
371// not an empty test suite) if there is no such test
372static Test *GetTestByName(const wxString& name)
373{
374 Test *
375 test = TestFactoryRegistry::getRegistry(string(name.mb_str())).makeTest();
376 if ( test )
377 {
378 TestSuite * const suite = dynamic_cast<TestSuite *>(test);
379 if ( !suite || !suite->countTestCases() )
380 {
381 // it's a bogus test, don't use it
382 delete test;
383 test = NULL;
384 }
385 }
386
387 return test;
388}
389
390
391// ----------------------------------------------------------------------------
392// TestApp
393// ----------------------------------------------------------------------------
394
670ec357 395TestApp::TestApp()
8dae9169
VS
396 : m_list(false),
397 m_longlist(false)
670ec357 398{
1649d288
VZ
399 m_filterEventFunc = NULL;
400 m_processEventFunc = NULL;
ad16130f
VZ
401
402 m_locale = NULL;
670ec357
VS
403}
404
405// Init
406//
407bool TestApp::OnInit()
408{
c0d9b217
VZ
409 if ( !TestAppBase::OnInit() )
410 return false;
411
9e91447c
FM
412#if wxUSE_GUI
413 cout << "Test program for wxWidgets GUI features\n"
414#else
415 cout << "Test program for wxWidgets non-GUI features\n"
416#endif
3e5f6c1c 417 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 418
c0d9b217
VZ
419#if wxUSE_GUI
420 // create a hidden parent window to be used as parent for the GUI controls
421 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
422#endif // wxUSE_GUI
423
424 return true;
9f10e7c7 425}
670ec357
VS
426
427// The table of command line options
428//
429void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
430{
c0d9b217 431 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
432
433 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
434 { wxCMD_LINE_SWITCH, "l", "list",
435 "list the test suites, do not run them",
a81f3066 436 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
437 { wxCMD_LINE_SWITCH, "L", "longlist",
438 "list the test cases, do not run them",
670ec357 439 wxCMD_LINE_VAL_NONE, 0 },
afd90468
VZ
440 { wxCMD_LINE_SWITCH, "d", "detail",
441 "print the test case names, run them",
442 wxCMD_LINE_VAL_NONE, 0 },
443 { wxCMD_LINE_SWITCH, "t", "timing",
20ba398d 444 "print names and measure running time of individual test, run them",
afd90468 445 wxCMD_LINE_VAL_NONE, 0 },
ad16130f
VZ
446 { wxCMD_LINE_OPTION, "", "locale",
447 "locale to use when running the program",
448 wxCMD_LINE_VAL_STRING, 0 },
aa3b041e 449 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 450 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 451 wxCMD_LINE_DESC_END
670ec357
VS
452 };
453
454 parser.SetDesc(cmdLineDesc);
455}
456
457// Handle command line options
458//
459bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
460{
a81f3066 461 if (parser.GetParamCount())
ad16130f 462 {
a81f3066 463 for (size_t i = 0; i < parser.GetParamCount(); i++)
6582f592 464 m_registries.push_back(parser.GetParam(i));
ad16130f 465 }
ad16130f
VZ
466
467 m_longlist = parser.Found("longlist");
468 m_list = m_longlist || parser.Found("list");
469 m_timing = parser.Found("timing");
470 m_detail = !m_timing && parser.Found("detail");
471
472 wxString loc;
473 if ( parser.Found("locale", &loc) )
474 {
475 const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(loc);
476 if ( !info )
477 {
478 cerr << "Locale \"" << string(loc.mb_str()) << "\" is unknown.\n";
479 return false;
480 }
3e5f6c1c 481
ad16130f
VZ
482 m_locale = new wxLocale(info->Language);
483 if ( !m_locale->IsOk() )
484 {
485 cerr << "Using locale \"" << string(loc.mb_str()) << "\" failed.\n";
486 return false;
487 }
488 }
670ec357 489
c0d9b217 490 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
491}
492
1649d288
VZ
493// Event handling
494int TestApp::FilterEvent(wxEvent& event)
495{
496 if ( m_filterEventFunc )
497 return (*m_filterEventFunc)(event);
498
499 return TestAppBase::FilterEvent(event);
500}
501
502bool TestApp::ProcessEvent(wxEvent& event)
503{
504 if ( m_processEventFunc )
505 return (*m_processEventFunc)(event);
506
507 return TestAppBase::ProcessEvent(event);
508}
509
670ec357
VS
510// Run
511//
512int TestApp::OnRun()
513{
8b7b1a57
SC
514#if wxUSE_GUI
515#ifdef __WXOSX__
516 // make sure there's always an autorelease pool ready
517 wxMacAutoreleasePool autoreleasepool;
518#endif
519#endif
520
875f82b1
VZ
521#if wxUSE_LOG
522 // Switch off logging unless --verbose
523 bool verbose = wxLog::GetVerbose();
524 wxLog::EnableLogging(verbose);
525#else
526 bool verbose = false;
527#endif
528
2976d6cb 529 CppUnit::TextTestRunner runner;
a81f3066 530
e992b97f 531 if ( m_registries.empty() )
6d423df0 532 {
20ba398d
FM
533 // run or list all tests which use the CPPUNIT_TEST_SUITE_REGISTRATION() macro
534 // (i.e. those registered in the "All tests" registry); if there are other
535 // tests not registered with the CPPUNIT_TEST_SUITE_REGISTRATION() macro
536 // then they won't be listed/run!
e992b97f 537 AddTest(runner, TestFactoryRegistry::getRegistry().makeTest());
20ba398d
FM
538
539 if (m_list)
540 {
541 cout << "\nNote that the list above is not complete as it doesn't include the \n";
542 cout << "tests disabled by default.\n";
543 }
e992b97f
VZ
544 }
545 else // run only the selected tests
546 {
547 for (size_t i = 0; i < m_registries.size(); i++)
d73a5209 548 {
e992b97f
VZ
549 const wxString reg = m_registries[i];
550 Test *test = GetTestByName(reg);
d73a5209
VZ
551
552 if ( !test && !reg.EndsWith("TestCase") )
553 {
554 test = GetTestByName(reg + "TestCase");
555 }
556
557 if ( !test )
558 {
559 cerr << "No such test suite: " << string(reg.mb_str()) << endl;
560 return 2;
561 }
a81f3066 562
e992b97f
VZ
563 AddTest(runner, test);
564 }
670ec357 565 }
a81f3066 566
2976d6cb 567 if ( m_list )
1dd8319a 568 return EXIT_SUCCESS;
2976d6cb
VZ
569
570 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 571
2976d6cb
VZ
572 // there is a bug
573 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
574 // in some versions of cppunit: they write progress dots to cout (and not
575 // cerr) and don't flush it so all the dots appear at once at the end which
576 // is not very useful so unbuffer cout to work around this
577 cout.setf(ios::unitbuf);
578
afd90468
VZ
579 // add detail listener if needed
580 DetailListener detailListener(m_timing);
581 if ( m_detail || m_timing )
582 runner.eventManager().addListener(&detailListener);
583
b33e98f0
VZ
584 // finally ensure that we report our own exceptions nicely instead of
585 // giving "uncaught exception of unknown type" messages
586 runner.eventManager().pushProtector(new wxUnitTestProtector);
587
29e0c68e
VZ
588 bool printProgress = !(verbose || m_detail || m_timing);
589 return runner.run("", false, true, printProgress) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
590}
591
c0d9b217
VZ
592int TestApp::OnExit()
593{
ad16130f
VZ
594 delete m_locale;
595
c0d9b217
VZ
596#if wxUSE_GUI
597 delete GetTopWindow();
598#endif // wxUSE_GUI
599
600 return 0;
601}
602
670ec357
VS
603// List the tests
604//
8dae9169 605void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 606{
670ec357 607 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
608 string name;
609
bc10103e 610 if (suite) {
8dae9169
VS
611 // take the last component of the name and append to the parent
612 name = test->getName();
613 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
614 if (i != string::npos)
615 name = name.substr(i + 1);
616 name = parent + "." + name;
8dae9169
VS
617
618 // drop the 1st component from the display and indent
619 if (parent != "") {
620 string::size_type j = i = name.find('.', 1);
621 while ((j = name.find('.', j + 1)) != string::npos)
622 cout << " ";
623 cout << " " << name.substr(i + 1) << "\n";
624 }
a81f3066 625
f69577be 626 typedef vector<Test*> Tests;
670ec357
VS
627 typedef Tests::const_iterator Iter;
628
f69577be 629 const Tests& tests = suite->getTests();
670ec357
VS
630
631 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 632 List(*it, name);
670ec357 633 }
bc10103e
VS
634 else if (m_longlist) {
635 string::size_type i = 0;
636 while ((i = parent.find('.', i + 1)) != string::npos)
637 cout << " ";
638 cout << " " << test->getName() << "\n";
639 }
670ec357 640}