summaryrefslogtreecommitdiffstats
path: root/googletest
diff options
context:
space:
mode:
authorJonny007-MKD <me@jonny007-mkd.de>2018-09-13 08:24:10 (GMT)
committerJonny007-MKD <me@jonny007-mkd.de>2018-09-13 08:24:10 (GMT)
commit0354ccb049ea2b9e4696adb8b5a013287daf0c33 (patch)
tree095bb31d7647df81162f3d75aefe661e4f659260 /googletest
parent5131cf737cf3a4d81923c9c72536a07384292cd7 (diff)
downloadgoogletest-0354ccb049ea2b9e4696adb8b5a013287daf0c33.zip
googletest-0354ccb049ea2b9e4696adb8b5a013287daf0c33.tar.gz
googletest-0354ccb049ea2b9e4696adb8b5a013287daf0c33.tar.bz2
Added special catch for std::exception in GTEST_TEST_NO_THROW_
Diffstat (limited to 'googletest')
-rw-r--r--googletest/include/gtest/internal/gtest-internal.h23
-rw-r--r--googletest/test/gtest_unittest.cc9
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 b762f61..b968fb1 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 fa10224..de19449 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.