From 0354ccb049ea2b9e4696adb8b5a013287daf0c33 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Thu, 13 Sep 2018 10:24:10 +0200 Subject: [PATCH] Added special catch for std::exception in GTEST_TEST_NO_THROW_ --- .../include/gtest/internal/gtest-internal.h | 23 ++++++++++++++++--- googletest/test/gtest_unittest.cc | 9 ++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index b762f61f..b968fb1d 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1189,6 +1189,19 @@ class NativeArray { GTEST_DISALLOW_ASSIGN_(NativeArray); }; +class AdditionalMessage +{ +public: + AdditionalMessage(const std::string& message) : value(message) {} + AdditionalMessage& operator=(const std::string& message) { value = message; return *this; } + operator bool() const { return ::testing::internal::AlwaysTrue(); } + + const std::string& get() const { return value; } + +private: + std::string value; +}; + } // namespace internal } // namespace testing @@ -1242,17 +1255,21 @@ class NativeArray { #define GTEST_TEST_NO_THROW_(statement, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ + if (::testing::internal::AdditionalMessage message = ".") { \ try { \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ } \ + catch (const std::exception& e) { \ + message = std::string(": ") + e.what(); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ + } \ catch (...) { \ goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ } \ } else \ GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ - fail("Expected: " #statement " doesn't throw an exception.\n" \ - " Actual: it throws.") + fail(("Expected: " #statement " doesn't throw an exception.\n" \ + " Actual: it throws" + message.get()).c_str()) #define GTEST_TEST_ANY_THROW_(statement, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index fa102240..de194491 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -3330,6 +3330,9 @@ TEST_F(SingleEvaluationTest, OtherCases) { void ThrowAnInteger() { throw 1; } +void ThrowAnException(const char* what) { + throw std::runtime_error(what); +} // Tests that assertion arguments are evaluated exactly once. TEST_F(SingleEvaluationTest, ExceptionTests) { @@ -3812,6 +3815,9 @@ TEST(AssertionTest, ASSERT_NO_THROW) { EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), "Expected: ThrowAnInteger() doesn't throw an exception." "\n Actual: it throws."); + EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnException("blablubb")), + "Expected: ThrowAnException(\"blablubb\") doesn't throw an exception." + "\n Actual: it throws: blablubb"); } // Tests ASSERT_ANY_THROW. @@ -4553,6 +4559,9 @@ TEST(ExpectTest, EXPECT_NO_THROW) { EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), "Expected: ThrowAnInteger() doesn't throw an " "exception.\n Actual: it throws."); + EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnException("blablubb")), + "Expected: ThrowAnException(\"blablubb\") doesn't throw an " + "exception.\n Actual: it throws: blablubb"); } // Tests EXPECT_ANY_THROW.