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