]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
Do the right thing for Smartphone if creating button sizer
[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;
30
31using std::string;
32using std::vector;
33using std::auto_ptr;
34using std::cout;
670ec357
VS
35
36// The application class
37//
38class TestApp : public wxAppConsole
39{
40public:
41 TestApp();
42
43 // standard overrides
44 void OnInitCmdLine(wxCmdLineParser& parser);
45 bool OnCmdLineParsed(wxCmdLineParser& parser);
46 bool OnInit();
47 int OnRun();
48
49private:
8dae9169 50 void List(Test *test, const string& parent = "") const;
670ec357
VS
51
52 // command lines options/parameters
53 bool m_list;
a81f3066
VS
54 bool m_longlist;
55 vector<string> m_registries;
670ec357
VS
56};
57
58IMPLEMENT_APP_CONSOLE(TestApp)
59
60TestApp::TestApp()
8dae9169
VS
61 : m_list(false),
62 m_longlist(false)
670ec357
VS
63{
64}
65
66// Init
67//
68bool TestApp::OnInit()
69{
70 cout << "Test program for wxWidgets\n"
3e5f6c1c 71 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
670ec357
VS
72 return wxAppConsole::OnInit();
73};
74
75// The table of command line options
76//
77void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
78{
79 wxAppConsole::OnInitCmdLine(parser);
80
81 static const wxCmdLineEntryDesc cmdLineDesc[] = {
82 { wxCMD_LINE_SWITCH, _T("l"), _T("list"),
a81f3066
VS
83 _T("list the test suites, do not run them"),
84 wxCMD_LINE_VAL_NONE, 0 },
85 { wxCMD_LINE_SWITCH, _T("L"), _T("longlist"),
86 _T("list the test cases, do not run them"),
670ec357
VS
87 wxCMD_LINE_VAL_NONE, 0 },
88 { wxCMD_LINE_PARAM, 0, 0, _T("REGISTRY"), wxCMD_LINE_VAL_STRING,
a81f3066 89 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
670ec357
VS
90 { wxCMD_LINE_NONE , 0, 0, 0, wxCMD_LINE_VAL_NONE, 0 }
91 };
92
93 parser.SetDesc(cmdLineDesc);
94}
95
96// Handle command line options
97//
98bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
99{
a81f3066
VS
100 if (parser.GetParamCount())
101 for (size_t i = 0; i < parser.GetParamCount(); i++)
102 m_registries.push_back(string(parser.GetParam(i).mb_str()));
103 else
104 m_registries.push_back("");
3e5f6c1c 105
a81f3066
VS
106 m_longlist = parser.Found(_T("longlist"));
107 m_list = m_longlist || parser.Found(_T("list"));
670ec357
VS
108
109 return wxAppConsole::OnCmdLineParsed(parser);
110}
111
112// Run
113//
114int TestApp::OnRun()
115{
3e5f6c1c 116 TestRunner runner;
a81f3066
VS
117
118 for (size_t i = 0; i < m_registries.size(); i++) {
119 auto_ptr<Test> test(m_registries[i].empty() ?
120 TestFactoryRegistry::getRegistry().makeTest() :
121 TestFactoryRegistry::getRegistry(m_registries[i]).makeTest());
122
123 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
124
125 if (suite && suite->countTestCases() == 0)
126 wxLogError(_T("No such test suite: %s"),
127 wxString(m_registries[i].c_str(), wxConvUTF8).c_str());
128 else if (m_list)
129 List(test.get());
130 else
131 runner.addTest(test.release());
670ec357 132 }
a81f3066 133
86ca2b4c 134#if wxUSE_LOG
a81f3066 135 // Switch off logging unless --verbose
3e5f6c1c
WS
136 bool verbose = wxLog::GetVerbose();
137 wxLog::EnableLogging(verbose);
86ca2b4c 138#else
3e5f6c1c
WS
139 bool verbose = false;
140#endif
141
142 return ( m_list || runner.run("", false, true, !verbose) )
143 ? EXIT_SUCCESS
144 : EXIT_FAILURE;
670ec357
VS
145}
146
147// List the tests
148//
8dae9169 149void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 150{
670ec357 151 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
152 string name;
153
bc10103e 154 if (suite) {
8dae9169
VS
155 // take the last component of the name and append to the parent
156 name = test->getName();
157 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
158 if (i != string::npos)
159 name = name.substr(i + 1);
160 name = parent + "." + name;
8dae9169
VS
161
162 // drop the 1st component from the display and indent
163 if (parent != "") {
164 string::size_type j = i = name.find('.', 1);
165 while ((j = name.find('.', j + 1)) != string::npos)
166 cout << " ";
167 cout << " " << name.substr(i + 1) << "\n";
168 }
a81f3066 169
f69577be 170 typedef vector<Test*> Tests;
670ec357
VS
171 typedef Tests::const_iterator Iter;
172
f69577be 173 const Tests& tests = suite->getTests();
670ec357
VS
174
175 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 176 List(*it, name);
670ec357 177 }
bc10103e
VS
178 else if (m_longlist) {
179 string::size_type i = 0;
180 while ((i = parent.find('.', i + 1)) != string::npos)
181 cout << " ";
182 cout << " " << test->getName() << "\n";
183 }
670ec357 184}