1 #ifndef _1_cppunit_cpp_
2 #define _1_cppunit_cpp_
5 * Copyright (c) 2008, Robert Emerson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Robert Emerson nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY ROBERT EMERSON ''AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL ROBERT EMERSON BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <cppunit/CompilerOutputter.h>
40 #include <cppunit/TextOutputter.h>
41 #include <cppunit/XmlOutputter.h>
42 #include <cppunit/extensions/TestFactoryRegistry.h>
43 #include <cppunit/TestResult.h>
44 #include <cppunit/TestResultCollector.h>
45 #include <cppunit/TestRunner.h>
46 #include <cppunit/TextTestProgressListener.h>
47 #include <cppunit/BriefTestProgressListener.h>
48 #include <cppunit/TestSuccessListener.h>
49 #include <cppunit/Portability.h>
51 using namespace CPPUNIT_NS;
62 static const int RET_OK = 0;
63 static const int RET_USAGE = -1;
64 static const int RET_BAD_OUTPUTTER = -2;
65 static const int RET_BAD_LISTENER = -3;
66 static const int RET_BAD_TEST = -4;
68 typedef map<string, Outputter *> OutputterMap;
69 typedef map<string, TestListener *> ListenerMap;
71 void usage(const char *image,
72 const char *defaultTest,
73 const OutputterMap &outputters,
74 const ListenerMap &listeners)
77 cout << "Usage: " << basename(image)
78 << " (-o outputter) (-p progress) (-t test) (-r count)" << endl;
79 cout << " " << basename(image)
80 << " [ -l | -h ]" << endl;
82 cout << "A CppUnit command line test execution tool. " << endl;
84 cout << "Built against cppunit version: " << CPPUNIT_VERSION << endl;
86 cout << "Options:" << endl;
88 cout << " -o Output style. Choices: ";
91 OutputterMap::const_iterator it = outputters.begin();
93 while (it != outputters.end()) {
94 cout << it->first << " ";
99 cout << "Default: compiler" << endl;
100 cout << " -p Progress style. Choices: ";
102 ListenerMap::const_iterator it = listeners.begin();
104 while (it != listeners.end()) {
105 cout << it->first << " ";
110 cout << "Default: dots";
112 cout << " -t Runs given the test only. Default: \"" << defaultTest << "\"" << endl;
113 cout << " -r Repeat count. Default: 1" << endl;
115 cout << " -l List all available tests." << endl;
116 cout << " -h Print this usage message." << endl;
118 cout << "Returns 0 on success, negative for usage errors, positive errors plus failures." << endl;
120 cout << "If the image name is a valid test name, then this is the default test executed." << endl;
126 // Recursive dumps the given Test heirarchy to cout
127 void dump(Test *test)
129 if (0 == test) return;
131 cout << test->getName() << endl;
133 if (0 == test->getChildTestCount()) return;
135 for (int i = 0; i < test->getChildTestCount(); i++) {
136 dump(test->getChildTestAt(i));
142 // Recursively seeks the Test matching the given name, returns 0 otherwise.
143 Test *find(Test *test, const string &name)
145 if (0 == test) return 0;
146 if (name == test->getName()) return test;
147 if (0 == test->getChildTestCount()) return 0;
149 for (int i = 0; i < test->getChildTestCount(); i++) {
150 Test *found = find(test->getChildTestAt(i), name);
151 if (found) return found;
157 int main(int argc, char **argv)
160 TestResultCollector collector;
161 result.addListener(&collector);
163 OutputterMap allOutputters;
165 allOutputters.insert(make_pair("compiler",
166 new CompilerOutputter(&collector, cout)));
167 allOutputters.insert(make_pair("text",
168 new TextOutputter(&collector, cout)));
169 allOutputters.insert(make_pair("xml",
170 new XmlOutputter(&collector, cout)));
171 allOutputters.insert(make_pair("none", static_cast<Outputter *>(0)));
173 Outputter *outputter = allOutputters.find("compiler")->second;
175 ListenerMap allListeners;
177 allListeners.insert(make_pair("dots", new TextTestProgressListener()));
178 allListeners.insert(make_pair("brief", new BriefTestProgressListener()));
179 allListeners.insert(make_pair("none", static_cast<TestListener *>(0)));
181 TestListener *listener = allListeners.find("dots")->second;
183 string runTest = basename(argv[0]);
185 if (!find(TestFactoryRegistry::getRegistry().makeTest(), runTest)) {
186 runTest = "All Tests";
191 while ((flag = getopt(argc, argv, "r:t:o:p:lh")) != -1) {
196 repeat = atoi(optarg);
201 OutputterMap::const_iterator it = allOutputters.find(optarg);
202 if (it == allOutputters.end()) {
203 cerr << "Unknown outputter: " << optarg << endl;
204 return RET_BAD_OUTPUTTER;
206 outputter = it->second;
212 string progress(optarg);
213 ListenerMap::const_iterator it = allListeners.find(optarg);
214 if (it == allListeners.end()) {
215 cerr << "Unknown listener: " << optarg << endl;
216 return RET_BAD_LISTENER;
218 listener = it->second;
224 Test *all = TestFactoryRegistry::getRegistry().makeTest();
228 break; // not reached
238 usage(argv[0], runTest.c_str(), allOutputters, allListeners);
240 break; // not reached
243 if (listener != 0) result.addListener(listener);
245 Test *run = find(TestFactoryRegistry::getRegistry().makeTest(), runTest);
247 cerr << "Unknown test case: " << runTest << endl;
254 for (int i = 0; i < repeat; i++) {
258 if (outputter) outputter->write();
260 return collector.testErrors() + collector.testFailures();
264 #endif // _1_cppunit_cpp_