]> git.saurik.com Git - wxWidgets.git/blob - tests/test.cpp
minor icc warning fixes
[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/beforestd.h"
24 #ifdef __VISUALC__
25 #pragma warning(disable:4100)
26 #endif
27 #include <cppunit/TestListener.h>
28 #ifdef __VISUALC__
29 #pragma warning(default:4100)
30 #endif
31 #include <cppunit/Test.h>
32 #include <cppunit/TestResult.h>
33 #include "wx/afterstd.h"
34
35 #include "wx/cmdline.h"
36 #include <iostream>
37
38 using CppUnit::Test;
39 using CppUnit::TestSuite;
40 using CppUnit::TestFactoryRegistry;
41
42
43 // Displays the test name before starting to execute it: this helps with
44 // diagnosing where exactly does a test crash or hang when/if it does.
45 class DetailListener : public CppUnit::TestListener
46 {
47 public:
48 DetailListener(bool doTiming = false):
49 CppUnit::TestListener(),
50 m_timing(doTiming)
51 {
52 }
53
54 virtual void startTest(CppUnit::Test *test)
55 {
56 std::cout << test->getName () << " ";
57 m_watch.Start();
58 }
59
60 virtual void endTest(CppUnit::Test * WXUNUSED(test))
61 {
62 m_watch.Pause();
63 if ( m_timing )
64 std::cout << " (in "<< m_watch.Time() << " ms )";
65 std::cout << "\n";
66 }
67
68 protected :
69 bool m_timing;
70 wxStopWatch m_watch;
71 };
72
73 using namespace std;
74
75 #if wxUSE_GUI
76 typedef wxApp TestAppBase;
77 #else
78 typedef wxAppConsole TestAppBase;
79 #endif
80
81 // The application class
82 //
83 class TestApp : public TestAppBase
84 {
85 public:
86 TestApp();
87
88 // standard overrides
89 virtual void OnInitCmdLine(wxCmdLineParser& parser);
90 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
91 virtual bool OnInit();
92 virtual int OnRun();
93 virtual int OnExit();
94
95 private:
96 void List(Test *test, const string& parent = "") const;
97
98 // command lines options/parameters
99 bool m_list;
100 bool m_longlist;
101 bool m_detail;
102 bool m_timing;
103 vector<string> m_registries;
104 };
105
106 IMPLEMENT_APP_CONSOLE(TestApp)
107
108 TestApp::TestApp()
109 : m_list(false),
110 m_longlist(false)
111 {
112 }
113
114 // Init
115 //
116 bool TestApp::OnInit()
117 {
118 if ( !TestAppBase::OnInit() )
119 return false;
120
121 cout << "Test program for wxWidgets\n"
122 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
123
124 #if wxUSE_GUI
125 // create a hidden parent window to be used as parent for the GUI controls
126 new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
127 #endif // wxUSE_GUI
128
129 return true;
130 }
131
132 // The table of command line options
133 //
134 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
135 {
136 TestAppBase::OnInitCmdLine(parser);
137
138 static const wxCmdLineEntryDesc cmdLineDesc[] = {
139 { wxCMD_LINE_SWITCH, "l", "list",
140 "list the test suites, do not run them",
141 wxCMD_LINE_VAL_NONE, 0 },
142 { wxCMD_LINE_SWITCH, "L", "longlist",
143 "list the test cases, do not run them",
144 wxCMD_LINE_VAL_NONE, 0 },
145 { wxCMD_LINE_SWITCH, "d", "detail",
146 "print the test case names, run them",
147 wxCMD_LINE_VAL_NONE, 0 },
148 { wxCMD_LINE_SWITCH, "t", "timing",
149 "print names and mesure running time of individual test, run them",
150 wxCMD_LINE_VAL_NONE, 0 },
151 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
152 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
153 wxCMD_LINE_DESC_END
154 };
155
156 parser.SetDesc(cmdLineDesc);
157 }
158
159 // Handle command line options
160 //
161 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
162 {
163 if (parser.GetParamCount())
164 for (size_t i = 0; i < parser.GetParamCount(); i++)
165 m_registries.push_back(string(parser.GetParam(i).mb_str()));
166 else
167 m_registries.push_back("");
168
169 m_longlist = parser.Found(_T("longlist"));
170 m_list = m_longlist || parser.Found(_T("list"));
171 m_timing = parser.Found(_T("timing"));
172 m_detail = !m_timing && parser.Found(_T("detail"));
173
174 return TestAppBase::OnCmdLineParsed(parser);
175 }
176
177 // Run
178 //
179 int TestApp::OnRun()
180 {
181 CppUnit::TextTestRunner runner;
182
183 for (size_t i = 0; i < m_registries.size(); i++) {
184 auto_ptr<Test> test(m_registries[i].empty() ?
185 TestFactoryRegistry::getRegistry().makeTest() :
186 TestFactoryRegistry::getRegistry(m_registries[i]).makeTest());
187
188 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
189
190 if (suite && suite->countTestCases() == 0)
191 wxLogError(_T("No such test suite: %s"),
192 wxString(m_registries[i].c_str(), wxConvUTF8).c_str());
193 else if (m_list)
194 List(test.get());
195 else
196 runner.addTest(test.release());
197 }
198
199 if ( m_list )
200 return EXIT_SUCCESS;
201
202 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
203
204 #if wxUSE_LOG
205 // Switch off logging unless --verbose
206 bool verbose = wxLog::GetVerbose();
207 wxLog::EnableLogging(verbose);
208 #else
209 bool verbose = false;
210 #endif
211
212 // there is a bug
213 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
214 // in some versions of cppunit: they write progress dots to cout (and not
215 // cerr) and don't flush it so all the dots appear at once at the end which
216 // is not very useful so unbuffer cout to work around this
217 cout.setf(ios::unitbuf);
218
219 // add detail listener if needed
220 DetailListener detailListener(m_timing);
221 if ( m_detail || m_timing )
222 runner.eventManager().addListener(&detailListener);
223
224 return runner.run("", false, true, !verbose) ? EXIT_SUCCESS : EXIT_FAILURE;
225 }
226
227 int TestApp::OnExit()
228 {
229 #if wxUSE_GUI
230 delete GetTopWindow();
231 #endif // wxUSE_GUI
232
233 return 0;
234 }
235
236 // List the tests
237 //
238 void TestApp::List(Test *test, const string& parent /*=""*/) const
239 {
240 TestSuite *suite = dynamic_cast<TestSuite*>(test);
241 string name;
242
243 if (suite) {
244 // take the last component of the name and append to the parent
245 name = test->getName();
246 string::size_type i = name.find_last_of(".:");
247 if (i != string::npos)
248 name = name.substr(i + 1);
249 name = parent + "." + name;
250
251 // drop the 1st component from the display and indent
252 if (parent != "") {
253 string::size_type j = i = name.find('.', 1);
254 while ((j = name.find('.', j + 1)) != string::npos)
255 cout << " ";
256 cout << " " << name.substr(i + 1) << "\n";
257 }
258
259 typedef vector<Test*> Tests;
260 typedef Tests::const_iterator Iter;
261
262 const Tests& tests = suite->getTests();
263
264 for (Iter it = tests.begin(); it != tests.end(); ++it)
265 List(*it, name);
266 }
267 else if (m_longlist) {
268 string::size_type i = 0;
269 while ((i = parent.find('.', i + 1)) != string::npos)
270 cout << " ";
271 cout << " " << test->getName() << "\n";
272 }
273 }