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/Test.h>
32 #include <cppunit/TestResult.h>
33 #include "wx/afterstd.h"
35 #include "wx/cmdline.h"
39 using CppUnit::TestSuite
;
40 using CppUnit::TestFactoryRegistry
;
43 // Displays the test name before starting to execute it: this helps with
44 // diagnosing where exactly does a test crash or hang when/if it does.
45 class DetailListener
: public CppUnit::TestListener
48 DetailListener(bool doTiming
= false):
49 CppUnit::TestListener(),
54 virtual void startTest(CppUnit::Test
*test
)
56 std::cout
<< test
->getName () << " ";
60 virtual void endTest(CppUnit::Test
* WXUNUSED(test
))
64 std::cout
<< " (in "<< m_watch
.Time() << " ms )";
76 typedef wxApp TestAppBase
;
78 typedef wxAppConsole TestAppBase
;
81 // The application class
83 class TestApp
: public TestAppBase
89 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
90 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
91 virtual bool OnInit();
95 // used by events propagation test
96 virtual int FilterEvent(wxEvent
& event
);
97 virtual bool ProcessEvent(wxEvent
& event
);
99 void SetFilterEventFunc(FilterEventFunc f
) { m_filterEventFunc
= f
; }
100 void SetProcessEventFunc(ProcessEventFunc f
) { m_processEventFunc
= f
; }
103 virtual void OnAssertFailure(const wxChar
*,
109 throw TestAssertFailure();
111 #endif // __WXDEBUG__
114 void List(Test
*test
, const string
& parent
= "") const;
116 // command lines options/parameters
121 wxArrayString m_registries
;
123 // event handling hooks
124 FilterEventFunc m_filterEventFunc
;
125 ProcessEventFunc m_processEventFunc
;
128 IMPLEMENT_APP_CONSOLE(TestApp
)
134 m_filterEventFunc
= NULL
;
135 m_processEventFunc
= NULL
;
140 bool TestApp::OnInit()
142 if ( !TestAppBase::OnInit() )
145 cout
<< "Test program for wxWidgets\n"
146 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
149 // create a hidden parent window to be used as parent for the GUI controls
150 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
156 // The table of command line options
158 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
160 TestAppBase::OnInitCmdLine(parser
);
162 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
163 { wxCMD_LINE_SWITCH
, "l", "list",
164 "list the test suites, do not run them",
165 wxCMD_LINE_VAL_NONE
, 0 },
166 { wxCMD_LINE_SWITCH
, "L", "longlist",
167 "list the test cases, do not run them",
168 wxCMD_LINE_VAL_NONE
, 0 },
169 { wxCMD_LINE_SWITCH
, "d", "detail",
170 "print the test case names, run them",
171 wxCMD_LINE_VAL_NONE
, 0 },
172 { wxCMD_LINE_SWITCH
, "t", "timing",
173 "print names and mesure running time of individual test, run them",
174 wxCMD_LINE_VAL_NONE
, 0 },
175 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
176 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
180 parser
.SetDesc(cmdLineDesc
);
183 // Handle command line options
185 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
187 if (parser
.GetParamCount())
188 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
189 m_registries
.push_back(parser
.GetParam(i
));
191 m_registries
.push_back("");
193 m_longlist
= parser
.Found(_T("longlist"));
194 m_list
= m_longlist
|| parser
.Found(_T("list"));
195 m_timing
= parser
.Found(_T("timing"));
196 m_detail
= !m_timing
&& parser
.Found(_T("detail"));
198 return TestAppBase::OnCmdLineParsed(parser
);
202 int TestApp::FilterEvent(wxEvent
& event
)
204 if ( m_filterEventFunc
)
205 return (*m_filterEventFunc
)(event
);
207 return TestAppBase::FilterEvent(event
);
210 bool TestApp::ProcessEvent(wxEvent
& event
)
212 if ( m_processEventFunc
)
213 return (*m_processEventFunc
)(event
);
215 return TestAppBase::ProcessEvent(event
);
218 extern void SetFilterEventFunc(FilterEventFunc func
)
220 wxGetApp().SetFilterEventFunc(func
);
223 extern void SetProcessEventFunc(ProcessEventFunc func
)
225 wxGetApp().SetProcessEventFunc(func
);
232 CppUnit::TextTestRunner runner
;
234 for (size_t i
= 0; i
< m_registries
.size(); i
++)
236 wxString reg
= m_registries
[i
];
237 if (!reg
.empty() && !reg
.EndsWith("TestCase"))
239 // allow the user to specify the name of the testcase "in short form"
240 // (all wx test cases end with TestCase postfix)
242 auto_ptr
<Test
> test(reg
.empty() ?
243 TestFactoryRegistry::getRegistry().makeTest() :
244 TestFactoryRegistry::getRegistry(string(reg
.mb_str())).makeTest());
246 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
248 if (suite
&& suite
->countTestCases() == 0)
249 wxLogError(_T("No such test suite: %s"), reg
);
253 runner
.addTest(test
.release());
259 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
262 // Switch off logging unless --verbose
263 bool verbose
= wxLog::GetVerbose();
264 wxLog::EnableLogging(verbose
);
266 bool verbose
= false;
270 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
271 // in some versions of cppunit: they write progress dots to cout (and not
272 // cerr) and don't flush it so all the dots appear at once at the end which
273 // is not very useful so unbuffer cout to work around this
274 cout
.setf(ios::unitbuf
);
276 // add detail listener if needed
277 DetailListener
detailListener(m_timing
);
278 if ( m_detail
|| m_timing
)
279 runner
.eventManager().addListener(&detailListener
);
281 return runner
.run("", false, true, !verbose
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
284 int TestApp::OnExit()
287 delete GetTopWindow();
295 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
297 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
301 // take the last component of the name and append to the parent
302 name
= test
->getName();
303 string::size_type i
= name
.find_last_of(".:");
304 if (i
!= string::npos
)
305 name
= name
.substr(i
+ 1);
306 name
= parent
+ "." + name
;
308 // drop the 1st component from the display and indent
310 string::size_type j
= i
= name
.find('.', 1);
311 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
313 cout
<< " " << name
.substr(i
+ 1) << "\n";
316 typedef vector
<Test
*> Tests
;
317 typedef Tests::const_iterator Iter
;
319 const Tests
& tests
= suite
->getTests();
321 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
324 else if (m_longlist
) {
325 string::size_type i
= 0;
326 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
328 cout
<< " " << test
->getName() << "\n";