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 "wx/afterstd.h"
36 #include "wx/cmdline.h"
40 #include "wx/msw/msvcrt.h"
46 using CppUnit::TestSuite
;
47 using CppUnit::TestFactoryRegistry
;
49 // exception class for MSVC debug CRT assertion failures
50 #ifdef wxUSE_VC_CRTDBG
52 struct CrtAssertFailure
54 CrtAssertFailure(const char *message
) : m_msg(message
) { }
58 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure
);
61 #endif // wxUSE_VC_CRTDBG
63 // this function should only be called from a catch clause
64 static string
GetExceptionMessage()
73 catch ( TestAssertFailure
& e
)
75 msg
<< "wxWidgets assert: " << e
.m_cond
<< " failed "
76 "at " << e
.m_file
<< ":" << e
.m_line
<< " in " << e
.m_func
77 << " with message " << e
.m_msg
;
79 #endif // wxDEBUG_LEVEL
80 #ifdef wxUSE_VC_CRTDBG
81 catch ( CrtAssertFailure
& e
)
83 msg
<< "CRT assert failure: " << e
.m_msg
;
85 #endif // wxUSE_VC_CRTDBG
86 catch ( std::exception
& e
)
88 msg
<< "std::exception: " << e
.what();
92 msg
= "Unknown exception caught.";
95 return string(msg
.mb_str());
98 // Protector adding handling of wx-specific (this includes MSVC debug CRT in
99 // this context) exceptions
100 class wxUnitTestProtector
: public CppUnit::Protector
103 virtual bool protect(const CppUnit::Functor
&functor
,
104 const CppUnit::ProtectorContext
& context
)
110 catch ( std::exception
& e
)
112 // cppunit deals with the standard exceptions itself, let it do as
113 // it output more details (especially for std::exception-derived
114 // CppUnit::Exception) than we do
119 reportError(context
, CppUnit::Message("Uncaught exception",
120 GetExceptionMessage()));
127 // Displays the test name before starting to execute it: this helps with
128 // diagnosing where exactly does a test crash or hang when/if it does.
129 class DetailListener
: public CppUnit::TestListener
132 DetailListener(bool doTiming
= false):
133 CppUnit::TestListener(),
138 virtual void startTest(CppUnit::Test
*test
)
140 std::cout
<< test
->getName () << " ";
144 virtual void endTest(CppUnit::Test
* WXUNUSED(test
))
148 std::cout
<< " (in "<< m_watch
.Time() << " ms )";
158 typedef wxApp TestAppBase
;
160 typedef wxAppConsole TestAppBase
;
163 // The application class
165 class TestApp
: public TestAppBase
170 // standard overrides
171 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
172 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
173 virtual bool OnInit();
175 virtual int OnExit();
177 // used by events propagation test
178 virtual int FilterEvent(wxEvent
& event
);
179 virtual bool ProcessEvent(wxEvent
& event
);
181 void SetFilterEventFunc(FilterEventFunc f
) { m_filterEventFunc
= f
; }
182 void SetProcessEventFunc(ProcessEventFunc f
) { m_processEventFunc
= f
; }
185 void List(Test
*test
, const string
& parent
= "") const;
187 // command lines options/parameters
192 wxArrayString m_registries
;
194 // event handling hooks
195 FilterEventFunc m_filterEventFunc
;
196 ProcessEventFunc m_processEventFunc
;
199 IMPLEMENT_APP_NO_MAIN(TestApp
)
201 #ifdef wxUSE_VC_CRTDBG
203 static int TestCrtReportHook(int reportType
, char *message
, int *)
205 if ( reportType
!= _CRT_ASSERT
)
208 throw CrtAssertFailure(message
);
211 #endif // wxUSE_VC_CRTDBG
215 static void TestAssertHandler(const wxString
& file
,
217 const wxString
& func
,
218 const wxString
& cond
,
221 throw TestAssertFailure(file
, line
, func
, cond
, msg
);
224 #endif // wxDEBUG_LEVEL
226 int main(int argc
, char **argv
)
228 // tests can be ran non-interactively so make sure we don't show any assert
229 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
230 // prevent them from completing
233 wxSetAssertHandler(TestAssertHandler
);
234 #endif // wxDEBUG_LEVEL
236 #ifdef wxUSE_VC_CRTDBG
237 _CrtSetReportHook(TestCrtReportHook
);
238 #endif // wxUSE_VC_CRTDBG
242 return wxEntry(argc
, argv
);
246 cerr
<< "\n" << GetExceptionMessage() << endl
;
256 m_filterEventFunc
= NULL
;
257 m_processEventFunc
= NULL
;
262 bool TestApp::OnInit()
264 if ( !TestAppBase::OnInit() )
267 cout
<< "Test program for wxWidgets\n"
268 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
271 // create a hidden parent window to be used as parent for the GUI controls
272 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
278 // The table of command line options
280 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
282 TestAppBase::OnInitCmdLine(parser
);
284 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
285 { wxCMD_LINE_SWITCH
, "l", "list",
286 "list the test suites, do not run them",
287 wxCMD_LINE_VAL_NONE
, 0 },
288 { wxCMD_LINE_SWITCH
, "L", "longlist",
289 "list the test cases, do not run them",
290 wxCMD_LINE_VAL_NONE
, 0 },
291 { wxCMD_LINE_SWITCH
, "d", "detail",
292 "print the test case names, run them",
293 wxCMD_LINE_VAL_NONE
, 0 },
294 { wxCMD_LINE_SWITCH
, "t", "timing",
295 "print names and mesure running time of individual test, run them",
296 wxCMD_LINE_VAL_NONE
, 0 },
297 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
298 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
302 parser
.SetDesc(cmdLineDesc
);
305 // Handle command line options
307 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
309 if (parser
.GetParamCount())
310 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
311 m_registries
.push_back(parser
.GetParam(i
));
313 m_registries
.push_back("");
315 m_longlist
= parser
.Found(_T("longlist"));
316 m_list
= m_longlist
|| parser
.Found(_T("list"));
317 m_timing
= parser
.Found(_T("timing"));
318 m_detail
= !m_timing
&& parser
.Found(_T("detail"));
320 return TestAppBase::OnCmdLineParsed(parser
);
324 int TestApp::FilterEvent(wxEvent
& event
)
326 if ( m_filterEventFunc
)
327 return (*m_filterEventFunc
)(event
);
329 return TestAppBase::FilterEvent(event
);
332 bool TestApp::ProcessEvent(wxEvent
& event
)
334 if ( m_processEventFunc
)
335 return (*m_processEventFunc
)(event
);
337 return TestAppBase::ProcessEvent(event
);
340 extern void SetFilterEventFunc(FilterEventFunc func
)
342 wxGetApp().SetFilterEventFunc(func
);
345 extern void SetProcessEventFunc(ProcessEventFunc func
)
347 wxGetApp().SetProcessEventFunc(func
);
354 CppUnit::TextTestRunner runner
;
356 for (size_t i
= 0; i
< m_registries
.size(); i
++)
358 wxString reg
= m_registries
[i
];
359 if (!reg
.empty() && !reg
.EndsWith("TestCase"))
361 // allow the user to specify the name of the testcase "in short form"
362 // (all wx test cases end with TestCase postfix)
364 auto_ptr
<Test
> test(reg
.empty() ?
365 TestFactoryRegistry::getRegistry().makeTest() :
366 TestFactoryRegistry::getRegistry(string(reg
.mb_str())).makeTest());
368 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
370 if (suite
&& suite
->countTestCases() == 0)
371 wxLogError(_T("No such test suite: %s"), reg
);
375 runner
.addTest(test
.release());
381 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
384 // Switch off logging unless --verbose
385 bool verbose
= wxLog::GetVerbose();
386 wxLog::EnableLogging(verbose
);
388 bool verbose
= false;
392 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
393 // in some versions of cppunit: they write progress dots to cout (and not
394 // cerr) and don't flush it so all the dots appear at once at the end which
395 // is not very useful so unbuffer cout to work around this
396 cout
.setf(ios::unitbuf
);
398 // add detail listener if needed
399 DetailListener
detailListener(m_timing
);
400 if ( m_detail
|| m_timing
)
401 runner
.eventManager().addListener(&detailListener
);
403 // finally ensure that we report our own exceptions nicely instead of
404 // giving "uncaught exception of unknown type" messages
405 runner
.eventManager().pushProtector(new wxUnitTestProtector
);
407 return runner
.run("", false, true, !verbose
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
410 int TestApp::OnExit()
413 delete GetTopWindow();
421 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
423 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
427 // take the last component of the name and append to the parent
428 name
= test
->getName();
429 string::size_type i
= name
.find_last_of(".:");
430 if (i
!= string::npos
)
431 name
= name
.substr(i
+ 1);
432 name
= parent
+ "." + name
;
434 // drop the 1st component from the display and indent
436 string::size_type j
= i
= name
.find('.', 1);
437 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
439 cout
<< " " << name
.substr(i
+ 1) << "\n";
442 typedef vector
<Test
*> Tests
;
443 typedef Tests::const_iterator Iter
;
445 const Tests
& tests
= suite
->getTests();
447 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
450 else if (m_longlist
) {
451 string::size_type i
= 0;
452 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
454 cout
<< " " << test
->getName() << "\n";