1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Test program for wxWidgets
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h"
18 // for all others, include the necessary headers
23 #include "wx/beforestd.h"
25 #pragma warning(disable:4100)
27 #include <cppunit/TestListener.h>
29 #pragma warning(default:4100)
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"
37 #include "wx/cmdline.h"
41 #include "wx/msw/msvcrt.h"
47 using CppUnit::TestSuite
;
48 using CppUnit::TestFactoryRegistry
;
50 // exception class for MSVC debug CRT assertion failures
51 #ifdef wxUSE_VC_CRTDBG
53 struct CrtAssertFailure
55 CrtAssertFailure(const char *message
) : m_msg(message
) { }
59 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure
);
62 #endif // wxUSE_VC_CRTDBG
64 // this function should only be called from a catch clause
65 static string
GetExceptionMessage()
74 catch ( TestAssertFailure
& e
)
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
;
80 #endif // wxDEBUG_LEVEL
81 #ifdef wxUSE_VC_CRTDBG
82 catch ( CrtAssertFailure
& e
)
84 msg
<< "CRT assert failure: " << e
.m_msg
;
86 #endif // wxUSE_VC_CRTDBG
87 catch ( std::exception
& e
)
89 msg
<< "std::exception: " << e
.what();
93 msg
= "Unknown exception caught.";
96 return string(msg
.mb_str());
99 // Protector adding handling of wx-specific (this includes MSVC debug CRT in
100 // this context) exceptions
101 class wxUnitTestProtector
: public CppUnit::Protector
104 virtual bool protect(const CppUnit::Functor
&functor
,
105 const CppUnit::ProtectorContext
& context
)
111 catch ( std::exception
& )
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
120 reportError(context
, CppUnit::Message("Uncaught exception",
121 GetExceptionMessage()));
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.
130 class DetailListener
: public CppUnit::TestListener
133 DetailListener(bool doTiming
= false):
134 CppUnit::TestListener(),
139 virtual void startTest(CppUnit::Test
*test
)
141 wxPrintf(" %-60s ", test
->getName());
142 m_result
= RESULT_OK
;
146 virtual void addFailure(const CppUnit::TestFailure
& failure
)
148 m_result
= failure
.isError() ? RESULT_ERROR
: RESULT_FAIL
;
151 virtual void endTest(CppUnit::Test
* WXUNUSED(test
))
154 wxPrintf(GetResultStr(m_result
));
156 wxPrintf(" %6d ms", m_watch
.Time());
169 wxString
GetResultStr(ResultType type
) const
171 static const char *resultTypeNames
[] =
178 wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames
) == RESULT_MAX
,
179 ResultTypeNamesMismatch
);
181 return resultTypeNames
[type
];
190 typedef wxApp TestAppBase
;
192 typedef wxAppConsole TestAppBase
;
195 // The application class
197 class TestApp
: public TestAppBase
202 // standard overrides
203 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
204 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
205 virtual bool OnInit();
207 virtual int OnExit();
209 // used by events propagation test
210 virtual int FilterEvent(wxEvent
& event
);
211 virtual bool ProcessEvent(wxEvent
& event
);
213 void SetFilterEventFunc(FilterEventFunc f
) { m_filterEventFunc
= f
; }
214 void SetProcessEventFunc(ProcessEventFunc f
) { m_processEventFunc
= f
; }
217 void List(Test
*test
, const string
& parent
= "") const;
219 // command lines options/parameters
224 wxArrayString m_registries
;
227 // event handling hooks
228 FilterEventFunc m_filterEventFunc
;
229 ProcessEventFunc m_processEventFunc
;
232 IMPLEMENT_APP_NO_MAIN(TestApp
)
234 #ifdef wxUSE_VC_CRTDBG
236 static int TestCrtReportHook(int reportType
, char *message
, int *)
238 if ( reportType
!= _CRT_ASSERT
)
241 throw CrtAssertFailure(message
);
244 #endif // wxUSE_VC_CRTDBG
248 static void TestAssertHandler(const wxString
& file
,
250 const wxString
& func
,
251 const wxString
& cond
,
254 throw TestAssertFailure(file
, line
, func
, cond
, msg
);
257 #endif // wxDEBUG_LEVEL
259 int main(int argc
, char **argv
)
261 // tests can be ran non-interactively so make sure we don't show any assert
262 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
263 // prevent them from completing
266 wxSetAssertHandler(TestAssertHandler
);
267 #endif // wxDEBUG_LEVEL
269 #ifdef wxUSE_VC_CRTDBG
270 _CrtSetReportHook(TestCrtReportHook
);
271 #endif // wxUSE_VC_CRTDBG
275 return wxEntry(argc
, argv
);
279 cerr
<< "\n" << GetExceptionMessage() << endl
;
289 m_filterEventFunc
= NULL
;
290 m_processEventFunc
= NULL
;
297 bool TestApp::OnInit()
299 if ( !TestAppBase::OnInit() )
302 cout
<< "Test program for wxWidgets\n"
303 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
306 // create a hidden parent window to be used as parent for the GUI controls
307 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
313 // The table of command line options
315 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
317 TestAppBase::OnInitCmdLine(parser
);
319 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
320 { wxCMD_LINE_SWITCH
, "l", "list",
321 "list the test suites, do not run them",
322 wxCMD_LINE_VAL_NONE
, 0 },
323 { wxCMD_LINE_SWITCH
, "L", "longlist",
324 "list the test cases, do not run them",
325 wxCMD_LINE_VAL_NONE
, 0 },
326 { wxCMD_LINE_SWITCH
, "d", "detail",
327 "print the test case names, run them",
328 wxCMD_LINE_VAL_NONE
, 0 },
329 { wxCMD_LINE_SWITCH
, "t", "timing",
330 "print names and mesure running time of individual test, run them",
331 wxCMD_LINE_VAL_NONE
, 0 },
332 { wxCMD_LINE_OPTION
, "", "locale",
333 "locale to use when running the program",
334 wxCMD_LINE_VAL_STRING
, 0 },
335 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
336 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
340 parser
.SetDesc(cmdLineDesc
);
343 // Handle command line options
345 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
347 if (parser
.GetParamCount())
349 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
350 m_registries
.push_back(parser
.GetParam(i
));
354 // FIXME: this is an ugly and unnecessary hack
355 m_registries
.push_back("");
358 m_longlist
= parser
.Found("longlist");
359 m_list
= m_longlist
|| parser
.Found("list");
360 m_timing
= parser
.Found("timing");
361 m_detail
= !m_timing
&& parser
.Found("detail");
364 if ( parser
.Found("locale", &loc
) )
366 const wxLanguageInfo
* const info
= wxLocale::FindLanguageInfo(loc
);
369 cerr
<< "Locale \"" << string(loc
.mb_str()) << "\" is unknown.\n";
373 m_locale
= new wxLocale(info
->Language
);
374 if ( !m_locale
->IsOk() )
376 cerr
<< "Using locale \"" << string(loc
.mb_str()) << "\" failed.\n";
381 return TestAppBase::OnCmdLineParsed(parser
);
385 int TestApp::FilterEvent(wxEvent
& event
)
387 if ( m_filterEventFunc
)
388 return (*m_filterEventFunc
)(event
);
390 return TestAppBase::FilterEvent(event
);
393 bool TestApp::ProcessEvent(wxEvent
& event
)
395 if ( m_processEventFunc
)
396 return (*m_processEventFunc
)(event
);
398 return TestAppBase::ProcessEvent(event
);
401 extern void SetFilterEventFunc(FilterEventFunc func
)
403 wxGetApp().SetFilterEventFunc(func
);
406 extern void SetProcessEventFunc(ProcessEventFunc func
)
408 wxGetApp().SetProcessEventFunc(func
);
411 // helper of OnRun(): gets the test with the given name, returning NULL (and
412 // not an empty test suite) if there is no such test
413 static Test
*GetTestByName(const wxString
& name
)
416 test
= TestFactoryRegistry::getRegistry(string(name
.mb_str())).makeTest();
419 TestSuite
* const suite
= dynamic_cast<TestSuite
*>(test
);
420 if ( !suite
|| !suite
->countTestCases() )
422 // it's a bogus test, don't use it
436 // Switch off logging unless --verbose
437 bool verbose
= wxLog::GetVerbose();
438 wxLog::EnableLogging(verbose
);
440 bool verbose
= false;
443 CppUnit::TextTestRunner runner
;
445 for (size_t i
= 0; i
< m_registries
.size(); i
++)
449 wxString reg
= m_registries
[i
];
452 // no test name, run all the tests
453 test
= TestFactoryRegistry::getRegistry().makeTest();
455 else // test name specified, run just this test
457 test
= GetTestByName(reg
);
459 if ( !test
&& !reg
.EndsWith("TestCase") )
461 test
= GetTestByName(reg
+ "TestCase");
466 cerr
<< "No such test suite: " << string(reg
.mb_str()) << endl
;
474 runner
.addTest(test
);
480 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
483 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
484 // in some versions of cppunit: they write progress dots to cout (and not
485 // cerr) and don't flush it so all the dots appear at once at the end which
486 // is not very useful so unbuffer cout to work around this
487 cout
.setf(ios::unitbuf
);
489 // add detail listener if needed
490 DetailListener
detailListener(m_timing
);
491 if ( m_detail
|| m_timing
)
492 runner
.eventManager().addListener(&detailListener
);
494 // finally ensure that we report our own exceptions nicely instead of
495 // giving "uncaught exception of unknown type" messages
496 runner
.eventManager().pushProtector(new wxUnitTestProtector
);
498 bool printProgress
= !(verbose
|| m_detail
|| m_timing
);
499 return runner
.run("", false, true, printProgress
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
502 int TestApp::OnExit()
507 delete GetTopWindow();
515 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
517 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
521 // take the last component of the name and append to the parent
522 name
= test
->getName();
523 string::size_type i
= name
.find_last_of(".:");
524 if (i
!= string::npos
)
525 name
= name
.substr(i
+ 1);
526 name
= parent
+ "." + name
;
528 // drop the 1st component from the display and indent
530 string::size_type j
= i
= name
.find('.', 1);
531 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
533 cout
<< " " << name
.substr(i
+ 1) << "\n";
536 typedef vector
<Test
*> Tests
;
537 typedef Tests::const_iterator Iter
;
539 const Tests
& tests
= suite
->getTests();
541 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
544 else if (m_longlist
) {
545 string::size_type i
= 0;
546 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
548 cout
<< " " << test
->getName() << "\n";