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