diff --git a/test/assert-test.cc b/test/assert-test.cc index 45612538..3a9c7ace 100644 --- a/test/assert-test.cc +++ b/test/assert-test.cc @@ -44,6 +44,8 @@ class SingleEvaluationTest : public ::testing::Test { int SingleEvaluationTest::a_; +void ThrowNothing() {} + void ThrowException() { throw std::runtime_error("test"); } @@ -77,6 +79,52 @@ TEST_F(SingleEvaluationTest, ExceptionTests) { EXPECT_EQ(4, a_); } -// TODO: more tests +// Tests that the compiler will not complain about unreachable code in the +// EXPECT_THROW_MSG macro. +TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { + int n = 0; + using std::runtime_error; + EXPECT_THROW_MSG(throw runtime_error(""), runtime_error, ""); + EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(n++, runtime_error, ""), ""); + EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(throw 1, runtime_error, ""), ""); + EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG( + throw runtime_error("a"), runtime_error, "b"), ""); +} + +TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { + if (::testing::internal::AlwaysFalse()) + EXPECT_THROW_MSG(ThrowNothing(), std::exception, ""); + + if (::testing::internal::AlwaysTrue()) + EXPECT_THROW_MSG(ThrowException(), std::exception, "test"); + else + ; // NOLINT +} + +// Tests EXPECT_THROW_MSG. +TEST(ExpectTest, EXPECT_THROW_MSG) { + EXPECT_THROW_MSG(ThrowException(), std::exception, "test"); + EXPECT_NONFATAL_FAILURE( + EXPECT_THROW_MSG(ThrowException(), std::logic_error, "test"), + "Expected: ThrowException() throws an exception of " + "type std::logic_error.\n Actual: it throws a different type."); + EXPECT_NONFATAL_FAILURE( + EXPECT_THROW_MSG(ThrowNothing(), std::exception, "test"), + "Expected: ThrowNothing() throws an exception of type std::exception.\n" + " Actual: it throws nothing."); + EXPECT_NONFATAL_FAILURE( + EXPECT_THROW_MSG(ThrowException(), std::exception, "other"), + "ThrowException() throws an exception with a different message.\n" + "Expected: other\n" + " Actual: test"); +} + +TEST(StreamingAssertionsTest, ThrowMsg) { + EXPECT_THROW_MSG(ThrowException(), std::exception, "test") + << "unexpected failure"; + EXPECT_NONFATAL_FAILURE( + EXPECT_THROW_MSG(ThrowException(), std::exception, "other") + << "expected failure", "expected failure"); +} } // namespace diff --git a/test/format-test.h b/test/format-test.h index 0e6f19b3..73b8b799 100644 --- a/test/format-test.h +++ b/test/format-test.h @@ -36,9 +36,10 @@ } \ catch (expected_exception const& e) { \ if (expected_message != std::string(e.what())) { \ - gtest_ar << "Statement throws an exception with a different message\n" \ + gtest_ar \ + << #statement " throws an exception with a different message.\n" \ << "Expected: " << expected_message << "\n" \ - << " Actual: " << e.what() << "\n"; \ + << " Actual: " << e.what(); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ gtest_caught_expected = true; \