summaryrefslogtreecommitdiff
path: root/test/ClientTestAssert.h
blob: 35e81e430e532725733560a6e11f0b3017916aec (plain)
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
/*
 * Copyright (C) 2011 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef INCL_CLIENTTESTASSERT
#define INCL_CLIENTTESTASSERT

#include <cppunit/Exception.h>
#include <syncevo/util.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * All of the macros below include the behavior of the corresponding
 * CPUNIT assertion. In addition they catch exceptions and turn them
 * into the following, extended CPPUnit exception by preserving the
 * information from the original exception and adding the current
 * source code line.
 *
 * Source information is listed as inner first (as part of the error
 * preamble), followed by error at that location and all locations
 * that the error passed through until caught at the top level of a
 * test.
 */
class CTException : public CppUnit::Exception
{

 public:
    CTException(const CppUnit::Message &message = CppUnit::Message(),
                const std::string &currentMessage = "",
                const CppUnit::SourceLine &currentSourceLine = CppUnit::SourceLine(),
                const CppUnit::SourceLine &previousSourceLine = CppUnit::SourceLine()) :
    CppUnit::Exception(message, previousSourceLine)
    {
        CppUnit::Message extendedMessage = message;
        if (!currentMessage.empty()) {
            extendedMessage.addDetail(currentMessage);
        }
        if (currentSourceLine.isValid()) {
            extendedMessage.addDetail(StringPrintf("%s:%d",
                                                   getBasename(currentSourceLine.fileName()).c_str(),
                                                   currentSourceLine.lineNumber()));
        }
        setMessage(extendedMessage);
    }
};

static void inline ClientTestExceptionHandle(const char *file, int line, const std::string &message = "")
{
    CppUnit::SourceLine here(file, line);
    try {
        throw;
    }  catch (const CTException &ex) {
        throw(CTException(ex.message(),
                          message,
                          here,
                          ex.sourceLine()));
    } catch (const CppUnit::Exception &ex) {
        if (ex.sourceLine() == CPPUNIT_SOURCELINE()) {
            /* failure in condition expression itself, already includes source info, pass through */
            throw;
        } else {
            throw(CTException(ex.message(),
                              message,
                              here,
                              ex.sourceLine()));
        }
    } catch (const Exception &ex) {
        throw(CTException(CppUnit::Message(ex.what()),
                          message,
                          here,
                          CppUnit::SourceLine(ex.m_file, ex.m_line)));
    } catch (const std::exception &ex) {
        CppUnit::Message msg(CPPUNIT_EXTRACT_EXCEPTION_TYPE_(ex,
                                                             "std::exception or derived"));
        msg.addDetail(std::string("What(): ") + ex.what());
        throw(CTException(msg,
                          message,
                          here));
    }
}

#define CT_WRAP_ASSERT(_file, _line, _assert) \
    do { \
       try { \
           SE_LOG_DEBUG(NULL, NULL, "%s:%d: starting %s", getBasename(_file).c_str(), _line, #_assert); \
           _assert; \
           SE_LOG_DEBUG(NULL, NULL, "%s:%d: ending %s", getBasename(_file).c_str(), _line, #_assert); \
       } catch (...) { \
           ClientTestExceptionHandle(_file, _line); \
       } \
    } while (false)

#define CT_WRAP_ASSERT_MESSAGE(_file, _line, _message, _assert)  \
    do { \
       try { \
           SE_LOG_DEBUG(NULL, NULL, "%s:%d: starting %s %s", \
                        getBasename(_file).c_str(), _line, \
                        std::string(_message).c_str(), \
                        #_assert); \
           _assert; \
           SE_LOG_DEBUG(NULL, NULL, "%s:%d: ending %s", getBasename(_file).c_str(), _line, #_assert); \
       } catch (...) { \
           ClientTestExceptionHandle(_file, _line, _message); \
       } \
    } while (false)

#define CT_ASSERT(condition) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT(condition))
#define CT_ASSERT_NO_THROW(expression) CT_WRAP_ASSERT(__FILE__, __LINE__, expression)
#define CT_ASSERT_NO_THROW_MESSAGE(message, expression) CT_WRAP_ASSERT_MESSAGE(__FILE__, __LINE__, message, expression)
#define CT_ASSERT_MESSAGE(message,condition) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT_MESSAGE(message,condition))
#define CT_FAIL(message) CPPUNIT_FAIL(message)
#define CT_ASSERT_EQUAL(expected,actual) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT_EQUAL(expected,actual))
#define CT_ASSERT_EQUAL_MESSAGE(message,expected,actual) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual))
#define CT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta))
#define CT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) CT_WRAP_ASSERT(__FILE__, __LINE__, CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(__FILE__, __LINE__, message,expected,actual,delta))

SE_END_CXX

#endif  // INCL_CLIENTTESTASSERT