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