Skip to content

Commit 097c71e

Browse files
authored
Merge pull request #114 from unittest-cpp/long_and_short_macros
Long and short macros
2 parents 77ae15a + 910381f commit 097c71e

File tree

8 files changed

+198
-105
lines changed

8 files changed

+198
-105
lines changed

UnitTest++/CheckMacros.h

Lines changed: 84 additions & 56 deletions
Large diffs are not rendered by default.

UnitTest++/Config.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifdef _USRDLL
1717
#define UNITTEST_WIN32_DLL
1818
#endif
19-
19+
2020
#define UNITTEST_WIN32
2121
#endif
2222

@@ -72,4 +72,13 @@
7272
#define UNIITEST_NS_QUAL_STD(x) ::std::x
7373
#endif
7474

75+
// By default, UnitTest++ will attempt to define "short" macro names like CHECK and CHECK_EQUAL
76+
// for "public" interface macros etc. Defining UNITTEST_DISABLE_SHORT_MACROS in your project
77+
// will disable this behavior, leaving only the longer macros "namespaced" with the UNITTEST_
78+
// prefix.
79+
//
80+
// "Internal" utility macros will only have the UNITTEST_IMPL_ prefix.
81+
82+
// #define UNITTEST_DISABLE_SHORT_MACROS
83+
7584
#endif

UnitTest++/ExceptionMacros.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
#include "Config.h"
55

66
#ifndef UNITTEST_NO_EXCEPTIONS
7-
#define UT_TRY(x) try x
8-
#define UT_THROW(x) throw x
9-
#define UT_RETHROW(ExceptionType) catch(ExceptionType&) { throw; }
10-
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
11-
#define UT_CATCH_ALL(CatchBody) catch(...) CatchBody
7+
#define UNITTEST_IMPL_TRY(x) try x
8+
#define UNITTEST_IMPL_THROW(x) throw x
9+
#define UNITTEST_IMPL_RETHROW(ExceptionType) catch(ExceptionType&) { throw; }
10+
#define UNITTEST_IMPL_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
11+
#define UNITTEST_IMPL_CATCH_ALL(CatchBody) catch(...) CatchBody
1212
#else
13-
#define UT_TRY(x) x
14-
#define UT_THROW(x)
15-
#define UT_RETHROW(ExceptionType)
16-
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody)
17-
#define UT_CATCH_ALL(CatchBody)
13+
#define UNITTEST_IMPL_TRY(x) x
14+
#define UNITTEST_IMPL_THROW(x)
15+
#define UNITTEST_IMPL_RETHROW(ExceptionType)
16+
#define UNITTEST_IMPL_CATCH(ExceptionType, ExceptionName, CatchBody)
17+
#define UNITTEST_IMPL_CATCH_ALL(CatchBody)
1818
#endif
1919

2020
#endif

UnitTest++/ExecuteTest.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ namespace UnitTest {
3131
{
3232
#endif
3333
#ifndef UNITTEST_POSIX
34-
UT_TRY({ testObject.RunImpl(); })
34+
UNITTEST_IMPL_TRY({ testObject.RunImpl(); })
3535
#else
36-
UT_TRY
36+
UNITTEST_IMPL_TRY
3737
({
3838
UNITTEST_THROW_SIGNALS_POSIX_ONLY
3939
testObject.RunImpl();
4040
})
4141
#endif
42-
UT_CATCH(RequiredCheckException, e, { (void)e; })
43-
UT_CATCH(AssertException, e, { (void)e; })
44-
UT_CATCH(std::exception, e,
42+
UNITTEST_IMPL_CATCH(RequiredCheckException, e, { (void)e; })
43+
UNITTEST_IMPL_CATCH(AssertException, e, { (void)e; })
44+
UNITTEST_IMPL_CATCH(std::exception, e,
4545
{
4646
MemoryOutStream stream;
4747
stream << "Unhandled exception: " << e.what();
4848
CurrentTest::Results()->OnTestFailure(details, stream.GetText());
4949
})
50-
UT_CATCH_ALL
50+
UNITTEST_IMPL_CATCH_ALL
5151
({
5252
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed");
5353
})

UnitTest++/RequireMacros.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
#include "RequiredCheckTestReporter.h"
55

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

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

1216
#endif

UnitTest++/TestMacros.h

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,7 @@
1515
#include "Posix/SignalTranslator.h"
1616
#endif
1717

18-
#ifdef TEST
19-
#error UnitTest++ redefines TEST
20-
#endif
21-
22-
#ifdef TEST_EX
23-
#error UnitTest++ redefines TEST_EX
24-
#endif
25-
26-
#ifdef TEST_FIXTURE_EX
27-
#error UnitTest++ redefines TEST_FIXTURE_EX
28-
#endif
29-
30-
#define SUITE(Name) \
18+
#define UNITTEST_SUITE(Name) \
3119
namespace Suite ## Name { \
3220
namespace UnitTestSuite { \
3321
inline char const* GetSuiteName () { \
@@ -37,7 +25,7 @@
3725
} \
3826
namespace Suite ## Name
3927

40-
#define TEST_EX(Name, List) \
28+
#define UNITTEST_IMPL_TEST(Name, List) \
4129
class Test ## Name : public UnitTest::Test \
4230
{ \
4331
public: \
@@ -51,10 +39,10 @@
5139
void Test ## Name::RunImpl() const
5240

5341

54-
#define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList())
42+
#define UNITTEST_TEST(Name) UNITTEST_IMPL_TEST(Name, UnitTest::Test::GetTestList())
5543

5644

57-
#define TEST_FIXTURE_EX(Fixture, Name, List) \
45+
#define UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, List) \
5846
class Fixture ## Name ## Helper : public Fixture \
5947
{ \
6048
public: \
@@ -79,23 +67,23 @@
7967
void Test ## Fixture ## Name::RunImpl() const \
8068
{ \
8169
volatile bool ctorOk = false; \
82-
UT_TRY \
70+
UNITTEST_IMPL_TRY \
8371
({ \
8472
Fixture ## Name ## Helper fixtureHelper(m_details); \
8573
ctorOk = true; \
8674
UnitTest::ExecuteTest(fixtureHelper, m_details, false); \
8775
}) \
88-
UT_CATCH (UnitTest::AssertException, e, \
76+
UNITTEST_IMPL_CATCH (UnitTest::AssertException, e, \
8977
{ \
9078
(void)e; \
9179
}) \
92-
UT_CATCH (std::exception, e, \
80+
UNITTEST_IMPL_CATCH (std::exception, e, \
9381
{ \
9482
UnitTest::MemoryOutStream stream; \
9583
stream << "Unhandled exception: " << e.what(); \
9684
UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \
9785
}) \
98-
UT_CATCH_ALL \
86+
UNITTEST_IMPL_CATCH_ALL \
9987
({ \
10088
if (ctorOk) \
10189
{ \
@@ -111,7 +99,26 @@
11199
} \
112100
void Fixture ## Name ## Helper::RunImpl()
113101

114-
#define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList())
102+
#define UNITTEST_TEST_FIXTURE(Fixture,Name) UNITTEST_IMPL_TEST_FIXTURE(Fixture, Name, UnitTest::Test::GetTestList())
103+
104+
#ifndef UNITTEST_DISABLE_SHORT_MACROS
105+
#ifdef SUITE
106+
#error SUITE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_SUITE instead
107+
#else
108+
#define SUITE UNITTEST_SUITE
109+
#endif
115110

111+
#ifdef TEST
112+
#error TEST already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST instead
113+
#else
114+
#define TEST UNITTEST_TEST
115+
#endif
116+
117+
#ifdef TEST_FIXTURE
118+
#error TEST_FIXTURE already defined, re-configure with UNITTEST_ENABLE_SHORT_MACROS set to 0 and use UNITTEST_TEST_FIXTURE instead
119+
#else
120+
#define TEST_FIXTURE UNITTEST_TEST_FIXTURE
121+
#endif
122+
#endif
116123

117124
#endif

tests/TestLongMacros.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#define UNITTEST_DISABLE_SHORT_MACROS
2+
3+
#include "UnitTest++/UnitTestPP.h"
4+
5+
// This file is not intended to test every little thing, just a few basics to hopefully ensure
6+
// the macros are working and the short macros are not defined.
7+
UNITTEST_SUITE(LongMacros)
8+
{
9+
UNITTEST_TEST(LongCheckMacroWorks)
10+
{
11+
UNITTEST_CHECK(true);
12+
}
13+
14+
class Fixture
15+
{
16+
public:
17+
Fixture() : sanity_(true) {}
18+
protected:
19+
bool sanity_;
20+
};
21+
22+
UNITTEST_TEST_FIXTURE(Fixture, LongFixtureMacroWorks)
23+
{
24+
UNITTEST_REQUIRE UNITTEST_CHECK(sanity_);
25+
}
26+
27+
UNITTEST_TEST(ShortMacrosAreNotDefined)
28+
{
29+
#if defined(CHECK) || \
30+
defined(CHECK_EQUAL) || \
31+
defined(CHECK_CLOSE) || \
32+
defined(CHECK_ARRAY_EQUAL) || \
33+
defined(CHECK_ARRAY_CLOSE) || \
34+
defined(CHECK_ARRAY2D_CLOSE) || \
35+
defined(CHECK_THROW) || \
36+
defined(CHECK_ASSERT) || \
37+
defined(SUITE) || \
38+
defined(TEST) || \
39+
defined(TEST_FIXTURE) || \
40+
defined(REQUIRE)
41+
42+
UNITTEST_CHECK(false);
43+
#endif
44+
}
45+
}

tests/TestTestMacros.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using namespace std;
4646
namespace {
4747

4848
TestList list1;
49-
TEST_EX(DummyTest, list1)
49+
UNITTEST_IMPL_TEST(DummyTest, list1)
5050
{}
5151

5252
TEST (TestsAreAddedToTheListThroughMacro)
@@ -69,7 +69,7 @@ namespace {
6969
};
7070

7171
TestList list2;
72-
TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2)
72+
UNITTEST_IMPL_TEST_FIXTURE(ThrowingThingie, DummyTestName, list2)
7373
{}
7474

7575
TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
@@ -113,7 +113,7 @@ namespace {
113113
}
114114

115115
TestList macroTestList1;
116-
TEST_EX(MacroTestHelper1, macroTestList1)
116+
UNITTEST_IMPL_TEST(MacroTestHelper1, macroTestList1)
117117
{}
118118

119119
TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite)
@@ -124,7 +124,7 @@ namespace {
124124
}
125125

126126
TestList macroTestList2;
127-
TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2)
127+
UNITTEST_IMPL_TEST_FIXTURE(DummyFixture, MacroTestHelper2, macroTestList2)
128128
{}
129129

130130
TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
@@ -144,7 +144,7 @@ namespace {
144144
};
145145

146146
TestList throwingFixtureTestList1;
147-
TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
147+
UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
148148
{}
149149

150150
TEST(FixturesWithThrowingCtorsAreFailures)
@@ -170,7 +170,7 @@ namespace {
170170
};
171171

172172
TestList throwingFixtureTestList2;
173-
TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
173+
UNITTEST_IMPL_TEST_FIXTURE(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
174174
{}
175175

176176
TEST(FixturesWithThrowingDtorsAreFailures)
@@ -200,7 +200,7 @@ namespace {
200200
};
201201

202202
TestList ctorAssertFixtureTestList;
203-
TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
203+
UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
204204
{}
205205

206206
TEST(CorrectlyReportsFixturesWithCtorsThatAssert)

0 commit comments

Comments
 (0)