TestInterface renamed to TestBase - It's now abstract class no interface.
[cppu.git] / cppu.h
blob54961c1124a5c0117c461fdba3d28a29705dd735
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
13 * TODO: Zaradit do vypisu chyby take nazev test case?
16 /**
17 * Struct describing error message.
19 struct error_message_t{
20 std::string file;
21 int line;
22 std::string message;
25 /**
26 * Struct describing state after run test.
28 struct test_state_t{
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();}
39 /**
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
44 * test suite.
46 class TestBase{
47 protected:
48 const std::string _name;
50 TestBase(const std::string n) : _name(n){}
51 public:
52 virtual ~TestBase(){}
53 virtual void run(bool verbose) = 0;
55 /**
56 * This two functions determines whether class is TestCase or
57 * TestSuite.
59 virtual bool isTestCase() const = 0;
60 virtual bool isTestSuite() const = 0;
62 /**
63 * Inherited class must redefine only these methods which make sense
64 * for them.
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;
84 /**
85 * Main test case class.
86 * This class collects all individual tests in one test case.
88 class TestCase : public TestBase{
89 protected:
90 int _tests;
91 int _fails;
92 int _successes;
94 /**
95 * List of states of all ran tests.
97 std::vector<test_state_t> _tests_states;
99 /**
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
108 bool _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;
120 * Usage:
121 * _prepareTest("nameOfTheTest");
122 * testMethod();
123 * _finishTest();
125 void _prepareTest(std::string test_name);
126 void _finishTest();
129 * Record assertation as succeed
131 void _successAssertation();
134 * Record assertations as failed and print error message to stdout (if
135 * verbose set)
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){}
147 public:
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
165 * test case running.
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;
196 public:
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{ \
218 public: \
219 name() : TestCase(#name){}
221 #define TEST_CASE_END };
223 #define TESTS void runTests()
225 #define TEST(test_name) \
226 _prepareTest(#test_name); \
227 this->test_name(); \
228 _finishTest();
232 #define assertTrueM(a, message) \
233 if (a){ \
234 _successAssertation(); \
235 }else{ \
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)
255 #endif
256 /* vim: set sw=4 ts=4 et ft=cpp tw=75 cindent: */