Another fix for Makefile.
[cppu.git] / cppu.h
blobbf6ef8c557edfe65beeb36770041bca048d8df84
1 /***
2 * CppU - C++ unit testing framework
3 * ---------------------------------
4 * Copyright (c)2007 Daniel Fiser <danfis@danfis.cz>
7 * This file is part of CppU.
9 * CppU is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * CppU is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /**
24 * \file cppu.h
25 * Header file of CppU.
29 #ifndef _CPPU_H_
30 #define _CPPU_H_
32 #include <string>
33 #include <vector>
34 #include <iostream>
36 * TODO: Dopsat vsechny aserce (zachytavani vyjimek -
37 * assertException(vyraz, ExceptionType);
41 /**
42 * Struct describing state after ran test.
44 struct test_state_t{
45 std::string test_name; //!< name of test
46 int fails; //!< num of failed assertations
47 int successes; //!< num of succeed assertations
49 inline void clean(void){fails=0;successes=0;}
52 /**
53 * Abstract base class.
55 * This defines the base methods which must have all classes which handle
56 * any tests, test cases and so on.\n
57 * Because of this class it's possible to register test suite into other
58 * test suite.
60 class TestBase{
61 protected:
62 const std::string _name;
64 TestBase(const std::string n) : _name(n){}
65 public:
66 virtual ~TestBase(){}
68 /**
69 * This runs all tests (and subtests) and print output to out.
70 * Parameter level specifies on which level is this test running.
72 virtual void run(std::ostream &out, const int level = 0) = 0;
74 /*@{*/
75 /**
76 * \name
77 * This two functions determines whether class is TestCase or
78 * TestSuite.
80 virtual bool isTestCase() const = 0;
81 virtual bool isTestSuite() const = 0;
82 /*@}*/
85 /*@{*/
86 /**
87 * \name
88 * Methods which returns number of assertations, tests etc.\
89 * Inheriting class must redefine only methods which make sense
90 * for them.
92 virtual int assertations() const { return 0;}
93 virtual int assertationsFailed() const { return 0;}
94 virtual int assertationsSucceed() const { return 0;}
95 virtual int testSuites() const { return 0;}
96 virtual int testSuitesFailed() const { return 0;}
97 virtual int testSuitesSucceed() const { return 0;}
98 virtual int testCases() const { return 0;}
99 virtual int testCasesFailed() const { return 0;}
100 virtual int testCasesSucceed() const { return 0;}
101 virtual int tests() const { return 0;}
102 virtual int testsFailed() const { return 0;}
103 virtual int testsSucceed() const { return 0;}
104 /*@}*/
107 * This method prints summary independently on type of class (TestCase
108 * or TestSuite).
109 * Class which inherits from TestBase must redefine only methods which
110 * count assertations, tests etc...
112 virtual void printSummary(std::ostream &out = std::cout) const;
118 * Main test case class.
119 * This class collects all individual tests in one test case.
121 class TestCase : public TestBase{
122 protected:
123 int _tests_failed; //!< number of failed tests
124 int _tests_succeed; //!< number of succeed tests
125 int _assertations_failed; //!< number of failed assertations
126 int _assertations_succeed; //!< number of succeed assertations
129 * State of currently running test.
130 * This struct must be empty before running next test method.
132 test_state_t _current_test;
135 * Which output stream has to be used.
137 std::ostream *_out;
141 * Returns formated error message
143 std::string _getErrorMessage(const std::string file, const int line,
144 const std::string test_method,
145 const std::string error_message) const;
148 /*@{*/
150 * \name
151 * Usage:
152 * \code
153 * _prepareTest("nameOfTheTestMethod");
154 * testMethod();
155 * _finishTest();
156 * \endcode
158 void _prepareTest(std::string test_name);
159 void _finishTest();
160 /*@}*/
163 * Record assertation as succeed
165 void _successAssertation();
168 * Record assertations as failed and print error message to stdout.
170 void _failAssertation(std::string file, int line,
171 std::string error_message);
174 * Constructor is protected because new test case (which inherits from
175 * TestCase class) defines its own constructor which only call
176 * this one with its name as argument.
178 explicit TestCase(const std::string& n) : TestBase(n),
179 _tests_failed(0), _tests_succeed(0),
180 _assertations_failed(0), _assertations_succeed(0){}
181 public:
182 virtual ~TestCase(){}
184 bool isTestCase() const { return true;}
185 bool isTestSuite() const { return false;}
187 virtual void setUp(){}
188 virtual void tearDown(){}
191 * Method describing tests.\
192 * This must be redefined in extended class.
194 virtual void runTests() = 0;
197 * This run the test case.
198 * If verbose is set to true, errors will be printed out during
199 * test case running.
201 void run(std::ostream &out = std::cout, const int level = 0);
204 int assertations() const { return _assertations_failed +
205 _assertations_succeed;}
206 int assertationsFailed() const { return _assertations_failed;}
207 int assertationsSucceed() const { return _assertations_succeed;}
208 int tests() const {return _tests_failed + _tests_succeed;}
209 int testsFailed() const {return _tests_failed;}
210 int testsSucceed() const {return _tests_succeed;}
216 * Main test suite class.
217 * This class collect all test cases and run them together.\n
218 * TestSuite can collect and mix test cases and other test suites together,
219 * so it's possible to create hirearchy of tests.\n
220 * Test cases and test suites are run in order they are registered.
222 class TestSuite : public TestBase{
223 protected:
225 * List of registered tests.
227 std::vector<TestBase *> _tests;
230 * Register test cases and test suites for later run.
232 void _reg(TestBase *tc){ _tests.push_back(tc); }
235 * Erase all tests from vector.
237 void _freeTests();
240 * Inheriting class must redefine this method.\
241 * In this method should be only list of _reg() methods calls.\
242 * This method is ran in run() method as first.
244 virtual void _registerAll() = 0;
245 public:
246 TestSuite(const std::string& n) : TestBase(n){}
247 virtual ~TestSuite();
249 bool isTestCase() const { return false;}
250 bool isTestSuite() const { return true;}
253 * This run the tests.
254 * If verbose is set to true, errors will be printed out during
255 * test case running.
257 void run(std::ostream &out = std::cout, const int level = 0);
260 int assertations() const;
261 int assertationsFailed() const;
262 int assertationsSucceed() const;
263 int testSuites() const;
264 int testSuitesFailed() const;
265 int testSuitesSucceed() const;
266 int testCases() const;
267 int testCasesFailed() const;
268 int testCasesSucceed() const;
269 int tests() const;
270 int testsFailed() const;
271 int testsSucceed() const;
276 * Function which runs test (given in argument) and all
277 * outputs go into out.
279 void runTest(TestBase * test, std::ostream &out = std::cout);
281 /************** MACROS ****************/
282 /********** TestCase **********/
284 * Create test case.\ This macro must be enclosed by TEST_CASE_END macro.
286 #define TEST_CASE(name) \
287 class name : public TestCase{ \
288 public: \
289 name() : TestCase(#name){}
291 * Close definition of test case.
293 #define TEST_CASE_END }
296 * Introduce block where has to be all test methods registered.
298 #define TESTS void runTests()
301 * Register method in TESTS block.
303 #define REG_TEST(test_name) \
304 _prepareTest(#test_name); \
305 this->test_name(); \
306 _finishTest();
309 /********** TestSuite **********/
311 * Create test suite.\ This macro has to be enclosed by TEST_SUITE_END
312 * macro.
314 #define TEST_SUITE(name) \
315 class name : public TestSuite{ \
316 public: \
317 name() : TestSuite(#name){} \
318 void _registerAll() \
321 * Close definition of test suite.
323 #define TEST_SUITE_END \
327 * Register test case or test suite into test suite.\ This macro has to be
328 * between TEST_SUITE and TEST_SUITE_END macro.
330 #define REG(name) \
331 _reg(new name());
336 * Runs test suite or test case.\ Output goes to stdout.
338 #define RUN(name) \
339 runTest(new name());
341 * Runs test suite or test case.\ Output goes to specified out.
343 #define RUN_OUT(name, out) \
344 runTest(new name(), out);
346 /********** assertations **********/
347 /*@{*/
349 * \name Assertations
350 * Assertations with suffix 'M' (e.g.\ assertTrueM) is variation of macro
351 * where is possible to specify error message.
353 #define assertTrueM(a, message) \
354 if (a){ \
355 _successAssertation(); \
356 }else{ \
357 _failAssertation(__FILE__, __LINE__, message); \
359 #define assertTrue(a) \
360 assertTrueM((a), #a " is not true")
362 #define assertFalseM(a, message) \
363 assertTrueM(!(a), message)
364 #define assertFalse(a) \
365 assertFalseM((a), #a " is not false")
367 #define assertEqualsM(a,b,message) \
368 assertTrueM((a) == (b), message)
369 #define assertEquals(a,b) \
370 assertEqualsM((a), (b), #a " not equals " #b)
372 #define assertNotEqualsM(a,b,message) \
373 assertTrueM((a) != (b), message)
374 #define assertNotEquals(a,b) \
375 assertNotEqualsM((a), (b), #a " equals " #b)
377 #define assertThrow(expr, except) \
378 try { \
379 (expr); \
380 assertTrueM(false, #except " is not throwed - nothing throwed"); \
381 }catch(except e){ \
382 }catch(std::exception &e){ \
383 assertTrueM(false, #except " is not throwed, but std::exception throwed"); \
384 }catch(std::string &e){ \
385 assertTrueM(false, #except " is not throwed, but std::string throwed"); \
387 #endif
388 /*@}*/
389 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */