]>
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 | ||
10 | #if defined(__GNUG__) && !defined(__APPLE__) | |
11 | #pragma implementation | |
12 | #pragma interface | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx/wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | // for all others, include the necessary headers | |
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/cmdline.h" | |
28 | #include "wx/cppunit.h" | |
29 | #include <iostream> | |
30 | ||
3e5f6c1c WS |
31 | using CppUnit::Test; |
32 | using CppUnit::TestSuite; | |
33 | using CppUnit::TestFactoryRegistry; | |
34 | using CppUnit::TextUi::TestRunner; | |
35 | ||
36 | using std::string; | |
37 | using std::vector; | |
38 | using std::auto_ptr; | |
39 | using std::cout; | |
670ec357 VS |
40 | |
41 | // The application class | |
42 | // | |
43 | class TestApp : public wxAppConsole | |
44 | { | |
45 | public: | |
46 | TestApp(); | |
47 | ||
48 | // standard overrides | |
49 | void OnInitCmdLine(wxCmdLineParser& parser); | |
50 | bool OnCmdLineParsed(wxCmdLineParser& parser); | |
51 | bool OnInit(); | |
52 | int OnRun(); | |
53 | ||
54 | private: | |
8dae9169 | 55 | void List(Test *test, const string& parent = "") const; |
670ec357 VS |
56 | |
57 | // command lines options/parameters | |
58 | bool m_list; | |
a81f3066 VS |
59 | bool m_longlist; |
60 | vector<string> m_registries; | |
670ec357 VS |
61 | }; |
62 | ||
63 | IMPLEMENT_APP_CONSOLE(TestApp) | |
64 | ||
65 | TestApp::TestApp() | |
8dae9169 VS |
66 | : m_list(false), |
67 | m_longlist(false) | |
670ec357 VS |
68 | { |
69 | } | |
70 | ||
71 | // Init | |
72 | // | |
73 | bool TestApp::OnInit() | |
74 | { | |
75 | cout << "Test program for wxWidgets\n" | |
3e5f6c1c | 76 | << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl; |
670ec357 VS |
77 | return wxAppConsole::OnInit(); |
78 | }; | |
79 | ||
80 | // The table of command line options | |
81 | // | |
82 | void TestApp::OnInitCmdLine(wxCmdLineParser& parser) | |
83 | { | |
84 | wxAppConsole::OnInitCmdLine(parser); | |
85 | ||
86 | static const wxCmdLineEntryDesc cmdLineDesc[] = { | |
87 | { wxCMD_LINE_SWITCH, _T("l"), _T("list"), | |
a81f3066 VS |
88 | _T("list the test suites, do not run them"), |
89 | wxCMD_LINE_VAL_NONE, 0 }, | |
90 | { wxCMD_LINE_SWITCH, _T("L"), _T("longlist"), | |
91 | _T("list the test cases, do not run them"), | |
670ec357 VS |
92 | wxCMD_LINE_VAL_NONE, 0 }, |
93 | { wxCMD_LINE_PARAM, 0, 0, _T("REGISTRY"), wxCMD_LINE_VAL_STRING, | |
a81f3066 | 94 | wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE }, |
670ec357 VS |
95 | { wxCMD_LINE_NONE , 0, 0, 0, wxCMD_LINE_VAL_NONE, 0 } |
96 | }; | |
97 | ||
98 | parser.SetDesc(cmdLineDesc); | |
99 | } | |
100 | ||
101 | // Handle command line options | |
102 | // | |
103 | bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser) | |
104 | { | |
a81f3066 VS |
105 | if (parser.GetParamCount()) |
106 | for (size_t i = 0; i < parser.GetParamCount(); i++) | |
107 | m_registries.push_back(string(parser.GetParam(i).mb_str())); | |
108 | else | |
109 | m_registries.push_back(""); | |
3e5f6c1c | 110 | |
a81f3066 VS |
111 | m_longlist = parser.Found(_T("longlist")); |
112 | m_list = m_longlist || parser.Found(_T("list")); | |
670ec357 VS |
113 | |
114 | return wxAppConsole::OnCmdLineParsed(parser); | |
115 | } | |
116 | ||
117 | // Run | |
118 | // | |
119 | int TestApp::OnRun() | |
120 | { | |
3e5f6c1c | 121 | TestRunner runner; |
a81f3066 VS |
122 | |
123 | for (size_t i = 0; i < m_registries.size(); i++) { | |
124 | auto_ptr<Test> test(m_registries[i].empty() ? | |
125 | TestFactoryRegistry::getRegistry().makeTest() : | |
126 | TestFactoryRegistry::getRegistry(m_registries[i]).makeTest()); | |
127 | ||
128 | TestSuite *suite = dynamic_cast<TestSuite*>(test.get()); | |
129 | ||
130 | if (suite && suite->countTestCases() == 0) | |
131 | wxLogError(_T("No such test suite: %s"), | |
132 | wxString(m_registries[i].c_str(), wxConvUTF8).c_str()); | |
133 | else if (m_list) | |
134 | List(test.get()); | |
135 | else | |
136 | runner.addTest(test.release()); | |
670ec357 | 137 | } |
a81f3066 | 138 | |
86ca2b4c | 139 | #if wxUSE_LOG |
a81f3066 | 140 | // Switch off logging unless --verbose |
3e5f6c1c WS |
141 | bool verbose = wxLog::GetVerbose(); |
142 | wxLog::EnableLogging(verbose); | |
86ca2b4c | 143 | #else |
3e5f6c1c WS |
144 | bool verbose = false; |
145 | #endif | |
146 | ||
147 | return ( m_list || runner.run("", false, true, !verbose) ) | |
148 | ? EXIT_SUCCESS | |
149 | : EXIT_FAILURE; | |
670ec357 VS |
150 | } |
151 | ||
152 | // List the tests | |
153 | // | |
8dae9169 | 154 | void TestApp::List(Test *test, const string& parent /*=""*/) const |
670ec357 | 155 | { |
670ec357 | 156 | TestSuite *suite = dynamic_cast<TestSuite*>(test); |
8dae9169 VS |
157 | string name; |
158 | ||
bc10103e | 159 | if (suite) { |
8dae9169 VS |
160 | // take the last component of the name and append to the parent |
161 | name = test->getName(); | |
162 | string::size_type i = name.find_last_of(".:"); | |
163 | name = parent + "." + (i != string::npos ? name.substr(i + 1) : name); | |
164 | ||
165 | // drop the 1st component from the display and indent | |
166 | if (parent != "") { | |
167 | string::size_type j = i = name.find('.', 1); | |
168 | while ((j = name.find('.', j + 1)) != string::npos) | |
169 | cout << " "; | |
170 | cout << " " << name.substr(i + 1) << "\n"; | |
171 | } | |
a81f3066 | 172 | |
f69577be | 173 | typedef vector<Test*> Tests; |
670ec357 VS |
174 | typedef Tests::const_iterator Iter; |
175 | ||
f69577be | 176 | const Tests& tests = suite->getTests(); |
670ec357 VS |
177 | |
178 | for (Iter it = tests.begin(); it != tests.end(); ++it) | |
8dae9169 | 179 | List(*it, name); |
670ec357 | 180 | } |
bc10103e VS |
181 | else if (m_longlist) { |
182 | string::size_type i = 0; | |
183 | while ((i = parent.find('.', i + 1)) != string::npos) | |
184 | cout << " "; | |
185 | cout << " " << test->getName() << "\n"; | |
186 | } | |
670ec357 | 187 | } |