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 | #if !defined(_WIN32) |
132 | catch (...) |
133 | { |
134 | CppUnitException *e = new CppUnitException ("unknown exception" ); |
135 | result->addError (this, e); |
136 | } |
137 | #endif |
138 | tearDown (); |
139 | result->endTest(this); |
140 | } |
141 | |
142 | |
143 | // A default run method |
144 | TestResult* TestCase::run() |
145 | { |
146 | TestResult* result = defaultResult(); |
147 | |
148 | run(result); |
149 | return result; |
150 | } |
151 | |
152 | |
153 | // All the work for runTest is deferred to subclasses |
154 | void TestCase::runTest() |
155 | { |
156 | } |
157 | |
158 | |
159 | // Build a message about a failed equality check |
160 | std::string TestCase::notEqualsMessage(long expected, long actual) |
161 | { |
162 | return "expected: " + estring(expected) + " but was: " + estring(actual); |
163 | } |
164 | |
165 | |
166 | // Build a message about a failed equality check |
167 | std::string TestCase::notEqualsMessage(double expected, double actual) |
168 | { |
169 | return "expected: " + estring(expected) + " but was: " + estring(actual); |
170 | } |
171 | |
172 | |
173 | // Build a message about a failed equality check |
174 | std::string TestCase::notEqualsMessage(const void* expected, const void* actual) |
175 | { |
176 | return "expected: " + estring(expected) + " but was: " + estring(actual); |
177 | } |
178 | |
179 | |
180 | // Build a message about a failed equality check |
181 | std::string TestCase::notEqualsMessage(const std::string& expected, const std::string& actual) |
182 | { |
183 | return "expected: \"" + expected + "\" but was: \"" + actual + "\"" ; |
184 | } |
185 | |
186 | |
187 | } // namespace CppUnit |
188 | |