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