]> git.saurik.com Git - wxWidgets.git/blob - tests/test.cpp
02d819133828534c913fc5c117f134b63d943e8e
[wxWidgets.git] / tests / test.cpp
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
10 // For compilers that support precompilation, includes "wx/wx.h"
11 // and "wx/cppunit.h"
12 #include "testprec.h"
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
23 #include "wx/beforestd.h"
24 #ifdef __VISUALC__
25 #pragma warning(disable:4100)
26 #endif
27 #include <cppunit/TestListener.h>
28 #ifdef __VISUALC__
29 #pragma warning(default:4100)
30 #endif
31 #include <cppunit/Protector.h>
32 #include <cppunit/Test.h>
33 #include <cppunit/TestResult.h>
34 #include "wx/afterstd.h"
35
36 #include "wx/cmdline.h"
37 #include <iostream>
38
39 #ifdef __WXMSW__
40 #include "wx/msw/msvcrt.h"
41 #endif
42
43 using namespace std;
44
45 using CppUnit::Test;
46 using CppUnit::TestSuite;
47 using CppUnit::TestFactoryRegistry;
48
49 // exception class for MSVC debug CRT assertion failures
50 #ifdef wxUSE_VC_CRTDBG
51
52 struct CrtAssertFailure
53 {
54 CrtAssertFailure(const char *message) : m_msg(message) { }
55
56 const wxString m_msg;
57
58 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
59 };
60
61 #endif // wxUSE_VC_CRTDBG
62
63 // this function should only be called from a catch clause
64 static string GetExceptionMessage()
65 {
66 wxString msg;
67
68 try
69 {
70 throw;
71 }
72 #if wxDEBUG_LEVEL
73 catch ( TestAssertFailure& e )
74 {
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;
78 }
79 #endif // wxDEBUG_LEVEL
80 #ifdef wxUSE_VC_CRTDBG
81 catch ( CrtAssertFailure& e )
82 {
83 msg << "CRT assert failure: " << e.m_msg;
84 }
85 #endif // wxUSE_VC_CRTDBG
86 catch ( std::exception& e )
87 {
88 msg << "std::exception: " << e.what();
89 }
90 catch ( ... )
91 {
92 msg = "Unknown exception caught.";
93 }
94
95 return string(msg.mb_str());
96 }
97
98 // Protector adding handling of wx-specific (this includes MSVC debug CRT in
99 // this context) exceptions
100 class wxUnitTestProtector : public CppUnit::Protector
101 {
102 public:
103 virtual bool protect(const CppUnit::Functor &functor,
104 const CppUnit::ProtectorContext& context)
105 {
106 try
107 {
108 return functor();
109 }
110 catch ( std::exception& e )
111 {
112 // cppunit deals with the standard exceptions itself, let it do as
113 // it output more details (especially for std::exception-derived
114 // CppUnit::Exception) than we do
115 throw;
116 }
117 catch ( ... )
118 {
119 reportError(context, CppUnit::Message("Uncaught exception",
120 GetExceptionMessage()));
121 }
122
123 return false;
124 }
125 };
126
127 // Displays the test name before starting to execute it: this helps with
128 // diagnosing where exactly does a test crash or hang when/if it does.
129 class DetailListener : public CppUnit::TestListener
130 {
131 public:
132 DetailListener(bool doTiming = false):
133 CppUnit::TestListener(),
134 m_timing(doTiming)
135 {
136 }
137
138 virtual void startTest(CppUnit::Test *test)
139 {
140 std::cout << test->getName () << " ";
141 m_watch.Start();
142 }
143
144 virtual void endTest(CppUnit::Test * WXUNUSED(test))
145 {
146 m_watch.Pause();
147 if ( m_timing )
148 std::cout << " (in "<< m_watch.Time() << " ms )";
149 std::cout << "\n";
150 }
151
152 protected :
153 bool m_timing;
154 wxStopWatch m_watch;
155 };
156
157 #if wxUSE_GUI
158 typedef wxApp TestAppBase;
159 #else
160 typedef wxAppConsole TestAppBase;
161 #endif
162
163 // The application class
164 //
165 class TestApp : public TestAppBase
166 {
167 public:
168 TestApp();
169
170 // standard overrides
171 virtual void OnInitCmdLine(wxCmdLineParser& parser);
172 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
173 virtual bool OnInit();
174 virtual int OnRun();
175 virtual int OnExit();
176
177 // used by events propagation test
178 virtual int FilterEvent(wxEvent& event);
179 virtual bool ProcessEvent(wxEvent& event);
180
181 void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
182 void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
183
184 private:
185 void List(Test *test, const string& parent = "") const;
186
187 // command lines options/parameters
188 bool m_list;
189 bool m_longlist;
190 bool m_detail;
191 bool m_timing;
192 wxArrayString m_registries;
193
194 // event handling hooks
195 FilterEventFunc m_filterEventFunc;
196 ProcessEventFunc m_processEventFunc;
197 };
198
199 IMPLEMENT_APP_NO_MAIN(TestApp)
200
201 #ifdef wxUSE_VC_CRTDBG
202
203 static int TestCrtReportHook(int reportType, char *message, int *)
204 {
205 if ( reportType != _CRT_ASSERT )
206 return FALSE;
207
208 throw CrtAssertFailure(message);
209 }
210
211 #endif // wxUSE_VC_CRTDBG
212
213 #if wxDEBUG_LEVEL
214
215 static void TestAssertHandler(const wxString& file,
216 int line,
217 const wxString& func,
218 const wxString& cond,
219 const wxString& msg)
220 {
221 throw TestAssertFailure(file, line, func, cond, msg);
222 }
223
224 #endif // wxDEBUG_LEVEL
225
226 int main(int argc, char **argv)
227 {
228 // tests can be ran non-interactively so make sure we don't show any assert
229 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
230 // prevent them from completing
231
232 #if wxDEBUG_LEVEL
233 wxSetAssertHandler(TestAssertHandler);
234 #endif // wxDEBUG_LEVEL
235
236 #ifdef wxUSE_VC_CRTDBG
237 _CrtSetReportHook(TestCrtReportHook);
238 #endif // wxUSE_VC_CRTDBG
239
240 try
241 {
242 return wxEntry(argc, argv);
243 }
244 catch ( ... )
245 {
246 cerr << "\n" << GetExceptionMessage() << endl;
247 }
248
249 return -1;
250 }
251
252 TestApp::TestApp()
253 : m_list(false),
254 m_longlist(false)
255 {
256 m_filterEventFunc = NULL;
257 m_processEventFunc = NULL;
258 }
259
260 // Init
261 //
262 bool TestApp::OnInit()
263 {
264 if ( !TestAppBase::OnInit() )
265 return false;
266
267 cout << "Test program for wxWidgets\n"
268 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
269
270 #if wxUSE_GUI
271 // create a hidden parent window to be used as parent for the GUI controls
272 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
273 #endif // wxUSE_GUI
274
275 return true;
276 }
277
278 // The table of command line options
279 //
280 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
281 {
282 TestAppBase::OnInitCmdLine(parser);
283
284 static const wxCmdLineEntryDesc cmdLineDesc[] = {
285 { wxCMD_LINE_SWITCH, "l", "list",
286 "list the test suites, do not run them",
287 wxCMD_LINE_VAL_NONE, 0 },
288 { wxCMD_LINE_SWITCH, "L", "longlist",
289 "list the test cases, do not run them",
290 wxCMD_LINE_VAL_NONE, 0 },
291 { wxCMD_LINE_SWITCH, "d", "detail",
292 "print the test case names, run them",
293 wxCMD_LINE_VAL_NONE, 0 },
294 { wxCMD_LINE_SWITCH, "t", "timing",
295 "print names and mesure running time of individual test, run them",
296 wxCMD_LINE_VAL_NONE, 0 },
297 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
298 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
299 wxCMD_LINE_DESC_END
300 };
301
302 parser.SetDesc(cmdLineDesc);
303 }
304
305 // Handle command line options
306 //
307 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
308 {
309 if (parser.GetParamCount())
310 for (size_t i = 0; i < parser.GetParamCount(); i++)
311 m_registries.push_back(parser.GetParam(i));
312 else
313 m_registries.push_back("");
314
315 m_longlist = parser.Found(_T("longlist"));
316 m_list = m_longlist || parser.Found(_T("list"));
317 m_timing = parser.Found(_T("timing"));
318 m_detail = !m_timing && parser.Found(_T("detail"));
319
320 return TestAppBase::OnCmdLineParsed(parser);
321 }
322
323 // Event handling
324 int TestApp::FilterEvent(wxEvent& event)
325 {
326 if ( m_filterEventFunc )
327 return (*m_filterEventFunc)(event);
328
329 return TestAppBase::FilterEvent(event);
330 }
331
332 bool TestApp::ProcessEvent(wxEvent& event)
333 {
334 if ( m_processEventFunc )
335 return (*m_processEventFunc)(event);
336
337 return TestAppBase::ProcessEvent(event);
338 }
339
340 extern void SetFilterEventFunc(FilterEventFunc func)
341 {
342 wxGetApp().SetFilterEventFunc(func);
343 }
344
345 extern void SetProcessEventFunc(ProcessEventFunc func)
346 {
347 wxGetApp().SetProcessEventFunc(func);
348 }
349
350 // Run
351 //
352 int TestApp::OnRun()
353 {
354 CppUnit::TextTestRunner runner;
355
356 for (size_t i = 0; i < m_registries.size(); i++)
357 {
358 wxString reg = m_registries[i];
359 if (!reg.empty() && !reg.EndsWith("TestCase"))
360 reg += "TestCase";
361 // allow the user to specify the name of the testcase "in short form"
362 // (all wx test cases end with TestCase postfix)
363
364 auto_ptr<Test> test(reg.empty() ?
365 TestFactoryRegistry::getRegistry().makeTest() :
366 TestFactoryRegistry::getRegistry(string(reg.mb_str())).makeTest());
367
368 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
369
370 if (suite && suite->countTestCases() == 0)
371 wxLogError(_T("No such test suite: %s"), reg);
372 else if (m_list)
373 List(test.get());
374 else
375 runner.addTest(test.release());
376 }
377
378 if ( m_list )
379 return EXIT_SUCCESS;
380
381 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
382
383 #if wxUSE_LOG
384 // Switch off logging unless --verbose
385 bool verbose = wxLog::GetVerbose();
386 wxLog::EnableLogging(verbose);
387 #else
388 bool verbose = false;
389 #endif
390
391 // there is a bug
392 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
393 // in some versions of cppunit: they write progress dots to cout (and not
394 // cerr) and don't flush it so all the dots appear at once at the end which
395 // is not very useful so unbuffer cout to work around this
396 cout.setf(ios::unitbuf);
397
398 // add detail listener if needed
399 DetailListener detailListener(m_timing);
400 if ( m_detail || m_timing )
401 runner.eventManager().addListener(&detailListener);
402
403 // finally ensure that we report our own exceptions nicely instead of
404 // giving "uncaught exception of unknown type" messages
405 runner.eventManager().pushProtector(new wxUnitTestProtector);
406
407 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
408 }
409
410 int TestApp::OnExit()
411 {
412 #if wxUSE_GUI
413 delete GetTopWindow();
414 #endif // wxUSE_GUI
415
416 return 0;
417 }
418
419 // List the tests
420 //
421 void TestApp::List(Test *test, const string& parent /*=""*/) const
422 {
423 TestSuite *suite = dynamic_cast<TestSuite*>(test);
424 string name;
425
426 if (suite) {
427 // take the last component of the name and append to the parent
428 name = test->getName();
429 string::size_type i = name.find_last_of(".:");
430 if (i != string::npos)
431 name = name.substr(i + 1);
432 name = parent + "." + name;
433
434 // drop the 1st component from the display and indent
435 if (parent != "") {
436 string::size_type j = i = name.find('.', 1);
437 while ((j = name.find('.', j + 1)) != string::npos)
438 cout << " ";
439 cout << " " << name.substr(i + 1) << "\n";
440 }
441
442 typedef vector<Test*> Tests;
443 typedef Tests::const_iterator Iter;
444
445 const Tests& tests = suite->getTests();
446
447 for (Iter it = tests.begin(); it != tests.end(); ++it)
448 List(*it, name);
449 }
450 else if (m_longlist) {
451 string::size_type i = 0;
452 while ((i = parent.find('.', i + 1)) != string::npos)
453 cout << " ";
454 cout << " " << test->getName() << "\n";
455 }
456 }