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