From 536e61386d3166b09780ad64ad84554880735bf5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 5 May 2014 07:31:38 -0700 Subject: [PATCH] Evaluate message argument passed to EXPECT_THROW_MSG once. --- test/gtest-extra-test.cc | 16 +++++++++++++++- test/gtest-extra.h | 5 +++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 3acf2269..f9f7e050 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -77,12 +77,18 @@ bool IsClosedInternal(int fd) { class SingleEvaluationTest : public ::testing::Test { protected: SingleEvaluationTest() { + p_ = s_; a_ = 0; } - + + static const char* const s_; + static const char* p_; + static int a_; }; +const char* const SingleEvaluationTest::s_ = "01234"; +const char* SingleEvaluationTest::p_; int SingleEvaluationTest::a_; void ThrowNothing() {} @@ -91,6 +97,14 @@ void ThrowException() { throw std::runtime_error("test"); } +// Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument +// exactly once. +TEST_F(SingleEvaluationTest, FailedASSERT_THROW_MSG) { + EXPECT_NONFATAL_FAILURE( + EXPECT_THROW_MSG(ThrowException(), std::exception, p_++), "01234"); + EXPECT_EQ(s_ + 1, p_); +} + // Tests that assertion arguments are evaluated exactly once. TEST_F(SingleEvaluationTest, ExceptionTests) { // successful EXPECT_THROW_MSG diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 6e4e4383..740dd8bb 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -44,15 +44,16 @@ #define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \ + std::string expected_message_str = expected_message; \ bool gtest_caught_expected = false; \ try { \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ } \ catch (expected_exception const& e) { \ - if (expected_message != std::string(e.what())) { \ + if (expected_message_str != e.what()) { \ gtest_ar \ << #statement " throws an exception with a different message.\n" \ - << "Expected: " << expected_message << "\n" \ + << "Expected: " << expected_message_str << "\n" \ << " Actual: " << e.what(); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \