Print message of unexpected std::exception in EXPECT_THROW, too
This commit is contained in:
parent
631e3a5838
commit
6494f5232b
@ -1194,7 +1194,7 @@ class AdditionalMessage
|
|||||||
public:
|
public:
|
||||||
AdditionalMessage(const char* message) : value(message) {}
|
AdditionalMessage(const char* message) : value(message) {}
|
||||||
AdditionalMessage& operator=(const std::string& message) { value = message; return *this; }
|
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; }
|
const std::string& get() const { return value; }
|
||||||
|
|
||||||
@ -1229,29 +1229,41 @@ private:
|
|||||||
|
|
||||||
#define GTEST_TEST_THROW_(statement, expected_exception, fail) \
|
#define GTEST_TEST_THROW_(statement, expected_exception, fail) \
|
||||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||||
if (::testing::internal::ConstCharPtr gtest_msg = "") { \
|
if (::testing::internal::AdditionalMessage message = "") { \
|
||||||
bool gtest_caught_expected = false; \
|
bool gtest_caught_expected = false; \
|
||||||
|
try { \
|
||||||
try { \
|
try { \
|
||||||
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
||||||
} \
|
} \
|
||||||
catch (expected_exception const&) { \
|
catch (expected_exception const&) { \
|
||||||
gtest_caught_expected = true; \
|
gtest_caught_expected = true; \
|
||||||
|
throw; \
|
||||||
} \
|
} \
|
||||||
catch (...) { \
|
} \
|
||||||
gtest_msg.value = \
|
catch (const std::exception& e) { \
|
||||||
"Expected: " #statement " throws an exception of type " \
|
if (!gtest_caught_expected) { \
|
||||||
#expected_exception ".\n Actual: it throws a different type."; \
|
message = \
|
||||||
|
"it throws a different type " \
|
||||||
|
"with message: " + std::string(e.what()); \
|
||||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
} \
|
} \
|
||||||
|
} \
|
||||||
|
catch (...) { \
|
||||||
if (!gtest_caught_expected) { \
|
if (!gtest_caught_expected) { \
|
||||||
gtest_msg.value = \
|
message = \
|
||||||
"Expected: " #statement " throws an exception of type " \
|
"it throws a different type."; \
|
||||||
#expected_exception ".\n Actual: it throws nothing."; \
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
if (!gtest_caught_expected) { \
|
||||||
|
message = \
|
||||||
|
"it throws nothing."; \
|
||||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||||
} \
|
} \
|
||||||
} else \
|
} else \
|
||||||
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
|
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) \
|
#define GTEST_TEST_NO_THROW_(statement, fail) \
|
||||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||||
|
@ -3373,6 +3373,20 @@ TEST_F(SingleEvaluationTest, ExceptionTests) {
|
|||||||
// failed EXPECT_ANY_THROW
|
// failed EXPECT_ANY_THROW
|
||||||
EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't");
|
EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't");
|
||||||
EXPECT_EQ(7, a_);
|
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
|
#endif // GTEST_HAS_EXCEPTIONS
|
||||||
@ -3805,6 +3819,11 @@ TEST(AssertionTest, ASSERT_THROW) {
|
|||||||
ASSERT_THROW(ThrowNothing(), bool),
|
ASSERT_THROW(ThrowNothing(), bool),
|
||||||
"Expected: ThrowNothing() throws an exception of type bool.\n"
|
"Expected: ThrowNothing() throws an exception of type bool.\n"
|
||||||
" Actual: it throws nothing.");
|
" 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.
|
// Tests ASSERT_NO_THROW.
|
||||||
@ -4542,13 +4561,16 @@ TEST(ExpectTest, EXPECT_GT) {
|
|||||||
// Tests EXPECT_THROW.
|
// Tests EXPECT_THROW.
|
||||||
TEST(ExpectTest, EXPECT_THROW) {
|
TEST(ExpectTest, EXPECT_THROW) {
|
||||||
EXPECT_THROW(ThrowAnInteger(), int);
|
EXPECT_THROW(ThrowAnInteger(), int);
|
||||||
|
EXPECT_THROW(ThrowAnException(""), std::exception);
|
||||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool),
|
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool),
|
||||||
"Expected: ThrowAnInteger() throws an exception of "
|
"Expected: ThrowAnInteger() throws an exception of type bool.\n"
|
||||||
"type bool.\n Actual: it throws a different type.");
|
" Actual: it throws a different type.");
|
||||||
EXPECT_NONFATAL_FAILURE(
|
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowNothing(), bool),
|
||||||
EXPECT_THROW(ThrowNothing(), bool),
|
|
||||||
"Expected: ThrowNothing() throws an exception of type bool.\n"
|
"Expected: ThrowNothing() throws an exception of type bool.\n"
|
||||||
" Actual: it throws nothing.");
|
" 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.
|
// Tests EXPECT_NO_THROW.
|
||||||
|
Loading…
Reference in New Issue
Block a user