applied path 906264: test program
[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 #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
31 using namespace std;
32 using namespace CppUnit;
33
34 // The application class
35 //
36 class TestApp : public wxAppConsole
37 {
38 public:
39 TestApp();
40
41 // standard overrides
42 void OnInitCmdLine(wxCmdLineParser& parser);
43 bool OnCmdLineParsed(wxCmdLineParser& parser);
44 bool OnInit();
45 int OnRun();
46
47 private:
48 void List(Test *test, int depth = 0) const;
49
50 // command lines options/parameters
51 bool m_list;
52 string m_registry;
53 };
54
55 IMPLEMENT_APP_CONSOLE(TestApp)
56
57 TestApp::TestApp()
58 : m_list(false)
59 {
60 }
61
62 // Init
63 //
64 bool TestApp::OnInit()
65 {
66 cout << "Test program for wxWidgets\n"
67 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << endl;
68 return wxAppConsole::OnInit();
69 };
70
71 // The table of command line options
72 //
73 void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
74 {
75 wxAppConsole::OnInitCmdLine(parser);
76
77 static const wxCmdLineEntryDesc cmdLineDesc[] = {
78 { wxCMD_LINE_SWITCH, _T("l"), _T("list"),
79 _T("list the tests, do not run them"),
80 wxCMD_LINE_VAL_NONE, 0 },
81 { wxCMD_LINE_PARAM, 0, 0, _T("REGISTRY"), wxCMD_LINE_VAL_STRING,
82 wxCMD_LINE_PARAM_OPTIONAL },
83 { wxCMD_LINE_NONE , 0, 0, 0, wxCMD_LINE_VAL_NONE, 0 }
84 };
85
86 parser.SetDesc(cmdLineDesc);
87 }
88
89 // Handle command line options
90 //
91 bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
92 {
93 if (parser.GetParamCount() > 0)
94 m_registry = parser.GetParam(0).mb_str();
95
96 m_list = parser.Found(_T("list"));
97
98 return wxAppConsole::OnCmdLineParsed(parser);
99 }
100
101 // Run
102 //
103 int TestApp::OnRun()
104 {
105 Test *test = m_registry.empty()?
106 TestFactoryRegistry::getRegistry().makeTest() :
107 TestFactoryRegistry::getRegistry(m_registry).makeTest();
108
109 if (m_list) {
110 List(test);
111 return EXIT_SUCCESS;
112 } else {
113 TextUi::TestRunner runner;
114 runner.addTest(test);
115 return runner.run("", false, true, false) ? EXIT_SUCCESS : EXIT_FAILURE;
116 }
117 }
118
119 // List the tests
120 //
121 void TestApp::List(Test *test, int depth /*=0*/) const
122 {
123 cout << string(depth * 2, ' ') << test->getName() << "\n";
124
125 TestSuite *suite = dynamic_cast<TestSuite*>(test);
126
127 if (suite) {
128 typedef const std::vector<Test*> Tests;
129 typedef Tests::const_iterator Iter;
130
131 Tests& tests = suite->getTests();
132
133 for (Iter it = tests.begin(); it != tests.end(); ++it)
134 List(*it, depth + 1);
135 }
136 }