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/cmdline.h"
27 using CppUnit::TestSuite
;
28 using CppUnit::TestFactoryRegistry
;
33 typedef wxApp TestAppBase
;
35 typedef wxAppConsole TestAppBase
;
38 // The application class
40 class TestApp
: public TestAppBase
46 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
47 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
48 virtual bool OnInit();
53 void List(Test
*test
, const string
& parent
= "") const;
55 // command lines options/parameters
58 vector
<string
> m_registries
;
61 IMPLEMENT_APP_CONSOLE(TestApp
)
71 bool TestApp::OnInit()
73 if ( !TestAppBase::OnInit() )
76 cout
<< "Test program for wxWidgets\n"
77 << "build: " << WX_BUILD_OPTIONS_SIGNATURE
<< std::endl
;
80 // create a hidden parent window to be used as parent for the GUI controls
81 new wxFrame(NULL
, wxID_ANY
, "Hidden wx test frame");
87 // The table of command line options
89 void TestApp::OnInitCmdLine(wxCmdLineParser
& parser
)
91 TestAppBase::OnInitCmdLine(parser
);
93 static const wxCmdLineEntryDesc cmdLineDesc
[] = {
94 { wxCMD_LINE_SWITCH
, "l", "list",
95 "list the test suites, do not run them",
96 wxCMD_LINE_VAL_NONE
, 0 },
97 { wxCMD_LINE_SWITCH
, "L", "longlist",
98 "list the test cases, do not run them",
99 wxCMD_LINE_VAL_NONE
, 0 },
100 { wxCMD_LINE_PARAM
, NULL
, NULL
, "REGISTRY", wxCMD_LINE_VAL_STRING
,
101 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
},
105 parser
.SetDesc(cmdLineDesc
);
108 // Handle command line options
110 bool TestApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
112 if (parser
.GetParamCount())
113 for (size_t i
= 0; i
< parser
.GetParamCount(); i
++)
114 m_registries
.push_back(string(parser
.GetParam(i
).mb_str()));
116 m_registries
.push_back("");
118 m_longlist
= parser
.Found(_T("longlist"));
119 m_list
= m_longlist
|| parser
.Found(_T("list"));
121 return TestAppBase::OnCmdLineParsed(parser
);
128 CppUnit::TextTestRunner runner
;
130 for (size_t i
= 0; i
< m_registries
.size(); i
++) {
131 auto_ptr
<Test
> test(m_registries
[i
].empty() ?
132 TestFactoryRegistry::getRegistry().makeTest() :
133 TestFactoryRegistry::getRegistry(m_registries
[i
]).makeTest());
135 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
.get());
137 if (suite
&& suite
->countTestCases() == 0)
138 wxLogError(_T("No such test suite: %s"),
139 wxString(m_registries
[i
].c_str(), wxConvUTF8
).c_str());
143 runner
.addTest(test
.release());
149 runner
.setOutputter(new CppUnit::CompilerOutputter(&runner
.result(), cout
));
152 // Switch off logging unless --verbose
153 bool verbose
= wxLog::GetVerbose();
154 wxLog::EnableLogging(verbose
);
156 bool verbose
= false;
160 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
161 // in some versions of cppunit: they write progress dots to cout (and not
162 // cerr) and don't flush it so all the dots appear at once at the end which
163 // is not very useful so unbuffer cout to work around this
164 cout
.setf(ios::unitbuf
);
166 return runner
.run("", false, true, !verbose
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
169 int TestApp::OnExit()
172 delete GetTopWindow();
180 void TestApp::List(Test
*test
, const string
& parent
/*=""*/) const
182 TestSuite
*suite
= dynamic_cast<TestSuite
*>(test
);
186 // take the last component of the name and append to the parent
187 name
= test
->getName();
188 string::size_type i
= name
.find_last_of(".:");
189 if (i
!= string::npos
)
190 name
= name
.substr(i
+ 1);
191 name
= parent
+ "." + name
;
193 // drop the 1st component from the display and indent
195 string::size_type j
= i
= name
.find('.', 1);
196 while ((j
= name
.find('.', j
+ 1)) != string::npos
)
198 cout
<< " " << name
.substr(i
+ 1) << "\n";
201 typedef vector
<Test
*> Tests
;
202 typedef Tests::const_iterator Iter
;
204 const Tests
& tests
= suite
->getTests();
206 for (Iter it
= tests
.begin(); it
!= tests
.end(); ++it
)
209 else if (m_longlist
) {
210 string::size_type i
= 0;
211 while ((i
= parent
.find('.', i
+ 1)) != string::npos
)
213 cout
<< " " << test
->getName() << "\n";