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