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();
96 virtual void OnAssertFailure(const wxChar
*,
102 throw TestAssertFailure();
104 #endif // __WXDEBUG__
107 void List(Test
*test
, const string
& parent
= "") const;
109 // command lines options/parameters
114 vector
<string
> m_registries
;
117 IMPLEMENT_APP_CONSOLE(TestApp
)
127 bool TestApp::OnInit()
129 if ( !TestAppBase::OnInit() )
132 cout
<< "Test program for wxWidgets\n"
133 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
136 // create a hidden parent window to be used as parent for the GUI controls
137 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
143 // The table of command line options
145 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
147 TestAppBase::OnInitCmdLine(parser
);
149 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
150 { wxCMD_LINE_SWITCH
, "l", "list",
151 "list the test suites, do not run them",
152 wxCMD_LINE_VAL_NONE
, 0 },
153 { wxCMD_LINE_SWITCH
, "L", "longlist",
154 "list the test cases, do not run them",
155 wxCMD_LINE_VAL_NONE
, 0 },
156 { wxCMD_LINE_SWITCH
, "d", "detail",
157 "print the test case names, run them",
158 wxCMD_LINE_VAL_NONE
, 0 },
159 { wxCMD_LINE_SWITCH
, "t", "timing",
160 "print names and mesure running time of individual test, run them",
161 wxCMD_LINE_VAL_NONE
, 0 },
162 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
163 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
167 parser
.SetDesc(cmdLineDesc
);
170 // Handle command line options
172 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
174 if (parser
.GetParamCount())
175 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
176 m_registries
.push_back(string(parser
.GetParam(i
).mb_str()));
178 m_registries
.push_back("");
180 m_longlist
= parser
.Found(_T("longlist"));
181 m_list
= m_longlist
|| parser
.Found(_T("list"));
182 m_timing
= parser
.Found(_T("timing"));
183 m_detail
= !m_timing
&& parser
.Found(_T("detail"));
185 return TestAppBase::OnCmdLineParsed(parser
);
192 CppUnit::TextTestRunner runner
;
194 for (size_t i
= 0; i
< m_registries
.size(); i
++) {
195 auto_ptr
<Test
> test(m_registries
[i
].empty() ?
196 TestFactoryRegistry::getRegistry().makeTest() :
197 TestFactoryRegistry::getRegistry(m_registries
[i
]).makeTest());
199 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
201 if (suite
&& suite
->countTestCases() == 0)
202 wxLogError(_T("No such test suite: %s"),
203 wxString(m_registries
[i
].c_str(), wxConvUTF8
).c_str());
207 runner
.addTest(test
.release());
213 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
216 // Switch off logging unless --verbose
217 bool verbose
= wxLog::GetVerbose();
218 wxLog::EnableLogging(verbose
);
220 bool verbose
= false;
224 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
225 // in some versions of cppunit: they write progress dots to cout (and not
226 // cerr) and don't flush it so all the dots appear at once at the end which
227 // is not very useful so unbuffer cout to work around this
228 cout
.setf(ios::unitbuf
);
230 // add detail listener if needed
231 DetailListener
detailListener(m_timing
);
232 if ( m_detail
|| m_timing
)
233 runner
.eventManager().addListener(&detailListener
);
235 return runner
.run("", false, true, !verbose
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
238 int TestApp::OnExit()
241 delete GetTopWindow();
249 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
251 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
255 // take the last component of the name and append to the parent
256 name
= test
->getName();
257 string::size_type i
= name
.find_last_of(".:");
258 if (i
!= string::npos
)
259 name
= name
.substr(i
+ 1);
260 name
= parent
+ "." + name
;
262 // drop the 1st component from the display and indent
264 string::size_type j
= i
= name
.find('.', 1);
265 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
267 cout
<< " " << name
.substr(i
+ 1) << "\n";
270 typedef vector
<Test
*> Tests
;
271 typedef Tests::const_iterator Iter
;
273 const Tests
& tests
= suite
->getTests();
275 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
278 else if (m_longlist
) {
279 string::size_type i
= 0;
280 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
282 cout
<< " " << test
->getName() << "\n";