From 7a574c99ea1d474f8ec5f040deba54169120f063 Mon Sep 17 00:00:00 2001 From: "jgm@google.com" Date: Fri, 9 Mar 2012 17:12:39 +0000 Subject: Misc small updates to some debug death code, and to messages streaming to macros git-svn-id: http://googletest.googlecode.com/svn/trunk@612 861a406c-534a-0410-8894-cb66d6ee9925 --- include/gtest/gtest-death-test.h | 6 +- include/gtest/gtest-param-test.h | 2 +- include/gtest/gtest-param-test.h.pump | 2 +- include/gtest/gtest.h | 6 - include/gtest/internal/gtest-death-test-internal.h | 11 ++ src/gtest-port.cc | 11 +- test/gtest-death-test_test.cc | 32 +++--- test/gtest_env_var_test.py | 3 +- test/gtest_output_test.py | 2 +- test/gtest_output_test_.cc | 8 +- test/gtest_shuffle_test.py | 3 +- test/gtest_unittest.cc | 121 ++++++++++++++++++++- 12 files changed, 164 insertions(+), 43 deletions(-) diff --git a/include/gtest/gtest-death-test.h b/include/gtest/gtest-death-test.h index 16f08b8..957a69c 100644 --- a/include/gtest/gtest-death-test.h +++ b/include/gtest/gtest-death-test.h @@ -86,7 +86,7 @@ GTEST_API_ bool InDeathTestChild(); // for (int i = 0; i < 5; i++) { // EXPECT_DEATH(server.ProcessRequest(i), // "Invalid request .* in ProcessRequest()") -// << "Failed to die on request " << i); +// << "Failed to die on request " << i; // } // // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); @@ -256,10 +256,10 @@ class GTEST_API_ KilledBySignal { # ifdef NDEBUG # define EXPECT_DEBUG_DEATH(statement, regex) \ - do { statement; } while (::testing::internal::AlwaysFalse()) + GTEST_EXECUTE_STATEMENT_(statement, regex) # define ASSERT_DEBUG_DEATH(statement, regex) \ - do { statement; } while (::testing::internal::AlwaysFalse()) + GTEST_EXECUTE_STATEMENT_(statement, regex) # else diff --git a/include/gtest/gtest-param-test.h b/include/gtest/gtest-param-test.h index 6407cfd..d6702c8 100644 --- a/include/gtest/gtest-param-test.h +++ b/include/gtest/gtest-param-test.h @@ -1257,7 +1257,7 @@ inline internal::ParamGenerator Bool() { // Boolean flags: // // class FlagDependentTest -// : public testing::TestWithParam > { +// : public testing::TestWithParam > { // virtual void SetUp() { // // Assigns external_flag_1 and external_flag_2 values from the tuple. // tie(external_flag_1, external_flag_2) = GetParam(); diff --git a/include/gtest/gtest-param-test.h.pump b/include/gtest/gtest-param-test.h.pump index 401cb51..2dc9303 100644 --- a/include/gtest/gtest-param-test.h.pump +++ b/include/gtest/gtest-param-test.h.pump @@ -414,7 +414,7 @@ inline internal::ParamGenerator Bool() { // Boolean flags: // // class FlagDependentTest -// : public testing::TestWithParam > { +// : public testing::TestWithParam > { // virtual void SetUp() { // // Assigns external_flag_1 and external_flag_2 values from the tuple. // tie(external_flag_1, external_flag_2) = GetParam(); diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 2d570a2..226307e 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -1731,12 +1731,6 @@ class TestWithParam : public Test, public WithParamInterface { // usually want the fail-fast behavior of FAIL and ASSERT_*, but those // writing data-driven tests often find themselves using ADD_FAILURE // and EXPECT_* more. -// -// Examples: -// -// EXPECT_TRUE(server.StatusIsOK()); -// ASSERT_FALSE(server.HasPendingRequest(port)) -// << "There are still pending requests " << "on port " << port; // Generates a nonfatal failure with a generic message. #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") diff --git a/include/gtest/internal/gtest-death-test-internal.h b/include/gtest/internal/gtest-death-test-internal.h index 1d9f83b..22bb97f 100644 --- a/include/gtest/internal/gtest-death-test-internal.h +++ b/include/gtest/internal/gtest-death-test-internal.h @@ -217,6 +217,17 @@ GTEST_API_ bool ExitedUnsuccessfully(int exit_status); // The symbol "fail" here expands to something into which a message // can be streamed. +// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in +// NDEBUG mode. In this case we need the statements to be executed, the regex is +// ignored, and the macro must accept a streamed message even though the message +// is never printed. +# define GTEST_EXECUTE_STATEMENT_(statement, regex) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } else \ + ::testing::Message() + // A class representing the parsed contents of the // --gtest_internal_run_death_test flag, as it existed when // RUN_ALL_TESTS was called. diff --git a/src/gtest-port.cc b/src/gtest-port.cc index 9648eee..a0e2d7c 100644 --- a/src/gtest-port.cc +++ b/src/gtest-port.cc @@ -530,10 +530,15 @@ class CapturedStream { << temp_file_path; filename_ = temp_file_path; # else - // There's no guarantee that a test has write access to the - // current directory, so we create the temporary file in the /tmp - // directory instead. + // There's no guarantee that a test has write access to the current + // directory, so we create the temporary file in the /tmp directory instead. + // We use /tmp on most systems, and /mnt/sdcard on Android. That's because + // Android doesn't have /tmp. +# if GTEST_OS_LINUX_ANDROID + char name_template[] = "/mnt/sdcard/gtest_captured_stream.XXXXXX"; +# else char name_template[] = "/tmp/captured_stream.XXXXXX"; +# endif // GTEST_OS_LINUX_ANDROID const int captured_fd = mkstemp(name_template); filename_ = name_template; # endif // GTEST_OS_WINDOWS diff --git a/test/gtest-death-test_test.cc b/test/gtest-death-test_test.cc index 5a4a989..b389e73 100644 --- a/test/gtest-death-test_test.cc +++ b/test/gtest-death-test_test.cc @@ -618,8 +618,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) { "illegal return in test statement."); } -// Tests that EXPECT_DEBUG_DEATH works as expected, -// that is, in debug mode, it: +// Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a +// message to it, and in debug mode it: // 1. Asserts on death. // 2. Has no side effect. // @@ -628,8 +628,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) { TEST_F(TestForDeathTest, TestExpectDebugDeath) { int sideeffect = 0; - EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), - "death.*DieInDebugElse12"); + EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") + << "Must accept a streamed message"; # ifdef NDEBUG @@ -644,22 +644,18 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) { # endif } -// Tests that ASSERT_DEBUG_DEATH works as expected -// In debug mode: -// 1. Asserts on debug death. +// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a +// message to it, and in debug mode it: +// 1. Asserts on death. // 2. Has no side effect. // -// In opt mode: -// 1. Has side effects and returns the expected value (12). +// And in opt mode, it: +// 1. Has side effects but does not assert. TEST_F(TestForDeathTest, TestAssertDebugDeath) { int sideeffect = 0; - ASSERT_DEBUG_DEATH({ // NOLINT - // Tests that the return value is 12 in opt mode. - EXPECT_EQ(12, DieInDebugElse12(&sideeffect)); - // Tests that the side effect occurred in opt mode. - EXPECT_EQ(12, sideeffect); - }, "death.*DieInDebugElse12"); + ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") + << "Must accept a streamed message"; # ifdef NDEBUG @@ -730,7 +726,7 @@ static void TestExitMacros() { // Of all signals effects on the process exit code, only those of SIGABRT // are documented on Windows. // See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx. - EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), ""); + EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar"; # else @@ -739,14 +735,14 @@ static void TestExitMacros() { EXPECT_FATAL_FAILURE({ // NOLINT ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "") - << "This failure is expected, too."; + << "This failure is expected, too."; }, "This failure is expected, too."); # endif // GTEST_OS_WINDOWS EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "") - << "This failure is expected."; + << "This failure is expected."; }, "This failure is expected."); } diff --git a/test/gtest_env_var_test.py b/test/gtest_env_var_test.py index ac24337..4728bbc 100755 --- a/test/gtest_env_var_test.py +++ b/test/gtest_env_var_test.py @@ -67,7 +67,8 @@ def GetFlag(flag): args = [COMMAND] if flag is not None: args += [flag] - return gtest_test_utils.Subprocess(args, env=environ).output + return gtest_test_utils.Subprocess(args, env=environ, + capture_stderr=False).output def TestFlag(flag, test_val, default_val): diff --git a/test/gtest_output_test.py b/test/gtest_output_test.py index f409e2a..72b3ae0 100755 --- a/test/gtest_output_test.py +++ b/test/gtest_output_test.py @@ -213,7 +213,7 @@ def GetShellCommandOutput(env_cmd): # Set and save the environment properly. environ = os.environ.copy() environ.update(env_cmd[0]) - p = gtest_test_utils.Subprocess(env_cmd[1], env=environ) + p = gtest_test_utils.Subprocess(env_cmd[1], env=environ, capture_stderr=False) return p.output diff --git a/test/gtest_output_test_.cc b/test/gtest_output_test_.cc index 031ae83..1b08b65 100644 --- a/test/gtest_output_test_.cc +++ b/test/gtest_output_test_.cc @@ -221,13 +221,13 @@ TEST(SCOPED_TRACETest, CanBeRepeated) { { SCOPED_TRACE("C"); - ADD_FAILURE() << "This failure is expected, and should contain " - << "trace point A, B, and C."; + ADD_FAILURE() << "This failure is expected, and should " + << "contain trace point A, B, and C."; } SCOPED_TRACE("D"); - ADD_FAILURE() << "This failure is expected, and should contain " - << "trace point A, B, and D."; + ADD_FAILURE() << "This failure is expected, and should " + << "contain trace point A, B, and D."; } #if GTEST_IS_THREADSAFE diff --git a/test/gtest_shuffle_test.py b/test/gtest_shuffle_test.py index 30d0303..d3e5780 100755 --- a/test/gtest_shuffle_test.py +++ b/test/gtest_shuffle_test.py @@ -81,7 +81,8 @@ def RunAndReturnOutput(extra_env, args): environ_copy = os.environ.copy() environ_copy.update(extra_env) - return gtest_test_utils.Subprocess([COMMAND] + args, env=environ_copy).output + return gtest_test_utils.Subprocess([COMMAND] + args, env=environ_copy, + capture_stderr=False).output def GetTestsForAllIterations(extra_env, args): diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index 8cd58be..e61f791 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -2642,6 +2642,11 @@ TEST(StringAssertionTest, STREQ_Wide) { // Strings containing wide characters. EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), "abc"); + + // The streaming variation. + EXPECT_NONFATAL_FAILURE({ // NOLINT + EXPECT_STREQ(L"abc\x8119", L"abc\x8121") << "Expected failure"; + }, "Expected failure"); } // Tests *_STRNE on wide strings. @@ -2668,6 +2673,9 @@ TEST(StringAssertionTest, STRNE_Wide) { // Strings containing wide characters. EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), "abc"); + + // The streaming variation. + ASSERT_STRNE(L"abc\x8119", L"abc\x8120") << "This shouldn't happen"; } // Tests for ::testing::IsSubstring(). @@ -4263,8 +4271,109 @@ TEST(SuccessfulAssertionTest, ASSERT_STR) { namespace { +// Tests the message streaming variation of assertions. + +TEST(AssertionWithMessageTest, EXPECT) { + EXPECT_EQ(1, 1) << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_NE(1, 1) << "Expected failure #1.", + "Expected failure #1"); + EXPECT_LE(1, 2) << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_LT(1, 0) << "Expected failure #2.", + "Expected failure #2."); + EXPECT_GE(1, 0) << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_GT(1, 2) << "Expected failure #3.", + "Expected failure #3."); + + EXPECT_STREQ("1", "1") << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("1", "1") << "Expected failure #4.", + "Expected failure #4."); + EXPECT_STRCASEEQ("a", "A") << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("a", "A") << "Expected failure #5.", + "Expected failure #5."); + + EXPECT_FLOAT_EQ(1, 1) << "This should succeed."; + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1, 1.2) << "Expected failure #6.", + "Expected failure #6."); + EXPECT_NEAR(1, 1.1, 0.2) << "This should succeed."; +} + +TEST(AssertionWithMessageTest, ASSERT) { + ASSERT_EQ(1, 1) << "This should succeed."; + ASSERT_NE(1, 2) << "This should succeed."; + ASSERT_LE(1, 2) << "This should succeed."; + ASSERT_LT(1, 2) << "This should succeed."; + ASSERT_GE(1, 0) << "This should succeed."; + EXPECT_FATAL_FAILURE(ASSERT_GT(1, 2) << "Expected failure.", + "Expected failure."); +} + +TEST(AssertionWithMessageTest, ASSERT_STR) { + ASSERT_STREQ("1", "1") << "This should succeed."; + ASSERT_STRNE("1", "2") << "This should succeed."; + ASSERT_STRCASEEQ("a", "A") << "This should succeed."; + EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("a", "A") << "Expected failure.", + "Expected failure."); +} + +TEST(AssertionWithMessageTest, ASSERT_FLOATING) { + ASSERT_FLOAT_EQ(1, 1) << "This should succeed."; + ASSERT_DOUBLE_EQ(1, 1) << "This should succeed."; + EXPECT_FATAL_FAILURE(ASSERT_NEAR(1,1.2, 0.1) << "Expect failure.", // NOLINT + "Expect failure."); + // To work around a bug in gcc 2.95.0, there is intentionally no + // space after the first comma in the previous statement. +} + +// Tests using ASSERT_FALSE with a streamed message. +TEST(AssertionWithMessageTest, ASSERT_FALSE) { + ASSERT_FALSE(false) << "This shouldn't fail."; + EXPECT_FATAL_FAILURE({ // NOLINT + ASSERT_FALSE(true) << "Expected failure: " << 2 << " > " << 1 + << " evaluates to " << true; + }, "Expected failure"); +} + +// Tests using FAIL with a streamed message. +TEST(AssertionWithMessageTest, FAIL) { + EXPECT_FATAL_FAILURE(FAIL() << 0, + "0"); +} + +// Tests using SUCCEED with a streamed message. +TEST(AssertionWithMessageTest, SUCCEED) { + SUCCEED() << "Success == " << 1; +} + +// Tests using ASSERT_TRUE with a streamed message. +TEST(AssertionWithMessageTest, ASSERT_TRUE) { + ASSERT_TRUE(true) << "This should succeed."; + ASSERT_TRUE(true) << true; + EXPECT_FATAL_FAILURE({ // NOLINT + ASSERT_TRUE(false) << static_cast(NULL) + << static_cast(NULL); + }, "(null)(null)"); +} + +#if GTEST_OS_WINDOWS +// Tests using wide strings in assertion messages. +TEST(AssertionWithMessageTest, WideStringMessage) { + EXPECT_NONFATAL_FAILURE({ // NOLINT + EXPECT_TRUE(false) << L"This failure is expected.\x8119"; + }, "This failure is expected."); + EXPECT_FATAL_FAILURE({ // NOLINT + ASSERT_EQ(1, 2) << "This failure is " + << L"expected too.\x8120"; + }, "This failure is expected too."); +} +#endif // GTEST_OS_WINDOWS + // Tests EXPECT_TRUE. TEST(ExpectTest, EXPECT_TRUE) { + EXPECT_TRUE(true) << "Intentional success"; + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #1.", + "Intentional failure #1."); + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #2.", + "Intentional failure #2."); EXPECT_TRUE(2 > 1); // NOLINT EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), "Value of: 2 < 1\n" @@ -4288,9 +4397,14 @@ TEST(ExpectTest, ExpectTrueWithAssertionResult) { "Expected: true"); } -// Tests EXPECT_FALSE. +// Tests EXPECT_FALSE with a streamed message. TEST(ExpectTest, EXPECT_FALSE) { EXPECT_FALSE(2 < 1); // NOLINT + EXPECT_FALSE(false) << "Intentional success"; + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #1.", + "Intentional failure #1."); + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #2.", + "Intentional failure #2."); EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1), "Value of: 2 > 1\n" " Actual: true\n" @@ -4564,7 +4678,7 @@ TEST(StreamableTest, BasicIoManip) { void AddFailureHelper(bool* aborted) { *aborted = true; - ADD_FAILURE() << "Failure"; + ADD_FAILURE() << "Intentional failure."; *aborted = false; } @@ -4572,7 +4686,7 @@ void AddFailureHelper(bool* aborted) { TEST(MacroTest, ADD_FAILURE) { bool aborted = true; EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), - "Failure"); + "Intentional failure."); EXPECT_FALSE(aborted); } @@ -4605,7 +4719,6 @@ TEST(MacroTest, SUCCEED) { SUCCEED() << "Explicit success."; } - // Tests for EXPECT_EQ() and ASSERT_EQ(). // // These tests fail *intentionally*, s.t. the failure messages can be -- cgit v1.2.1