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