Added assertation for detection if exception throwed.
[cppu.git] / cppu.cpp
blob7567e51d1b3c3c958557e919c20f18b612806ed4
1 /***
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/>.
23 /**
24 * \file cppu.cpp
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.
30 #include <iostream>
31 #include <sstream>
32 #include "cppu.h"
33 using namespace std;
35 /************** TestBase **************/
36 void TestBase::printSummary(std::ostream& out) const
38 out << endl;
39 out << "==================================================" << endl;
40 out << "| | failed | succeed | total |" << endl;
41 out << "|------------------------------------------------|" << endl;
42 out << "| assertations: | ";
43 out.width(6);
44 out << left << assertationsFailed();
45 out << " | ";
46 out.width(7);
47 out << left << assertationsSucceed();
48 out << " | ";
49 out.width(5);
50 out << left << assertations();
51 out << " |" << endl;
52 out << "| tests: | ";
53 out.width(6);
54 out << left << testsFailed();
55 out << " | ";
56 out.width(7);
57 out << left << testsSucceed();
58 out << " | ";
59 out.width(5);
60 out << left << tests();
61 out << " |" << endl;
62 out << "| test cases: | ";
63 out.width(6);
64 out << left << testCasesFailed();
65 out << " | ";
66 out.width(7);
67 out << left << testCasesSucceed();
68 out << " | ";
69 out.width(5);
70 out << left << testCases();
71 out << " |" << endl;
72 out << "| test suites: | ";
73 out.width(6);
74 out << left << testSuitesFailed();
75 out << " | ";
76 out.width(7);
77 out << left << testSuitesSucceed();
78 out << " | ";
79 out.width(5);
80 out << left << testSuites();
81 out << " |" << endl;
82 out << "==================================================" << endl;
84 /************** TestBase END **********/
87 /************** TestCase **************/
88 void TestCase::run(ostream &out, const int level)
90 this->_out = &out;
92 (*_out) << string(2*level, ' ') << "=> " << "started test case " << _name << endl;
94 setUp();
95 runTests();
96 tearDown();
98 (*_out) << string(2*level, ' ') << "<= " << "ended test case " << _name << endl;
102 //protected:
103 string TestCase::_getErrorMessage(const string file, const int line,
104 const string test_method, const string error_message) const
106 std::stringstream ss;
107 std::string l;
108 ss << line;
109 ss >> l;
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){
125 _tests_failed++;
126 }else{
127 _tests_succeed++;
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,
142 error_message)
143 << endl;
145 /************** TestCase END **********/
149 /************** TestSuite *************/
150 TestSuite::~TestSuite()
152 _freeTests();
155 void TestSuite::run(ostream &out, const int level)
157 _freeTests();
158 _registerAll();
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
174 int num = 0;
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();
182 return num;
184 int TestSuite::assertationsFailed() const
186 int num = 0;
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();
194 return num;
196 int TestSuite::assertationsSucceed() const
198 return assertations() - assertationsFailed();
201 int TestSuite::tests() const
203 int num = 0;
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();
211 return num;
213 int TestSuite::testsFailed() const
215 int num = 0;
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();
223 return num;
225 int TestSuite::testsSucceed() const
227 return tests() - testsFailed();
230 int TestSuite::testCases() const
232 int num = 0;
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()){
238 num ++;
239 }else if ((*it)->isTestSuite()){
240 num += (*it)->testCases();
244 return num;
246 int TestSuite::testCasesFailed() const
248 int num = 0;
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)
255 num ++;
256 }else if ((*it)->isTestSuite()){
257 num += (*it)->testCasesFailed();
261 return num;
263 int TestSuite::testCasesSucceed() const
265 return testCases() - testCasesFailed();
268 int TestSuite::testSuites() const
270 int num = 0;
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();
280 return num;
282 int TestSuite::testSuitesFailed() const
284 int num = 0;
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)
292 num++;
293 num += (*it)->testSuitesFailed();
297 return num;
299 int TestSuite::testSuitesSucceed() const
301 return testSuites() - testSuitesFailed();
304 //protected:
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++){
310 delete (*it);
312 _tests.clear();
314 /************** TestSuite END *********/
316 void runTest(TestBase * test, ostream &out)
318 test->run(out);
319 test->printSummary(out);
320 delete test;