1 | // |
2 | // TestRunner.cpp |
3 | // |
4 | |
5 | |
6 | #include "Poco/CppUnit/TestRunner.h" |
7 | #include "Poco/CppUnit/Test.h" |
8 | #include "Poco/CppUnit/TestSuite.h" |
9 | #include "Poco/CppUnit/TextTestResult.h" |
10 | #include <iostream> |
11 | |
12 | |
13 | namespace CppUnit { |
14 | |
15 | |
16 | TestRunner::TestRunner(): |
17 | _ostr(std::clog) |
18 | { |
19 | } |
20 | |
21 | |
22 | TestRunner::TestRunner(std::ostream& ostr): |
23 | _ostr(ostr) |
24 | { |
25 | } |
26 | |
27 | |
28 | TestRunner::~TestRunner() |
29 | { |
30 | for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) |
31 | delete it->second; |
32 | } |
33 | |
34 | |
35 | void TestRunner::printBanner() |
36 | { |
37 | _ostr |
38 | << "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl |
39 | << " where name is the name of a test case class" << std::endl; |
40 | } |
41 | |
42 | |
43 | bool TestRunner::run(const std::vector<std::string>& args) |
44 | { |
45 | std::string testCase; |
46 | int numberOfTests = 0; |
47 | bool success = true; |
48 | bool all = false; |
49 | bool wait = false; |
50 | bool printed = false; |
51 | |
52 | for (int i = 1; i < args.size(); i++) |
53 | { |
54 | const std::string& arg = args[i]; |
55 | if (arg == "-wait" ) |
56 | { |
57 | wait = true; |
58 | continue; |
59 | } |
60 | else if (arg == "-all" ) |
61 | { |
62 | all = true; |
63 | continue; |
64 | } |
65 | else if (arg == "-print" ) |
66 | { |
67 | for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) |
68 | { |
69 | print(it->first, it->second, 0); |
70 | } |
71 | printed = true; |
72 | continue; |
73 | } |
74 | |
75 | if (!all) |
76 | { |
77 | testCase = arg; |
78 | |
79 | if (testCase == "" ) |
80 | { |
81 | printBanner(); |
82 | return false; |
83 | } |
84 | |
85 | Test* testToRun = 0; |
86 | for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it) |
87 | { |
88 | testToRun = find(testCase, it->second, it->first); |
89 | } |
90 | if (testToRun) |
91 | { |
92 | if (!run(testToRun)) success = false; |
93 | } |
94 | numberOfTests++; |
95 | |
96 | if (!testToRun) |
97 | { |
98 | _ostr << "Test " << testCase << " not found." << std::endl; |
99 | return false; |
100 | } |
101 | } |
102 | } |
103 | |
104 | if (all) |
105 | { |
106 | for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) |
107 | { |
108 | if (!run(it->second)) success = false; |
109 | numberOfTests++; |
110 | } |
111 | } |
112 | |
113 | if (numberOfTests == 0 && !printed) |
114 | { |
115 | printBanner(); |
116 | return false; |
117 | } |
118 | |
119 | if (wait) |
120 | { |
121 | _ostr << "<RETURN> to continue" << std::endl; |
122 | std::cin.get(); |
123 | } |
124 | |
125 | return success; |
126 | } |
127 | |
128 | |
129 | bool TestRunner::run(Test* test) |
130 | { |
131 | TextTestResult result(_ostr); |
132 | |
133 | test->run(&result); |
134 | _ostr << result << std::endl; |
135 | |
136 | return result.wasSuccessful(); |
137 | } |
138 | |
139 | |
140 | void TestRunner::addTest(const std::string& name, Test* test) |
141 | { |
142 | _mappings.push_back(Mapping(name, test)); |
143 | } |
144 | |
145 | |
146 | void TestRunner::print(const std::string& name, Test* pTest, int indent) |
147 | { |
148 | for (int i = 0; i < indent; ++i) |
149 | _ostr << " " ; |
150 | _ostr << name << std::endl; |
151 | TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest); |
152 | if (pSuite) |
153 | { |
154 | const std::vector<Test*>& tests = pSuite->tests(); |
155 | for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it) |
156 | { |
157 | print((*it)->toString(), *it, indent + 1); |
158 | } |
159 | } |
160 | } |
161 | |
162 | |
163 | Test* TestRunner::find(const std::string& name, Test* pTest, const std::string& testName) |
164 | { |
165 | if (testName.find(name) != std::string::npos) |
166 | { |
167 | return pTest; |
168 | } |
169 | else |
170 | { |
171 | TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest); |
172 | if (pSuite) |
173 | { |
174 | const std::vector<Test*>& tests = pSuite->tests(); |
175 | for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it) |
176 | { |
177 | Test* result = find(name, *it, (*it)->toString()); |
178 | if (result) return result; |
179 | } |
180 | } |
181 | return 0; |
182 | } |
183 | } |
184 | |
185 | |
186 | } // namespace CppUnit |
187 | |