]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
update from Laurent
[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
23#include "wx/cmdline.h"
670ec357
VS
24#include <iostream>
25
3e5f6c1c
WS
26using CppUnit::Test;
27using CppUnit::TestSuite;
28using CppUnit::TestFactoryRegistry;
3e5f6c1c 29
5098c258 30using namespace std;
670ec357 31
c0d9b217
VZ
32#if wxUSE_GUI
33 typedef wxApp TestAppBase;
34#else
35 typedef wxAppConsole TestAppBase;
36#endif
37
670ec357
VS
38// The application class
39//
c0d9b217 40class TestApp : public TestAppBase
670ec357
VS
41{
42public:
43 TestApp();
44
45 // standard overrides
c0d9b217
VZ
46 virtual void OnInitCmdLine(wxCmdLineParser& parser);
47 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
48 virtual bool OnInit();
49 virtual int OnRun();
50 virtual int OnExit();
670ec357
VS
51
52private:
8dae9169 53 void List(Test *test, const string& parent = "") const;
670ec357
VS
54
55 // command lines options/parameters
56 bool m_list;
a81f3066
VS
57 bool m_longlist;
58 vector<string> m_registries;
670ec357
VS
59};
60
61IMPLEMENT_APP_CONSOLE(TestApp)
62
63TestApp::TestApp()
8dae9169
VS
64 : m_list(false),
65 m_longlist(false)
670ec357
VS
66{
67}
68
69// Init
70//
71bool TestApp::OnInit()
72{
c0d9b217
VZ
73 if ( !TestAppBase::OnInit() )
74 return false;
75
670ec357 76 cout << "Test program for wxWidgets\n"
3e5f6c1c 77 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 78
c0d9b217
VZ
79#if wxUSE_GUI
80 // create a hidden parent window to be used as parent for the GUI controls
81 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
82#endif // wxUSE_GUI
83
84 return true;
9f10e7c7 85}
670ec357
VS
86
87// The table of command line options
88//
89void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
90{
c0d9b217 91 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
92
93 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
94 { wxCMD_LINE_SWITCH, "l", "list",
95 "list the test suites, do not run them",
a81f3066 96 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
97 { wxCMD_LINE_SWITCH, "L", "longlist",
98 "list the test cases, do not run them",
670ec357 99 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e 100 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 101 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 102 wxCMD_LINE_DESC_END
670ec357
VS
103 };
104
105 parser.SetDesc(cmdLineDesc);
106}
107
108// Handle command line options
109//
110bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
111{
a81f3066
VS
112 if (parser.GetParamCount())
113 for (size_t i = 0; i < parser.GetParamCount(); i++)
114 m_registries.push_back(string(parser.GetParam(i).mb_str()));
115 else
116 m_registries.push_back("");
3e5f6c1c 117
a81f3066
VS
118 m_longlist = parser.Found(_T("longlist"));
119 m_list = m_longlist || parser.Found(_T("list"));
670ec357 120
c0d9b217 121 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
122}
123
124// Run
125//
126int TestApp::OnRun()
127{
2976d6cb 128 CppUnit::TextTestRunner runner;
a81f3066
VS
129
130 for (size_t i = 0; i < m_registries.size(); i++) {
131 auto_ptr<Test> test(m_registries[i].empty() ?
132 TestFactoryRegistry::getRegistry().makeTest() :
133 TestFactoryRegistry::getRegistry(m_registries[i]).makeTest());
134
135 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
136
137 if (suite && suite->countTestCases() == 0)
138 wxLogError(_T("No such test suite: %s"),
139 wxString(m_registries[i].c_str(), wxConvUTF8).c_str());
140 else if (m_list)
141 List(test.get());
142 else
143 runner.addTest(test.release());
670ec357 144 }
a81f3066 145
2976d6cb
VZ
146 if ( m_list )
147 return EXIT_SUCCESS;
148
149 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 150
86ca2b4c 151#if wxUSE_LOG
a81f3066 152 // Switch off logging unless --verbose
3e5f6c1c
WS
153 bool verbose = wxLog::GetVerbose();
154 wxLog::EnableLogging(verbose);
86ca2b4c 155#else
3e5f6c1c
WS
156 bool verbose = false;
157#endif
158
2976d6cb
VZ
159 // there is a bug
160 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
161 // in some versions of cppunit: they write progress dots to cout (and not
162 // cerr) and don't flush it so all the dots appear at once at the end which
163 // is not very useful so unbuffer cout to work around this
164 cout.setf(ios::unitbuf);
165
166 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
167}
168
c0d9b217
VZ
169int TestApp::OnExit()
170{
171#if wxUSE_GUI
172 delete GetTopWindow();
173#endif // wxUSE_GUI
174
175 return 0;
176}
177
670ec357
VS
178// List the tests
179//
8dae9169 180void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 181{
670ec357 182 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
183 string name;
184
bc10103e 185 if (suite) {
8dae9169
VS
186 // take the last component of the name and append to the parent
187 name = test->getName();
188 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
189 if (i != string::npos)
190 name = name.substr(i + 1);
191 name = parent + "." + name;
8dae9169
VS
192
193 // drop the 1st component from the display and indent
194 if (parent != "") {
195 string::size_type j = i = name.find('.', 1);
196 while ((j = name.find('.', j + 1)) != string::npos)
197 cout << " ";
198 cout << " " << name.substr(i + 1) << "\n";
199 }
a81f3066 200
f69577be 201 typedef vector<Test*> Tests;
670ec357
VS
202 typedef Tests::const_iterator Iter;
203
f69577be 204 const Tests& tests = suite->getTests();
670ec357
VS
205
206 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 207 List(*it, name);
670ec357 208 }
bc10103e
VS
209 else if (m_longlist) {
210 string::size_type i = 0;
211 while ((i = parent.find('.', i + 1)) != string::npos)
212 cout << " ";
213 cout << " " << test->getName() << "\n";
214 }
670ec357 215}