1 //
2 // TestRunner.h
3 //
4 
5 
6 #ifndef CppUnit_TestRunner_INCLUDED
7 #define CppUnit_TestRunner_INCLUDED
8 
9 
10 #include "CppUnit/CppUnit.h"
11 #include <vector>
12 #include <string>
13 #include <ostream>
14 #if defined(POCO_VXWORKS)
15 #include <cstdarg>
16 #endif
17 
18 
19 namespace CppUnit {
20 
21 
22 class Test;
23 
24 
25 /*
26  * A command line based tool to run tests.
27  * TestRunner expects as its only argument the name of a TestCase class.
28  * TestRunner prints out a trace as the tests are executed followed by a
29  * summary at the end.
30  *
31  * You can add to the tests that the TestRunner knows about by
32  * making additional calls to "addTest (...)" in main.
33  *
34  * Here is the synopsis:
35  *
36  * TestRunner [-all] [-long] [-print] [-wait] ExampleTestCase
37  *
38  */
39 class CppUnit_API TestRunner
40 {
41 	typedef std::pair<std::string, Test*> Mapping;
42 	typedef std::vector<Mapping> Mappings;
43 
44 public:
45 	TestRunner();
46 	TestRunner(std::ostream& ostr);
47 	~TestRunner();
48 
49 	bool run(const std::vector<std::string>& args);
50 	void addTest(const std::string& name, Test* test);
51 
52 protected:
53 	void printBanner();
54 	void print(const std::string& name, Test* pTest, int indent);
55 	Test* find(const std::string& name, Test* pTest, const std::string& testName);
56 	int collectAllTestCases(Test* pTest, std::vector<Test*>& tests);
57 
58 private:
59 	std::ostream& _ostr;
60 	Mappings _mappings;
61 };
62 
63 
64 } // namespace CppUnit
65 
66 
67 #if defined(POCO_VXWORKS)
68 #define CppUnitMain(testCase) \
69 	int testCase##Runner(const char* arg0, ...) \
70 	{ \
71 		std::vector<std::string> args; \
72 		args.push_back(#testCase "Runner"); \
73 		args.push_back(std::string(arg0)); \
74 		va_list vargs; \
75 		va_start(vargs, arg0); \
76 		const char* arg = va_arg(vargs, const char*); \
77 		while (arg) \
78 		{ \
79 			args.push_back(std::string(arg)); \
80 			arg = va_arg(vargs, const char*); \
81 		} \
82 		va_end(vargs); \
83 		CppUnit::TestRunner runner; \
84 		runner.addTest(#testCase, testCase::suite()); \
85 		return runner.run(args) ? 0 : 1; \
86 	}
87 #else
88 #define CppUnitMain(testCase) \
89 	int main(int ac, char **av)							\
90 	{													\
91 		std::vector<std::string> args;					\
92 		for (int i = 0; i < ac; ++i)					\
93 			args.push_back(std::string(av[i]));			\
94 		CppUnit::TestRunner runner;						\
95 		runner.addTest(#testCase, testCase::suite());	\
96 		return runner.run(args) ? 0 : 1;				\
97 	}
98 #endif
99 
100 
101 #endif // CppUnit_TestRunner_INCLUDED
102