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