Added special catch for std::exception in GTEST_TEST_NO_THROW_

This commit is contained in:
Jonny007-MKD 2018-09-13 10:24:10 +02:00
parent 5131cf737c
commit 0354ccb049
2 changed files with 29 additions and 3 deletions

View File

@ -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_ \

View File

@ -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.