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
& e
)
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
) {
147 m_result
= failure
.isError() ? RESULT_ERROR
: RESULT_FAIL
;
150 virtual void endTest(CppUnit::Test
* WXUNUSED(test
))
153 wxPrintf(GetResultStr(m_result
));
155 wxPrintf(" %6d ms", m_watch
.Time());
166 wxString
GetResultStr(ResultType type
) const {
167 static const wxChar
* ResultTypeNames
[] = {
172 wxCHECK_MSG(static_cast<size_t>(type
) < WXSIZEOF(ResultTypeNames
),
173 ResultTypeNames
[RESULT_ERROR
], "invalid entry type");
174 return ResultTypeNames
[type
];
183 typedef wxApp TestAppBase
;
185 typedef wxAppConsole TestAppBase
;
188 // The application class
190 class TestApp
: public TestAppBase
195 // standard overrides
196 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
197 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
198 virtual bool OnInit();
200 virtual int OnExit();
202 // used by events propagation test
203 virtual int FilterEvent(wxEvent
& event
);
204 virtual bool ProcessEvent(wxEvent
& event
);
206 void SetFilterEventFunc(FilterEventFunc f
) { m_filterEventFunc
= f
; }
207 void SetProcessEventFunc(ProcessEventFunc f
) { m_processEventFunc
= f
; }
210 void List(Test
*test
, const string
& parent
= "") const;
212 // command lines options/parameters
217 wxArrayString m_registries
;
219 // event handling hooks
220 FilterEventFunc m_filterEventFunc
;
221 ProcessEventFunc m_processEventFunc
;
224 IMPLEMENT_APP_NO_MAIN(TestApp
)
226 #ifdef wxUSE_VC_CRTDBG
228 static int TestCrtReportHook(int reportType
, char *message
, int *)
230 if ( reportType
!= _CRT_ASSERT
)
233 throw CrtAssertFailure(message
);
236 #endif // wxUSE_VC_CRTDBG
240 static void TestAssertHandler(const wxString
& file
,
242 const wxString
& func
,
243 const wxString
& cond
,
246 throw TestAssertFailure(file
, line
, func
, cond
, msg
);
249 #endif // wxDEBUG_LEVEL
251 int main(int argc
, char **argv
)
253 // tests can be ran non-interactively so make sure we don't show any assert
254 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
255 // prevent them from completing
258 wxSetAssertHandler(TestAssertHandler
);
259 #endif // wxDEBUG_LEVEL
261 #ifdef wxUSE_VC_CRTDBG
262 _CrtSetReportHook(TestCrtReportHook
);
263 #endif // wxUSE_VC_CRTDBG
267 return wxEntry(argc
, argv
);
271 cerr
<< "\n" << GetExceptionMessage() << endl
;
281 m_filterEventFunc
= NULL
;
282 m_processEventFunc
= NULL
;
287 bool TestApp::OnInit()
289 if ( !TestAppBase::OnInit() )
292 cout
<< "Test program for wxWidgets\n"
293 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
296 // create a hidden parent window to be used as parent for the GUI controls
297 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
303 // The table of command line options
305 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
307 TestAppBase::OnInitCmdLine(parser
);
309 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
310 { wxCMD_LINE_SWITCH
, "l", "list",
311 "list the test suites, do not run them",
312 wxCMD_LINE_VAL_NONE
, 0 },
313 { wxCMD_LINE_SWITCH
, "L", "longlist",
314 "list the test cases, do not run them",
315 wxCMD_LINE_VAL_NONE
, 0 },
316 { wxCMD_LINE_SWITCH
, "d", "detail",
317 "print the test case names, run them",
318 wxCMD_LINE_VAL_NONE
, 0 },
319 { wxCMD_LINE_SWITCH
, "t", "timing",
320 "print names and mesure running time of individual test, run them",
321 wxCMD_LINE_VAL_NONE
, 0 },
322 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
323 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
327 parser
.SetDesc(cmdLineDesc
);
330 // Handle command line options
332 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
334 if (parser
.GetParamCount())
335 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
336 m_registries
.push_back(parser
.GetParam(i
));
338 m_registries
.push_back("");
340 m_longlist
= parser
.Found(_T("longlist"));
341 m_list
= m_longlist
|| parser
.Found(_T("list"));
342 m_timing
= parser
.Found(_T("timing"));
343 m_detail
= !m_timing
&& parser
.Found(_T("detail"));
345 return TestAppBase::OnCmdLineParsed(parser
);
349 int TestApp::FilterEvent(wxEvent
& event
)
351 if ( m_filterEventFunc
)
352 return (*m_filterEventFunc
)(event
);
354 return TestAppBase::FilterEvent(event
);
357 bool TestApp::ProcessEvent(wxEvent
& event
)
359 if ( m_processEventFunc
)
360 return (*m_processEventFunc
)(event
);
362 return TestAppBase::ProcessEvent(event
);
365 extern void SetFilterEventFunc(FilterEventFunc func
)
367 wxGetApp().SetFilterEventFunc(func
);
370 extern void SetProcessEventFunc(ProcessEventFunc func
)
372 wxGetApp().SetProcessEventFunc(func
);
379 CppUnit::TextTestRunner runner
;
381 for (size_t i
= 0; i
< m_registries
.size(); i
++)
383 wxString reg
= m_registries
[i
];
384 if (!reg
.empty() && !reg
.EndsWith("TestCase"))
386 // allow the user to specify the name of the testcase "in short form"
387 // (all wx test cases end with TestCase postfix)
389 auto_ptr
<Test
> test(reg
.empty() ?
390 TestFactoryRegistry::getRegistry().makeTest() :
391 TestFactoryRegistry::getRegistry(string(reg
.mb_str())).makeTest());
393 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
395 if (suite
&& suite
->countTestCases() == 0)
396 wxLogError(_T("No such test suite: %s"), reg
);
400 runner
.addTest(test
.release());
406 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
409 // Switch off logging unless --verbose
410 bool verbose
= wxLog::GetVerbose();
411 wxLog::EnableLogging(verbose
);
413 bool verbose
= false;
417 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
418 // in some versions of cppunit: they write progress dots to cout (and not
419 // cerr) and don't flush it so all the dots appear at once at the end which
420 // is not very useful so unbuffer cout to work around this
421 cout
.setf(ios::unitbuf
);
423 // add detail listener if needed
424 DetailListener
detailListener(m_timing
);
425 if ( m_detail
|| m_timing
)
426 runner
.eventManager().addListener(&detailListener
);
428 // finally ensure that we report our own exceptions nicely instead of
429 // giving "uncaught exception of unknown type" messages
430 runner
.eventManager().pushProtector(new wxUnitTestProtector
);
432 bool printProgress
= !(verbose
|| m_detail
|| m_timing
);
433 return runner
.run("", false, true, printProgress
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
436 int TestApp::OnExit()
439 delete GetTopWindow();
447 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
449 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
453 // take the last component of the name and append to the parent
454 name
= test
->getName();
455 string::size_type i
= name
.find_last_of(".:");
456 if (i
!= string::npos
)
457 name
= name
.substr(i
+ 1);
458 name
= parent
+ "." + name
;
460 // drop the 1st component from the display and indent
462 string::size_type j
= i
= name
.find('.', 1);
463 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
465 cout
<< " " << name
.substr(i
+ 1) << "\n";
468 typedef vector
<Test
*> Tests
;
469 typedef Tests::const_iterator Iter
;
471 const Tests
& tests
= suite
->getTests();
473 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
476 else if (m_longlist
) {
477 string::size_type i
= 0;
478 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
480 cout
<< " " << test
->getName() << "\n";