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