Source

neglect / neglect / cpplib / include / neglect / unittest.hpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* implements a lightweight unittest system purely with templates
   and macros which is primarily used to test neglect itself, but
   can also be used to test other cpp and c libraries.
   
   Currently a lot here is a bad hack, but at least it can be used
   for basic testing without external dependencies besides boost. */

#include <neglect/boot.hpp>
#include <neglect/math.hpp>
#include <neglect/path.hpp>

#include <fstream>
#include <stack>

#include <boost/noncopyable.hpp>
#include <boost/preprocessor.hpp>

namespace neglect { namespace unittest {

    class test_runner;
    class test_node;
    class test_group;
    class test_case;

    test_group &get_root_test_group();

    /* internally used marker to end a test early */
    class abort_test {};

    /* stores the location of an assertion/check */
    struct location {
        neglect::string filename;
        int lineno;
        const test_case *test;

        location() : filename(""), lineno(0), test(0) {}
        location(const neglect::string &filename, int lineno, const test_case *test)
            : filename(filename), lineno(lineno), test(test) {}
    };

    /* holds test error details */
    struct test_error {
        location loc;
        neglect::string type;
        neglect::string message;
        neglect::string details;
    };


    class test_node : public boost::noncopyable {
    public:
        test_node(const neglect::string &name, test_node *parent,
                  const neglect::string &filename)
            : m_name(name), m_parent(parent), m_filename(filename)
        {
            if (parent)
                parent->add_node(this);
        }

        virtual ~test_node() {};

        const neglect::string &name() const
        {
            return m_name;
        }

        virtual neglect::string header() const
        {
            return name();
        }

        const neglect::string full_name() const
        {
            neglect::string out = name();
            test_node *node = m_parent;
            while (node) {
                neglect::string name = node->name();
                if (!name.empty())
                    out = name + "." + out;
                node = node->parent();
            }
            return out;
        }

        const neglect::string &filename() const
        {
            return m_filename;
        }

        test_node *parent() { return m_parent; }
        const test_node *parent() const { return m_parent; }

        virtual void set_up(test_runner &tr) const {};
        virtual void tear_down(test_runner &tr) const {};
        virtual void test(test_runner &tr) const = 0;

    protected:
        virtual void add_node(const test_node *node)
        {
            assert(false);
        }

    private:
        neglect::string m_name;
        neglect::string m_filename;
        test_node *m_parent;
    };

    class test_group : public test_node {
    public:

        test_group(const neglect::string &name, test_node *parent,
                   const neglect::string &filename)
            : test_node(name, parent, filename)
        {
        }

        neglect::string header() const
        {
            if (this == &get_root_test_group())
                return "Test Suite";
            return "Group " + name();
        }

        void test(test_runner &runner) const;

    protected:
        void add_node(const test_node *node)
        {
            m_children.push_back(node);
        }

    private:
        std::vector<const test_node *> m_children;
    };

    class test_file : public test_group {
    public:
        test_file(test_group *parent, const neglect::string &filename)
            : test_group("", parent, filename)
        {
        }

        neglect::string header() const
        {
            return "File " + path::basename(filename());
        }
    };

    class test_case : public test_node {
    public:
        test_case(const neglect::string &name, test_node *parent,
                  const neglect::string &filename, int lineno)
            : test_node(name, parent, filename)
        {
            m_lineno = lineno;
        }

        int lineno() const { return m_lineno; }

    private:
        int m_lineno;
    };

    inline test_group &get_root_test_group()
    {
        static test_group root("", 0, "");
        return root;
    }


    class test_runner : public boost::noncopyable {
    public:
        test_runner(std::ostream &out) : m_stream(out)
        {
            m_test_count = 0;
            m_failed_tests = 0;
            m_indentation = 0;
        }

        /* write output to the stream */

        void write_indentation()
        {
            for (int i = 0; i < m_indentation; i++)
                m_stream << "  ";
        }

        void write_line(const neglect::string &msg)
        {
            write_indentation();
            m_stream << msg << std::endl;
        }

        void begin_test(const test_node &node)
        {
            m_errcnt_stack.push(m_errors.size());
            write_indentation();
            m_stream << node.header();
            if (dynamic_cast<const test_case *>(&node)) {
                m_test_count++;
                m_stream << "... ";
            }
            else {
                m_stream << std::endl;
                m_indentation += 1;
            }
        }

        void end_test(const test_node &node)
        {
            int old_count = m_errcnt_stack.top();
            m_errcnt_stack.pop();
            if (dynamic_cast<const test_case *>(&node)) {
                if (old_count != m_errors.size()) {
                    m_stream << "E";
                    m_failed_tests++;
                }
                else
                    m_stream << "OK";
                m_stream << std::endl;
            }
            else
                m_indentation -= 1;
        }

        int failed_tests() const { return m_failed_tests; }
        int successful_tests() const { return m_test_count - m_failed_tests; }
        int test_count() const { return m_test_count; }
        int error_count() const { return m_errors.size(); }
        bool has_errors() const { return !m_errors.empty(); }

        typedef std::vector<test_error>::const_iterator error_iterator;
        error_iterator errors_begin() const { return m_errors.begin(); }
        error_iterator errors_end() const { return m_errors.end(); }

        /* start one or multiple tests */
        inline void test(const test_node &node)
        {
            begin_test(node);
            bool tear_down = false;
            try {
                node.set_up(*this);
                tear_down = true;
                node.test(*this);
            }
            catch (abort_test &) {
                /* just ignore */
            }
            catch (std::exception &e) {
                record_exception(e, node);
            }
            if (tear_down)
                node.tear_down(*this);
            end_test(node);
        }

        inline void test_all(bool details_and_summary=true)
        {
            test(get_root_test_group());
            if (details_and_summary) {
                m_stream << std::endl;
                print_details();
                m_stream << std::endl;
                print_summary();
            }
        }

        void record_test_error(const test_error &err)
        {
            m_errors.push_back(err);
        }

        void record_exception(const std::exception &e, const test_node &node)
        {
            test_error err;
            err.loc.filename = node.filename();
            const test_case *tcase = dynamic_cast<const test_case *>(&node);
            if (tcase) {
                err.loc.lineno = tcase->lineno();
                err.loc.test = tcase;
            }

            err.type = "Exception in test";
            err.message = "received an expression";
            err.details = e.what();
            record_test_error(err);
        }

        /* check and requirement functions */
        bool check(bool expr, const neglect::string &expr_repr, const location &loc)
        {
            if (expr)
                return true;

            neglect::stringstream details;
            details << "expression: " << expr_repr;

            test_error err;
            err.loc = loc;
            err.type = "Check failed";
            err.message = "free expression failed";
            err.details = details.str();
            record_test_error(err);
            return false;
        }

        template <class A, class B>
        bool check_equal(A a, B b, const location &loc)
        {
            if (a == b)
                return true;

            neglect::stringstream details;
            details << "expression: " << a << " == " << b;

            test_error err;
            err.loc = loc;
            err.type = "Equality check failed";
            err.message = "the two objects are not equal";
            err.details = details.str();
            record_test_error(err);
            return false;
        }

        template <class A, class B>
        bool check_not_equal(A a, B b, const location &loc)
        {
            if (a != b)
                return true;

            neglect::stringstream details;
            details << "expression: " << a << " != " << b;

            test_error err;
            err.loc = loc;
            err.type = "Inequality check failed";
            err.message = "the two objects are equal";
            err.details = details.str();
            record_test_error(err);
            return false;
        }

        template <class A, class B>
        bool check_almost_equal(A a, B b, const location &loc)
        {
            if (almost_equal(a, b))
                return true;

            neglect::stringstream details;
            details << "expression: " << a << " almost equal to " << b;

            test_error err;
            err.loc = loc;
            err.type = "Almost-Equality check failed";
            err.message = "the two objects are too different";
            err.details = details.str();
            record_test_error(err);
            return false;
        }

        void print_summary()
        {
            m_stream << "Ran " << test_count() << " test(s)" << std::endl;
            if (error_count() == 0)
                m_stream << "OK";
            else
                m_stream << "FAILED(errors=" << error_count() << ")";
            m_stream << std::endl;
        }

        void print_error(const test_error &te)
        {
            m_stream
                << (te.loc.test ? te.loc.test->full_name() : "unknown test") << std::endl
                << "  in " << te.loc.filename << ":" << te.loc.lineno << std::endl
                << "error: " << te.type << std::endl
                << "message: " << te.message << std::endl
                << te.details << std::endl;
        }

        void print_details()
        {
            if (error_count())
                m_stream << "Error Details:" << std::endl;
            for (error_iterator iter = errors_begin();
                 iter != errors_end(); ++iter) {
                m_stream << std::endl;
                print_error(*iter);
            }
        }

    private:
        std::ostream &m_stream;
        std::vector<test_error> m_errors;
        std::stack<int> m_errcnt_stack;
        int m_test_count;
        int m_failed_tests;
        int m_indentation;
    };

    inline void test_group::test(test_runner &runner) const
    {
        for (std::vector<const test_node *>::const_iterator iter = m_children.begin();
             iter != m_children.end(); ++iter)
            runner.test(**iter);
    }
}}

static inline ::neglect::unittest::test_group
    *get_neglect_test_group(const char *filename)
{
    static ::neglect::unittest::test_file file(
        &::neglect::unittest::get_root_test_group(), filename);
    return &file;
}

/* helper macros to create testcases and groups */
#define NUT_TESTGROUP(name)                                                 \
    namespace BOOST_PP_CAT(_, BOOST_PP_CAT(name, _support)) {               \
        static ::neglect::unittest::test_group *get_parent_test_group() {   \
            return get_neglect_test_group(__FILE__);                        \
        }                                                                   \
    }                                                                       \
    namespace BOOST_PP_CAT(name, _group) {                                  \
        static ::neglect::unittest::test_group                              \
            *get_neglect_test_group(const char *filename) {                 \
            static ::neglect::unittest::test_group the_group(#name,         \
                BOOST_PP_CAT(_, BOOST_PP_CAT(name, _support))               \
                    ::get_parent_test_group(), __FILE__);                   \
            return &the_group;                                              \
        }                                                                   \
    }                                                                       \
    namespace BOOST_PP_CAT(name, _group)

#define NUT_TESTCASE(name)                                                  \
    class BOOST_PP_CAT(test_, name)                                         \
        : public ::neglect::unittest::test_case {                           \
    public:                                                                 \
        BOOST_PP_CAT(test_, name)()                                         \
        : ::neglect::unittest::test_case(#name,                             \
                                         get_neglect_test_group(__FILE__),  \
                                         __FILE__, __LINE__) {}             \
        void test(::neglect::unittest::test_runner &runner) const;          \
    } BOOST_PP_CAT(BOOST_PP_CAT(test_, name), _instance);                   \
    void BOOST_PP_CAT(test_, name)::test(                                   \
        ::neglect::unittest::test_runner &runner) const

/* test helpers */
#define _NUT_LOCATION ::neglect::unittest::location(__FILE__, __LINE__, this)
#define _NUT_ABORTIF(expr) \
    do { if (expr) throw ::neglect::unittest::abort_test(); } while (0)

#define NUT_CHECK(expr) runner.check(expr, #expr, _NUT_LOCATION)
#define NUT_CHECK_EQUAL(a, b) runner.check_equal(a, b, _NUT_LOCATION)
#define NUT_CHECK_NOT_EQUAL(a, b) runner.check_not_equal(a, b, _NUT_LOCATION)
#define NUT_CHECK_ALMOST_EQUAL(a, b) runner.check_almost_equal(a, b, _NUT_LOCATION)
#define NUT_REQUIRE(expr) _NUT_ABORTIF(!NUT_CHECK(expr))
#define NUT_REQUIRE_EQUAL(a, b) _NUT_ABORTIF(!NUT_CHECK_EQUAL(a, b))
#define NUT_REQUIRE_NOT_EQUAL(a, b) _NUT_ABORTIF(!NUT_CHECK_NOT_EQUAL(a, b))
#define NUT_REQUIRE_ALMOST_EQUAL(a, b) _NUT_ABORTIF(!NUT_CHECK_ALMOST_EQUAL(a, b))