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