Created TestSuite class.
[cppu.git] / cppu.h
blob3bc54fd9d31f13a5497262a28d44ca71a68d718e
1 #ifndef _CPPU_H_
2 #define _CPPU_H_
4 #include <string>
5 #include <vector>
6 /**
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
14 /**
15 * Struct describing error message.
17 struct error_message_t{
18 std::string file;
19 int line;
20 std::string message;
23 /**
24 * Struct describing state after run test.
26 struct test_state_t{
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();}
37 /**
38 * Provides interface for all test classes.
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
42 * test suite.
44 class TestInterface{
45 public:
46 virtual ~TestInterface(){}
47 virtual void run(bool verbose) = 0;
52 /**
53 * Main test case class.
54 * This class collect all individual tests in one test case.
56 class TestCase : public TestInterface{
57 protected:
58 int tests;
59 int fails;
60 int successes;
62 /**
63 * Name of current test case
65 const std::string name;
67 /**
68 * List of states of all ran tests.
70 std::vector<test_state_t> tests_states;
72 /**
73 * State of currently running test.
74 * This struct must be empty before running next test method.
76 test_state_t current_test;
78 /**
79 * Indicates if current running test case has to be verbose
81 bool verbose;
84 std::string _getErrorMessage(std::string file, int line,
85 std::string test_method, std::string error_message);
88 void _prepareTest(std::string test_name);
89 void _finishTest();
91 void _successAssertation();
92 void _failAssertation(std::string file, int line,
93 std::string error_message);
95 explicit TestCase(const std::string& n) : tests(0), fails(0), successes(0),
96 name(n), tests_states(), current_test(), verbose(false){}
97 public:
98 virtual ~TestCase(){}
99 virtual void setUp(){}
100 virtual void tearDown(){}
103 * Method describing tests.
104 * This must be redefined in extended class.
106 virtual void runTests() = 0;
109 * This run the test case.
110 * If verbose is set to true, errors will be printed during
111 * test case running.
112 * Returns number of failed tests or 255 (lesser number)
114 void run(bool verbose = false);
117 int numTests() const {return tests;}
118 int numFails() const {return fails;}
119 int numSuccesses() const {return successes;}
122 * Print summary of test case into given stream.
124 void summary(std::ostream &out = std::cout);
127 * Print error messages into given stream
129 void errorMessages(std::ostream &out = std::cout);
135 * Main test suite class.
136 * This class collect all test cases and run them together.
137 * TestSuite can collect and mix test cases and other test suites together,
138 * so it's possible to create hirearchy of tests.
139 * Test cases and test suites are run in order they are registered.
141 class TestSuite : public TestInterface{
142 const std::string name;
144 std::vector<TestInterface *> tests;
146 public:
147 TestSuite(const std::string& n) : name(n){}
148 virtual ~TestSuite();
151 * Register test case for later run.
153 void reg(TestInterface *tc){ tests.push_back(tc); }
156 * Run all test cases.
158 void run(bool verbose);
160 void summary(std::ostream &out = std::cout){}
161 void errorMessages(std::ostream &out = std::cout){}
165 /***** TestCase MACROS *****/
166 #define TEST_CASE(name) \
167 class name : public TestCase{ \
168 public: \
169 name() : TestCase(#name){}
171 #define TEST_CASE_END };
173 #define TESTS void runTests()
175 #define TEST(test_name) \
176 _prepareTest(#test_name); \
177 this->test_name(); \
178 _finishTest();
182 #define assertTrueM(a, message) \
183 if (a){ \
184 _successAssertation(); \
185 }else{ \
186 _failAssertation(__FILE__, __LINE__, message); \
188 #define assertTrue(a) \
189 assertTrueM((a), #a " is not true")
191 #define assertFalseM(a, message) \
192 assertTrueM(!(a), message)
193 #define assertFalse(a) \
194 assertFalseM((a), #a " is not false")
196 #define assertEqualsM(a,b,message) \
197 assertTrueM((a) == (b), message)
198 #define assertEquals(a,b) \
199 assertEqualsM((a), (b), #a " not equals " #b)
201 #define assertNotEqualsM(a,b,message) \
202 assertTrueM((a) != (b), message)
203 #define assertNotEquals(a,b) \
204 assertNotEqualsM((a), (b), #a " equals " #b)
205 #endif
206 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */