/[escript]/trunk/tools/CppUnitTest/src/TestCase.h
ViewVC logotype

Contents of /trunk/tools/CppUnitTest/src/TestCase.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3150 - (show annotations)
Fri Sep 3 05:40:27 2010 UTC (9 years, 1 month ago) by caltinay
File MIME type: text/plain
File size: 8414 byte(s)
Removed executable prop from non-executables.

1 //
2 // Permission to reproduce and create derivative works from the Software ("Software Derivative Works")
3 // is hereby granted to you under the copyright of Michael Feathers. Michael Feathers also grants you
4 // the right to distribute the Software and Software Derivative Works.
5 //
6 // Michael Feathers licenses the Software to you on an "AS IS" basis, without warranty of any kind.
7 // Michael Feathers HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED,
8 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, NON INFRINGEMENT
9 // AND FITNESS FOR A PARTICULAR PURPOSE.  You are solely responsible for determining the appropriateness
10 // of using the Software and assume all risks associated with the use and distribution of this Software,
11 // including but not limited to the risks of program errors, damage to or loss of data, programs or
12 // equipment, and unavailability or interruption of operations.  MICHAEL FEATHERS WILL NOT BE
13 // LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC
14 // CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF MICHAEL FEATHERS HAD BEEN ADVISED
15 // OF THE POSSIBILITY OF SUCH DAMAGE.  Michael Feathers will not be liable for the loss of, or damage
16 // to, your records or data, or any damages claimed by you based on a third party claim.
17 //
18 // You agree to distribute the Software and any Software Derivatives under a license agreement that:
19 //
20 // 1) is sufficient to notify all licensees of the Software and Software Derivatives that Michael
21 // Feathers assumes no liability for any claim that may arise regarding the Software or
22 // Software Derivatives, and
23 // 2) that disclaims all warranties, both express and implied, from Michael Feathers regarding the
24 // Software and Software Derivatives.  (If you include this Agreement with any distribution
25 // of the Software and Software Derivatives you will have meet this requirement) You agree that
26 // you will not delete any copyright notices in the Software.
27 //
28 // This Agreement is the exclusive statement of your rights in the Software as provided by Michael
29 // Feathers. Except for the licenses granted to you in the second paragraph above, no other licenses
30 // are granted hereunder, by estoppel, implication or otherwise.
31 //
32 #ifndef CPPUNIT_TESTCASE_H
33 #define CPPUNIT_TESTCASE_H
34
35 #include <string>
36 #include <vector>
37 //#include <typeinfo>
38
39 #include "Guards.h"
40
41 #include "Test.h"
42
43 #include "CppUnitException.h"
44 #include "CppUnitTestNamespace.h"
45
46 BEGIN_NAMESPACE_CPPUNITTEST
47
48 class TestResult;
49
50 /*
51 * A test case defines the fixture to run multiple tests. To define a test case
52 * 1) implement a subclass of TestCase
53 * 2) define instance variables that store the state of the fixture
54 * 3) initialize the fixture state by overriding setUp
55 * 4) clean-up after a test by overriding tearDown.
56 *
57 * Each test runs in its own fixture so there
58 * can be no side effects among test runs.
59 * Here is an example:
60 *
61 * class MathTest : public TestCase {
62 * protected: int m_value1;
63 * protected: int m_value2;
64 *
65 * public: MathTest (std::string name)
66 * : TestCase (name) {
67 * }
68 *
69 * protected: void setUp () {
70 * m_value1 = 2;
71 * m_value2 = 3;
72 * }
73 * }
74 *
75 *
76 * For each test implement a method which interacts
77 * with the fixture. Verify the expected results with assertions specified
78 * by calling assert on the expression you want to test:
79 *
80 * protected: void testAdd () {
81 * int result = value1 + value2;
82 * assert (result == 5);
83 * }
84 *
85 * Once the methods are defined you can run them. To do this, use
86 * a TestCaller.
87 *
88 * Test *test = new TestCaller<MathTest>("testAdd", MathTest::testAdd);
89 * test->run ();
90 *
91 *
92 * The tests to be run can be collected into a TestSuite. CppUnit provides
93 * different test runners which can run a test suite and collect the results.
94 * The test runners expect a static method suite as the entry
95 * point to get a test to run.
96 *
97 * public: static MathTest::suite () {
98 * TestSuite *suiteOfTests = new TestSuite;
99 * suiteOfTests->addTest(new TestCaller<MathTest>("testAdd", testAdd));
100 * suiteOfTests->addTest(new TestCaller<MathTest>("testDivideByZero", testDivideByZero));
101 * return suiteOfTests;
102 * }
103 *
104 * Note that the caller of suite assumes lifetime control
105 * for the returned suite.
106 *
107 * see TestResult, TestSuite and TestCaller
108 *
109 */
110
111
112 class TestCase : public Test
113 {
114 REFERENCEOBJECT (TestCase)
115
116 public:
117 TestCase (std::string Name);
118 ~TestCase ();
119
120 virtual void run (TestResult *result);
121 virtual TestResult *run ();
122 virtual int countTestCases ();
123 std::string toString ();
124
125 virtual void setUp ();
126 virtual void tearDown ();
127
128 void setCheckErrors(const std::vector<std::string>& checkErrors);
129
130 const std::vector<std::string>& getCheckErrors();
131
132
133 protected:
134 virtual void runTest ();
135
136 TestResult *defaultResult ();
137 void checkImplementation
138 (bool condition,
139 std::string conditionExpression = "",
140 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
141 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
142
143 void assertImplementation
144 (bool condition,
145 std::string conditionExpression = "",
146 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
147 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
148
149 void assertEquals (long expected,
150 long actual,
151 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
152 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
153
154 void assertEquals (double expected,
155 double actual,
156 double delta,
157 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
158 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
159
160 std::string notEqualsMessage (long expected,
161 long actual);
162
163 std::string notEqualsMessage (double expected,
164 double actual);
165
166
167 private:
168
169 std::vector<std::string> m_errorLog;
170
171 };
172
173
174 // Constructs a test case
175 inline TestCase::TestCase (std::string name) : Test(name)
176 {}
177
178
179 // Destructs a test case
180 inline TestCase::~TestCase ()
181 {}
182
183
184 // Returns a count of all the tests executed
185 inline int TestCase::countTestCases ()
186 { return 1; }
187
188 // A hook for fixture set up
189 inline void TestCase::setUp ()
190 {}
191
192
193 // A hook for fixture tear down
194 inline void TestCase::tearDown ()
195 {}
196
197
198 // Returns the name of the test case instance
199 inline std::string TestCase::toString () {return name();}
200 //{ const std::type_info& thisClass = typeid (*this); return std::string (thisClass.name ()) + "." + name (); }
201
202
203
204 // A set of macros which allow us to get the line number
205 // and file name at the point of an error.
206 // Just goes to show that preprocessors do have some
207 // redeeming qualities.
208
209 #define CPPUNIT_SOURCEANNOTATION
210
211 #ifdef CPPUNIT_SOURCEANNOTATION
212
213 #undef assert
214 #define assert(condition)\
215 (this->assertImplementation ((condition),(#condition),\
216 __LINE__, __FILE__))
217
218 #undef CHECK
219 #define CHECK(condition)\
220 (this->checkImplementation ((condition),(#condition),\
221 __LINE__, __FILE__))
222
223 #else
224
225 #undef assert
226 #define assert(condition)\
227 (this->assertImplementation ((condition),"",\
228 __LINE__, __FILE__))
229
230 #undef CHECK
231 #define CHECK(condition)\
232 (this->checkImplementation ((condition),"",\
233 __LINE__, __FILE__))
234
235 #endif
236
237
238 // Macros for primitive value comparisons
239 #define assertDoublesEqual(expected,actual,delta)\
240 (this->assertEquals ((expected),\
241 (actual),(delta),__LINE__,__FILE__))
242
243 #define assertLongsEqual(expected,actual)\
244 (this->assertEquals ((expected),\
245 (actual),__LINE__,__FILE__))
246
247 END_NAMESPACE_CPPUNITTEST
248
249 #endif
250
251
252
253
254
255
256
257
258
259
260
261
262

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26