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
)
112 reportError(context
, CppUnit::Message("Uncaught exception",
113 GetExceptionMessage()));
120 // Displays the test name before starting to execute it: this helps with
121 // diagnosing where exactly does a test crash or hang when/if it does.
122 class DetailListener
: public CppUnit::TestListener
125 DetailListener(bool doTiming
= false):
126 CppUnit::TestListener(),
131 virtual void startTest(CppUnit::Test
*test
)
133 std::cout
<< test
->getName () << " ";
137 virtual void endTest(CppUnit::Test
* WXUNUSED(test
))
141 std::cout
<< " (in "<< m_watch
.Time() << " ms )";
151 typedef wxApp TestAppBase
;
153 typedef wxAppConsole TestAppBase
;
156 // The application class
158 class TestApp
: public TestAppBase
163 // standard overrides
164 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
165 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
166 virtual bool OnInit();
168 virtual int OnExit();
170 // used by events propagation test
171 virtual int FilterEvent(wxEvent
& event
);
172 virtual bool ProcessEvent(wxEvent
& event
);
174 void SetFilterEventFunc(FilterEventFunc f
) { m_filterEventFunc
= f
; }
175 void SetProcessEventFunc(ProcessEventFunc f
) { m_processEventFunc
= f
; }
178 void List(Test
*test
, const string
& parent
= "") const;
180 // command lines options/parameters
185 wxArrayString m_registries
;
187 // event handling hooks
188 FilterEventFunc m_filterEventFunc
;
189 ProcessEventFunc m_processEventFunc
;
192 IMPLEMENT_APP_NO_MAIN(TestApp
)
194 #ifdef wxUSE_VC_CRTDBG
196 static int TestCrtReportHook(int reportType
, char *message
, int *)
198 if ( reportType
!= _CRT_ASSERT
)
201 throw CrtAssertFailure(message
);
204 #endif // wxUSE_VC_CRTDBG
208 static void TestAssertHandler(const wxString
& file
,
210 const wxString
& func
,
211 const wxString
& cond
,
214 throw TestAssertFailure(file
, line
, func
, cond
, msg
);
217 #endif // wxDEBUG_LEVEL
219 int main(int argc
, char **argv
)
221 // tests can be ran non-interactively so make sure we don't show any assert
222 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
223 // prevent them from completing
226 wxSetAssertHandler(TestAssertHandler
);
227 #endif // wxDEBUG_LEVEL
229 #ifdef wxUSE_VC_CRTDBG
230 _CrtSetReportHook(TestCrtReportHook
);
231 #endif // wxUSE_VC_CRTDBG
235 return wxEntry(argc
, argv
);
239 cerr
<< "\n" << GetExceptionMessage() << endl
;
249 m_filterEventFunc
= NULL
;
250 m_processEventFunc
= NULL
;
255 bool TestApp::OnInit()
257 if ( !TestAppBase::OnInit() )
260 cout
<< "Test program for wxWidgets\n"
261 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
264 // create a hidden parent window to be used as parent for the GUI controls
265 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
271 // The table of command line options
273 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
275 TestAppBase::OnInitCmdLine(parser
);
277 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
278 { wxCMD_LINE_SWITCH
, "l", "list",
279 "list the test suites, do not run them",
280 wxCMD_LINE_VAL_NONE
, 0 },
281 { wxCMD_LINE_SWITCH
, "L", "longlist",
282 "list the test cases, do not run them",
283 wxCMD_LINE_VAL_NONE
, 0 },
284 { wxCMD_LINE_SWITCH
, "d", "detail",
285 "print the test case names, run them",
286 wxCMD_LINE_VAL_NONE
, 0 },
287 { wxCMD_LINE_SWITCH
, "t", "timing",
288 "print names and mesure running time of individual test, run them",
289 wxCMD_LINE_VAL_NONE
, 0 },
290 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
291 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
295 parser
.SetDesc(cmdLineDesc
);
298 // Handle command line options
300 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
302 if (parser
.GetParamCount())
303 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
304 m_registries
.push_back(parser
.GetParam(i
));
306 m_registries
.push_back("");
308 m_longlist
= parser
.Found(_T("longlist"));
309 m_list
= m_longlist
|| parser
.Found(_T("list"));
310 m_timing
= parser
.Found(_T("timing"));
311 m_detail
= !m_timing
&& parser
.Found(_T("detail"));
313 return TestAppBase::OnCmdLineParsed(parser
);
317 int TestApp::FilterEvent(wxEvent
& event
)
319 if ( m_filterEventFunc
)
320 return (*m_filterEventFunc
)(event
);
322 return TestAppBase::FilterEvent(event
);
325 bool TestApp::ProcessEvent(wxEvent
& event
)
327 if ( m_processEventFunc
)
328 return (*m_processEventFunc
)(event
);
330 return TestAppBase::ProcessEvent(event
);
333 extern void SetFilterEventFunc(FilterEventFunc func
)
335 wxGetApp().SetFilterEventFunc(func
);
338 extern void SetProcessEventFunc(ProcessEventFunc func
)
340 wxGetApp().SetProcessEventFunc(func
);
347 CppUnit::TextTestRunner runner
;
349 for (size_t i
= 0; i
< m_registries
.size(); i
++)
351 wxString reg
= m_registries
[i
];
352 if (!reg
.empty() && !reg
.EndsWith("TestCase"))
354 // allow the user to specify the name of the testcase "in short form"
355 // (all wx test cases end with TestCase postfix)
357 auto_ptr
<Test
> test(reg
.empty() ?
358 TestFactoryRegistry::getRegistry().makeTest() :
359 TestFactoryRegistry::getRegistry(string(reg
.mb_str())).makeTest());
361 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
363 if (suite
&& suite
->countTestCases() == 0)
364 wxLogError(_T("No such test suite: %s"), reg
);
368 runner
.addTest(test
.release());
374 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
377 // Switch off logging unless --verbose
378 bool verbose
= wxLog::GetVerbose();
379 wxLog::EnableLogging(verbose
);
381 bool verbose
= false;
385 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
386 // in some versions of cppunit: they write progress dots to cout (and not
387 // cerr) and don't flush it so all the dots appear at once at the end which
388 // is not very useful so unbuffer cout to work around this
389 cout
.setf(ios::unitbuf
);
391 // add detail listener if needed
392 DetailListener
detailListener(m_timing
);
393 if ( m_detail
|| m_timing
)
394 runner
.eventManager().addListener(&detailListener
);
396 // finally ensure that we report our own exceptions nicely instead of
397 // giving "uncaught exception of unknown type" messages
398 runner
.eventManager().pushProtector(new wxUnitTestProtector
);
400 return runner
.run("", false, true, !verbose
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
403 int TestApp::OnExit()
406 delete GetTopWindow();
414 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
416 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
420 // take the last component of the name and append to the parent
421 name
= test
->getName();
422 string::size_type i
= name
.find_last_of(".:");
423 if (i
!= string::npos
)
424 name
= name
.substr(i
+ 1);
425 name
= parent
+ "." + name
;
427 // drop the 1st component from the display and indent
429 string::size_type j
= i
= name
.find('.', 1);
430 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
432 cout
<< " " << name
.substr(i
+ 1) << "\n";
435 typedef vector
<Test
*> Tests
;
436 typedef Tests::const_iterator Iter
;
438 const Tests
& tests
= suite
->getTests();
440 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
443 else if (m_longlist
) {
444 string::size_type i
= 0;
445 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
447 cout
<< " " << test
->getName() << "\n";