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
|
/////////////////////////////////////////////////////////////////////////////
// Name: wx/catch_cppunit.h
// Purpose: Reimplementation of CppUnit macros in terms of CATCH
// Author: Vadim Zeitlin
// Created: 2017-10-30
// Copyright: (c) 2017 Vadim Zeitlin <vadim@wxwidgets.org>
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_CATCH_CPPUNIT_H_
#define _WX_CATCH_CPPUNIT_H_
#include "catch.hpp"
// CppUnit-compatible macros.
// CPPUNIT_ASSERTs are mapped to REQUIRE(), not CHECK(), as this is how CppUnit
// works but in many cases they really should be CHECK()s instead, i.e. the
// test should continue to run even if one assert failed. Unfortunately there
// is no automatic way to know it, so the existing code will need to be
// reviewed and CHECK() used explicitly where appropriate.
//
// Also notice that we don't use parentheses around x and y macro arguments in
// the macro expansion, as usual. This is because these parentheses would then
// appear in CATCH error messages if the assertion fails, making them much less
// readable and omitting should be fine here, exceptionally, as the arguments
// of these macros are usually just simple expressions.
#define CPPUNIT_ASSERT(cond) REQUIRE(cond)
#define CPPUNIT_ASSERT_EQUAL(x, y) REQUIRE(x == y)
// Using INFO() disallows putting more than one of these macros on the same
// line but this can happen if they're used inside another macro, so wrap it
// inside a scope.
#define CPPUNIT_ASSERT_MESSAGE(msg, cond) \
do { INFO(msg); REQUIRE(cond); } while (Catch::alwaysFalse())
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, x, y) \
do { INFO(msg); REQUIRE(x == y); } while (Catch::alwaysFalse())
// CATCH Approx class uses the upper bound of "epsilon*(scale + max(|x|, |y|))"
// for |x - y| which is not really compatible with our fixed delta, so we can't
// use it here.
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(x, y, delta) \
REQUIRE(std::abs(x - y) < delta)
#define CPPUNIT_FAIL(msg) FAIL(msg)
#define CPPUNIT_ASSERT_THROW(expr, exception) \
try \
{ \
expr; \
FAIL("Expected exception of type " #exception \
" not thrown by " #expr); \
} \
catch ( exception& ) {}
// Define conversions to strings for some common wxWidgets types.
namespace Catch
{
template <>
struct StringMaker<wxUniChar>
{
static std::string convert(wxUniChar uc)
{
return wxString(uc).ToStdString(wxConvUTF8);
}
};
template <>
struct StringMaker<wxUniCharRef>
{
static std::string convert(wxUniCharRef ucr)
{
return wxString(ucr).ToStdString(wxConvUTF8);
}
};
// While this conversion already works due to the existence of the stream
// insertion operator for wxString, define a custom one making it more
// obvious when strings containing non-printable characters differ.
template <>
struct StringMaker<wxString>
{
static std::string convert(const wxString& wxs)
{
std::string s;
s.reserve(wxs.length());
for ( wxString::const_iterator i = wxs.begin();
i != wxs.end();
++i )
{
#if wxUSE_UNICODE
if ( !iswprint(*i) )
s += wxString::Format(wxASCII_STR("\\u%04X"), *i).ToAscii();
else
#endif // wxUSE_UNICODE
s += *i;
}
return s;
}
};
}
// Use a different namespace for our mock ups of the real declarations in
// CppUnit namespace to avoid clashes if we end up being linked with the real
// CppUnit library, but bring it into scope with a using directive below to
// make it possible to compile the original code using CppUnit unmodified.
namespace CatchCppUnit
{
namespace CppUnit
{
// These classes don't really correspond to the real CppUnit ones, but contain
// just the minimum we need to make CPPUNIT_TEST() macro and our mock up of
// TestSuite class work.
class Test
{
public:
// Name argument exists only for compatibility with the real CppUnit but is
// not used here.
explicit Test(const std::string& name = std::string()) : m_name(name) { }
virtual ~Test() { }
virtual void runTest() = 0;
const std::string& getName() const { return m_name; }
private:
std::string m_name;
};
class TestCase : public Test
{
public:
explicit TestCase(const std::string& name = std::string()) : Test(name) { }
virtual void setUp() {}
virtual void tearDown() {}
};
class TestSuite : public Test
{
public:
explicit TestSuite(const std::string& name = std::string()) : Test(name) { }
~TestSuite()
{
for ( size_t n = 0; n < m_tests.size(); ++n )
{
delete m_tests[n];
}
}
void addTest(Test* test) { m_tests.push_back(test); }
size_t getChildTestCount() const { return m_tests.size(); }
void runTest() wxOVERRIDE
{
for ( size_t n = 0; n < m_tests.size(); ++n )
{
m_tests[n]->runTest();
}
}
private:
std::vector<Test*> m_tests;
wxDECLARE_NO_COPY_CLASS(TestSuite);
};
} // namespace CppUnit
} // namespace CatchCppUnit
using namespace CatchCppUnit;
// Helpers used in the implementation of the macros below.
namespace wxPrivate
{
// An object which resets a string to its old value when going out of scope.
class TempStringAssign
{
public:
explicit TempStringAssign(std::string& str, const char* value)
: m_str(str),
m_orig(str)
{
str = value;
}
~TempStringAssign()
{
m_str = m_orig;
}
private:
std::string& m_str;
const std::string m_orig;
wxDECLARE_NO_COPY_CLASS(TempStringAssign);
};
// These two strings are used to implement wxGetCurrentTestName() and must be
// defined in the test driver.
extern std::string wxTheCurrentTestClass, wxTheCurrentTestMethod;
} // namespace wxPrivate
inline std::string wxGetCurrentTestName()
{
std::string s = wxPrivate::wxTheCurrentTestClass;
if ( !s.empty() && !wxPrivate::wxTheCurrentTestMethod.empty() )
s += "::";
s += wxPrivate::wxTheCurrentTestMethod;
return s;
}
// Notice that the use of this macro unconditionally changes the protection for
// everything that follows it to "public". This is necessary to allow taking
// the address of the runTest() method in CPPUNIT_TEST_SUITE_REGISTRATION()
// below and there just doesn't seem to be any way around it.
#define CPPUNIT_TEST_SUITE(testclass) \
public: \
void runTest() wxOVERRIDE \
{ \
using namespace wxPrivate; \
TempStringAssign setClass(wxTheCurrentTestClass, #testclass)
#define CPPUNIT_TEST(testname) \
SECTION(#testname) \
{ \
TempStringAssign setMethod(wxTheCurrentTestMethod, #testname); \
setUp(); \
try \
{ \
testname(); \
} \
catch ( ... ) \
{ \
tearDown(); \
throw; \
} \
tearDown(); \
}
#define CPPUNIT_TEST_SUITE_END() \
} \
struct EatNextSemicolon
#define wxREGISTER_UNIT_TEST_WITH_TAGS(testclass, tags) \
METHOD_AS_TEST_CASE(testclass::runTest, #testclass, tags) \
struct EatNextSemicolon
#define CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(testclass, suitename) \
wxREGISTER_UNIT_TEST_WITH_TAGS(testclass, "[" suitename "]")
// Existings tests always use both this macro and the named registration one
// above, but we can't register the same test case twice with CATCH, so simply
// ignore this one.
#define CPPUNIT_TEST_SUITE_REGISTRATION(testclass) \
struct EatNextSemicolon
// ----------------------------------------------------------------------------
// wxWidgets-specific macros
// ----------------------------------------------------------------------------
// Convenient variant of INFO() which uses wxString::Format() internally.
#define wxINFO_FMT_HELPER(fmt, ...) \
wxString::Format(fmt, __VA_ARGS__).ToStdString(wxConvUTF8)
#define wxINFO_FMT(...) INFO(wxINFO_FMT_HELPER(__VA_ARGS__))
// Use this macro to assert with the given formatted message (it should contain
// the format string and arguments in a separate pair of parentheses)
#define WX_ASSERT_MESSAGE(msg, cond) \
CPPUNIT_ASSERT_MESSAGE(std::string(wxString::Format msg .mb_str(wxConvLibc)), (cond))
#define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str(wxConvLibc)), \
(expected), (actual))
#endif // _WX_CATCH_CPPUNIT_H_
|