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