TestInterface renamed to TestBase - It's now abstract class no interface.
[cppu.git] / cppu.cpp
blob02875fb656c30409cf76b10f825b7cdfcc3b7760
1 #include <iostream>
2 #include <sstream>
3 #include "cppu.h"
4 using namespace std;
6 /************** TestBase **************/
7 void TestBase::printSummary(std::ostream& out) const
9 out << endl;
10 out << "==================================================" << endl;
11 out << "| | failed | succeed | total |" << endl;
12 out << "|------------------------------------------------|" << endl;
13 out << "| assertations: | ";
14 out.width(6);
15 out << left << assertationsFailed();
16 out << " | ";
17 out.width(7);
18 out << left << assertationsSucceed();
19 out << " | ";
20 out.width(5);
21 out << left << assertations();
22 out << " |" << endl;
23 out << "| tests: | ";
24 out.width(6);
25 out << left << testsFailed();
26 out << " | ";
27 out.width(7);
28 out << left << testsSucceed();
29 out << " | ";
30 out.width(5);
31 out << left << tests();
32 out << " |" << endl;
33 out << "| tests cases: | ";
34 out.width(6);
35 out << left << testCasesFailed();
36 out << " | ";
37 out.width(7);
38 out << left << testCasesSucceed();
39 out << " | ";
40 out.width(5);
41 out << left << testCases();
42 out << " |" << endl;
43 out << "| tests suites: | ";
44 out.width(6);
45 out << left << testSuitesFailed();
46 out << " | ";
47 out.width(7);
48 out << left << testSuitesSucceed();
49 out << " | ";
50 out.width(5);
51 out << left << testSuites();
52 out << " |" << endl;
53 out << "==================================================" << endl;
55 /************** TestBase END **********/
58 /************** TestCase **************/
59 //TODO: 3 almost same functions: how reduce duplicities?
60 int TestCase::assertations() const
62 int assertations = 0;
64 vector<test_state_t>::const_iterator it = _tests_states.begin();
65 vector<test_state_t>::const_iterator it_end = _tests_states.end();
66 for (;it != it_end; it++){
67 assertations += it->assertations;
70 return assertations;
73 int TestCase::assertationsFailed() const
75 int assertations = 0;
77 vector<test_state_t>::const_iterator it = _tests_states.begin();
78 vector<test_state_t>::const_iterator it_end = _tests_states.end();
79 for (;it != it_end; it++){
80 assertations += it->fails;
83 return assertations;
86 int TestCase::assertationsSucceed() const
88 int assertations = 0;
90 vector<test_state_t>::const_iterator it = _tests_states.begin();
91 vector<test_state_t>::const_iterator it_end = _tests_states.end();
92 for (;it != it_end; it++){
93 assertations += it->successes;
96 return assertations;
99 void TestCase::run(bool verbose)
101 //turn verbose on
102 if (verbose)
103 this->_verbose = true;
105 setUp();
106 runTests();
107 tearDown();
109 //turn verbose off
110 this->_verbose = false;
113 void TestCase::errorMessages(ostream &out) const
115 vector<error_message_t>::const_iterator error_it;
116 vector<error_message_t>::const_iterator error_it_end;
117 vector<test_state_t>::const_iterator it = _tests_states.begin();
118 vector<test_state_t>::const_iterator it_end = _tests_states.end();
119 for (;it != it_end; ++it){
120 if (it->error_messages.size() > 0){
121 error_it = it->error_messages.begin();
122 error_it_end = it->error_messages.end();
123 for (;error_it != error_it_end; ++error_it){
124 out << _getErrorMessage(error_it->file, error_it->line,
125 it->test_name, error_it->message) << endl;
127 //cout << endl;
133 //protected:
134 string TestCase::_getErrorMessage(const string file, const int line,
135 const string test_method, const string error_message) const
137 std::stringstream ss;
138 std::string l;
139 ss << line;
140 ss >> l;
141 return (file + ":" + l + ": " + _name + "::" + test_method + " :: " + error_message);
144 void TestCase::_prepareTest(string name)
146 _tests++;
148 _current_test.clean();
149 _current_test.test_name = name;
152 void TestCase::_finishTest(void)
154 if (_current_test.fails > 0){
155 _fails++;
156 }else{
157 _successes++;
159 _tests_states.push_back(_current_test);
163 void TestCase::_successAssertation(void)
165 _current_test.assertations++;
166 _current_test.successes++;
169 void TestCase::_failAssertation(string file, int line,
170 string error_message)
172 error_message_t tmp;
174 _current_test.assertations++;
175 _current_test.fails++;
177 tmp.file = file;
178 tmp.line = line;
179 tmp.message = error_message;
180 _current_test.error_messages.push_back(tmp);
182 if (_verbose)
183 cout << _getErrorMessage(file, line, _current_test.test_name,
184 error_message) << endl;
186 /************** TestCase END **********/
190 /************** TestSuite *************/
191 TestSuite::~TestSuite()
193 vector<TestBase *>::iterator it = tests.begin();
194 vector<TestBase *>::iterator it_end = tests.end();
195 for (;it != it_end; it++){
196 delete (*it);
200 void TestSuite::run(bool verbose)
202 vector<TestBase *>::iterator it = tests.begin();
203 vector<TestBase *>::iterator it_end = tests.end();
204 for (;it != it_end; it++){
205 (*it)->run(verbose);
208 /************** TestSuite END *********/