Skip to content

Commit 4f556d6

Browse files
committed
r4 | charles.nicholson | 2010-03-08 23:23:36 -0600 (Mon, 08 Mar 2010) | 1 line
all try/catch calls are either #ifdefd out or replaced with UT_TRY/UT_CATCH macro madness
1 parent 67d4ecb commit 4f556d6

File tree

8 files changed

+53
-42
lines changed

8 files changed

+53
-42
lines changed

src/Config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
// by default, MemoryOutStream is implemented in terms of std::ostringstream, which can be expensive.
2727
// uncomment this line to use the custom MemoryOutStream (no deps on std::ostringstream).
2828

29-
//#define UNITTEST_USE_CUSTOM_STREAMS
30-
29+
#define UNITTEST_USE_CUSTOM_STREAMS
3130
#define UNITTEST_USE_EXCEPTIONS
3231

3332
#endif

src/ExecuteTest.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef UNITTEST_EXECUTE_TEST_H
22
#define UNITTEST_EXECUTE_TEST_H
33

4+
#include "ExceptionMacros.h"
45
#include "TestDetails.h"
56
#include "MemoryOutStream.h"
67
#include "AssertException.h"
@@ -17,28 +18,34 @@ void ExecuteTest(T& testObject, TestDetails const& details)
1718
{
1819
CurrentTest::Details() = &details;
1920

20-
try
21-
{
22-
#ifdef UNITTEST_POSIX
23-
UNITTEST_THROW_SIGNALS
24-
#endif
21+
#ifndef UNITTEST_POSIX
22+
UT_TRY
23+
({
24+
testObject.RunImpl();
25+
})
26+
#else
27+
UT_TRY
28+
({
29+
UNITTEST_THROW_SIGNALS_POSIX_ONLY
2530
testObject.RunImpl();
26-
}
27-
catch (AssertException const& e)
31+
})
32+
#endif
33+
34+
UT_CATCH(AssertException, e,
2835
{
2936
CurrentTest::Results()->OnTestFailure(
3037
TestDetails(details.testName, details.suiteName, e.Filename(), e.LineNumber()), e.what());
31-
}
32-
catch (std::exception const& e)
38+
})
39+
UT_CATCH(std::exception, e,
3340
{
3441
MemoryOutStream stream;
3542
stream << "Unhandled exception: " << e.what();
3643
CurrentTest::Results()->OnTestFailure(details, stream.GetText());
37-
}
38-
catch (...)
39-
{
44+
})
45+
UT_CATCH_ALL
46+
({
4047
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: Crash!");
41-
}
48+
})
4249
}
4350

4451
}

src/Posix/SignalTranslator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SignalTranslator
3232
#define UNITTEST_EXTENSION __extension__
3333
#endif
3434

35-
#define UNITTEST_THROW_SIGNALS \
35+
#define UNITTEST_THROW_SIGNALS_POSIX_ONLY \
3636
UnitTest::SignalTranslator sig; \
3737
if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \
3838
throw ("Unhandled system exception");

src/TestMacros.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#define UNITTEST_TESTMACROS_H
33

44
#include "Config.h"
5+
#include "ExceptionMacros.h"
56
#include "ExecuteTest.h"
67
#include "AssertException.h"
78
#include "TestDetails.h"
89
#include "MemoryOutStream.h"
910

1011
#ifndef UNITTEST_POSIX
11-
#define UNITTEST_THROW_SIGNALS
12+
#define UNITTEST_THROW_SIGNALS_POSIX_ONLY
1213
#else
1314
#include "Posix/SignalTranslator.h"
1415
#endif
@@ -77,22 +78,24 @@
7778
void Test##Fixture##Name::RunImpl() const \
7879
{ \
7980
bool ctorOk = false; \
80-
try { \
81+
UT_TRY \
82+
({ \
8183
Fixture##Name##Helper fixtureHelper(m_details); \
8284
ctorOk = true; \
83-
UnitTest::ExecuteTest(fixtureHelper, m_details); \
84-
} \
85-
catch (UnitTest::AssertException const& e) \
86-
{ \
85+
UnitTest::ExecuteTest(fixtureHelper, m_details); \
86+
}) \
87+
UT_CATCH (UnitTest::AssertException, e, \
88+
{ \
8789
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \
88-
} \
89-
catch (std::exception const& e) \
90-
{ \
90+
}) \
91+
UT_CATCH (std::exception, e, \
92+
{ \
9193
UnitTest::MemoryOutStream stream; \
9294
stream << "Unhandled exception: " << e.what(); \
9395
UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \
94-
} \
95-
catch (...) { \
96+
}) \
97+
UT_CATCH_ALL \
98+
({ \
9699
if (ctorOk) \
97100
{ \
98101
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
@@ -103,7 +106,7 @@
103106
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
104107
"Unhandled exception while constructing fixture " #Fixture); \
105108
} \
106-
} \
109+
}) \
107110
} \
108111
void Fixture##Name##Helper::RunImpl()
109112

src/tests/TestAssertHandler.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include "../Config.h"
2+
3+
#ifdef UNITTEST_USE_EXCEPTIONS
4+
15
#include "../unittestpp.h"
26
#include "../AssertException.h"
37
#include "../ReportAssert.h"
@@ -40,5 +44,6 @@ TEST(ReportAssertSetsCorrectInfoInException)
4044
}
4145
}
4246

43-
4447
}
48+
49+
#endif

src/tests/TestTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TEST(FailingTestHasFailures)
5050
CHECK_EQUAL(1, results.GetFailureCount());
5151
}
5252

53-
53+
#ifdef UNITTEST_USE_EXCEPTIONS
5454
TEST(ThrowingTestsAreReportedAsFailures)
5555
{
5656
class CrashingTest : public Test
@@ -72,7 +72,6 @@ TEST(ThrowingTestsAreReportedAsFailures)
7272
CHECK_EQUAL(1, results.GetFailureCount());
7373
}
7474

75-
7675
#ifndef UNITTEST_MINGW
7776
TEST(CrashingTestsAreReportedAsFailures)
7877
{
@@ -95,6 +94,7 @@ TEST(CrashingTestsAreReportedAsFailures)
9594
CHECK_EQUAL(1, results.GetFailureCount());
9695
}
9796
#endif
97+
#endif
9898

9999
TEST(TestWithUnspecifiedSuiteGetsDefaultSuite)
100100
{

src/tests/TestTestMacros.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ TEST (TestsAreAddedToTheListThroughMacro)
2222
CHECK(list1.GetHead()->next == 0);
2323
}
2424

25+
#ifdef UNITTEST_USE_EXCEPTIONS
26+
2527
struct ThrowingThingie
2628
{
2729
ThrowingThingie() : dummy(false)
@@ -52,6 +54,8 @@ TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
5254
CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie"));
5355
}
5456

57+
#endif
58+
5559
struct DummyFixture
5660
{
5761
int x;
@@ -104,6 +108,8 @@ TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
104108
CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName);
105109
}
106110

111+
#ifdef UNITTEST_USE_EXCEPTIONS
112+
107113
struct FixtureCtorThrows
108114
{
109115
FixtureCtorThrows() { throw "exception"; }
@@ -185,6 +191,8 @@ TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
185191
CHECK(strstr(reporter.lastFailedMessage, "assert failure"));
186192
}
187193

194+
#endif
195+
188196
}
189197

190198
// We're really testing if it's possible to use the same suite in two files

src/tests/TestUnitTestPP.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include "../ReportAssert.h"
33
#include "ScopedCurrentTest.h"
44

5-
#include <vector>
6-
75
// These are sample tests that show the different features of the framework
86

97
namespace {
@@ -47,15 +45,6 @@ TEST(ArrayCloseSucceeds)
4745
CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f);
4846
}
4947

50-
TEST (CheckArrayCloseWorksWithVectors)
51-
{
52-
std::vector< float > a(4);
53-
for (int i = 0; i < 4; ++i)
54-
a[i] = (float)i;
55-
56-
CHECK_ARRAY_CLOSE(a, a, (int)a.size(), 0.0001f);
57-
}
58-
5948
#ifdef UNITTEST_USE_EXCEPTIONS
6049

6150
TEST(CheckThrowMacroSucceedsOnCorrectException)

0 commit comments

Comments
 (0)