2 * CppU - C++ unit testing framework
3 * ---------------------------------
4 * Copyright (c)2007 Daniel Fiser <danfis@danfis.cz>
7 * This file is part of CppU.
9 * CppU is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * CppU is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Compile this file as object file (e.g.\ gcc -c -o cppu.o cppu.c) and
26 * link the object file with your test suite.
35 /************** TestBase **************/
36 void TestBase::printSummary(std::ostream
& out
) const
39 out
<< "==================================================" << endl
;
40 out
<< "| | failed | succeed | total |" << endl
;
41 out
<< "|------------------------------------------------|" << endl
;
42 out
<< "| assertations: | ";
44 out
<< left
<< assertationsFailed();
47 out
<< left
<< assertationsSucceed();
50 out
<< left
<< assertations();
54 out
<< left
<< testsFailed();
57 out
<< left
<< testsSucceed();
60 out
<< left
<< tests();
62 out
<< "| test cases: | ";
64 out
<< left
<< testCasesFailed();
67 out
<< left
<< testCasesSucceed();
70 out
<< left
<< testCases();
72 out
<< "| test suites: | ";
74 out
<< left
<< testSuitesFailed();
77 out
<< left
<< testSuitesSucceed();
80 out
<< left
<< testSuites();
82 out
<< "==================================================" << endl
;
84 /************** TestBase END **********/
87 /************** TestCase **************/
88 void TestCase::run(ostream
&out
, const int level
)
92 (*_out
) << string(2*level
, ' ') << "=> " << "started test case " << _name
<< endl
;
98 (*_out
) << string(2*level
, ' ') << "<= " << "ended test case " << _name
<< endl
;
103 string
TestCase::_getErrorMessage(const string file
, const int line
,
104 const string test_method
, const string error_message
) const
106 std::stringstream ss
;
110 return (file
+ ":" + l
+ ": " + _name
+ "::" + test_method
+ " :: " + error_message
);
113 void TestCase::_prepareTest(string name
)
115 _current_test
.clean();
116 _current_test
.test_name
= name
;
119 void TestCase::_finishTest(void)
121 _assertations_failed
+= _current_test
.fails
;
122 _assertations_succeed
+= _current_test
.successes
;
124 if (_current_test
.fails
> 0){
132 void TestCase::_successAssertation(void)
134 _current_test
.successes
++;
137 void TestCase::_failAssertation(string file
, int line
,
138 string error_message
)
140 _current_test
.fails
++;
141 (*_out
) << _getErrorMessage(file
, line
, _current_test
.test_name
,
145 /************** TestCase END **********/
149 /************** TestSuite *************/
150 TestSuite::~TestSuite()
155 void TestSuite::run(ostream
&out
, const int level
)
160 out
<< string(2*level
, ' ') << "=> " << "started test suite " << _name
<< endl
;
162 vector
<TestBase
*>::iterator it
= _tests
.begin();
163 vector
<TestBase
*>::iterator it_end
= _tests
.end();
164 for (;it
!= it_end
; it
++){
165 (*it
)->run(out
, level
+1);
168 out
<< string(2*level
, ' ') << "=> " << "ended test suite " << _name
<< endl
;
172 int TestSuite::assertations() const
176 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
177 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
178 for (;it
!= it_end
; it
++){
179 num
+= (*it
)->assertations();
184 int TestSuite::assertationsFailed() const
188 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
189 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
190 for (;it
!= it_end
; it
++){
191 num
+= (*it
)->assertationsFailed();
196 int TestSuite::assertationsSucceed() const
198 return assertations() - assertationsFailed();
201 int TestSuite::tests() const
205 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
206 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
207 for (;it
!= it_end
; it
++){
208 num
+= (*it
)->tests();
213 int TestSuite::testsFailed() const
217 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
218 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
219 for (;it
!= it_end
; it
++){
220 num
+= (*it
)->testsFailed();
225 int TestSuite::testsSucceed() const
227 return tests() - testsFailed();
230 int TestSuite::testCases() const
234 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
235 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
236 for (;it
!= it_end
; it
++){
237 if ((*it
)->isTestCase()){
239 }else if ((*it
)->isTestSuite()){
240 num
+= (*it
)->testCases();
246 int TestSuite::testCasesFailed() const
250 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
251 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
252 for (;it
!= it_end
; it
++){
253 if ((*it
)->isTestCase()){
254 if ((*it
)->testsFailed() > 0)
256 }else if ((*it
)->isTestSuite()){
257 num
+= (*it
)->testCasesFailed();
263 int TestSuite::testCasesSucceed() const
265 return testCases() - testCasesFailed();
268 int TestSuite::testSuites() const
272 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
273 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
274 for (;it
!= it_end
; it
++){
275 if ((*it
)->isTestSuite()){
276 num
+= 1 + (*it
)->testSuites();
282 int TestSuite::testSuitesFailed() const
286 vector
<TestBase
*>::const_iterator it
= _tests
.begin();
287 vector
<TestBase
*>::const_iterator it_end
= _tests
.end();
288 for (;it
!= it_end
; it
++){
289 if ((*it
)->isTestSuite()){
290 if ((*it
)->testCasesFailed() > 0
291 || (*it
)->testSuitesFailed() > 0)
293 num
+= (*it
)->testSuitesFailed();
299 int TestSuite::testSuitesSucceed() const
301 return testSuites() - testSuitesFailed();
305 void TestSuite::_freeTests()
307 vector
<TestBase
*>::iterator it
= _tests
.begin();
308 vector
<TestBase
*>::iterator it_end
= _tests
.end();
309 for (;it
!= it_end
; it
++){
314 /************** TestSuite END *********/
316 void runTest(TestBase
* test
, ostream
&out
)
319 test
->printSummary(out
);