Added special catch for std::exception in GTEST_TEST_NO_THROW_
This commit is contained in:
parent
5131cf737c
commit
0354ccb049
|
@ -1189,6 +1189,19 @@ class NativeArray {
|
||||||
GTEST_DISALLOW_ASSIGN_(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 internal
|
||||||
} // namespace testing
|
} // namespace testing
|
||||||
|
|
||||||
|
@ -1242,17 +1255,21 @@ class NativeArray {
|
||||||
|
|
||||||
#define GTEST_TEST_NO_THROW_(statement, fail) \
|
#define GTEST_TEST_NO_THROW_(statement, fail) \
|
||||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||||
if (::testing::internal::AlwaysTrue()) { \
|
if (::testing::internal::AdditionalMessage message = ".") { \
|
||||||
try { \
|
try { \
|
||||||
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
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 (...) { \
|
catch (...) { \
|
||||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
|
goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
|
||||||
} \
|
} \
|
||||||
} else \
|
} else \
|
||||||
GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
|
GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
|
||||||
fail("Expected: " #statement " doesn't throw an exception.\n" \
|
fail(("Expected: " #statement " doesn't throw an exception.\n" \
|
||||||
" Actual: it throws.")
|
" Actual: it throws" + message.get()).c_str())
|
||||||
|
|
||||||
#define GTEST_TEST_ANY_THROW_(statement, fail) \
|
#define GTEST_TEST_ANY_THROW_(statement, fail) \
|
||||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||||
|
|
|
@ -3330,6 +3330,9 @@ TEST_F(SingleEvaluationTest, OtherCases) {
|
||||||
void ThrowAnInteger() {
|
void ThrowAnInteger() {
|
||||||
throw 1;
|
throw 1;
|
||||||
}
|
}
|
||||||
|
void ThrowAnException(const char* what) {
|
||||||
|
throw std::runtime_error(what);
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that assertion arguments are evaluated exactly once.
|
// Tests that assertion arguments are evaluated exactly once.
|
||||||
TEST_F(SingleEvaluationTest, ExceptionTests) {
|
TEST_F(SingleEvaluationTest, ExceptionTests) {
|
||||||
|
@ -3812,6 +3815,9 @@ TEST(AssertionTest, ASSERT_NO_THROW) {
|
||||||
EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()),
|
EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()),
|
||||||
"Expected: ThrowAnInteger() doesn't throw an exception."
|
"Expected: ThrowAnInteger() doesn't throw an exception."
|
||||||
"\n Actual: it throws.");
|
"\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.
|
// Tests ASSERT_ANY_THROW.
|
||||||
|
@ -4553,6 +4559,9 @@ TEST(ExpectTest, EXPECT_NO_THROW) {
|
||||||
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()),
|
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()),
|
||||||
"Expected: ThrowAnInteger() doesn't throw an "
|
"Expected: ThrowAnInteger() doesn't throw an "
|
||||||
"exception.\n Actual: it throws.");
|
"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.
|
// Tests EXPECT_ANY_THROW.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user