diff --git a/src/Config.h b/src/Config.h index 7cdaa5c..f223558 100644 --- a/src/Config.h +++ b/src/Config.h @@ -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 diff --git a/src/ExecuteTest.h b/src/ExecuteTest.h index 3ccd426..c09eca2 100644 --- a/src/ExecuteTest.h +++ b/src/ExecuteTest.h @@ -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!"); - } + }) } } diff --git a/src/Posix/SignalTranslator.h b/src/Posix/SignalTranslator.h index f3c3563..2152b9c 100644 --- a/src/Posix/SignalTranslator.h +++ b/src/Posix/SignalTranslator.h @@ -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"); diff --git a/src/TestMacros.h b/src/TestMacros.h index 8d29f04..16520e8 100644 --- a/src/TestMacros.h +++ b/src/TestMacros.h @@ -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() diff --git a/src/tests/TestAssertHandler.cpp b/src/tests/TestAssertHandler.cpp index bd7cbea..664a670 100644 --- a/src/tests/TestAssertHandler.cpp +++ b/src/tests/TestAssertHandler.cpp @@ -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 diff --git a/src/tests/TestTest.cpp b/src/tests/TestTest.cpp index 92e4f0a..cc48884 100644 --- a/src/tests/TestTest.cpp +++ b/src/tests/TestTest.cpp @@ -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) { diff --git a/src/tests/TestTestMacros.cpp b/src/tests/TestTestMacros.cpp index 1149b0c..a1506e5 100644 --- a/src/tests/TestTestMacros.cpp +++ b/src/tests/TestTestMacros.cpp @@ -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 diff --git a/src/tests/TestUnitTestPP.cpp b/src/tests/TestUnitTestPP.cpp index b8e325b..9237aa9 100644 --- a/src/tests/TestUnitTestPP.cpp +++ b/src/tests/TestUnitTestPP.cpp @@ -2,8 +2,6 @@ #include "../ReportAssert.h" #include "ScopedCurrentTest.h" -#include - // 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)