diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 2445582c..de69265d 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -109,6 +109,13 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) { EXPECT_EQ(s_ + 1, p_); } +// Tests that when EXPECT_STDOUT fails, it evaluates its message argument +// exactly once. +TEST_F(SingleEvaluationTest, FailedEXPECT_STDOUT) { + EXPECT_NONFATAL_FAILURE(EXPECT_STDOUT(std::printf("test"), 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 740dd8bb..badfc6ba 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -44,16 +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; \ + std::string gtest_expected_message = expected_message; \ bool gtest_caught_expected = false; \ try { \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ } \ catch (expected_exception const& e) { \ - if (expected_message_str != e.what()) { \ + if (gtest_expected_message != e.what()) { \ gtest_ar \ << #statement " throws an exception with a different message.\n" \ - << "Expected: " << expected_message_str << "\n" \ + << "Expected: " << gtest_expected_message << "\n" \ << " Actual: " << e.what(); \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \ @@ -340,16 +340,14 @@ class OutputRedirect { #define FMT_TEST_PRINT_(statement, expected_output, file, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \ - std::string gtest_output; \ - { \ - OutputRedirect gtest_redir(file); \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - gtest_output = gtest_redir.RestoreAndRead(); \ - } \ - if (gtest_output != expected_output) { \ + std::string gtest_expected_output = expected_output; \ + OutputRedirect gtest_redir(file); \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + std::string gtest_output = gtest_redir.RestoreAndRead(); \ + if (gtest_output != gtest_expected_output) { \ gtest_ar \ << #statement " produces different output.\n" \ - << "Expected: " << expected_output << "\n" \ + << "Expected: " << gtest_expected_output << "\n" \ << " Actual: " << gtest_output; \ goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ } \