~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to CppUnit/include/CppUnit/TestCase.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

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