Ignore:
Timestamp:
Sep 8, 2011, 2:17:00 PM (8 years ago)
Author:
sam
Message:

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:
1 edited

Legend:

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

    r925 r927  
    1818
    1919#include <iostream>
     20#include <iomanip>
    2021#include <sstream>
    2122#include <cstdio>
     
    6465    int m_asserts, m_failure;
    6566    char const *m_fixturename, *m_currentname;
    66     std::stringstream m_errorlog;
     67    std::stringstream m_errorlog, m_context;
    6768};
    6869
     
    106107            m_failure = false;
    107108            m_currentname = c->m_testname;
     109            m_context.str("");
    108110            (static_cast<FixtureClass *>(this)->*c->m_fun)();
    109111            std::cout << (m_failure ? 'F' : '.');
     
    227229                       << __FILE__ << std::endl; \
    228230            m_errorlog << "equality assertion failed" << std::endl; \
    229             m_errorlog << "- Expected: " << (a) << std::endl; \
    230             m_errorlog << "- Actual  : " << (b) << std::endl; \
     231            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
     232            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
    231233            m_errorlog << message; \
     234            m_errorlog << m_context.str(); \
    232235            m_failure = true; \
    233236            return; \
     
    246249                       << __FILE__ << std::endl; \
    247250            m_errorlog << "double equality assertion failed" << std::endl; \
    248             m_errorlog << "- Expected: " << (a) << std::endl; \
    249             m_errorlog << "- Actual  : " << (b) << std::endl; \
    250             m_errorlog << "- Delta   : " << (t) << 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); \
    251257            m_errorlog << message; \
     258            m_errorlog << m_context.str(); \
    252259            m_failure = true; \
    253260            return; \
     
    265272        m_errorlog << "forced failure" << std::endl; \
    266273        m_errorlog << "- " << message << std::endl; \
     274        m_errorlog << m_context.str(); \
    267275        m_failure = true; \
    268276        return; \
    269277    } while(!True())
    270278
     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
    271288#define LOLUNIT_ASSERT(cond) \
    272289    LOLUNIT_ASSERT_GENERIC("", cond)
Note: See TracChangeset for help on using the changeset viewer.