From 7501a8949706538935ff155e1856afefdf3cbe8e Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Sat, 1 Nov 2014 12:41:48 +0000 Subject: [PATCH] Added CHECK_NO_THROW macro Added CHECK_NO_THROW macro to check that an expression does not throw an exception. --- UnitTest++/CheckMacros.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 9d6a759..f135c7c 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -156,7 +156,7 @@ UNITTEST_MULTILINE_MACRO_END -// CHECK_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h) +// CHECK_THROW, CHECK_NO_THROW and CHECK_ASSERT only exist when UNITTEST_NO_EXCEPTIONS isn't defined (see config.h) #ifndef UNITTEST_NO_EXCEPTIONS #define CHECK_THROW(expression, ExpectedExceptionType) \ UNITTEST_MULTILINE_MACRO_BEGIN \ @@ -168,6 +168,15 @@ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ UNITTEST_MULTILINE_MACRO_END +#define CHECK_NO_THROW(expression, NotExpectedExceptionType) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + bool caught_ = false; \ + try { expression; } \ + catch (NotExpectedExceptionType const&) { caught_ = true; } \ + catch (...) {} \ +if (caught_) \ + UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Non-expected exception: \"" #NotExpectedExceptionType "\" thrown"); \ + UNITTEST_MULTILINE_MACRO_END #define CHECK_ASSERT(expression) \ UNITTEST_MULTILINE_MACRO_BEGIN \