]> git.saurik.com Git - wxWidgets.git/blob - tests/test.cpp
Build fix: forgotten part of the previous commit.
[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: wxWidgets 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 <iostream>
45
46 #ifdef __WXMSW__
47 #include "wx/msw/msvcrt.h"
48 #endif
49
50 #ifdef __WXOSX__
51 #include "wx/osx/private.h"
52 #endif
53
54 #include "wx/socket.h"
55
56 using namespace std;
57
58 using CppUnit::Test;
59 using CppUnit::TestSuite;
60 using CppUnit::TestFactoryRegistry;
61
62
63 // ----------------------------------------------------------------------------
64 // helper classes
65 // ----------------------------------------------------------------------------
66
67 // exception class for MSVC debug CRT assertion failures
68 #ifdef wxUSE_VC_CRTDBG
69
70 struct 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
81 #if wxDEBUG_LEVEL
82
83 static 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
96 static 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
116 // this function should only be called from a catch clause
117 static string GetExceptionMessage()
118 {
119 wxString msg;
120
121 try
122 {
123 throw;
124 }
125 #if wxDEBUG_LEVEL
126 catch ( TestAssertFailure& e )
127 {
128 msg << FormatAssertMessage(e.m_file, e.m_line, e.m_func,
129 e.m_cond, e.m_msg);
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
152 class wxUnitTestProtector : public CppUnit::Protector
153 {
154 public:
155 virtual bool protect(const CppUnit::Functor &functor,
156 const CppUnit::ProtectorContext& context)
157 {
158 try
159 {
160 return functor();
161 }
162 catch ( std::exception& )
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 }
169 catch ( ... )
170 {
171 reportError(context, CppUnit::Message("Uncaught exception",
172 GetExceptionMessage()));
173 }
174
175 return false;
176 }
177 };
178
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.
181 class DetailListener : public CppUnit::TestListener
182 {
183 public:
184 DetailListener(bool doTiming = false):
185 CppUnit::TestListener(),
186 m_timing(doTiming)
187 {
188 }
189
190 virtual void startTest(CppUnit::Test *test)
191 {
192 wxPrintf(" %-60s ", test->getName());
193 m_result = RESULT_OK;
194 m_watch.Start();
195 }
196
197 virtual void addFailure(const CppUnit::TestFailure& failure)
198 {
199 m_result = failure.isError() ? RESULT_ERROR : RESULT_FAIL;
200 }
201
202 virtual void endTest(CppUnit::Test * WXUNUSED(test))
203 {
204 m_watch.Pause();
205 wxPrintf(GetResultStr(m_result));
206 if (m_timing)
207 wxPrintf(" %6d ms", m_watch.Time());
208 wxPrintf("\n");
209 }
210
211 protected :
212 enum ResultType
213 {
214 RESULT_OK = 0,
215 RESULT_FAIL,
216 RESULT_ERROR,
217 RESULT_MAX
218 };
219
220 wxString GetResultStr(ResultType type) const
221 {
222 static const char *resultTypeNames[] =
223 {
224 " OK",
225 "FAIL",
226 " ERR"
227 };
228
229 wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames) == RESULT_MAX,
230 ResultTypeNamesMismatch );
231
232 return resultTypeNames[type];
233 }
234
235 bool m_timing;
236 wxStopWatch m_watch;
237 ResultType m_result;
238 };
239
240 #if wxUSE_GUI
241 typedef wxApp TestAppBase;
242 #else
243 typedef wxAppConsole TestAppBase;
244 #endif
245
246 // The application class
247 //
248 class TestApp : public TestAppBase
249 {
250 public:
251 TestApp();
252
253 // standard overrides
254 virtual void OnInitCmdLine(wxCmdLineParser& parser);
255 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
256 virtual bool OnInit();
257 virtual int OnRun();
258 virtual int OnExit();
259
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
267 private:
268 void List(Test *test, const string& parent = "") const;
269
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
279 // command lines options/parameters
280 bool m_list;
281 bool m_longlist;
282 bool m_detail;
283 bool m_timing;
284 wxArrayString m_registries;
285 wxLocale *m_locale;
286
287 // event handling hooks
288 FilterEventFunc m_filterEventFunc;
289 ProcessEventFunc m_processEventFunc;
290 };
291
292 IMPLEMENT_APP_NO_MAIN(TestApp)
293
294
295 // ----------------------------------------------------------------------------
296 // global functions
297 // ----------------------------------------------------------------------------
298
299 #ifdef wxUSE_VC_CRTDBG
300
301 static 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
311 int 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 }
336
337 extern void SetFilterEventFunc(FilterEventFunc func)
338 {
339 wxGetApp().SetFilterEventFunc(func);
340 }
341
342 extern void SetProcessEventFunc(ProcessEventFunc func)
343 {
344 wxGetApp().SetProcessEventFunc(func);
345 }
346
347 extern 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;
362 sock.SetTimeout(10); // 10 secs
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
372 static 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
395 TestApp::TestApp()
396 : m_list(false),
397 m_longlist(false)
398 {
399 m_filterEventFunc = NULL;
400 m_processEventFunc = NULL;
401
402 m_locale = NULL;
403 }
404
405 // Init
406 //
407 bool TestApp::OnInit()
408 {
409 if ( !TestAppBase::OnInit() )
410 return false;
411
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
417 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
418
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;
425 }
426
427 // The table of command line options
428 //
429 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
430 {
431 TestAppBase::OnInitCmdLine(parser);
432
433 static const wxCmdLineEntryDesc cmdLineDesc[] = {
434 { wxCMD_LINE_SWITCH, "l", "list",
435 "list the test suites, do not run them",
436 wxCMD_LINE_VAL_NONE, 0 },
437 { wxCMD_LINE_SWITCH, "L", "longlist",
438 "list the test cases, do not run them",
439 wxCMD_LINE_VAL_NONE, 0 },
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",
444 "print names and mesure running time of individual test, run them",
445 wxCMD_LINE_VAL_NONE, 0 },
446 { wxCMD_LINE_OPTION, "", "locale",
447 "locale to use when running the program",
448 wxCMD_LINE_VAL_STRING, 0 },
449 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
450 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
451 wxCMD_LINE_DESC_END
452 };
453
454 parser.SetDesc(cmdLineDesc);
455 }
456
457 // Handle command line options
458 //
459 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
460 {
461 if (parser.GetParamCount())
462 {
463 for (size_t i = 0; i < parser.GetParamCount(); i++)
464 m_registries.push_back(parser.GetParam(i));
465 }
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 }
481
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 }
489
490 return TestAppBase::OnCmdLineParsed(parser);
491 }
492
493 // Event handling
494 int TestApp::FilterEvent(wxEvent& event)
495 {
496 if ( m_filterEventFunc )
497 return (*m_filterEventFunc)(event);
498
499 return TestAppBase::FilterEvent(event);
500 }
501
502 bool TestApp::ProcessEvent(wxEvent& event)
503 {
504 if ( m_processEventFunc )
505 return (*m_processEventFunc)(event);
506
507 return TestAppBase::ProcessEvent(event);
508 }
509
510 // Run
511 //
512 int TestApp::OnRun()
513 {
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
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
529 CppUnit::TextTestRunner runner;
530
531 if ( m_registries.empty() )
532 {
533 // run or list all tests
534 AddTest(runner, TestFactoryRegistry::getRegistry().makeTest());
535 }
536 else // run only the selected tests
537 {
538 for (size_t i = 0; i < m_registries.size(); i++)
539 {
540 const wxString reg = m_registries[i];
541 Test *test = GetTestByName(reg);
542
543 if ( !test && !reg.EndsWith("TestCase") )
544 {
545 test = GetTestByName(reg + "TestCase");
546 }
547
548 if ( !test )
549 {
550 cerr << "No such test suite: " << string(reg.mb_str()) << endl;
551 return 2;
552 }
553
554 AddTest(runner, test);
555 }
556 }
557
558 if ( m_list )
559 return EXIT_SUCCESS;
560
561 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
562
563 // there is a bug
564 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
565 // in some versions of cppunit: they write progress dots to cout (and not
566 // cerr) and don't flush it so all the dots appear at once at the end which
567 // is not very useful so unbuffer cout to work around this
568 cout.setf(ios::unitbuf);
569
570 // add detail listener if needed
571 DetailListener detailListener(m_timing);
572 if ( m_detail || m_timing )
573 runner.eventManager().addListener(&detailListener);
574
575 // finally ensure that we report our own exceptions nicely instead of
576 // giving "uncaught exception of unknown type" messages
577 runner.eventManager().pushProtector(new wxUnitTestProtector);
578
579 bool printProgress = !(verbose || m_detail || m_timing);
580 return runner.run("", false, true, printProgress) ? EXIT_SUCCESS : EXIT_FAILURE;
581 }
582
583 int TestApp::OnExit()
584 {
585 delete m_locale;
586
587 #if wxUSE_GUI
588 delete GetTopWindow();
589 #endif // wxUSE_GUI
590
591 return 0;
592 }
593
594 // List the tests
595 //
596 void TestApp::List(Test *test, const string& parent /*=""*/) const
597 {
598 TestSuite *suite = dynamic_cast<TestSuite*>(test);
599 string name;
600
601 if (suite) {
602 // take the last component of the name and append to the parent
603 name = test->getName();
604 string::size_type i = name.find_last_of(".:");
605 if (i != string::npos)
606 name = name.substr(i + 1);
607 name = parent + "." + name;
608
609 // drop the 1st component from the display and indent
610 if (parent != "") {
611 string::size_type j = i = name.find('.', 1);
612 while ((j = name.find('.', j + 1)) != string::npos)
613 cout << " ";
614 cout << " " << name.substr(i + 1) << "\n";
615 }
616
617 typedef vector<Test*> Tests;
618 typedef Tests::const_iterator Iter;
619
620 const Tests& tests = suite->getTests();
621
622 for (Iter it = tests.begin(); it != tests.end(); ++it)
623 List(*it, name);
624 }
625 else if (m_longlist) {
626 string::size_type i = 0;
627 while ((i = parent.find('.', i + 1)) != string::npos)
628 cout << " ";
629 cout << " " << test->getName() << "\n";
630 }
631 }