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
This commit is contained in:
Patrick Johnmeyer 2013-01-09 23:42:29 -06:00
parent 67d4ecb5c5
commit 4f556d6539
8 changed files with 53 additions and 42 deletions

View file

@ -26,8 +26,7 @@
// by default, MemoryOutStream is implemented in terms of std::ostringstream, which can be expensive.
// uncomment this line to use the custom MemoryOutStream (no deps on std::ostringstream).
//#define UNITTEST_USE_CUSTOM_STREAMS
#define UNITTEST_USE_CUSTOM_STREAMS
#define UNITTEST_USE_EXCEPTIONS
#endif

View file

@ -1,6 +1,7 @@
#ifndef UNITTEST_EXECUTE_TEST_H
#define UNITTEST_EXECUTE_TEST_H
#include "ExceptionMacros.h"
#include "TestDetails.h"
#include "MemoryOutStream.h"
#include "AssertException.h"
@ -17,28 +18,34 @@ void ExecuteTest(T& testObject, TestDetails const& details)
{
CurrentTest::Details() = &details;
try
{
#ifdef UNITTEST_POSIX
UNITTEST_THROW_SIGNALS
#endif
#ifndef UNITTEST_POSIX
UT_TRY
({
testObject.RunImpl();
}
catch (AssertException const& e)
})
#else
UT_TRY
({
UNITTEST_THROW_SIGNALS_POSIX_ONLY
testObject.RunImpl();
})
#endif
UT_CATCH(AssertException, e,
{
CurrentTest::Results()->OnTestFailure(
TestDetails(details.testName, details.suiteName, e.Filename(), e.LineNumber()), e.what());
}
catch (std::exception const& e)
})
UT_CATCH(std::exception, e,
{
MemoryOutStream stream;
stream << "Unhandled exception: " << e.what();
CurrentTest::Results()->OnTestFailure(details, stream.GetText());
}
catch (...)
{
})
UT_CATCH_ALL
({
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: Crash!");
}
})
}
}

View file

@ -32,7 +32,7 @@ private:
#define UNITTEST_EXTENSION __extension__
#endif
#define UNITTEST_THROW_SIGNALS \
#define UNITTEST_THROW_SIGNALS_POSIX_ONLY \
UnitTest::SignalTranslator sig; \
if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \
throw ("Unhandled system exception");

View file

@ -2,13 +2,14 @@
#define UNITTEST_TESTMACROS_H
#include "Config.h"
#include "ExceptionMacros.h"
#include "ExecuteTest.h"
#include "AssertException.h"
#include "TestDetails.h"
#include "MemoryOutStream.h"
#ifndef UNITTEST_POSIX
#define UNITTEST_THROW_SIGNALS
#define UNITTEST_THROW_SIGNALS_POSIX_ONLY
#else
#include "Posix/SignalTranslator.h"
#endif
@ -77,22 +78,24 @@
void Test##Fixture##Name::RunImpl() const \
{ \
bool ctorOk = false; \
try { \
UT_TRY \
({ \
Fixture##Name##Helper fixtureHelper(m_details); \
ctorOk = true; \
UnitTest::ExecuteTest(fixtureHelper, m_details); \
} \
catch (UnitTest::AssertException const& e) \
{ \
UnitTest::ExecuteTest(fixtureHelper, m_details); \
}) \
UT_CATCH (UnitTest::AssertException, e, \
{ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \
} \
catch (std::exception const& e) \
{ \
}) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream stream; \
stream << "Unhandled exception: " << e.what(); \
UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \
} \
catch (...) { \
}) \
UT_CATCH_ALL \
({ \
if (ctorOk) \
{ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
@ -103,7 +106,7 @@
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
"Unhandled exception while constructing fixture " #Fixture); \
} \
} \
}) \
} \
void Fixture##Name##Helper::RunImpl()

View file

@ -1,3 +1,7 @@
#include "../Config.h"
#ifdef UNITTEST_USE_EXCEPTIONS
#include "../unittestpp.h"
#include "../AssertException.h"
#include "../ReportAssert.h"
@ -40,5 +44,6 @@ TEST(ReportAssertSetsCorrectInfoInException)
}
}
}
#endif

View file

@ -50,7 +50,7 @@ TEST(FailingTestHasFailures)
CHECK_EQUAL(1, results.GetFailureCount());
}
#ifdef UNITTEST_USE_EXCEPTIONS
TEST(ThrowingTestsAreReportedAsFailures)
{
class CrashingTest : public Test
@ -72,7 +72,6 @@ TEST(ThrowingTestsAreReportedAsFailures)
CHECK_EQUAL(1, results.GetFailureCount());
}
#ifndef UNITTEST_MINGW
TEST(CrashingTestsAreReportedAsFailures)
{
@ -95,6 +94,7 @@ TEST(CrashingTestsAreReportedAsFailures)
CHECK_EQUAL(1, results.GetFailureCount());
}
#endif
#endif
TEST(TestWithUnspecifiedSuiteGetsDefaultSuite)
{

View file

@ -22,6 +22,8 @@ TEST (TestsAreAddedToTheListThroughMacro)
CHECK(list1.GetHead()->next == 0);
}
#ifdef UNITTEST_USE_EXCEPTIONS
struct ThrowingThingie
{
ThrowingThingie() : dummy(false)
@ -52,6 +54,8 @@ TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie"));
}
#endif
struct DummyFixture
{
int x;
@ -104,6 +108,8 @@ TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName);
}
#ifdef UNITTEST_USE_EXCEPTIONS
struct FixtureCtorThrows
{
FixtureCtorThrows() { throw "exception"; }
@ -185,6 +191,8 @@ TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
CHECK(strstr(reporter.lastFailedMessage, "assert failure"));
}
#endif
}
// We're really testing if it's possible to use the same suite in two files

View file

@ -2,8 +2,6 @@
#include "../ReportAssert.h"
#include "ScopedCurrentTest.h"
#include <vector>
// These are sample tests that show the different features of the framework
namespace {
@ -47,15 +45,6 @@ TEST(ArrayCloseSucceeds)
CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f);
}
TEST (CheckArrayCloseWorksWithVectors)
{
std::vector< float > a(4);
for (int i = 0; i < 4; ++i)
a[i] = (float)i;
CHECK_ARRAY_CLOSE(a, a, (int)a.size(), 0.0001f);
}
#ifdef UNITTEST_USE_EXCEPTIONS
TEST(CheckThrowMacroSucceedsOnCorrectException)