Skip to content

Long and short macros #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 84 additions & 56 deletions UnitTest++/CheckMacros.h

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion UnitTest++/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifdef _USRDLL
#define UNITTEST_WIN32_DLL
#endif

#define UNITTEST_WIN32
#endif

Expand Down Expand Up @@ -71,4 +71,13 @@
#define UNIITEST_NS_QUAL_STD(x) ::std::x
#endif

// By default, UnitTest++ will attempt to define "short" macro names like CHECK and CHECK_EQUAL
// for "public" interface macros etc. Defining UNITTEST_DISABLE_SHORT_MACROS in your project
// will disable this behavior, leaving only the longer macros "namespaced" with the UNITTEST_
// prefix.
//
// "Internal" utility macros will only have the UNITTEST_IMPL_ prefix.

// #define UNITTEST_DISABLE_SHORT_MACROS

#endif
20 changes: 10 additions & 10 deletions UnitTest++/ExceptionMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include "Config.h"

#ifndef UNITTEST_NO_EXCEPTIONS
#define UT_TRY(x) try x
#define UT_THROW(x) throw x
#define UT_RETHROW(ExceptionType) catch(ExceptionType&) { throw; }
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
#define UT_CATCH_ALL(CatchBody) catch(...) CatchBody
#define UNITTEST_IMPL_TRY(x) try x
#define UNITTEST_IMPL_THROW(x) throw x
#define UNITTEST_IMPL_RETHROW(ExceptionType) catch(ExceptionType&) { throw; }
#define UNITTEST_IMPL_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
#define UNITTEST_IMPL_CATCH_ALL(CatchBody) catch(...) CatchBody
#else
#define UT_TRY(x) x
#define UT_THROW(x)
#define UT_RETHROW(ExceptionType)
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody)
#define UT_CATCH_ALL(CatchBody)
#define UNITTEST_IMPL_TRY(x) x
#define UNITTEST_IMPL_THROW(x)
#define UNITTEST_IMPL_RETHROW(ExceptionType)
#define UNITTEST_IMPL_CATCH(ExceptionType, ExceptionName, CatchBody)
#define UNITTEST_IMPL_CATCH_ALL(CatchBody)
#endif

#endif
12 changes: 6 additions & 6 deletions UnitTest++/ExecuteTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ namespace UnitTest {
{
#endif
#ifndef UNITTEST_POSIX
UT_TRY({ testObject.RunImpl(); })
UNITTEST_IMPL_TRY({ testObject.RunImpl(); })
#else
UT_TRY
UNITTEST_IMPL_TRY
({
UNITTEST_THROW_SIGNALS_POSIX_ONLY
testObject.RunImpl();
})
#endif
UT_CATCH(RequiredCheckException, e, { (void)e; })
UT_CATCH(AssertException, e, { (void)e; })
UT_CATCH(std::exception, e,
UNITTEST_IMPL_CATCH(RequiredCheckException, e, { (void)e; })
UNITTEST_IMPL_CATCH(AssertException, e, { (void)e; })
UNITTEST_IMPL_CATCH(std::exception, e,
{
MemoryOutStream stream;
stream << "Unhandled exception: " << e.what();
CurrentTest::Results()->OnTestFailure(details, stream.GetText());
})
UT_CATCH_ALL
UNITTEST_IMPL_CATCH_ALL
({
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed");
})
Expand Down
12 changes: 8 additions & 4 deletions UnitTest++/RequireMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

#include "RequiredCheckTestReporter.h"

#ifdef REQUIRE
#error UnitTest++ redefines REQUIRE
#endif
#define UNITTEST_REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(*UnitTest::CurrentTest::Results()); decoratedReporter.Next(); )

#define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(*UnitTest::CurrentTest::Results()); decoratedReporter.Next(); )
#ifndef UNITTEST_DISABLE_SHORT_MACROS
#ifdef REQUIRE
#error REQUIRE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_REQUIRE instead
#else
#define REQUIRE UNITTEST_REQUIRE
#endif
#endif

#endif
49 changes: 28 additions & 21 deletions UnitTest++/TestMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,7 @@
#include "Posix/SignalTranslator.h"
#endif

#ifdef TEST
#error UnitTest++ redefines TEST
#endif

#ifdef TEST_EX
#error UnitTest++ redefines TEST_EX
#endif

#ifdef TEST_FIXTURE_EX
#error UnitTest++ redefines TEST_FIXTURE_EX
#endif

#define SUITE(Name) \
#define UNITTEST_SUITE(Name) \
namespace Suite ## Name { \
namespace UnitTestSuite { \
inline char const* GetSuiteName () { \
Expand All @@ -37,7 +25,7 @@
} \
namespace Suite ## Name

#define TEST_EX(Name, List) \
#define UNITTEST_IMPL_TEST(Name, List) \
class Test ## Name : public UnitTest::Test \
{ \
public: \
Expand All @@ -51,10 +39,10 @@
void Test ## Name::RunImpl() const


#define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList())
#define UNITTEST_TEST(Name) UNITTEST_IMPL_TEST(Name, UnitTest::Test::GetTestList())


#define TEST_FIXTURE_EX(Fixture, Name, List) \
#define UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, List) \
class Fixture ## Name ## Helper : public Fixture \
{ \
public: \
Expand All @@ -79,23 +67,23 @@
void Test ## Fixture ## Name::RunImpl() const \
{ \
volatile bool ctorOk = false; \
UT_TRY \
UNITTEST_IMPL_TRY \
({ \
Fixture ## Name ## Helper fixtureHelper(m_details); \
ctorOk = true; \
UnitTest::ExecuteTest(fixtureHelper, m_details, false); \
}) \
UT_CATCH (UnitTest::AssertException, e, \
UNITTEST_IMPL_CATCH (UnitTest::AssertException, e, \
{ \
(void)e; \
}) \
UT_CATCH (std::exception, e, \
UNITTEST_IMPL_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream stream; \
stream << "Unhandled exception: " << e.what(); \
UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \
}) \
UT_CATCH_ALL \
UNITTEST_IMPL_CATCH_ALL \
({ \
if (ctorOk) \
{ \
Expand All @@ -111,7 +99,26 @@
} \
void Fixture ## Name ## Helper::RunImpl()

#define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList())
#define UNITTEST_TEST_FIXTURE(Fixture,Name) UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, UnitTest::Test::GetTestList())

#ifndef UNITTEST_DISABLE_SHORT_MACROS
#ifdef SUITE
#error SUITE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_SUITE instead
#else
#define SUITE UNITTEST_SUITE
#endif

#ifdef TEST
#error TEST already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST instead
#else
#define TEST UNITTEST_TEST
#endif

#ifdef TEST_FIXTURE
#error TEST_FIXTURE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST_FIXTURE instead
#else
#define TEST_FIXTURE UNITTEST_TEST_FIXTURE
#endif
#endif

#endif
45 changes: 45 additions & 0 deletions tests/TestLongMacros.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#define UNITTEST_DISABLE_SHORT_MACROS

#include "UnitTest++/UnitTestPP.h"

// This file is not intended to test every little thing, just a few basics to hopefully ensure
// the macros are working and the short macros are not defined.
UNITTEST_SUITE(LongMacros)
{
UNITTEST_TEST(LongCheckMacroWorks)
{
UNITTEST_CHECK(true);
}

class Fixture
{
public:
Fixture() : sanity_(true) {}
protected:
bool sanity_;
};

UNITTEST_TEST_FIXTURE(Fixture, LongFixtureMacroWorks)
{
UNITTEST_REQUIRE UNITTEST_CHECK(sanity_);
}

UNITTEST_TEST(ShortMacrosAreNotDefined)
{
#if defined(CHECK) || \
defined(CHECK_EQUAL) || \
defined(CHECK_CLOSE) || \
defined(CHECK_ARRAY_EQUAL) || \
defined(CHECK_ARRAY_CLOSE) || \
defined(CHECK_ARRAY2D_CLOSE) || \
defined(CHECK_THROW) || \
defined(CHECK_ASSERT) || \
defined(SUITE) || \
defined(TEST) || \
defined(TEST_FIXTURE) || \
defined(REQUIRE)

UNITTEST_CHECK(false);
#endif
}
}
14 changes: 7 additions & 7 deletions tests/TestTestMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using namespace std;
namespace {

TestList list1;
TEST_EX(DummyTest, list1)
UNITTEST_IMPL_TEST(DummyTest, list1)
{}

TEST (TestsAreAddedToTheListThroughMacro)
Expand All @@ -69,7 +69,7 @@ namespace {
};

TestList list2;
TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2)
UNITTEST_IMPL_TEST_FIXTURE(ThrowingThingie, DummyTestName, list2)
{}

TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
Expand Down Expand Up @@ -113,7 +113,7 @@ namespace {
}

TestList macroTestList1;
TEST_EX(MacroTestHelper1, macroTestList1)
UNITTEST_IMPL_TEST(MacroTestHelper1, macroTestList1)
{}

TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite)
Expand All @@ -124,7 +124,7 @@ namespace {
}

TestList macroTestList2;
TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2)
UNITTEST_IMPL_TEST_FIXTURE(DummyFixture, MacroTestHelper2, macroTestList2)
{}

TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
Expand All @@ -144,7 +144,7 @@ namespace {
};

TestList throwingFixtureTestList1;
TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
{}

TEST(FixturesWithThrowingCtorsAreFailures)
Expand All @@ -170,7 +170,7 @@ namespace {
};

TestList throwingFixtureTestList2;
TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
UNITTEST_IMPL_TEST_FIXTURE(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
{}

TEST(FixturesWithThrowingDtorsAreFailures)
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace {
};

TestList ctorAssertFixtureTestList;
TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
{}

TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
Expand Down