Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / testframework / easyunit / test.h
1 /*
2 EasyUnit : Simple C++ Unit testing framework
3 Copyright (C) 2004 Barthelemy Dagenais
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Barthelemy Dagenais
20 barthelemy@prologique.com
21 */
22
23 #ifndef TEST_H
24 #define TEST_H
25
26 #include "testcase.h"
27 #include "testpartresult.h"
28
29
30
31
32
33 /**
34 * EasyUnit namespace.
35 * This is the namespace containing all easyunit classes.
36 */
37
38 /**
39 * Test class containing all macros to do unit testing.
40 * A test object represents a test that will be executed. Once it has been
41 * executed, it reports all results in the testPartResult linked list.
42 *
43 * A failure occurs when a test fails (condition is false).
44 * An error occurs when an exception is thrown during a test.
45 * A success occurs if a test succeed (condition is true).
46 */
47 class Test
48 {
49 public:
50
51 /**
52 * Main Test constructor. Used to create a test that will register itself
53 * with TestRegistry and with its test case.
54 * @param testCaseName Name of the test case this test belongs to
55 * @param testName Name of this test
56 */
57 Test(const SimpleString& testCaseName, const SimpleString& testName);
58
59 /**
60 * Main Test desctructor
61 * Delete the testPartResult linked list. This is why the user should
62 * only use the macro provided by easyunit to report a test result.
63 */
64 virtual ~Test();
65
66 /**
67 * Fixtures that will be called after run().
68 */
69 virtual void tearDown();
70
71 /**
72 * Fixtures that will be called before run().
73 */
74 virtual void setUp();
75
76 /**
77 * Test code should be in this method.
78 * run() will be called by the Test's TestCase, hence subclasses of Test
79 * should override this method.
80 */
81 virtual void run();
82
83 /**
84 * Set the TestCase this test belongs to.
85 *
86 * @param testCase The TestCase this test belongs to
87 */
88 void setTestCase(TestCase *testCase);
89
90 /**
91 * Get the TestCase this test belongs to. A test always belongs to
92 * only one TestCase. This is the TestCase identified by the first
93 * parameter of the test declaration. For example, if there is a
94 * test declared as TEST(TESTCASE1, TEST1), this test will be
95 * associated with the TestCase TESTCASE1.
96 *
97 * @return The TestCase this test belongs to
98 */
99 TestCase* getTestCase() const;
100
101 /**
102 * Add a testpartresult to the testpartresult list of this test.
103 * This method is used by the assertion macros to report success,
104 * failure or error.
105 *
106 * @param testPartResult The testpartresult to be added to the list
107 */
108 virtual void addTestPartResult(TestPartResult *testPartResult);
109
110 /**
111 * Get the testpartresult list of this test. If assertion macros
112 * and TEST and TESTF macros are used, there may be more than
113 * one successful testpartresult and no more than one error or failure.
114 *
115 * @return testPartResult The list of testpartresults of this test
116 */
117 TestPartResult* getTestPartResult() const;
118
119 /**
120 * Returns number of failures found in this test.
121 * If macro TEST or TESTF is used, failuresCount <= 1.
122 * If Test class is extended and ASSERT macros are used in different
123 * test methods, than failuresCount may be more than 1.
124 *
125 * @return Number of failures in this test
126 */
127 int getFailuresCount() const;
128
129 /**
130 * Returns number of successes found in this test.
131 * There may be more than one success since each ASSERT macro
132 * that succeeded generate a success.
133 *
134 * @return Number of successes in this test
135 */
136 int getSuccessesCount() const;
137
138 /**
139 * Returns number of errors found in this test.
140 * ErrorsCount <= 1, since exception are caught
141 * for the whole run() method.
142 *
143 * @return Number of errors in this test
144 */
145 int getErrorsCount() const;
146
147
148 /**
149 * Set the next test in the linked list.
150 *
151 * @param nextTest Next test in the linked list
152 */
153 void setNext(Test *nextTest);
154
155 /**
156 * Get the next test in the linked list.
157 *
158 * @return The next test in the linked list
159 */
160 Test* getNext() const;
161
162 /**
163 * Get the name of the TestCase this test belongs to. The name of the
164 * TestCase is the first parameter of the test declaration. For example,
165 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
166 * "TESTCASE1".
167 *
168 * @return The TestCase name of this test
169 */
170 const SimpleString& getTestCaseName() const;
171
172 /**
173 * Get the name of this test. The name of the test is the second
174 * parameter of the test declaration. For example,
175 * if a test is declared as TEST(TESTCASE1, TEST1), this method will return
176 * "TEST1".
177 *
178 * @return The name of this test.
179 */
180 const SimpleString& getTestName() const;
181
182 protected:
183 SimpleString testCaseName_;
184 SimpleString testName_;
185 TestCase *testCase_;
186 TestPartResult *testPartResult_;
187 Test *nextTest_;
188 int failuresCount_;
189 int successesCount_;
190 int errorsCount_;
191 };
192
193
194
195
196 /*
197 * Helper macros
198 */
199
200 #define EQUALS_DELTA(expected,actual,delta)\
201 ((actual - expected) <= delta && actual >= expected) || ((expected - actual) <= delta && expected >= actual)
202
203 #define TO_STRING_EQUALS_F(expected,actual)\
204 StringFrom("Expected : ") + StringFrom(expected) + StringFrom(" but Actual : ") + StringFrom(actual)
205
206 #define TO_STRING_EQUALS_S(expected,actual)\
207 StringFrom(expected) + StringFrom(" == ") + StringFrom(actual)
208
209 #define TO_S_E_DELTA_F(expected,actual,delta)\
210 StringFrom("Expected : ") + StringFrom(expected) + StringFrom(" but Actual : ") + StringFrom(actual) + StringFrom(" with delta = ") + StringFrom(delta)
211
212 #define TO_S_E_DELTA_S(expected,actual,delta)\
213 StringFrom(expected) + StringFrom(" == ") + StringFrom(actual) + StringFrom(" with delta = ") + StringFrom(delta)
214
215 /**
216 * Asserts that a condition is true.
217 * If the condition is not true, a failure is generated.
218 * @param condition Condition to fullfill for the assertion to pass
219 */
220 #define ASSERT_TRUE(condition)\
221 { if (condition) {\
222 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,#condition,success));\
223 } else {\
224 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__, #condition,failure)); return;\
225 }}
226
227 /**
228 * Asserts that a condition is true.
229 * If the condition is not true, a failure is generated.
230 * @param condition Condition to fullfill for the assertion to pass
231 * @param message Message that will be displayed if this assertion fails
232 */
233 #define ASSERT_TRUE_M(condition,message)\
234 { if (condition) {\
235 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,#condition,success));\
236 } else {\
237 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__, message,failure)); return;\
238 }}
239
240 /**
241 * Asserts that the two parameters are equals. Operator == must be defined.
242 * If the two parameters are not equals, a failure is generated.
243 * @param expected Expected value
244 * @param actual Actual value to be compared
245 */
246 #define ASSERT_EQUALS(expected,actual)\
247 { if (expected == actual) {\
248 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_STRING_EQUALS_S(#expected,#actual),success));\
249 } else {\
250 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_STRING_EQUALS_F(#expected,#actual),failure)); return;\
251 }}
252
253 /**
254 * Asserts that the two parameters are equals. Operator == must be defined.
255 * If the two parameters are not equals, a failure is generated.
256 *
257 * Parameters must be primitive data types or StringFrom (custom type) must
258 * be overloaded.
259 *
260 * @see SimpleString
261 * @param expected Expected value
262 * @param actual Actual value to be compared
263 */
264 #define ASSERT_EQUALS_V(expected,actual)\
265 { if (expected == actual) {\
266 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_STRING_EQUALS_S(expected,actual),success));\
267 } else {\
268 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_STRING_EQUALS_F(expected,actual),failure)); return;\
269 }}
270
271 /**
272 * Asserts that the two parameters are equals. Operator == must be defined.
273 * If the two parameters are not equals, a failure is generated.
274 * @param expected Expected value
275 * @param actual Actual value to be compared
276 * @param message Message that will be displayed if this assertion fails
277 */
278 #define ASSERT_EQUALS_M(expected,actual,message)\
279 { if (expected == actual) {\
280 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,#expected,success));\
281 } else {\
282 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,message,failure)); return;\
283 }}
284
285 /**
286 * Asserts that the two parameters are equals within a delta. Operators == and - must be defined.
287 * If the two parameters are not equals, a failure is generated.
288 * @param expected Expected value
289 * @param actual Actual value to be compared
290 * @param delta Delta accepted between the two values
291 */
292 #define ASSERT_EQUALS_DELTA(expected,actual,delta)\
293 { if (EQUALS_DELTA(expected,actual,delta) ) {\
294 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_S_E_DELTA_S(#expected,#actual,#delta),success));\
295 } else {\
296 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_S_E_DELTA_F(#expected,#actual,#delta),failure)); return;\
297 }}
298
299 /**
300 * Asserts that the two parameters are equals within a delta. Operators == and - must be defined.
301 * If the two parameters are not equals, a failure is generated.
302 * @param expected Expected value
303 * @param actual Actual value to be compared
304 * @param delta Delta accepted between the two values
305 * @param message Message that will be displayed if this assertion fails
306 */
307 #define ASSERT_EQUALS_DELTA_M(expected,actual,delta,message)\
308 { if (EQUALS_DELTA(expected,actual,delta)) {\
309 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,#expected,success));\
310 } else {\
311 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,message,failure)); return;\
312 }}
313
314 /**
315 * Asserts that the two parameters are equals within a delta. Operators == and - must be defined.
316 * If the two parameters are not equals, a failure is generated.
317 *
318 * Parameters must be primitive data types or StringFrom (custom type) must
319 * be overloaded.
320 *
321 * @see SimpleString
322 * @param expected Expected value
323 * @param actual Actual value to be compared
324 * @param delta Delta accepted between the two values
325 */
326 #define ASSERT_EQUALS_DELTA_V(expected,actual,delta)\
327 { if (EQUALS_DELTA(expected,actual,delta)) {\
328 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_S_E_DELTA_S(expected,actual,delta),success));\
329 } else {\
330 addTestPartResult(new TestPartResult(this, __FILE__,__LINE__,TO_S_E_DELTA_F(expected,actual,delta),failure)); return;\
331 }}
332
333
334 /**
335 * Make a test fails.
336 */
337 #define FAIL()\
338 { addTestPartResult(new TestPartResult(this, __FILE__, __LINE__,("Test failed."),failure)); return; }
339
340 /**
341 * Make a test fails with the given message.
342 * @param text Failure message
343 */
344 #define FAIL_M(text)\
345 { addTestPartResult(new TestPartResult(this, __FILE__, __LINE__,text,failure)); return; }
346
347
348 /**
349 * Define a test in a TestCase.
350 * User should put his test code between brackets after using this macro.
351 * @param testCaseName TestCase name where the test belongs to
352 * @param testName Unique test name
353 */
354 #define TEST(testCaseName, testName)\
355 class testCaseName##testName##Test : public Test \
356 { public: testCaseName##testName##Test() : Test (#testCaseName , #testName) {} \
357 void run(); } \
358 testCaseName##testName##Instance; \
359 void testCaseName##testName##Test::run ()
360
361
362 /**
363 * Define a test in a TestCase using test fixtures.
364 * User should put his test code between brackets after using this macro.
365 *
366 * This macro should only be used if test fixtures were declared earlier in
367 * this order: DECLARE, SETUP, TEARDOWN.
368 * @param testCaseName TestCase name where the test belongs to. Should be
369 * the same name of DECLARE, SETUP and TEARDOWN.
370 * @param testName Unique test name.
371 */
372 #define TESTF(testCaseName, testName)\
373 class testCaseName##testName##Test : public testCaseName##Declare##Test \
374 { public: testCaseName##testName##Test() : testCaseName##Declare##Test (#testCaseName , #testName) {} \
375 void run(); } \
376 testCaseName##testName##Instance; \
377 void testCaseName##testName##Test::run ()
378
379
380 /**
381 * Setup code for test fixtures.
382 * This code is executed before each TESTF.
383 *
384 * User should put his setup code between brackets after using this macro.
385 *
386 * @param testCaseName TestCase name of the fixtures.
387 */
388 #define SETUP(testCaseName)\
389 void testCaseName##Declare##Test::setUp ()
390
391
392 /**
393 * Teardown code for test fixtures.
394 * This code is executed after each TESTF.
395 *
396 * User should put his setup code between brackets after using this macro.
397 *
398 * @param testCaseName TestCase name of the fixtures.
399 */
400 #define TEARDOWN(testCaseName)\
401 void testCaseName##Declare##Test::tearDown ()
402
403
404 /**
405 * Location to declare variables and objets.
406 * This is where user should declare members accessible by TESTF,
407 * SETUP and TEARDOWN.
408 *
409 * User should not use brackets after using this macro. User should
410 * not initialize any members here.
411 *
412 * @param testCaseName TestCase name of the fixtures
413 * @see END_DECLARE for more information.
414 */
415 #define DECLARE(testCaseName)\
416 class testCaseName##Declare##Test : public Test \
417 { public: testCaseName##Declare##Test(const SimpleString& testCaseName, const SimpleString& testName) : Test (testCaseName , testName) {} \
418 virtual void run() = 0; void setUp(); void tearDown(); \
419 protected:
420
421
422 /**
423 * Ending macro used after DECLARE.
424 *
425 * User should use this macro after declaring members with
426 * DECLARE macro.
427 */
428 #define END_DECLARE \
429 };
430
431 #endif // TEST_H
432
433