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