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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (show annotations)
Wed Nov 9 02:02:19 2005 UTC (17 years, 4 months ago) by jgs
File MIME type: text/plain
File size: 6019 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

1
2 #ifndef CPPUNIT_TESTCASE_H
3 #define CPPUNIT_TESTCASE_H
4
5 #include <string>
6 #include <vector>
7 //#include <typeinfo>
8
9 #include "Guards.h"
10
11 #include "Test.h"
12
13 #include "CppUnitException.h"
14
15 #include "CppUnitTest/CppUnitTestNamespace.h"
16 BEGIN_NAMESPACE_CPPUNITTEST
17
18 class TestResult;
19
20 /*
21 * A test case defines the fixture to run multiple tests. To define a test case
22 * 1) implement a subclass of TestCase
23 * 2) define instance variables that store the state of the fixture
24 * 3) initialize the fixture state by overriding setUp
25 * 4) clean-up after a test by overriding tearDown.
26 *
27 * Each test runs in its own fixture so there
28 * can be no side effects among test runs.
29 * Here is an example:
30 *
31 * class MathTest : public TestCase {
32 * protected: int m_value1;
33 * protected: int m_value2;
34 *
35 * public: MathTest (std::string name)
36 * : TestCase (name) {
37 * }
38 *
39 * protected: void setUp () {
40 * m_value1 = 2;
41 * m_value2 = 3;
42 * }
43 * }
44 *
45 *
46 * For each test implement a method which interacts
47 * with the fixture. Verify the expected results with assertions specified
48 * by calling assert on the expression you want to test:
49 *
50 * protected: void testAdd () {
51 * int result = value1 + value2;
52 * assert (result == 5);
53 * }
54 *
55 * Once the methods are defined you can run them. To do this, use
56 * a TestCaller.
57 *
58 * Test *test = new TestCaller<MathTest>("testAdd", MathTest::testAdd);
59 * test->run ();
60 *
61 *
62 * The tests to be run can be collected into a TestSuite. CppUnit provides
63 * different test runners which can run a test suite and collect the results.
64 * The test runners expect a static method suite as the entry
65 * point to get a test to run.
66 *
67 * public: static MathTest::suite () {
68 * TestSuite *suiteOfTests = new TestSuite;
69 * suiteOfTests->addTest(new TestCaller<MathTest>("testAdd", testAdd));
70 * suiteOfTests->addTest(new TestCaller<MathTest>("testDivideByZero", testDivideByZero));
71 * return suiteOfTests;
72 * }
73 *
74 * Note that the caller of suite assumes lifetime control
75 * for the returned suite.
76 *
77 * see TestResult, TestSuite and TestCaller
78 *
79 */
80
81
82 class TestCase : public Test
83 {
84 REFERENCEOBJECT (TestCase)
85
86 public:
87 TestCase (std::string Name);
88 ~TestCase ();
89
90 virtual void run (TestResult *result);
91 virtual TestResult *run ();
92 virtual int countTestCases ();
93 std::string toString ();
94
95 virtual void setUp ();
96 virtual void tearDown ();
97
98 void setCheckErrors(const std::vector<std::string>& checkErrors);
99
100 const std::vector<std::string>& getCheckErrors();
101
102
103 protected:
104 virtual void runTest ();
105
106 TestResult *defaultResult ();
107 void checkImplementation
108 (bool condition,
109 std::string conditionExpression = "",
110 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
111 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
112
113 void assertImplementation
114 (bool condition,
115 std::string conditionExpression = "",
116 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
117 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
118
119 void assertEquals (long expected,
120 long actual,
121 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
122 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
123
124 void assertEquals (double expected,
125 double actual,
126 double delta,
127 long lineNumber = CPPUNIT_UNKNOWNLINENUMBER,
128 std::string fileName = CPPUNIT_UNKNOWNFILENAME);
129
130 std::string notEqualsMessage (long expected,
131 long actual);
132
133 std::string notEqualsMessage (double expected,
134 double actual);
135
136
137 private:
138
139 std::vector<std::string> m_errorLog;
140
141 };
142
143
144 // Constructs a test case
145 inline TestCase::TestCase (std::string name) : Test(name)
146 {}
147
148
149 // Destructs a test case
150 inline TestCase::~TestCase ()
151 {}
152
153
154 // Returns a count of all the tests executed
155 inline int TestCase::countTestCases ()
156 { return 1; }
157
158 // A hook for fixture set up
159 inline void TestCase::setUp ()
160 {}
161
162
163 // A hook for fixture tear down
164 inline void TestCase::tearDown ()
165 {}
166
167
168 // Returns the name of the test case instance
169 inline std::string TestCase::toString () {return name();}
170 //{ const std::type_info& thisClass = typeid (*this); return std::string (thisClass.name ()) + "." + name (); }
171
172
173
174 // A set of macros which allow us to get the line number
175 // and file name at the point of an error.
176 // Just goes to show that preprocessors do have some
177 // redeeming qualities.
178
179 #define CPPUNIT_SOURCEANNOTATION
180
181 #ifdef CPPUNIT_SOURCEANNOTATION
182
183 #undef assert
184 #define assert(condition)\
185 (this->assertImplementation ((condition),(#condition),\
186 __LINE__, __FILE__))
187
188 #undef CHECK
189 #define CHECK(condition)\
190 (this->checkImplementation ((condition),(#condition),\
191 __LINE__, __FILE__))
192
193 #else
194
195 #undef assert
196 #define assert(condition)\
197 (this->assertImplementation ((condition),"",\
198 __LINE__, __FILE__))
199
200 #undef CHECK
201 #define CHECK(condition)\
202 (this->checkImplementation ((condition),"",\
203 __LINE__, __FILE__))
204
205 #endif
206
207
208 // Macros for primitive value comparisons
209 #define assertDoublesEqual(expected,actual,delta)\
210 (this->assertEquals ((expected),\
211 (actual),(delta),__LINE__,__FILE__))
212
213 #define assertLongsEqual(expected,actual)\
214 (this->assertEquals ((expected),\
215 (actual),__LINE__,__FILE__))
216
217 END_NAMESPACE_CPPUNITTEST
218
219 #endif
220
221
222
223
224
225
226
227
228
229
230
231
232

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26