]> git.saurik.com Git - wxWidgets.git/blob - tests/test.cpp
corrected version in the libraries names: it's only 2 digits even in development...
[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/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 return wxAppConsole::OnInit();
74 };
75
76 // The table of command line options
77 //
78 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
79 {
80 wxAppConsole::OnInitCmdLine(parser);
81
82 static const wxCmdLineEntryDesc cmdLineDesc[] = {
83 { wxCMD_LINE_SWITCH, _T("l"), _T("list"),
84 _T("list the test suites, do not run them"),
85 wxCMD_LINE_VAL_NONE, 0 },
86 { wxCMD_LINE_SWITCH, _T("L"), _T("longlist"),
87 _T("list the test cases, do not run them"),
88 wxCMD_LINE_VAL_NONE, 0 },
89 { wxCMD_LINE_PARAM, 0, 0, _T("REGISTRY"), wxCMD_LINE_VAL_STRING,
90 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
91 { wxCMD_LINE_NONE , 0, 0, 0, wxCMD_LINE_VAL_NONE, 0 }
92 };
93
94 parser.SetDesc(cmdLineDesc);
95 }
96
97 // Handle command line options
98 //
99 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
100 {
101 if (parser.GetParamCount())
102 for (size_t i = 0; i < parser.GetParamCount(); i++)
103 m_registries.push_back(string(parser.GetParam(i).mb_str()));
104 else
105 m_registries.push_back("");
106
107 m_longlist = parser.Found(_T("longlist"));
108 m_list = m_longlist || parser.Found(_T("list"));
109
110 return wxAppConsole::OnCmdLineParsed(parser);
111 }
112
113 // Run
114 //
115 int TestApp::OnRun()
116 {
117 TestRunner runner;
118
119 for (size_t i = 0; i < m_registries.size(); i++) {
120 auto_ptr<Test> test(m_registries[i].empty() ?
121 TestFactoryRegistry::getRegistry().makeTest() :
122 TestFactoryRegistry::getRegistry(m_registries[i]).makeTest());
123
124 TestSuite *suite = dynamic_cast<TestSuite*>(test.get());
125
126 if (suite && suite->countTestCases() == 0)
127 wxLogError(_T("No such test suite: %s"),
128 wxString(m_registries[i].c_str(), wxConvUTF8).c_str());
129 else if (m_list)
130 List(test.get());
131 else
132 runner.addTest(test.release());
133 }
134
135 runner.setOutputter(new CompilerOutputter(&runner.result(), cout));
136
137 #if wxUSE_LOG
138 // Switch off logging unless --verbose
139 bool verbose = wxLog::GetVerbose();
140 wxLog::EnableLogging(verbose);
141 #else
142 bool verbose = false;
143 #endif
144
145 return ( m_list || runner.run("", false, true, !verbose) )
146 ? EXIT_SUCCESS
147 : EXIT_FAILURE;
148 }
149
150 // List the tests
151 //
152 void TestApp::List(Test *test, const string& parent /*=""*/) const
153 {
154 TestSuite *suite = dynamic_cast<TestSuite*>(test);
155 string name;
156
157 if (suite) {
158 // take the last component of the name and append to the parent
159 name = test->getName();
160 string::size_type i = name.find_last_of(".:");
161 if (i != string::npos)
162 name = name.substr(i + 1);
163 name = parent + "." + 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 }
172
173 typedef vector<Test*> Tests;
174 typedef Tests::const_iterator Iter;
175
176 const Tests& tests = suite->getTests();
177
178 for (Iter it = tests.begin(); it != tests.end(); ++it)
179 List(*it, name);
180 }
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 }
187 }