]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
fixed writing to wxUniCharRef after its 'parent' iterator was destroyed
[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 80
c0d9b217
VZ
81#if wxUSE_GUI
82 // create a hidden parent window to be used as parent for the GUI controls
83 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
84#endif // wxUSE_GUI
85
86 return true;
9f10e7c7 87}
670ec357
VS
88
89// The table of command line options
90//
91void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
92{
c0d9b217 93 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
94
95 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
96 { wxCMD_LINE_SWITCH, "l", "list",
97 "list the test suites, do not run them",
a81f3066 98 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
99 { wxCMD_LINE_SWITCH, "L", "longlist",
100 "list the test cases, do not run them",
670ec357 101 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e 102 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 103 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 104 wxCMD_LINE_DESC_END
670ec357
VS
105 };
106
107 parser.SetDesc(cmdLineDesc);
108}
109
110// Handle command line options
111//
112bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
113{
a81f3066
VS
114 if (parser.GetParamCount())
115 for (size_t i = 0; i < parser.GetParamCount(); i++)
116 m_registries.push_back(string(parser.GetParam(i).mb_str()));
117 else
118 m_registries.push_back("");
3e5f6c1c 119
a81f3066
VS
120 m_longlist = parser.Found(_T("longlist"));
121 m_list = m_longlist || parser.Found(_T("list"));
670ec357 122
c0d9b217 123 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
124}
125
126// Run
127//
128int TestApp::OnRun()
129{
3e5f6c1c 130 TestRunner runner;
a81f3066
VS
131
132 for (size_t i = 0; i < m_registries.size(); i++) {
133 auto_ptr<Test> test(m_registries[i].empty() ?
134 TestFactoryRegistry::getRegistry().makeTest() :
135 TestFactoryRegistry::getRegistry(m_registries[i]).makeTest());
136
137 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
138
139 if (suite && suite->countTestCases() == 0)
140 wxLogError(_T("No such test suite: %s"),
141 wxString(m_registries[i].c_str(), wxConvUTF8).c_str());
142 else if (m_list)
143 List(test.get());
144 else
145 runner.addTest(test.release());
670ec357 146 }
a81f3066 147
14dc53b2
MW
148 runner.setOutputter(new CompilerOutputter(&runner.result(), cout));
149
86ca2b4c 150#if wxUSE_LOG
a81f3066 151 // Switch off logging unless --verbose
3e5f6c1c
WS
152 bool verbose = wxLog::GetVerbose();
153 wxLog::EnableLogging(verbose);
86ca2b4c 154#else
3e5f6c1c
WS
155 bool verbose = false;
156#endif
157
158 return ( m_list || runner.run("", false, true, !verbose) )
159 ? EXIT_SUCCESS
160 : EXIT_FAILURE;
670ec357
VS
161}
162
c0d9b217
VZ
163int TestApp::OnExit()
164{
165#if wxUSE_GUI
166 delete GetTopWindow();
167#endif // wxUSE_GUI
168
169 return 0;
170}
171
670ec357
VS
172// List the tests
173//
8dae9169 174void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 175{
670ec357 176 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
177 string name;
178
bc10103e 179 if (suite) {
8dae9169
VS
180 // take the last component of the name and append to the parent
181 name = test->getName();
182 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
183 if (i != string::npos)
184 name = name.substr(i + 1);
185 name = parent + "." + name;
8dae9169
VS
186
187 // drop the 1st component from the display and indent
188 if (parent != "") {
189 string::size_type j = i = name.find('.', 1);
190 while ((j = name.find('.', j + 1)) != string::npos)
191 cout << " ";
192 cout << " " << name.substr(i + 1) << "\n";
193 }
a81f3066 194
f69577be 195 typedef vector<Test*> Tests;
670ec357
VS
196 typedef Tests::const_iterator Iter;
197
f69577be 198 const Tests& tests = suite->getTests();
670ec357
VS
199
200 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 201 List(*it, name);
670ec357 202 }
bc10103e
VS
203 else if (m_longlist) {
204 string::size_type i = 0;
205 while ((i = parent.find('.', i + 1)) != string::npos)
206 cout << " ";
207 cout << " " << test->getName() << "\n";
208 }
670ec357 209}