source: trunk/src/lol/unit.h @ 927

Last change on this file since 927 was 927, checked in by sam, 8 years ago

lolunit: add a LOLUNIT_SET_CONTEXT / LOLUNIT_UNSET_CONTEXT macro pair.

This is useful when doing several asserts in one loop, for instance: there
isn't always a way to deduce the loop index from the assert failure, so
these macros allow the user to give more context information.

File size: 9.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The Unit test framework
13// -----------------------
14//
15
16#if !defined __LOL_UNIT_H__
17#define __LOL_UNIT_H__
18
19#include <iostream>
20#include <iomanip>
21#include <sstream>
22#include <cstdio>
23#include <cmath>
24
25namespace lol
26{
27
28using namespace std;
29
30class FixtureBase
31{
32    friend class TextTestRunner;
33
34public:
35    virtual void setUp(void) {};
36    virtual void tearDown(void) {};
37
38protected:
39    FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {}
40    virtual ~FixtureBase() {}
41
42    /* The FixtureBase class keeps track of all instanciated children
43     * fixtures through this method. */
44    static FixtureBase *GetOrSetTest(FixtureBase *set = NULL)
45    {
46        static FixtureBase *head = NULL, *tail = NULL;
47        if (set)
48        {
49            if (!head)
50                head = set;
51            if (tail)
52                tail->m_next = set;
53            tail = set;
54        }
55        return head;
56    }
57
58    virtual void runFixture() = 0;
59
60    /* Prevent compiler complaints about unreachable code */
61    static inline bool True() { return true; }
62
63    FixtureBase *m_next;
64    int m_testcases, m_failcases;
65    int m_asserts, m_failure;
66    char const *m_fixturename, *m_currentname;
67    std::stringstream m_errorlog, m_context;
68};
69
70template<class T> class Fixture : protected FixtureBase
71{
72public:
73    typedef T FixtureClass;
74
75    class TestCase
76    {
77    public:
78        void (FixtureClass::* m_fun)();
79        char const *m_testname;
80        TestCase *m_next;
81
82        static void AddTestCase(TestCase *that, char const *name,
83                                void (FixtureClass::*fun)())
84        {
85            that->m_fun = fun;
86            that->m_testname = name;
87            that->m_next = NULL;
88            GetOrSetTestCase(that);
89        }
90    };
91
92    Fixture<T>()
93    {
94        GetOrSetTest(this);
95    }
96
97    /* Run all test cases in this fixture. */
98    virtual void runFixture()
99    {
100        m_errorlog.str("");
101        m_testcases = 0;
102        m_failcases = 0;
103        for (TestCase *c = GetOrSetTestCase(); c; c = c->m_next)
104        {
105            m_testcases++;
106            m_asserts = 0;
107            m_failure = false;
108            m_currentname = c->m_testname;
109            m_context.str("");
110            (static_cast<FixtureClass *>(this)->*c->m_fun)();
111            std::cout << (m_failure ? 'F' : '.');
112        }
113    }
114
115    /* Each Fixture class specialisation keeps track of its instanciated
116     * test cases through this method. */
117    static TestCase *GetOrSetTestCase(TestCase *set = NULL)
118    {
119        static TestCase *head = NULL, *tail = NULL;
120        if (set)
121        {
122            if (!head) head = set;
123            if (tail) tail->m_next = set;
124            tail = set;
125        }
126        return head;
127    }
128};
129
130class TextTestRunner
131{
132public:
133    bool run()
134    {
135        bool ret = true;
136        std::stringstream errors("");
137        int failcases = 0, testcases = 0;
138
139        for (FixtureBase *f = FixtureBase::GetOrSetTest(); f; f = f->m_next)
140        {
141            f->setUp();
142            f->runFixture();
143            f->tearDown();
144
145            errors << f->m_errorlog.str();
146            testcases += f->m_testcases;
147            failcases += f->m_failcases;
148        }
149        std::cout << std::endl;
150
151        std::cout << std::endl << std::endl;
152        if (failcases)
153        {
154            std::cout << "!!!FAILURES!!!" << std::endl;
155            std::cout << "Test Results:" << std::endl;
156            std::cout << "Run:  " << testcases
157                      << "  Failures: " << failcases
158                      << "  Errors: 0" << std::endl; /* TODO: handle errors */
159
160            std::cout << errors.str();
161            ret = false;
162        }
163        else
164        {
165            std::cout << "OK (" << testcases << " tests)" << std::endl;
166        }
167        std::cout << std::endl << std::endl;
168
169        return ret;
170    }
171};
172
173#define LOLUNIT_FIXTURE(FixtureName) \
174    class FixtureName; \
175    template<typename T> struct Make##FixtureName \
176    { \
177        Make##FixtureName() { p = new T(); } \
178        ~Make##FixtureName() { delete p; } \
179        T *p; \
180    }; \
181    Make##FixtureName<FixtureName> lol_unit_fixture_##FixtureName; \
182    static char const *LolUnitFixtureName(FixtureName *p) \
183    { \
184        (void)p; \
185        return #FixtureName; \
186    } \
187    class FixtureName : public lol::Fixture<FixtureName>
188
189#define LOLUNIT_TEST(TestCaseName) \
190    class TestCase##TestCaseName : public TestCase \
191    { \
192    public: \
193        TestCase##TestCaseName() \
194        { \
195            AddTestCase(this, #TestCaseName, \
196                    (void (FixtureClass::*)()) &FixtureClass::TestCaseName); \
197        } \
198    }; \
199    TestCase##TestCaseName lol_unit_test_case_##TestCaseName; \
200    void TestCaseName()
201
202#define LOLUNIT_ASSERT_GENERIC(message, cond) \
203    do { \
204        m_asserts++; \
205        if (True() && !(cond)) \
206        { \
207            m_errorlog << std::endl << std::endl; \
208            m_errorlog << ++m_failcases << ") test: " \
209                       << LolUnitFixtureName(this) << "::" << m_currentname \
210                       << " (F) line: " << __LINE__ << " " \
211                       << __FILE__ << std::endl; \
212            m_errorlog << "assertion failed" << std::endl; \
213            m_errorlog << "- Expression: " << #cond << std::endl; \
214            m_errorlog << message; \
215            m_failure = true; \
216            return; \
217        } \
218    } while(!True())
219
220#define LOLUNIT_ASSERT_EQUAL_GENERIC(message, a, b) \
221    do { \
222        m_asserts++; \
223        if (True() && (a) != (b)) \
224        { \
225            m_errorlog << std::endl << std::endl; \
226            m_errorlog << ++m_failcases << ") test: " \
227                       << LolUnitFixtureName(this) << "::" << m_currentname \
228                       << " (F) line: " << __LINE__ << " " \
229                       << __FILE__ << std::endl; \
230            m_errorlog << "equality assertion failed" << std::endl; \
231            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
232            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
233            m_errorlog << message; \
234            m_errorlog << m_context.str(); \
235            m_failure = true; \
236            return; \
237        } \
238    } while(!True())
239
240#define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(message, a, b, t) \
241    do { \
242        m_asserts++; \
243        if (True() && fabs((a) - (b)) > fabs((t))) \
244        { \
245            m_errorlog << std::endl << std::endl; \
246            m_errorlog << ++m_failcases << ") test: " \
247                       << LolUnitFixtureName(this) << "::" << m_currentname \
248                       << " (F) line: " << __LINE__ << " " \
249                       << __FILE__ << std::endl; \
250            m_errorlog << "double equality assertion failed" << std::endl; \
251            std::streamsize old_prec = m_errorlog.precision(); \
252            m_errorlog << std::setprecision(16); \
253            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
254            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
255            m_errorlog << "- Delta   : " << #t << " = " << (t) << std::endl; \
256            m_errorlog << std::setprecision(old_prec); \
257            m_errorlog << message; \
258            m_errorlog << m_context.str(); \
259            m_failure = true; \
260            return; \
261        } \
262    } while(!True())
263
264#define LOLUNIT_FAIL(message) \
265    do { \
266        m_asserts++; \
267        m_errorlog << std::endl << std::endl; \
268        m_errorlog << ++m_failcases << ") test: " \
269                   << LolUnitFixtureName(this) << "::" << m_currentname \
270                   << " (F) line: " << __LINE__ << " " \
271                   << __FILE__ << std::endl; \
272        m_errorlog << "forced failure" << std::endl; \
273        m_errorlog << "- " << message << std::endl; \
274        m_errorlog << m_context.str(); \
275        m_failure = true; \
276        return; \
277    } while(!True())
278
279#define LOLUNIT_SET_CONTEXT(n) \
280    do { \
281        m_context.str(""); \
282        m_context << "- Context : " << #n << " = " << (n) << std::endl; \
283    } while(!True())
284
285#define LOLUNIT_UNSET_CONTEXT(n) \
286    m_context.str("")
287
288#define LOLUNIT_ASSERT(cond) \
289    LOLUNIT_ASSERT_GENERIC("", cond)
290
291#define LOLUNIT_ASSERT_MESSAGE(message, cond) \
292    LOLUNIT_ASSERT_GENERIC("- " << message << std::endl, cond)
293
294#define LOLUNIT_ASSERT_EQUAL(a, b) \
295    LOLUNIT_ASSERT_EQUAL_GENERIC("", a, b)
296
297#define LOLUNIT_ASSERT_EQUAL_MESSAGE(message, a, b) \
298    LOLUNIT_ASSERT_EQUAL_GENERIC("- " << message << std::endl, a, b)
299
300#define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \
301    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("", a, b, t)
302
303#define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message, a, b, t) \
304    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("- " << message << std::endl, a, b, t)
305
306} /* namespace lol */
307
308#endif // __LOL_UNIT_H__
309
Note: See TracBrowser for help on using the repository browser.