]>
Commit | Line | Data |
---|---|---|
670ec357 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: Test program for wxWidgets | |
4 | // Author: Mike Wetherell | |
670ec357 | 5 | // Copyright: (c) 2004 Mike Wetherell |
526954c5 | 6 | // Licence: wxWindows licence |
670ec357 VS |
7 | /////////////////////////////////////////////////////////////////////////////// |
8 | ||
1f51673b FM |
9 | // ---------------------------------------------------------------------------- |
10 | // headers | |
11 | // ---------------------------------------------------------------------------- | |
12 | ||
8899b155 RN |
13 | // For compilers that support precompilation, includes "wx/wx.h" |
14 | // and "wx/cppunit.h" | |
15 | #include "testprec.h" | |
670ec357 VS |
16 | |
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | // for all others, include the necessary headers | |
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/wx.h" | |
24 | #endif | |
25 | ||
afd90468 | 26 | #include "wx/beforestd.h" |
6d9407fe VZ |
27 | #ifdef __VISUALC__ |
28 | #pragma warning(disable:4100) | |
29 | #endif | |
de830910 | 30 | |
afd90468 | 31 | #include <cppunit/TestListener.h> |
b33e98f0 | 32 | #include <cppunit/Protector.h> |
afd90468 VZ |
33 | #include <cppunit/Test.h> |
34 | #include <cppunit/TestResult.h> | |
29e0c68e | 35 | #include <cppunit/TestFailure.h> |
0dffa805 | 36 | #include <cppunit/TestResultCollector.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" |
33f7fa34 | 44 | #include <exception> |
670ec357 VS |
45 | #include <iostream> |
46 | ||
bb5a9514 | 47 | #ifdef __WINDOWS__ |
b33e98f0 VZ |
48 | #include "wx/msw/msvcrt.h" |
49 | #endif | |
50 | ||
8b7b1a57 SC |
51 | #ifdef __WXOSX__ |
52 | #include "wx/osx/private.h" | |
53 | #endif | |
54 | ||
232fdc63 VZ |
55 | #if wxUSE_GUI |
56 | #include "testableframe.h" | |
57 | #endif | |
58 | ||
1f51673b | 59 | #include "wx/socket.h" |
232fdc63 | 60 | #include "wx/evtloop.h" |
1f51673b | 61 | |
b33e98f0 VZ |
62 | using namespace std; |
63 | ||
3e5f6c1c WS |
64 | using CppUnit::Test; |
65 | using CppUnit::TestSuite; | |
66 | using CppUnit::TestFactoryRegistry; | |
3e5f6c1c | 67 | |
1f51673b FM |
68 | |
69 | // ---------------------------------------------------------------------------- | |
70 | // helper classes | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
b33e98f0 VZ |
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 | ||
884ca4e4 VZ |
87 | #if wxDEBUG_LEVEL |
88 | ||
b5765512 VZ |
89 | // Information about the last not yet handled assertion. |
90 | static wxString s_lastAssertMessage; | |
91 | ||
884ca4e4 VZ |
92 | static wxString FormatAssertMessage(const wxString& file, |
93 | int line, | |
94 | const wxString& func, | |
95 | const wxString& cond, | |
96 | const wxString& msg) | |
97 | { | |
98 | wxString str; | |
99 | str << "wxWidgets assert: " << cond << " failed " | |
100 | "at " << file << ":" << line << " in " << func | |
101 | << " with message '" << msg << "'"; | |
102 | return str; | |
103 | } | |
104 | ||
105 | static void TestAssertHandler(const wxString& file, | |
106 | int line, | |
107 | const wxString& func, | |
108 | const wxString& cond, | |
109 | const wxString& msg) | |
110 | { | |
33f7fa34 VZ |
111 | // Determine whether we can safely throw an exception to just make the test |
112 | // fail or whether we need to abort (in this case "msg" will contain the | |
113 | // explanation why did we decide to do it). | |
114 | wxString abortReason; | |
b5765512 VZ |
115 | |
116 | const wxString | |
117 | assertMessage = FormatAssertMessage(file, line, func, cond, msg); | |
118 | ||
884ca4e4 VZ |
119 | if ( !wxIsMainThread() ) |
120 | { | |
33f7fa34 VZ |
121 | // Exceptions thrown from worker threads are not caught currently and |
122 | // so we'd just die without any useful information -- abort instead. | |
b5765512 | 123 | abortReason << assertMessage << "in a worker thread."; |
33f7fa34 VZ |
124 | } |
125 | else if ( uncaught_exception() ) | |
126 | { | |
127 | // Throwing while already handling an exception would result in | |
128 | // terminate() being called and we wouldn't get any useful information | |
129 | // about why the test failed then. | |
b5765512 VZ |
130 | if ( s_lastAssertMessage.empty() ) |
131 | { | |
132 | abortReason << assertMessage << "while handling an exception"; | |
133 | } | |
134 | else // In this case the exception is due to a previous assert. | |
135 | { | |
136 | abortReason << s_lastAssertMessage << "\n and another " | |
137 | << assertMessage << " while handling it."; | |
138 | } | |
33f7fa34 VZ |
139 | } |
140 | else // Can "safely" throw from here. | |
141 | { | |
b5765512 VZ |
142 | // Remember this in case another assert happens while handling this |
143 | // exception: we want to show the original assert as it's usually more | |
144 | // useful to determine the real root of the problem. | |
145 | s_lastAssertMessage = assertMessage; | |
146 | ||
33f7fa34 | 147 | throw TestAssertFailure(file, line, func, cond, msg); |
884ca4e4 VZ |
148 | } |
149 | ||
b5765512 | 150 | wxFputs(abortReason, stderr); |
33f7fa34 VZ |
151 | fflush(stderr); |
152 | _exit(-1); | |
884ca4e4 VZ |
153 | } |
154 | ||
155 | #endif // wxDEBUG_LEVEL | |
156 | ||
b33e98f0 VZ |
157 | // this function should only be called from a catch clause |
158 | static string GetExceptionMessage() | |
159 | { | |
160 | wxString msg; | |
161 | ||
162 | try | |
163 | { | |
164 | throw; | |
165 | } | |
166 | #if wxDEBUG_LEVEL | |
d01ec096 | 167 | catch ( TestAssertFailure& ) |
b33e98f0 | 168 | { |
b5765512 VZ |
169 | msg = s_lastAssertMessage; |
170 | s_lastAssertMessage.clear(); | |
b33e98f0 VZ |
171 | } |
172 | #endif // wxDEBUG_LEVEL | |
173 | #ifdef wxUSE_VC_CRTDBG | |
174 | catch ( CrtAssertFailure& e ) | |
175 | { | |
176 | msg << "CRT assert failure: " << e.m_msg; | |
177 | } | |
178 | #endif // wxUSE_VC_CRTDBG | |
179 | catch ( std::exception& e ) | |
180 | { | |
181 | msg << "std::exception: " << e.what(); | |
182 | } | |
183 | catch ( ... ) | |
184 | { | |
185 | msg = "Unknown exception caught."; | |
186 | } | |
187 | ||
188 | return string(msg.mb_str()); | |
189 | } | |
190 | ||
191 | // Protector adding handling of wx-specific (this includes MSVC debug CRT in | |
192 | // this context) exceptions | |
193 | class wxUnitTestProtector : public CppUnit::Protector | |
194 | { | |
195 | public: | |
196 | virtual bool protect(const CppUnit::Functor &functor, | |
197 | const CppUnit::ProtectorContext& context) | |
198 | { | |
199 | try | |
200 | { | |
201 | return functor(); | |
202 | } | |
92f8206f | 203 | catch ( std::exception& ) |
9912799c VZ |
204 | { |
205 | // cppunit deals with the standard exceptions itself, let it do as | |
206 | // it output more details (especially for std::exception-derived | |
207 | // CppUnit::Exception) than we do | |
208 | throw; | |
209 | } | |
b33e98f0 VZ |
210 | catch ( ... ) |
211 | { | |
212 | reportError(context, CppUnit::Message("Uncaught exception", | |
213 | GetExceptionMessage())); | |
214 | } | |
215 | ||
216 | return false; | |
217 | } | |
218 | }; | |
afd90468 | 219 | |
6d9407fe VZ |
220 | // Displays the test name before starting to execute it: this helps with |
221 | // diagnosing where exactly does a test crash or hang when/if it does. | |
afd90468 VZ |
222 | class DetailListener : public CppUnit::TestListener |
223 | { | |
224 | public: | |
225 | DetailListener(bool doTiming = false): | |
226 | CppUnit::TestListener(), | |
227 | m_timing(doTiming) | |
228 | { | |
229 | } | |
230 | ||
231 | virtual void startTest(CppUnit::Test *test) | |
232 | { | |
e84d9899 | 233 | printf(" %-60s ", test->getName().c_str()); |
29e0c68e | 234 | m_result = RESULT_OK; |
afd90468 VZ |
235 | m_watch.Start(); |
236 | } | |
237 | ||
62714742 VZ |
238 | virtual void addFailure(const CppUnit::TestFailure& failure) |
239 | { | |
29e0c68e VZ |
240 | m_result = failure.isError() ? RESULT_ERROR : RESULT_FAIL; |
241 | } | |
242 | ||
afd90468 VZ |
243 | virtual void endTest(CppUnit::Test * WXUNUSED(test)) |
244 | { | |
245 | m_watch.Pause(); | |
e84d9899 | 246 | printf("%s", GetResultStr(m_result)); |
29e0c68e | 247 | if (m_timing) |
e84d9899 VZ |
248 | printf(" %6ld ms", m_watch.Time()); |
249 | printf("\n"); | |
afd90468 VZ |
250 | } |
251 | ||
252 | protected : | |
62714742 VZ |
253 | enum ResultType |
254 | { | |
29e0c68e VZ |
255 | RESULT_OK = 0, |
256 | RESULT_FAIL, | |
62714742 VZ |
257 | RESULT_ERROR, |
258 | RESULT_MAX | |
29e0c68e VZ |
259 | }; |
260 | ||
e84d9899 | 261 | const char* GetResultStr(ResultType type) const |
62714742 VZ |
262 | { |
263 | static const char *resultTypeNames[] = | |
264 | { | |
265 | " OK", | |
266 | "FAIL", | |
267 | " ERR" | |
29e0c68e | 268 | }; |
62714742 VZ |
269 | |
270 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames) == RESULT_MAX, | |
271 | ResultTypeNamesMismatch ); | |
272 | ||
273 | return resultTypeNames[type]; | |
29e0c68e VZ |
274 | } |
275 | ||
afd90468 VZ |
276 | bool m_timing; |
277 | wxStopWatch m_watch; | |
29e0c68e | 278 | ResultType m_result; |
afd90468 VZ |
279 | }; |
280 | ||
c0d9b217 VZ |
281 | #if wxUSE_GUI |
282 | typedef wxApp TestAppBase; | |
283 | #else | |
284 | typedef wxAppConsole TestAppBase; | |
285 | #endif | |
286 | ||
670ec357 VS |
287 | // The application class |
288 | // | |
c0d9b217 | 289 | class TestApp : public TestAppBase |
670ec357 VS |
290 | { |
291 | public: | |
292 | TestApp(); | |
293 | ||
294 | // standard overrides | |
c0d9b217 VZ |
295 | virtual void OnInitCmdLine(wxCmdLineParser& parser); |
296 | virtual bool OnCmdLineParsed(wxCmdLineParser& parser); | |
297 | virtual bool OnInit(); | |
c0d9b217 | 298 | virtual int OnExit(); |
670ec357 | 299 | |
1649d288 VZ |
300 | // used by events propagation test |
301 | virtual int FilterEvent(wxEvent& event); | |
302 | virtual bool ProcessEvent(wxEvent& event); | |
303 | ||
304 | void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; } | |
305 | void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; } | |
306 | ||
e77dc839 VZ |
307 | // In console applications we run the tests directly from the overridden |
308 | // OnRun(), but in the GUI ones we run them when we get the first call to | |
309 | // our EVT_IDLE handler to ensure that we do everything from inside the | |
310 | // main event loop. This is especially important under wxOSX/Cocoa where | |
311 | // the main event loop is different from the others but it's also safer to | |
312 | // do it like this in the other ports as we test the GUI code in the same | |
313 | // context as it's used usually, in normal programs, and it might behave | |
314 | // differently without the event loop. | |
315 | #if wxUSE_GUI | |
316 | void OnIdle(wxIdleEvent& event) | |
317 | { | |
318 | if ( m_runTests ) | |
319 | { | |
320 | m_runTests = false; | |
321 | ||
322 | #ifdef __WXOSX__ | |
323 | // we need to wait until the window is activated and fully ready | |
324 | // otherwise no events can be posted | |
325 | wxEventLoopBase* const loop = wxEventLoop::GetActive(); | |
326 | if ( loop ) | |
327 | { | |
328 | loop->DispatchTimeout(1000); | |
329 | loop->Yield(); | |
330 | } | |
331 | #endif // __WXOSX__ | |
332 | ||
333 | m_exitcode = RunTests(); | |
334 | ExitMainLoop(); | |
335 | } | |
336 | ||
337 | event.Skip(); | |
338 | } | |
339 | #else // !wxUSE_GUI | |
340 | virtual int OnRun() | |
341 | { | |
342 | m_exitcode = RunTests(); | |
343 | return m_exitcode; | |
344 | } | |
345 | #endif // wxUSE_GUI/!wxUSE_GUI | |
346 | ||
670ec357 | 347 | private: |
8dae9169 | 348 | void List(Test *test, const string& parent = "") const; |
670ec357 | 349 | |
e992b97f VZ |
350 | // call List() if m_list or runner.addTest() otherwise |
351 | void AddTest(CppUnit::TestRunner& runner, Test *test) | |
352 | { | |
353 | if (m_list) | |
354 | List(test); | |
355 | else | |
356 | runner.addTest(test); | |
357 | } | |
358 | ||
e77dc839 VZ |
359 | int RunTests(); |
360 | ||
361 | // flag telling us whether we should run tests from our EVT_IDLE handler | |
362 | bool m_runTests; | |
363 | ||
670ec357 VS |
364 | // command lines options/parameters |
365 | bool m_list; | |
a81f3066 | 366 | bool m_longlist; |
afd90468 VZ |
367 | bool m_detail; |
368 | bool m_timing; | |
6582f592 | 369 | wxArrayString m_registries; |
ad16130f | 370 | wxLocale *m_locale; |
1649d288 VZ |
371 | |
372 | // event handling hooks | |
373 | FilterEventFunc m_filterEventFunc; | |
374 | ProcessEventFunc m_processEventFunc; | |
e77dc839 VZ |
375 | |
376 | // the program exit code | |
377 | int m_exitcode; | |
670ec357 VS |
378 | }; |
379 | ||
b33e98f0 VZ |
380 | IMPLEMENT_APP_NO_MAIN(TestApp) |
381 | ||
1f51673b FM |
382 | |
383 | // ---------------------------------------------------------------------------- | |
384 | // global functions | |
385 | // ---------------------------------------------------------------------------- | |
386 | ||
b33e98f0 VZ |
387 | #ifdef wxUSE_VC_CRTDBG |
388 | ||
389 | static int TestCrtReportHook(int reportType, char *message, int *) | |
390 | { | |
391 | if ( reportType != _CRT_ASSERT ) | |
392 | return FALSE; | |
393 | ||
394 | throw CrtAssertFailure(message); | |
395 | } | |
396 | ||
397 | #endif // wxUSE_VC_CRTDBG | |
398 | ||
b33e98f0 VZ |
399 | int main(int argc, char **argv) |
400 | { | |
401 | // tests can be ran non-interactively so make sure we don't show any assert | |
402 | // dialog boxes -- neither our own nor from MSVC debug CRT -- which would | |
403 | // prevent them from completing | |
404 | ||
405 | #if wxDEBUG_LEVEL | |
406 | wxSetAssertHandler(TestAssertHandler); | |
407 | #endif // wxDEBUG_LEVEL | |
408 | ||
409 | #ifdef wxUSE_VC_CRTDBG | |
410 | _CrtSetReportHook(TestCrtReportHook); | |
411 | #endif // wxUSE_VC_CRTDBG | |
412 | ||
413 | try | |
414 | { | |
415 | return wxEntry(argc, argv); | |
416 | } | |
417 | catch ( ... ) | |
418 | { | |
419 | cerr << "\n" << GetExceptionMessage() << endl; | |
420 | } | |
421 | ||
422 | return -1; | |
423 | } | |
670ec357 | 424 | |
1f51673b FM |
425 | extern void SetFilterEventFunc(FilterEventFunc func) |
426 | { | |
427 | wxGetApp().SetFilterEventFunc(func); | |
428 | } | |
429 | ||
430 | extern void SetProcessEventFunc(ProcessEventFunc func) | |
431 | { | |
432 | wxGetApp().SetProcessEventFunc(func); | |
433 | } | |
434 | ||
435 | extern bool IsNetworkAvailable() | |
436 | { | |
437 | // NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in | |
438 | // wxCore we use a simple rough test: | |
439 | ||
440 | wxSocketBase::Initialize(); | |
441 | ||
442 | wxIPV4address addr; | |
443 | if (!addr.Hostname("www.google.com") || !addr.Service("www")) | |
444 | { | |
445 | wxSocketBase::Shutdown(); | |
446 | return false; | |
447 | } | |
448 | ||
449 | wxSocketClient sock; | |
5b105c6f | 450 | sock.SetTimeout(10); // 10 secs |
1f51673b FM |
451 | bool online = sock.Connect(addr); |
452 | ||
453 | wxSocketBase::Shutdown(); | |
454 | ||
455 | return online; | |
456 | } | |
457 | ||
f1287154 VZ |
458 | extern bool IsAutomaticTest() |
459 | { | |
460 | static int s_isAutomatic = -1; | |
461 | if ( s_isAutomatic == -1 ) | |
462 | { | |
463 | // Allow setting an environment variable to emulate buildslave user for | |
464 | // testing. | |
465 | wxString username; | |
466 | if ( !wxGetEnv("WX_TEST_USER", &username) ) | |
467 | username = wxGetUserId(); | |
468 | ||
6524d302 VZ |
469 | username.MakeLower(); |
470 | s_isAutomatic = username.Matches("buildslave*") || | |
92318ee1 | 471 | username.Matches("sandbox*"); |
f1287154 VZ |
472 | } |
473 | ||
474 | return s_isAutomatic == 1; | |
475 | } | |
476 | ||
e77dc839 | 477 | // helper of RunTests(): gets the test with the given name, returning NULL (and |
1f51673b FM |
478 | // not an empty test suite) if there is no such test |
479 | static Test *GetTestByName(const wxString& name) | |
480 | { | |
481 | Test * | |
482 | test = TestFactoryRegistry::getRegistry(string(name.mb_str())).makeTest(); | |
483 | if ( test ) | |
484 | { | |
485 | TestSuite * const suite = dynamic_cast<TestSuite *>(test); | |
486 | if ( !suite || !suite->countTestCases() ) | |
487 | { | |
488 | // it's a bogus test, don't use it | |
489 | delete test; | |
490 | test = NULL; | |
491 | } | |
492 | } | |
493 | ||
494 | return test; | |
495 | } | |
496 | ||
497 | ||
498 | // ---------------------------------------------------------------------------- | |
499 | // TestApp | |
500 | // ---------------------------------------------------------------------------- | |
501 | ||
670ec357 | 502 | TestApp::TestApp() |
8dae9169 VS |
503 | : m_list(false), |
504 | m_longlist(false) | |
670ec357 | 505 | { |
e77dc839 VZ |
506 | m_runTests = true; |
507 | ||
1649d288 VZ |
508 | m_filterEventFunc = NULL; |
509 | m_processEventFunc = NULL; | |
ad16130f VZ |
510 | |
511 | m_locale = NULL; | |
e77dc839 VZ |
512 | |
513 | m_exitcode = EXIT_SUCCESS; | |
670ec357 VS |
514 | } |
515 | ||
516 | // Init | |
517 | // | |
518 | bool TestApp::OnInit() | |
519 | { | |
c0d9b217 VZ |
520 | if ( !TestAppBase::OnInit() ) |
521 | return false; | |
522 | ||
9e91447c FM |
523 | #if wxUSE_GUI |
524 | cout << "Test program for wxWidgets GUI features\n" | |
525 | #else | |
526 | cout << "Test program for wxWidgets non-GUI features\n" | |
527 | #endif | |
6524d302 VZ |
528 | << "build: " << WX_BUILD_OPTIONS_SIGNATURE << "\n" |
529 | << "running under " << wxGetOsDescription() | |
530 | << " as " << wxGetUserId() << std::endl; | |
9f7a07ab | 531 | |
c66bfe45 VZ |
532 | if ( m_detail ) |
533 | { | |
534 | // Output some important information about the test environment. | |
535 | cout << "Running under " << wxGetOsDescription() << ", " | |
536 | "locale is " << setlocale(LC_ALL, NULL) << std::endl; | |
537 | } | |
538 | ||
c0d9b217 VZ |
539 | #if wxUSE_GUI |
540 | // create a hidden parent window to be used as parent for the GUI controls | |
232fdc63 VZ |
541 | wxTestableFrame* frame = new wxTestableFrame(); |
542 | frame->Show(); | |
543 | ||
e77dc839 | 544 | Connect(wxEVT_IDLE, wxIdleEventHandler(TestApp::OnIdle)); |
c0d9b217 VZ |
545 | #endif // wxUSE_GUI |
546 | ||
547 | return true; | |
9f10e7c7 | 548 | } |
670ec357 VS |
549 | |
550 | // The table of command line options | |
551 | // | |
552 | void TestApp::OnInitCmdLine(wxCmdLineParser& parser) | |
553 | { | |
c0d9b217 | 554 | TestAppBase::OnInitCmdLine(parser); |
670ec357 VS |
555 | |
556 | static const wxCmdLineEntryDesc cmdLineDesc[] = { | |
aa3b041e VZ |
557 | { wxCMD_LINE_SWITCH, "l", "list", |
558 | "list the test suites, do not run them", | |
a81f3066 | 559 | wxCMD_LINE_VAL_NONE, 0 }, |
aa3b041e VZ |
560 | { wxCMD_LINE_SWITCH, "L", "longlist", |
561 | "list the test cases, do not run them", | |
670ec357 | 562 | wxCMD_LINE_VAL_NONE, 0 }, |
afd90468 VZ |
563 | { wxCMD_LINE_SWITCH, "d", "detail", |
564 | "print the test case names, run them", | |
565 | wxCMD_LINE_VAL_NONE, 0 }, | |
566 | { wxCMD_LINE_SWITCH, "t", "timing", | |
20ba398d | 567 | "print names and measure running time of individual test, run them", |
afd90468 | 568 | wxCMD_LINE_VAL_NONE, 0 }, |
ad16130f VZ |
569 | { wxCMD_LINE_OPTION, "", "locale", |
570 | "locale to use when running the program", | |
571 | wxCMD_LINE_VAL_STRING, 0 }, | |
aa3b041e | 572 | { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING, |
a81f3066 | 573 | wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE }, |
aa3b041e | 574 | wxCMD_LINE_DESC_END |
670ec357 VS |
575 | }; |
576 | ||
577 | parser.SetDesc(cmdLineDesc); | |
578 | } | |
579 | ||
580 | // Handle command line options | |
581 | // | |
582 | bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser) | |
583 | { | |
a81f3066 | 584 | if (parser.GetParamCount()) |
ad16130f | 585 | { |
a81f3066 | 586 | for (size_t i = 0; i < parser.GetParamCount(); i++) |
6582f592 | 587 | m_registries.push_back(parser.GetParam(i)); |
ad16130f | 588 | } |
ad16130f VZ |
589 | |
590 | m_longlist = parser.Found("longlist"); | |
591 | m_list = m_longlist || parser.Found("list"); | |
592 | m_timing = parser.Found("timing"); | |
593 | m_detail = !m_timing && parser.Found("detail"); | |
594 | ||
595 | wxString loc; | |
596 | if ( parser.Found("locale", &loc) ) | |
597 | { | |
598 | const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(loc); | |
599 | if ( !info ) | |
600 | { | |
601 | cerr << "Locale \"" << string(loc.mb_str()) << "\" is unknown.\n"; | |
602 | return false; | |
603 | } | |
3e5f6c1c | 604 | |
ad16130f VZ |
605 | m_locale = new wxLocale(info->Language); |
606 | if ( !m_locale->IsOk() ) | |
607 | { | |
608 | cerr << "Using locale \"" << string(loc.mb_str()) << "\" failed.\n"; | |
609 | return false; | |
610 | } | |
611 | } | |
670ec357 | 612 | |
c0d9b217 | 613 | return TestAppBase::OnCmdLineParsed(parser); |
670ec357 VS |
614 | } |
615 | ||
1649d288 VZ |
616 | // Event handling |
617 | int TestApp::FilterEvent(wxEvent& event) | |
618 | { | |
619 | if ( m_filterEventFunc ) | |
620 | return (*m_filterEventFunc)(event); | |
621 | ||
622 | return TestAppBase::FilterEvent(event); | |
623 | } | |
624 | ||
625 | bool TestApp::ProcessEvent(wxEvent& event) | |
626 | { | |
627 | if ( m_processEventFunc ) | |
628 | return (*m_processEventFunc)(event); | |
629 | ||
630 | return TestAppBase::ProcessEvent(event); | |
631 | } | |
632 | ||
670ec357 VS |
633 | // Run |
634 | // | |
e77dc839 | 635 | int TestApp::RunTests() |
670ec357 | 636 | { |
875f82b1 VZ |
637 | #if wxUSE_LOG |
638 | // Switch off logging unless --verbose | |
639 | bool verbose = wxLog::GetVerbose(); | |
640 | wxLog::EnableLogging(verbose); | |
641 | #else | |
642 | bool verbose = false; | |
643 | #endif | |
644 | ||
2976d6cb | 645 | CppUnit::TextTestRunner runner; |
a81f3066 | 646 | |
e992b97f | 647 | if ( m_registries.empty() ) |
6d423df0 | 648 | { |
20ba398d FM |
649 | // run or list all tests which use the CPPUNIT_TEST_SUITE_REGISTRATION() macro |
650 | // (i.e. those registered in the "All tests" registry); if there are other | |
651 | // tests not registered with the CPPUNIT_TEST_SUITE_REGISTRATION() macro | |
652 | // then they won't be listed/run! | |
e992b97f | 653 | AddTest(runner, TestFactoryRegistry::getRegistry().makeTest()); |
20ba398d FM |
654 | |
655 | if (m_list) | |
656 | { | |
657 | cout << "\nNote that the list above is not complete as it doesn't include the \n"; | |
658 | cout << "tests disabled by default.\n"; | |
659 | } | |
e992b97f VZ |
660 | } |
661 | else // run only the selected tests | |
662 | { | |
663 | for (size_t i = 0; i < m_registries.size(); i++) | |
d73a5209 | 664 | { |
e992b97f VZ |
665 | const wxString reg = m_registries[i]; |
666 | Test *test = GetTestByName(reg); | |
d73a5209 VZ |
667 | |
668 | if ( !test && !reg.EndsWith("TestCase") ) | |
669 | { | |
670 | test = GetTestByName(reg + "TestCase"); | |
671 | } | |
672 | ||
673 | if ( !test ) | |
674 | { | |
675 | cerr << "No such test suite: " << string(reg.mb_str()) << endl; | |
676 | return 2; | |
677 | } | |
a81f3066 | 678 | |
e992b97f VZ |
679 | AddTest(runner, test); |
680 | } | |
670ec357 | 681 | } |
a81f3066 | 682 | |
2976d6cb | 683 | if ( m_list ) |
1dd8319a | 684 | return EXIT_SUCCESS; |
2976d6cb VZ |
685 | |
686 | runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout)); | |
14dc53b2 | 687 | |
2976d6cb VZ |
688 | // there is a bug |
689 | // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795) | |
690 | // in some versions of cppunit: they write progress dots to cout (and not | |
691 | // cerr) and don't flush it so all the dots appear at once at the end which | |
692 | // is not very useful so unbuffer cout to work around this | |
693 | cout.setf(ios::unitbuf); | |
694 | ||
afd90468 VZ |
695 | // add detail listener if needed |
696 | DetailListener detailListener(m_timing); | |
697 | if ( m_detail || m_timing ) | |
698 | runner.eventManager().addListener(&detailListener); | |
699 | ||
b33e98f0 VZ |
700 | // finally ensure that we report our own exceptions nicely instead of |
701 | // giving "uncaught exception of unknown type" messages | |
702 | runner.eventManager().pushProtector(new wxUnitTestProtector); | |
703 | ||
29e0c68e | 704 | bool printProgress = !(verbose || m_detail || m_timing); |
0dffa805 MW |
705 | runner.run("", false, true, printProgress); |
706 | ||
707 | return runner.result().testFailures() == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
670ec357 VS |
708 | } |
709 | ||
c0d9b217 VZ |
710 | int TestApp::OnExit() |
711 | { | |
ad16130f VZ |
712 | delete m_locale; |
713 | ||
c0d9b217 VZ |
714 | #if wxUSE_GUI |
715 | delete GetTopWindow(); | |
716 | #endif // wxUSE_GUI | |
717 | ||
e77dc839 | 718 | return m_exitcode; |
c0d9b217 VZ |
719 | } |
720 | ||
670ec357 VS |
721 | // List the tests |
722 | // | |
8dae9169 | 723 | void TestApp::List(Test *test, const string& parent /*=""*/) const |
670ec357 | 724 | { |
670ec357 | 725 | TestSuite *suite = dynamic_cast<TestSuite*>(test); |
8dae9169 VS |
726 | string name; |
727 | ||
bc10103e | 728 | if (suite) { |
8dae9169 VS |
729 | // take the last component of the name and append to the parent |
730 | name = test->getName(); | |
731 | string::size_type i = name.find_last_of(".:"); | |
f44eaed6 RN |
732 | if (i != string::npos) |
733 | name = name.substr(i + 1); | |
734 | name = parent + "." + name; | |
8dae9169 VS |
735 | |
736 | // drop the 1st component from the display and indent | |
737 | if (parent != "") { | |
738 | string::size_type j = i = name.find('.', 1); | |
739 | while ((j = name.find('.', j + 1)) != string::npos) | |
740 | cout << " "; | |
741 | cout << " " << name.substr(i + 1) << "\n"; | |
742 | } | |
a81f3066 | 743 | |
f69577be | 744 | typedef vector<Test*> Tests; |
670ec357 VS |
745 | typedef Tests::const_iterator Iter; |
746 | ||
f69577be | 747 | const Tests& tests = suite->getTests(); |
670ec357 VS |
748 | |
749 | for (Iter it = tests.begin(); it != tests.end(); ++it) | |
8dae9169 | 750 | List(*it, name); |
670ec357 | 751 | } |
bc10103e VS |
752 | else if (m_longlist) { |
753 | string::size_type i = 0; | |
754 | while ((i = parent.find('.', i + 1)) != string::npos) | |
755 | cout << " "; | |
756 | cout << " " << test->getName() << "\n"; | |
757 | } | |
670ec357 | 758 | } |