Changeset 913


Ignore:
Timestamp:
Sep 8, 2011, 12:21:26 AM (12 years ago)
Author:
sam
Message:

test: make LolUnit's output match CppUnit's more closely.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/unit.h

    r912 r913  
    1818
    1919#include <iostream>
     20#include <sstream>
    2021#include <cstdio>
    2122#include <cmath>
     
    3536
    3637protected:
    37     FixtureBase() : m_next(NULL), m_tests(0), m_fails(0) {}
     38    FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {}
    3839    virtual ~FixtureBase() {}
    3940
     
    5455    }
    5556
    56     virtual int RunFixture() = 0;
     57    virtual void RunFixture() = 0;
    5758
    5859    /* Prevent compiler complaints about unreachable code */
     
    6061
    6162    FixtureBase *m_next;
    62     int m_tests, m_fails;
     63    int m_testcases, m_failcases;
     64    int m_asserts, m_failure;
     65    char const *m_fixturename, *m_currentname;
     66    std::stringstream m_errorlog;
    6367};
    6468
     
    7276    public:
    7377        void (FixtureClass::* m_fun)();
    74         char const *m_name;
     78        char const *m_testname;
    7579        TestCase *m_next;
    7680
     
    7882                                void (FixtureClass::*fun)())
    7983        {
    80             that->m_name = name;
     84            that->m_testname = name;
    8185            that->m_fun = fun;
    8286            GetOrSetTestCase(that);
     
    9094
    9195    /* Run all test cases in this fixture. */
    92     virtual int RunFixture()
    93     {
    94         for (TestCase *head = GetOrSetTestCase(); head; head = head->m_next)
    95         {
    96             (static_cast<FixtureClass *>(this)->*head->m_fun)();
    97             std::cout << ".";
    98         }
    99         return 0;
     96    virtual void RunFixture()
     97    {
     98        m_errorlog.str("");
     99        m_testcases = 0;
     100        m_failcases = 0;
     101        for (TestCase *c = GetOrSetTestCase(); c; c = c->m_next)
     102        {
     103            m_testcases++;
     104            m_asserts = 0;
     105            m_failure = false;
     106            m_currentname = c->m_testname;
     107            (static_cast<FixtureClass *>(this)->*c->m_fun)();
     108            std::cout << (m_failure ? 'F' : '.');
     109        }
    100110    }
    101111
     
    118128{
    119129public:
    120     int Run()
    121     {
    122         int ret = 0;
    123         for (FixtureBase *head = FixtureBase::GetOrSetTest(); head; head = head->m_next)
    124         {
    125             head->setUp();
    126             if (head->RunFixture())
    127                 ret = 1;
    128             head->tearDown();
     130    bool Run()
     131    {
     132        bool ret = true;
     133        std::stringstream errors("");
     134        int failcases = 0, testcases = 0;
     135
     136        for (FixtureBase *f = FixtureBase::GetOrSetTest(); f; f = f->m_next)
     137        {
     138            f->setUp();
     139            f->RunFixture();
     140            f->tearDown();
     141
     142            errors << f->m_errorlog.str();
     143            testcases += f->m_testcases;
     144            failcases += f->m_failcases;
    129145        }
    130146        std::cout << std::endl;
     147
     148        std::cout << std::endl << std::endl;
     149        if (failcases)
     150        {
     151            std::cout << "!!!FAILURES!!!" << std::endl;
     152            std::cout << "Test Results:" << std::endl;
     153            std::cout << "Run:  " << testcases
     154                      << "  Failures: " << failcases
     155                      << "  Errors: 0" << std::endl; /* TODO: handle errors */
     156
     157            std::cout << errors.str();
     158            ret = false;
     159        }
     160        else
     161        {
     162            std::cout << "OK (" << testcases << " tests)" << std::endl;
     163        }
     164        std::cout << std::endl << std::endl;
     165
    131166        return ret;
    132167    }
     
    134169
    135170#define LOLUNIT_FIXTURE(FixtureName) \
     171    class FixtureName; \
     172    static char const *LolUnitFixtureName(FixtureName *p) \
     173    { \
     174        (void)p; \
     175        return #FixtureName; \
     176    } \
    136177    class FixtureName : public lol::Fixture<FixtureName>
    137178
     
    146187        } \
    147188    }; \
    148     TestCase##TestCaseName test_case_##TestCaseName; \
     189    TestCase##TestCaseName lol_unit_test_case_##TestCaseName; \
    149190    void TestCaseName()
    150191
     
    153194
    154195#define LOLUNIT_ASSERT(cond) \
    155     do { if (True() && !(cond)) \
    156     { \
    157         std::cout << "FAIL! " #cond << std::endl; \
    158         return; \
    159     } } while(!True())
     196    do { \
     197        m_asserts++; \
     198        if (True() && !(cond)) \
     199        { \
     200            m_errorlog << std::endl << std::endl; \
     201            m_errorlog << ++m_failcases << ") test: " \
     202                       << LolUnitFixtureName(this) << "::" << m_currentname \
     203                       << " (F) line: " << __LINE__ << " " \
     204                       << __FILE__ << std::endl; \
     205            m_errorlog << "assertion failed" << std::endl; \
     206            m_errorlog << "- Expression: " << #cond << std::endl; \
     207            m_failure = true; \
     208            return; \
     209        } \
     210    } while(!True())
    160211
    161212#define LOLUNIT_ASSERT_EQUAL(a, b) \
    162     do { if (True() && (a) != (b)) \
    163     { \
    164         std::cout << "FAIL! " #a " != " #b << std::endl; \
    165         std::cout << "expected: " << (a) << std::endl; \
    166         std::cout << "returned: " << (b) << std::endl; \
    167         return; \
    168     } } while(!True())
     213    do { \
     214        m_asserts++; \
     215        if (True() && (a) != (b)) \
     216        { \
     217            m_errorlog << std::endl << std::endl; \
     218            m_errorlog << ++m_failcases << ") test: " \
     219                       << LolUnitFixtureName(this) << "::" << m_currentname \
     220                       << " (F) line: " << __LINE__ << " " \
     221                       << __FILE__ << std::endl; \
     222            m_errorlog << "equality assertion failed" << std::endl; \
     223            m_errorlog << "- Expected: " << (b) << std::endl; \
     224            m_errorlog << "- Actual  : " << (a) << std::endl; \
     225            m_errorlog << message; \
     226            m_failure = true; \
     227            return; \
     228        } \
     229    } while(!True())
    169230
    170231#define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \
    171     do { if (True() && fabs((a) - (b)) > fabs((t))) \
    172     { \
    173         std::cout << "FAIL! " #a " != " #b << std::endl; \
    174         std::cout << "expected: " << (a) << std::endl; \
    175         std::cout << "returned: " << (b) << std::endl; \
    176         return; \
    177     } } while(!True())
     232    do { \
     233        m_asserts++; \
     234        if (True() && fabs((a) - (b)) > fabs((t))) \
     235        { \
     236            m_errorlog << std::endl << std::endl; \
     237            m_errorlog << ++m_failcases << ") test: " \
     238                       << LolUnitFixtureName(this) << "::" << m_currentname \
     239                       << " (F) line: " << __LINE__ << " " \
     240                       << __FILE__ << std::endl; \
     241            m_errorlog << "double equality assertion failed" << std::endl; \
     242            m_errorlog << "- Expected: " << (b) << std::endl; \
     243            m_errorlog << "- Actual  : " << (a) << std::endl; \
     244            m_errorlog << "- Delta   : " << (t) << std::endl; \
     245            m_failure = true; \
     246            return; \
     247        } \
     248    } while(!True())
    178249
    179250} /* namespace lol */
  • trunk/test/lol-test.cpp

    r912 r913  
    1414
    1515#include <cstdio>
     16#include <cstdlib>
    1617
    1718#include <lol/unit.h>
     
    2021{
    2122    lol::TestRunner runner;
    22     int ret = !runner.Run();
    23     return ret;
     23    bool success = runner.Run();
     24    return success ? EXIT_SUCCESS : EXIT_FAILURE;
    2425}
    2526
Note: See TracChangeset for help on using the changeset viewer.