1 |
#include <iostream> |
2 |
#include <sstream> |
3 |
#include <stdexcept> |
4 |
#include <math.h> |
5 |
|
6 |
#include "CppUnitTest/TestCase.h" |
7 |
#include "CppUnitTest/TestResult.h" |
8 |
#include "CppUnitTest/estring.h" |
9 |
|
10 |
USING_NAMESPACE_CPPUNITTEST |
11 |
|
12 |
// Create a default TestResult |
13 |
TestResult *TestCase::defaultResult () |
14 |
{ return new TestResult; } |
15 |
|
16 |
|
17 |
// Check for a failed general assertion |
18 |
void TestCase::assertImplementation (bool condition, |
19 |
std::string conditionExpression, |
20 |
long lineNumber, |
21 |
std::string fileName) |
22 |
{ |
23 |
if (!condition) |
24 |
throw CppUnitException (conditionExpression, lineNumber, fileName); |
25 |
} |
26 |
|
27 |
void TestCase::setCheckErrors(const std::vector<std::string>& checkErrors) { |
28 |
m_errorLog=checkErrors; |
29 |
} |
30 |
|
31 |
const std::vector<std::string>& TestCase::getCheckErrors() { |
32 |
return(m_errorLog); |
33 |
} |
34 |
|
35 |
// Check for a failed check |
36 |
void TestCase::checkImplementation (bool condition, |
37 |
std::string conditionExpression, |
38 |
long lineNumber, |
39 |
std::string fileName) |
40 |
{ |
41 |
if (!condition) { |
42 |
std::ostringstream errorLog; |
43 |
errorLog << "Check Failed - " << std::endl |
44 |
<< "File : " << fileName << std::endl |
45 |
<< "Line Number : " << lineNumber << std::endl |
46 |
<< "Condition : " << conditionExpression << std::endl |
47 |
<< std::endl; |
48 |
m_errorLog.push_back(errorLog.str()); |
49 |
} |
50 |
} |
51 |
|
52 |
|
53 |
// Check for a failed equality assertion |
54 |
void TestCase::assertEquals (long expected, |
55 |
long actual, |
56 |
long lineNumber, |
57 |
std::string fileName) |
58 |
{ |
59 |
if (expected != actual) |
60 |
assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName); |
61 |
} |
62 |
|
63 |
|
64 |
// Check for a failed equality assertion |
65 |
void TestCase::assertEquals (double expected, |
66 |
double actual, |
67 |
double delta, |
68 |
long lineNumber, |
69 |
std::string fileName) |
70 |
{ |
71 |
if (fabs (expected - actual) > delta) |
72 |
assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName); |
73 |
|
74 |
} |
75 |
|
76 |
|
77 |
// Run the test and catch any exceptions that are triggered by it |
78 |
void TestCase::run (TestResult *result) |
79 |
{ |
80 |
// |
81 |
// flag, if true the test has been a success |
82 |
bool success=false; |
83 |
result->startTest (this); |
84 |
try { |
85 |
m_errorLog.clear(); |
86 |
setUp (); |
87 |
runTest (); |
88 |
success=true; |
89 |
} |
90 |
|
91 |
catch (CppUnitException& e) { |
92 |
CppUnitException *copy = new CppUnitException (e); |
93 |
result->addFailure (this, copy); |
94 |
|
95 |
} |
96 |
catch (std::exception& e) { |
97 |
std::cout << "TestCase Caught exception: " << e.what() << std::endl; |
98 |
result->addError (this, new CppUnitException (e.what ())); |
99 |
|
100 |
} |
101 |
|
102 |
catch (...) { |
103 |
CppUnitException *e = new CppUnitException ("unknown exception"); |
104 |
result->addError (this, e); |
105 |
} |
106 |
|
107 |
if(m_errorLog.size()>0) { |
108 |
std::ostringstream errorLog; |
109 |
errorLog << std::endl; |
110 |
for(int i=0; i<m_errorLog.size(); ++i) { |
111 |
errorLog << m_errorLog[i]; |
112 |
} |
113 |
errorLog << std::endl; |
114 |
result->addFailure (this, new CppUnitException (errorLog.str())); |
115 |
} |
116 |
|
117 |
try { |
118 |
tearDown (); |
119 |
} |
120 |
|
121 |
catch (CppUnitException& e) { |
122 |
std::ostringstream temp; |
123 |
temp << "Exception from tearDown: " << e.what () << std::flush; |
124 |
CppUnitException *copy = new CppUnitException (temp.str().c_str()); |
125 |
result->addFailure (this, copy); |
126 |
success=false; |
127 |
} |
128 |
catch (std::exception& e) { |
129 |
std::ostringstream temp; |
130 |
temp << "Exception from tearDown: " << e.what () << std::flush; |
131 |
result->addError (this, new CppUnitException (temp.str().c_str())); |
132 |
success=false; |
133 |
} |
134 |
|
135 |
catch (...) { |
136 |
std::ostringstream temp; |
137 |
temp << "Exception from tearDown: Unknown exception." << std::flush; |
138 |
CppUnitException *e = new CppUnitException (temp.str().c_str()); |
139 |
result->addError (this, e); |
140 |
success=false; |
141 |
} |
142 |
|
143 |
result->endTest (this); |
144 |
if (success) { |
145 |
std::cerr << " OK" << std::endl; |
146 |
} |
147 |
else { |
148 |
setFailure(); |
149 |
} |
150 |
} |
151 |
|
152 |
|
153 |
// A default run method |
154 |
TestResult *TestCase::run () |
155 |
{ |
156 |
TestResult *result = defaultResult (); |
157 |
|
158 |
run (result); |
159 |
return result; |
160 |
|
161 |
} |
162 |
|
163 |
|
164 |
// All the work for runTest is deferred to subclasses |
165 |
void TestCase::runTest () |
166 |
{ |
167 |
} |
168 |
|
169 |
|
170 |
// Build a message about a failed equality check |
171 |
std::string TestCase::notEqualsMessage (long expected, long actual) |
172 |
{ |
173 |
return "expected: " + estring (expected) + " but was: " + estring (actual); |
174 |
} |
175 |
|
176 |
|
177 |
// Build a message about a failed equality check |
178 |
std::string TestCase::notEqualsMessage (double expected, double actual) |
179 |
{ |
180 |
return "expected: " + estring (expected) + " but was: " + estring (actual); |
181 |
} |
182 |
|