From 0354ccb049ea2b9e4696adb8b5a013287daf0c33 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Thu, 13 Sep 2018 10:24:10 +0200 Subject: [PATCH 1/7] 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. From 8c849c584c65a187ae472af466cb7cdaa98685d6 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Thu, 13 Sep 2018 13:57:27 +0200 Subject: [PATCH 2/7] Try to fix gcc and clang issues --- googletest/include/gtest/internal/gtest-internal.h | 2 +- googletest/test/gtest_unittest.cc | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index b968fb1d..da3ebd2b 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1192,7 +1192,7 @@ class NativeArray { class AdditionalMessage { public: - AdditionalMessage(const std::string& message) : value(message) {} + AdditionalMessage(const char* message) : value(message) {} AdditionalMessage& operator=(const std::string& message) { value = message; return *this; } operator bool() const { return ::testing::internal::AlwaysTrue(); } diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index de194491..2c970830 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -3748,6 +3748,19 @@ TEST(ExpectTest, ASSERT_EQ_0) { " 0\n 5.6"); } +TEST(AssertionTest, AdditionalMessage) { + ::testing::internal::AdditionalMessage m = "servus"; + EXPECT_EQ(m.get(), "servus"); + + const char* cc = "hello"; + m = cc; + EXPECT_EQ(m.get(), cc); + + std::string s = "hi"; + m = s; + EXPECT_EQ(m.get(), s); +} + // Tests ASSERT_NE. TEST(AssertionTest, ASSERT_NE) { ASSERT_NE(6, 7); From e86d1df3e177bb5782a97fd52a513b7b6983cea6 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Thu, 13 Sep 2018 14:13:23 +0200 Subject: [PATCH 3/7] Avoid implicit move operator --- googletest/include/gtest/internal/gtest-internal.h | 1 + 1 file changed, 1 insertion(+) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index da3ebd2b..7901cd3b 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1199,6 +1199,7 @@ public: const std::string& get() const { return value; } private: + AdditionalMessage& operator=(AdditionalMessage&&); std::string value; }; From 82eeb009b25a4b607984cfd076e46094473ef090 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Thu, 13 Sep 2018 14:49:15 +0200 Subject: [PATCH 4/7] Make it public again *sigh* --- googletest/include/gtest/internal/gtest-internal.h | 1 - googletest/test/gtest_unittest.cc | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 7901cd3b..da3ebd2b 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1199,7 +1199,6 @@ public: const std::string& get() const { return value; } private: - AdditionalMessage& operator=(AdditionalMessage&&); std::string value; }; diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index 2c970830..de194491 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -3748,19 +3748,6 @@ TEST(ExpectTest, ASSERT_EQ_0) { " 0\n 5.6"); } -TEST(AssertionTest, AdditionalMessage) { - ::testing::internal::AdditionalMessage m = "servus"; - EXPECT_EQ(m.get(), "servus"); - - const char* cc = "hello"; - m = cc; - EXPECT_EQ(m.get(), cc); - - std::string s = "hi"; - m = s; - EXPECT_EQ(m.get(), s); -} - // Tests ASSERT_NE. TEST(AssertionTest, ASSERT_NE) { ASSERT_NE(6, 7); From 6494f5232b130a29321e661166442bac324c4383 Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Fri, 14 Sep 2018 23:22:04 +0200 Subject: [PATCH 5/7] Print message of unexpected std::exception in EXPECT_THROW, too --- .../include/gtest/internal/gtest-internal.h | 38 ++++++++++++------- googletest/test/gtest_unittest.cc | 34 ++++++++++++++--- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index da3ebd2b..5a1181aa 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1194,7 +1194,7 @@ class AdditionalMessage public: AdditionalMessage(const char* message) : value(message) {} AdditionalMessage& operator=(const std::string& message) { value = message; return *this; } - operator bool() const { return ::testing::internal::AlwaysTrue(); } + operator bool() const { return true; } const std::string& get() const { return value; } @@ -1229,29 +1229,41 @@ private: #define GTEST_TEST_THROW_(statement, expected_exception, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::ConstCharPtr gtest_msg = "") { \ + if (::testing::internal::AdditionalMessage message = "") { \ bool gtest_caught_expected = false; \ try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } \ + catch (expected_exception const&) { \ + gtest_caught_expected = true; \ + throw; \ + } \ } \ - catch (expected_exception const&) { \ - gtest_caught_expected = true; \ + catch (const std::exception& e) { \ + if (!gtest_caught_expected) { \ + message = \ + "it throws a different type " \ + "with message: " + std::string(e.what()); \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } \ } \ catch (...) { \ - gtest_msg.value = \ - "Expected: " #statement " throws an exception of type " \ - #expected_exception ".\n Actual: it throws a different type."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + if (!gtest_caught_expected) { \ + message = \ + "it throws a different type."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } \ } \ if (!gtest_caught_expected) { \ - gtest_msg.value = \ - "Expected: " #statement " throws an exception of type " \ - #expected_exception ".\n Actual: it throws nothing."; \ + message = \ + "it throws nothing."; \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } else \ GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ - fail(gtest_msg.value) + fail(("Expected: " #statement " throws an exception of type " \ + #expected_exception ".\n Actual: " + message.get()).c_str()) #define GTEST_TEST_NO_THROW_(statement, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index bd1117b3..6b75ab47 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -3373,6 +3373,20 @@ TEST_F(SingleEvaluationTest, ExceptionTests) { // failed EXPECT_ANY_THROW EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); EXPECT_EQ(7, a_); + + // failed EXPECT_THROW std::exception, throws different + EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT + a_++; + ThrowAnInteger(); + }, std::exception), "throws a different type"); + EXPECT_EQ(8, a_); + + // failed EXPECT_THROW, throws std::exception + EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT + a_++; + ThrowAnException("blablubb"); + }, bool), "throws a different type with message: blablubb"); + EXPECT_EQ(9, a_); } #endif // GTEST_HAS_EXCEPTIONS @@ -3805,6 +3819,11 @@ TEST(AssertionTest, ASSERT_THROW) { ASSERT_THROW(ThrowNothing(), bool), "Expected: ThrowNothing() throws an exception of type bool.\n" " Actual: it throws nothing."); + + EXPECT_FATAL_FAILURE( + ASSERT_THROW(ThrowAnException("buuh"), bool), + "Expected: ThrowAnException(\"buuh\") throws an exception of type bool.\n" + " Actual: it throws a different type with message: buuh"); } // Tests ASSERT_NO_THROW. @@ -4542,13 +4561,16 @@ TEST(ExpectTest, EXPECT_GT) { // Tests EXPECT_THROW. TEST(ExpectTest, EXPECT_THROW) { EXPECT_THROW(ThrowAnInteger(), int); + EXPECT_THROW(ThrowAnException(""), std::exception); EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), - "Expected: ThrowAnInteger() throws an exception of " - "type bool.\n Actual: it throws a different type."); - EXPECT_NONFATAL_FAILURE( - EXPECT_THROW(ThrowNothing(), bool), - "Expected: ThrowNothing() throws an exception of type bool.\n" - " Actual: it throws nothing."); + "Expected: ThrowAnInteger() throws an exception of type bool.\n" + " Actual: it throws a different type."); + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowNothing(), bool), + "Expected: ThrowNothing() throws an exception of type bool.\n" + " Actual: it throws nothing."); + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnException("buuh"), bool), + "Expected: ThrowAnException(\"buuh\") throws an exception of type bool.\n" + " Actual: it throws a different type with message: buuh"); } // Tests EXPECT_NO_THROW. From c40f55a229f6d3d33f00c942585ea642135881db Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Sat, 15 Sep 2018 09:00:39 +0200 Subject: [PATCH 6/7] Avoid these ambiguities --- .../include/gtest/internal/gtest-internal.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 5a1181aa..50bc52d5 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1193,7 +1193,7 @@ class AdditionalMessage { public: AdditionalMessage(const char* message) : value(message) {} - AdditionalMessage& operator=(const std::string& message) { value = message; return *this; } + void set(const std::string& message) { value = message; } operator bool() const { return true; } const std::string& get() const { return value; } @@ -1242,22 +1242,22 @@ private: } \ catch (const std::exception& e) { \ if (!gtest_caught_expected) { \ - message = \ + message.set( \ "it throws a different type " \ - "with message: " + std::string(e.what()); \ + "with message: " + std::string(e.what())); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } \ catch (...) { \ if (!gtest_caught_expected) { \ - message = \ - "it throws a different type."; \ + message.set( \ + "it throws a different type."); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } \ if (!gtest_caught_expected) { \ - message = \ - "it throws nothing."; \ + message.set( \ + "it throws nothing."); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } else \ @@ -1272,7 +1272,7 @@ private: GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ } \ catch (const std::exception& e) { \ - message = std::string(": ") + e.what(); \ + message.set(std::string(": ") + e.what()); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ } \ catch (...) { \ From 6a1c3d9b788bedad993aa57690cc1c8cb4b871ba Mon Sep 17 00:00:00 2001 From: Jonny007-MKD Date: Tue, 18 Sep 2018 21:54:36 +0200 Subject: [PATCH 7/7] Removed some newlines --- googletest/include/gtest/internal/gtest-internal.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 50bc52d5..0dbf100c 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -1242,22 +1242,19 @@ private: } \ catch (const std::exception& e) { \ if (!gtest_caught_expected) { \ - message.set( \ - "it throws a different type " \ + message.set("it throws a different type " \ "with message: " + std::string(e.what())); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } \ catch (...) { \ if (!gtest_caught_expected) { \ - message.set( \ - "it throws a different type."); \ + message.set("it throws a different type."); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } \ if (!gtest_caught_expected) { \ - message.set( \ - "it throws nothing."); \ + message.set("it throws nothing."); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ } else \