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