7 * TODO: Copyright, license...
8 * TODO: Udelat to tak, aby se vse vypisovalo za behu a nemuselo se cekat
9 * na dobehnuti celeho testu
11 * TODO: Dopsat vsechny aserce
15 * Struct describing error message.
17 struct error_message_t
{
24 * Struct describing state after run test.
27 std::string test_name
; // name of test
28 int assertations
; // num of assertations
29 int fails
; // num of failed assertations
30 int successes
; // num of succeed assertations
31 std::vector
<error_message_t
> error_messages
; // list of error messages
33 inline void clean(void){assertations
=0;fails
=0;successes
=0;error_messages
.clear();}
38 * Abstract base class.
39 * This defines the base methods which must have all classes which handle
40 * any tests, test cases and so on.
41 * Because of this class it's possible to register test suite into other
46 const std::string _name
;
48 TestBase(const std::string n
) : _name(n
){}
51 virtual void run(bool verbose
) = 0;
54 * This two functions determines whether class is TestCase or
57 virtual bool isTestCase() const = 0;
58 virtual bool isTestSuite() const = 0;
61 * Inherited class must redefine only these methods which make sense
64 virtual int assertations() const { return 0;}
65 virtual int assertationsFailed() const { return 0;}
66 virtual int assertationsSucceed() const { return 0;}
67 virtual int testSuites() const { return 0;}
68 virtual int testSuitesFailed() const { return 0;}
69 virtual int testSuitesSucceed() const { return 0;}
70 virtual int testCases() const { return 0;}
71 virtual int testCasesFailed() const { return 0;}
72 virtual int testCasesSucceed() const { return 0;}
73 virtual int tests() const { return 0;}
74 virtual int testsFailed() const { return 0;}
75 virtual int testsSucceed() const { return 0;}
77 virtual void printSummary(std::ostream
&out
= std::cout
) const;
83 * Main test case class.
84 * This class collects all individual tests in one test case.
86 class TestCase
: public TestBase
{
93 * List of states of all ran tests.
95 std::vector
<test_state_t
> _tests_states
;
98 * State of currently running test.
99 * This struct must be empty before running next test method.
101 test_state_t _current_test
;
104 * Indicates if current running test case has to be verbose
110 * Returns formated error message
112 std::string
_getErrorMessage(const std::string file
, const int line
,
113 const std::string test_method
,
114 const std::string error_message
) const;
119 * _prepareTest("nameOfTheTest");
123 void _prepareTest(std::string test_name
);
127 * Record assertation as succeed
129 void _successAssertation();
132 * Record assertations as failed and print error message to stdout (if
135 void _failAssertation(std::string file
, int line
,
136 std::string error_message
);
139 * Constructor is protected because new test case (which inherits from
140 * TestCase class) only defines its own constructor which only call
141 * this one with its name as argument.
143 explicit TestCase(const std::string
& n
) : TestBase(n
), _tests(0),
144 _fails(0), _successes(0), _verbose(false){}
146 virtual ~TestCase(){}
148 bool isTestCase() const { return true;}
149 bool isTestSuite() const { return false;}
151 virtual void setUp(){}
152 virtual void tearDown(){}
155 * Method describing tests.
156 * This must be redefined in extended class.
158 virtual void runTests() = 0;
161 * This run the test case.
162 * If verbose is set to true, errors will be printed during
164 * Returns number of failed tests or 255 (lesser number)
166 void run(bool verbose
= false);
169 int assertations() const;
170 int assertationsFailed() const;
171 int assertationsSucceed() const;
172 int tests() const {return _tests
;}
173 int testsFailed() const {return _fails
;}
174 int testsSucceed() const {return _successes
;}
177 * Print error messages into given stream
179 void errorMessages(std::ostream
&out
= std::cout
) const;
185 * Main test suite class.
186 * This class collect all test cases and run them together.
187 * TestSuite can collect and mix test cases and other test suites together,
188 * so it's possible to create hirearchy of tests.
189 * Test cases and test suites are run in order they are registered.
191 class TestSuite
: public TestBase
{
192 std::vector
<TestBase
*> tests
;
195 TestSuite(const std::string
& n
) : TestBase(n
){}
196 virtual ~TestSuite();
198 bool isTestCase() const { return false;}
199 bool isTestSuite() const { return true;}
202 * Register test cases and test suites for later run.
204 void reg(TestBase
*tc
){ tests
.push_back(tc
); }
207 * Run all test cases.
209 void run(bool verbose
);
213 /***** TestCase MACROS *****/
214 #define TEST_CASE(name) \
215 class name : public TestCase{ \
217 name() : TestCase(#name){}
219 #define TEST_CASE_END };
221 #define TESTS void runTests()
223 #define TEST(test_name) \
224 _prepareTest(#test_name); \
230 #define assertTrueM(a, message) \
232 _successAssertation(); \
234 _failAssertation(__FILE__, __LINE__, message); \
236 #define assertTrue(a) \
237 assertTrueM((a), #a " is not true")
239 #define assertFalseM(a, message) \
240 assertTrueM(!(a), message)
241 #define assertFalse(a) \
242 assertFalseM((a), #a " is not false")
244 #define assertEqualsM(a,b,message) \
245 assertTrueM((a) == (b), message)
246 #define assertEquals(a,b) \
247 assertEqualsM((a), (b), #a " not equals " #b)
249 #define assertNotEqualsM(a,b,message) \
250 assertTrueM((a) != (b), message)
251 #define assertNotEquals(a,b) \
252 assertNotEqualsM((a), (b), #a " equals " #b)
254 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */