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
13 * TODO: Zaradit do vypisu chyby take nazev test case?
17 * Struct describing error message.
19 struct error_message_t
{
26 * Struct describing state after run test.
29 std::string test_name
; // name of test
30 int assertations
; // num of assertations
31 int fails
; // num of failed assertations
32 int successes
; // num of succeed assertations
33 std::vector
<error_message_t
> error_messages
; // list of error messages
35 inline void clean(void){assertations
=0;fails
=0;successes
=0;error_messages
.clear();}
40 * Abstract base class.
41 * This defines the base methods which must have all classes which handle
42 * any tests, test cases and so on.
43 * Because of this class it's possible to register test suite into other
48 const std::string _name
;
50 TestBase(const std::string n
) : _name(n
){}
53 virtual void run(bool verbose
) = 0;
56 * This two functions determines whether class is TestCase or
59 virtual bool isTestCase() const = 0;
60 virtual bool isTestSuite() const = 0;
63 * Inherited class must redefine only these methods which make sense
66 virtual int assertations() const { return 0;}
67 virtual int assertationsFailed() const { return 0;}
68 virtual int assertationsSucceed() const { return 0;}
69 virtual int testSuites() const { return 0;}
70 virtual int testSuitesFailed() const { return 0;}
71 virtual int testSuitesSucceed() const { return 0;}
72 virtual int testCases() const { return 0;}
73 virtual int testCasesFailed() const { return 0;}
74 virtual int testCasesSucceed() const { return 0;}
75 virtual int tests() const { return 0;}
76 virtual int testsFailed() const { return 0;}
77 virtual int testsSucceed() const { return 0;}
79 virtual void printSummary(std::ostream
&out
= std::cout
) const;
85 * Main test case class.
86 * This class collects all individual tests in one test case.
88 class TestCase
: public TestBase
{
95 * List of states of all ran tests.
97 std::vector
<test_state_t
> _tests_states
;
100 * State of currently running test.
101 * This struct must be empty before running next test method.
103 test_state_t _current_test
;
106 * Indicates if current running test case has to be verbose
112 * Returns formated error message
114 std::string
_getErrorMessage(const std::string file
, const int line
,
115 const std::string test_method
,
116 const std::string error_message
) const;
121 * _prepareTest("nameOfTheTest");
125 void _prepareTest(std::string test_name
);
129 * Record assertation as succeed
131 void _successAssertation();
134 * Record assertations as failed and print error message to stdout (if
137 void _failAssertation(std::string file
, int line
,
138 std::string error_message
);
141 * Constructor is protected because new test case (which inherits from
142 * TestCase class) only defines its own constructor which only call
143 * this one with its name as argument.
145 explicit TestCase(const std::string
& n
) : TestBase(n
), _tests(0),
146 _fails(0), _successes(0), _verbose(false){}
148 virtual ~TestCase(){}
150 bool isTestCase() const { return true;}
151 bool isTestSuite() const { return false;}
153 virtual void setUp(){}
154 virtual void tearDown(){}
157 * Method describing tests.
158 * This must be redefined in extended class.
160 virtual void runTests() = 0;
163 * This run the test case.
164 * If verbose is set to true, errors will be printed during
166 * Returns number of failed tests or 255 (lesser number)
168 void run(bool verbose
= false);
171 int assertations() const;
172 int assertationsFailed() const;
173 int assertationsSucceed() const;
174 int tests() const {return _tests
;}
175 int testsFailed() const {return _fails
;}
176 int testsSucceed() const {return _successes
;}
179 * Print error messages into given stream
181 void errorMessages(std::ostream
&out
= std::cout
) const;
187 * Main test suite class.
188 * This class collect all test cases and run them together.
189 * TestSuite can collect and mix test cases and other test suites together,
190 * so it's possible to create hirearchy of tests.
191 * Test cases and test suites are run in order they are registered.
193 class TestSuite
: public TestBase
{
194 std::vector
<TestBase
*> tests
;
197 TestSuite(const std::string
& n
) : TestBase(n
){}
198 virtual ~TestSuite();
200 bool isTestCase() const { return false;}
201 bool isTestSuite() const { return true;}
204 * Register test cases and test suites for later run.
206 void reg(TestBase
*tc
){ tests
.push_back(tc
); }
209 * Run all test cases.
211 void run(bool verbose
);
215 /***** TestCase MACROS *****/
216 #define TEST_CASE(name) \
217 class name : public TestCase{ \
219 name() : TestCase(#name){}
221 #define TEST_CASE_END };
223 #define TESTS void runTests()
225 #define TEST(test_name) \
226 _prepareTest(#test_name); \
232 #define assertTrueM(a, message) \
234 _successAssertation(); \
236 _failAssertation(__FILE__, __LINE__, message); \
238 #define assertTrue(a) \
239 assertTrueM((a), #a " is not true")
241 #define assertFalseM(a, message) \
242 assertTrueM(!(a), message)
243 #define assertFalse(a) \
244 assertFalseM((a), #a " is not false")
246 #define assertEqualsM(a,b,message) \
247 assertTrueM((a) == (b), message)
248 #define assertEquals(a,b) \
249 assertEqualsM((a), (b), #a " not equals " #b)
251 #define assertNotEqualsM(a,b,message) \
252 assertTrueM((a) != (b), message)
253 #define assertNotEquals(a,b) \
254 assertNotEqualsM((a), (b), #a " equals " #b)
256 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */