]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
Make dir names unique.
[wxWidgets.git] / tests / test.cpp
CommitLineData
670ec357
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: test.cpp
3// Purpose: Test program for wxWidgets
4// Author: Mike Wetherell
5// RCS-ID: $Id$
6// Copyright: (c) 2004 Mike Wetherell
7// Licence: wxWidgets licence
8///////////////////////////////////////////////////////////////////////////////
9
8899b155
RN
10// For compilers that support precompilation, includes "wx/wx.h"
11// and "wx/cppunit.h"
12#include "testprec.h"
670ec357
VS
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18// for all others, include the necessary headers
19#ifndef WX_PRECOMP
20 #include "wx/wx.h"
21#endif
22
afd90468 23#include "wx/beforestd.h"
6d9407fe
VZ
24#ifdef __VISUALC__
25 #pragma warning(disable:4100)
26#endif
afd90468 27#include <cppunit/TestListener.h>
6d9407fe
VZ
28#ifdef __VISUALC__
29 #pragma warning(default:4100)
30#endif
afd90468
VZ
31#include <cppunit/Test.h>
32#include <cppunit/TestResult.h>
33#include "wx/afterstd.h"
34
670ec357 35#include "wx/cmdline.h"
670ec357
VS
36#include <iostream>
37
3e5f6c1c
WS
38using CppUnit::Test;
39using CppUnit::TestSuite;
40using CppUnit::TestFactoryRegistry;
3e5f6c1c 41
afd90468 42
6d9407fe
VZ
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.
afd90468
VZ
45class DetailListener : public CppUnit::TestListener
46{
47public:
48 DetailListener(bool doTiming = false):
49 CppUnit::TestListener(),
50 m_timing(doTiming)
51 {
52 }
53
54 virtual void startTest(CppUnit::Test *test)
55 {
6d9407fe 56 std::cout << test->getName () << " ";
afd90468
VZ
57 m_watch.Start();
58 }
59
60 virtual void endTest(CppUnit::Test * WXUNUSED(test))
61 {
62 m_watch.Pause();
63 if ( m_timing )
6d9407fe
VZ
64 std::cout << " (in "<< m_watch.Time() << " ms )";
65 std::cout << "\n";
afd90468
VZ
66 }
67
68protected :
69 bool m_timing;
70 wxStopWatch m_watch;
71};
72
5098c258 73using namespace std;
670ec357 74
c0d9b217
VZ
75#if wxUSE_GUI
76 typedef wxApp TestAppBase;
77#else
78 typedef wxAppConsole TestAppBase;
79#endif
80
670ec357
VS
81// The application class
82//
c0d9b217 83class TestApp : public TestAppBase
670ec357
VS
84{
85public:
86 TestApp();
87
88 // standard overrides
c0d9b217
VZ
89 virtual void OnInitCmdLine(wxCmdLineParser& parser);
90 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
91 virtual bool OnInit();
92 virtual int OnRun();
93 virtual int OnExit();
670ec357 94
1649d288
VZ
95 // used by events propagation test
96 virtual int FilterEvent(wxEvent& event);
97 virtual bool ProcessEvent(wxEvent& event);
98
99 void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
100 void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
101
0468a58a
VZ
102#ifdef __WXDEBUG__
103 virtual void OnAssertFailure(const wxChar *,
104 int,
105 const wxChar *,
106 const wxChar *,
107 const wxChar *)
108 {
109 throw TestAssertFailure();
110 }
111#endif // __WXDEBUG__
112
670ec357 113private:
8dae9169 114 void List(Test *test, const string& parent = "") const;
670ec357
VS
115
116 // command lines options/parameters
117 bool m_list;
a81f3066 118 bool m_longlist;
afd90468
VZ
119 bool m_detail;
120 bool m_timing;
6582f592 121 wxArrayString m_registries;
1649d288
VZ
122
123 // event handling hooks
124 FilterEventFunc m_filterEventFunc;
125 ProcessEventFunc m_processEventFunc;
670ec357
VS
126};
127
128IMPLEMENT_APP_CONSOLE(TestApp)
129
130TestApp::TestApp()
8dae9169
VS
131 : m_list(false),
132 m_longlist(false)
670ec357 133{
1649d288
VZ
134 m_filterEventFunc = NULL;
135 m_processEventFunc = NULL;
670ec357
VS
136}
137
138// Init
139//
140bool TestApp::OnInit()
141{
c0d9b217
VZ
142 if ( !TestAppBase::OnInit() )
143 return false;
144
670ec357 145 cout << "Test program for wxWidgets\n"
3e5f6c1c 146 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 147
c0d9b217
VZ
148#if wxUSE_GUI
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");
151#endif // wxUSE_GUI
152
153 return true;
9f10e7c7 154}
670ec357
VS
155
156// The table of command line options
157//
158void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
159{
c0d9b217 160 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
161
162 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
163 { wxCMD_LINE_SWITCH, "l", "list",
164 "list the test suites, do not run them",
a81f3066 165 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
166 { wxCMD_LINE_SWITCH, "L", "longlist",
167 "list the test cases, do not run them",
670ec357 168 wxCMD_LINE_VAL_NONE, 0 },
afd90468
VZ
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 },
aa3b041e 175 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 176 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 177 wxCMD_LINE_DESC_END
670ec357
VS
178 };
179
180 parser.SetDesc(cmdLineDesc);
181}
182
183// Handle command line options
184//
185bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
186{
a81f3066
VS
187 if (parser.GetParamCount())
188 for (size_t i = 0; i < parser.GetParamCount(); i++)
6582f592 189 m_registries.push_back(parser.GetParam(i));
a81f3066
VS
190 else
191 m_registries.push_back("");
3e5f6c1c 192
a81f3066
VS
193 m_longlist = parser.Found(_T("longlist"));
194 m_list = m_longlist || parser.Found(_T("list"));
afd90468
VZ
195 m_timing = parser.Found(_T("timing"));
196 m_detail = !m_timing && parser.Found(_T("detail"));
670ec357 197
c0d9b217 198 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
199}
200
1649d288
VZ
201// Event handling
202int TestApp::FilterEvent(wxEvent& event)
203{
204 if ( m_filterEventFunc )
205 return (*m_filterEventFunc)(event);
206
207 return TestAppBase::FilterEvent(event);
208}
209
210bool TestApp::ProcessEvent(wxEvent& event)
211{
212 if ( m_processEventFunc )
213 return (*m_processEventFunc)(event);
214
215 return TestAppBase::ProcessEvent(event);
216}
217
218extern void SetFilterEventFunc(FilterEventFunc func)
219{
220 wxGetApp().SetFilterEventFunc(func);
221}
222
223extern void SetProcessEventFunc(ProcessEventFunc func)
224{
225 wxGetApp().SetProcessEventFunc(func);
226}
227
670ec357
VS
228// Run
229//
230int TestApp::OnRun()
231{
2976d6cb 232 CppUnit::TextTestRunner runner;
a81f3066 233
6d423df0
FM
234 for (size_t i = 0; i < m_registries.size(); i++)
235 {
6582f592
VZ
236 wxString reg = m_registries[i];
237 if (!reg.empty() && !reg.EndsWith("TestCase"))
6d423df0
FM
238 reg += "TestCase";
239 // allow the user to specify the name of the testcase "in short form"
240 // (all wx test cases end with TestCase postfix)
241
242 auto_ptr<Test> test(reg.empty() ?
a81f3066 243 TestFactoryRegistry::getRegistry().makeTest() :
6582f592 244 TestFactoryRegistry::getRegistry(string(reg.mb_str())).makeTest());
a81f3066
VS
245
246 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
247
248 if (suite && suite->countTestCases() == 0)
6582f592 249 wxLogError(_T("No such test suite: %s"), reg);
a81f3066
VS
250 else if (m_list)
251 List(test.get());
252 else
253 runner.addTest(test.release());
670ec357 254 }
a81f3066 255
2976d6cb 256 if ( m_list )
1dd8319a 257 return EXIT_SUCCESS;
2976d6cb
VZ
258
259 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 260
86ca2b4c 261#if wxUSE_LOG
a81f3066 262 // Switch off logging unless --verbose
3e5f6c1c
WS
263 bool verbose = wxLog::GetVerbose();
264 wxLog::EnableLogging(verbose);
86ca2b4c 265#else
3e5f6c1c
WS
266 bool verbose = false;
267#endif
268
2976d6cb
VZ
269 // there is a bug
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);
275
afd90468
VZ
276 // add detail listener if needed
277 DetailListener detailListener(m_timing);
278 if ( m_detail || m_timing )
279 runner.eventManager().addListener(&detailListener);
280
1dd8319a 281 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
282}
283
c0d9b217
VZ
284int TestApp::OnExit()
285{
286#if wxUSE_GUI
287 delete GetTopWindow();
288#endif // wxUSE_GUI
289
290 return 0;
291}
292
670ec357
VS
293// List the tests
294//
8dae9169 295void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 296{
670ec357 297 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
298 string name;
299
bc10103e 300 if (suite) {
8dae9169
VS
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(".:");
f44eaed6
RN
304 if (i != string::npos)
305 name = name.substr(i + 1);
306 name = parent + "." + name;
8dae9169
VS
307
308 // drop the 1st component from the display and indent
309 if (parent != "") {
310 string::size_type j = i = name.find('.', 1);
311 while ((j = name.find('.', j + 1)) != string::npos)
312 cout << " ";
313 cout << " " << name.substr(i + 1) << "\n";
314 }
a81f3066 315
f69577be 316 typedef vector<Test*> Tests;
670ec357
VS
317 typedef Tests::const_iterator Iter;
318
f69577be 319 const Tests& tests = suite->getTests();
670ec357
VS
320
321 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 322 List(*it, name);
670ec357 323 }
bc10103e
VS
324 else if (m_longlist) {
325 string::size_type i = 0;
326 while ((i = parent.find('.', i + 1)) != string::npos)
327 cout << " ";
328 cout << " " << test->getName() << "\n";
329 }
670ec357 330}