]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
give more detailed message when the test fails in TestTimeFormat()
[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
b33e98f0 31#include <cppunit/Protector.h>
afd90468
VZ
32#include <cppunit/Test.h>
33#include <cppunit/TestResult.h>
34#include "wx/afterstd.h"
35
670ec357 36#include "wx/cmdline.h"
670ec357
VS
37#include <iostream>
38
b33e98f0
VZ
39#ifdef __WXMSW__
40 #include "wx/msw/msvcrt.h"
41#endif
42
43using namespace std;
44
3e5f6c1c
WS
45using CppUnit::Test;
46using CppUnit::TestSuite;
47using CppUnit::TestFactoryRegistry;
3e5f6c1c 48
b33e98f0
VZ
49// exception class for MSVC debug CRT assertion failures
50#ifdef wxUSE_VC_CRTDBG
51
52struct 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
64static 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
100class wxUnitTestProtector : public CppUnit::Protector
101{
102public:
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};
afd90468 119
6d9407fe
VZ
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.
afd90468
VZ
122class DetailListener : public CppUnit::TestListener
123{
124public:
125 DetailListener(bool doTiming = false):
126 CppUnit::TestListener(),
127 m_timing(doTiming)
128 {
129 }
130
131 virtual void startTest(CppUnit::Test *test)
132 {
6d9407fe 133 std::cout << test->getName () << " ";
afd90468
VZ
134 m_watch.Start();
135 }
136
137 virtual void endTest(CppUnit::Test * WXUNUSED(test))
138 {
139 m_watch.Pause();
140 if ( m_timing )
6d9407fe
VZ
141 std::cout << " (in "<< m_watch.Time() << " ms )";
142 std::cout << "\n";
afd90468
VZ
143 }
144
145protected :
146 bool m_timing;
147 wxStopWatch m_watch;
148};
149
c0d9b217
VZ
150#if wxUSE_GUI
151 typedef wxApp TestAppBase;
152#else
153 typedef wxAppConsole TestAppBase;
154#endif
155
670ec357
VS
156// The application class
157//
c0d9b217 158class TestApp : public TestAppBase
670ec357
VS
159{
160public:
161 TestApp();
162
163 // standard overrides
c0d9b217
VZ
164 virtual void OnInitCmdLine(wxCmdLineParser& parser);
165 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
166 virtual bool OnInit();
167 virtual int OnRun();
168 virtual int OnExit();
670ec357 169
1649d288
VZ
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
670ec357 177private:
8dae9169 178 void List(Test *test, const string& parent = "") const;
670ec357
VS
179
180 // command lines options/parameters
181 bool m_list;
a81f3066 182 bool m_longlist;
afd90468
VZ
183 bool m_detail;
184 bool m_timing;
6582f592 185 wxArrayString m_registries;
1649d288
VZ
186
187 // event handling hooks
188 FilterEventFunc m_filterEventFunc;
189 ProcessEventFunc m_processEventFunc;
670ec357
VS
190};
191
b33e98f0
VZ
192IMPLEMENT_APP_NO_MAIN(TestApp)
193
194#ifdef wxUSE_VC_CRTDBG
195
196static 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
208static 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
219int 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}
670ec357
VS
244
245TestApp::TestApp()
8dae9169
VS
246 : m_list(false),
247 m_longlist(false)
670ec357 248{
1649d288
VZ
249 m_filterEventFunc = NULL;
250 m_processEventFunc = NULL;
670ec357
VS
251}
252
253// Init
254//
255bool TestApp::OnInit()
256{
c0d9b217
VZ
257 if ( !TestAppBase::OnInit() )
258 return false;
259
670ec357 260 cout << "Test program for wxWidgets\n"
3e5f6c1c 261 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 262
c0d9b217
VZ
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;
9f10e7c7 269}
670ec357
VS
270
271// The table of command line options
272//
273void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
274{
c0d9b217 275 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
276
277 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
278 { wxCMD_LINE_SWITCH, "l", "list",
279 "list the test suites, do not run them",
a81f3066 280 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
281 { wxCMD_LINE_SWITCH, "L", "longlist",
282 "list the test cases, do not run them",
670ec357 283 wxCMD_LINE_VAL_NONE, 0 },
afd90468
VZ
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 },
aa3b041e 290 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 291 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 292 wxCMD_LINE_DESC_END
670ec357
VS
293 };
294
295 parser.SetDesc(cmdLineDesc);
296}
297
298// Handle command line options
299//
300bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
301{
a81f3066
VS
302 if (parser.GetParamCount())
303 for (size_t i = 0; i < parser.GetParamCount(); i++)
6582f592 304 m_registries.push_back(parser.GetParam(i));
a81f3066
VS
305 else
306 m_registries.push_back("");
3e5f6c1c 307
a81f3066
VS
308 m_longlist = parser.Found(_T("longlist"));
309 m_list = m_longlist || parser.Found(_T("list"));
afd90468
VZ
310 m_timing = parser.Found(_T("timing"));
311 m_detail = !m_timing && parser.Found(_T("detail"));
670ec357 312
c0d9b217 313 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
314}
315
1649d288
VZ
316// Event handling
317int TestApp::FilterEvent(wxEvent& event)
318{
319 if ( m_filterEventFunc )
320 return (*m_filterEventFunc)(event);
321
322 return TestAppBase::FilterEvent(event);
323}
324
325bool TestApp::ProcessEvent(wxEvent& event)
326{
327 if ( m_processEventFunc )
328 return (*m_processEventFunc)(event);
329
330 return TestAppBase::ProcessEvent(event);
331}
332
333extern void SetFilterEventFunc(FilterEventFunc func)
334{
335 wxGetApp().SetFilterEventFunc(func);
336}
337
338extern void SetProcessEventFunc(ProcessEventFunc func)
339{
340 wxGetApp().SetProcessEventFunc(func);
341}
342
670ec357
VS
343// Run
344//
345int TestApp::OnRun()
346{
2976d6cb 347 CppUnit::TextTestRunner runner;
a81f3066 348
6d423df0
FM
349 for (size_t i = 0; i < m_registries.size(); i++)
350 {
6582f592
VZ
351 wxString reg = m_registries[i];
352 if (!reg.empty() && !reg.EndsWith("TestCase"))
6d423df0
FM
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() ?
a81f3066 358 TestFactoryRegistry::getRegistry().makeTest() :
6582f592 359 TestFactoryRegistry::getRegistry(string(reg.mb_str())).makeTest());
a81f3066
VS
360
361 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
362
363 if (suite && suite->countTestCases() == 0)
6582f592 364 wxLogError(_T("No such test suite: %s"), reg);
a81f3066
VS
365 else if (m_list)
366 List(test.get());
367 else
368 runner.addTest(test.release());
670ec357 369 }
a81f3066 370
2976d6cb 371 if ( m_list )
1dd8319a 372 return EXIT_SUCCESS;
2976d6cb
VZ
373
374 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 375
86ca2b4c 376#if wxUSE_LOG
a81f3066 377 // Switch off logging unless --verbose
3e5f6c1c
WS
378 bool verbose = wxLog::GetVerbose();
379 wxLog::EnableLogging(verbose);
86ca2b4c 380#else
3e5f6c1c
WS
381 bool verbose = false;
382#endif
383
2976d6cb
VZ
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
afd90468
VZ
391 // add detail listener if needed
392 DetailListener detailListener(m_timing);
393 if ( m_detail || m_timing )
394 runner.eventManager().addListener(&detailListener);
395
b33e98f0
VZ
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
1dd8319a 400 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
401}
402
c0d9b217
VZ
403int TestApp::OnExit()
404{
405#if wxUSE_GUI
406 delete GetTopWindow();
407#endif // wxUSE_GUI
408
409 return 0;
410}
411
670ec357
VS
412// List the tests
413//
8dae9169 414void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 415{
670ec357 416 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
417 string name;
418
bc10103e 419 if (suite) {
8dae9169
VS
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(".:");
f44eaed6
RN
423 if (i != string::npos)
424 name = name.substr(i + 1);
425 name = parent + "." + name;
8dae9169
VS
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 }
a81f3066 434
f69577be 435 typedef vector<Test*> Tests;
670ec357
VS
436 typedef Tests::const_iterator Iter;
437
f69577be 438 const Tests& tests = suite->getTests();
670ec357
VS
439
440 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 441 List(*it, name);
670ec357 442 }
bc10103e
VS
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 }
670ec357 449}