]> git.saurik.com Git - wxWidgets.git/blob - tests/test.cpp
fix off by one byg in ReserveId() (closes #10020)
[wxWidgets.git] / tests / test.cpp
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
10 // For compilers that support precompilation, includes "wx/wx.h"
11 // and "wx/cppunit.h"
12 #include "testprec.h"
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"
24 #include <iostream>
25
26 using CppUnit::Test;
27 using CppUnit::TestSuite;
28 using CppUnit::TestFactoryRegistry;
29
30 using namespace std;
31
32 #if wxUSE_GUI
33 typedef wxApp TestAppBase;
34 #else
35 typedef wxAppConsole TestAppBase;
36 #endif
37
38 // The application class
39 //
40 class TestApp : public TestAppBase
41 {
42 public:
43 TestApp();
44
45 // standard overrides
46 virtual void OnInitCmdLine(wxCmdLineParser& parser);
47 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
48 virtual bool OnInit();
49 virtual int OnRun();
50 virtual int OnExit();
51
52 private:
53 void List(Test *test, const string& parent = "") const;
54
55 // command lines options/parameters
56 bool m_list;
57 bool m_longlist;
58 vector<string> m_registries;
59 };
60
61 IMPLEMENT_APP_CONSOLE(TestApp)
62
63 TestApp::TestApp()
64 : m_list(false),
65 m_longlist(false)
66 {
67 }
68
69 // Init
70 //
71 bool TestApp::OnInit()
72 {
73 if ( !TestAppBase::OnInit() )
74 return false;
75
76 cout << "Test program for wxWidgets\n"
77 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
78
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;
85 }
86
87 // The table of command line options
88 //
89 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
90 {
91 TestAppBase::OnInitCmdLine(parser);
92
93 static const wxCmdLineEntryDesc cmdLineDesc[] = {
94 { wxCMD_LINE_SWITCH, "l", "list",
95 "list the test suites, do not run them",
96 wxCMD_LINE_VAL_NONE, 0 },
97 { wxCMD_LINE_SWITCH, "L", "longlist",
98 "list the test cases, do not run them",
99 wxCMD_LINE_VAL_NONE, 0 },
100 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
101 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
102 wxCMD_LINE_DESC_END
103 };
104
105 parser.SetDesc(cmdLineDesc);
106 }
107
108 // Handle command line options
109 //
110 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
111 {
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("");
117
118 m_longlist = parser.Found(_T("longlist"));
119 m_list = m_longlist || parser.Found(_T("list"));
120
121 return TestAppBase::OnCmdLineParsed(parser);
122 }
123
124 // Run
125 //
126 int TestApp::OnRun()
127 {
128 CppUnit::TextTestRunner runner;
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());
144 }
145
146 if ( m_list )
147 return EXIT_SUCCESS;
148
149 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
150
151 #if wxUSE_LOG
152 // Switch off logging unless --verbose
153 bool verbose = wxLog::GetVerbose();
154 wxLog::EnableLogging(verbose);
155 #else
156 bool verbose = false;
157 #endif
158
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;
167 }
168
169 int TestApp::OnExit()
170 {
171 #if wxUSE_GUI
172 delete GetTopWindow();
173 #endif // wxUSE_GUI
174
175 return 0;
176 }
177
178 // List the tests
179 //
180 void TestApp::List(Test *test, const string& parent /*=""*/) const
181 {
182 TestSuite *suite = dynamic_cast<TestSuite*>(test);
183 string name;
184
185 if (suite) {
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(".:");
189 if (i != string::npos)
190 name = name.substr(i + 1);
191 name = parent + "." + name;
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 }
200
201 typedef vector<Test*> Tests;
202 typedef Tests::const_iterator Iter;
203
204 const Tests& tests = suite->getTests();
205
206 for (Iter it = tests.begin(); it != tests.end(); ++it)
207 List(*it, name);
208 }
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 }
215 }