wxWidgets/include/wx/catch_cppunit.h

260 lines
8.1 KiB
C
Raw Normal View History

Replace CppUnit with Catch for unit tests Drop the legacy CppUnit testing framework used for the unit tests. Replacing it with Catch has the advantage of not requiring CppUnit libraries to be installed on the system in order to be able to run tests (Catch is header-only and a copy of it is now included in the main repository itself) and, in the future, of being able to write the tests in a much more natural way. For now, however, avoid changing the existing tests code as much as [reasonably] possible to avoid introducing bugs in them and provide the CppUnit compatibility macros in the new wx/catch_cppunit.h header which allow to preserve the 99% of the existing code unchanged. Some of the required changes are: - Decompose asserts using "a && b" conditions into multiple asserts checking "a" and "b" independently. This would have been better even with CppUnit (to know which part of condition exactly failed) and is required with Catch. - Use extra parentheses around such conditions when they can't be easily decomposed in the arrays test, due to the use of macros. This is not ideal from the point of view of messages given when the tests fail but will do for now. - Rewrite asserts using "a || b" as a combination of condition checks and assert macros. Again, this is better anyhow, and is required with Catch. Incidentally, this allowed to fix a bug in the "exec" unit test which didn't leave enough time for the new process to be launched before trying to kill it. - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros, our emulation of this macro can be used only once. - Provide string conversions using Catch-specific StringMaker for a couple of types. - Replace custom wxImage comparison with a Catch-specific matcher class. - Remove most of test running logic from test.cpp, in particular don't parse command line ourselves any longer but use Catch built-in command line parser. This is a source of a minor regression: previously, both "Foo" and "FooTestCase" could be used as the name of the test to run, but now only the latter is accepted.
2017-11-01 18:15:24 +00:00
/////////////////////////////////////////////////////////////////////////////
// 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);
}
};
}
// 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 \
{ \
struct EatNextSemicolon
#define CPPUNIT_TEST(testname) \
SECTION(#testname " test") \
{ \
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()), (cond))
#define WX_ASSERT_EQUAL_MESSAGE(msg, expected, actual) \
CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string(wxString::Format msg .mb_str()), \
(expected), (actual))
#endif // _WX_CATCH_CPPUNIT_H_