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