"vscode:/vscode.git/clone" did not exist on "704105148860d6b90b009681b221a76575e5c88c"
Commit f0b86fc3 authored by jgm's avatar jgm
Browse files

Misc small updates to some debug death code, and to messages streaming to macros

parent cfb40870
...@@ -86,7 +86,7 @@ GTEST_API_ bool InDeathTestChild(); ...@@ -86,7 +86,7 @@ GTEST_API_ bool InDeathTestChild();
// for (int i = 0; i < 5; i++) { // for (int i = 0; i < 5; i++) {
// EXPECT_DEATH(server.ProcessRequest(i), // EXPECT_DEATH(server.ProcessRequest(i),
// "Invalid request .* in ProcessRequest()") // "Invalid request .* in ProcessRequest()")
// << "Failed to die on request " << i); // << "Failed to die on request " << i;
// } // }
// //
// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); // ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
...@@ -256,10 +256,10 @@ class GTEST_API_ KilledBySignal { ...@@ -256,10 +256,10 @@ class GTEST_API_ KilledBySignal {
# ifdef NDEBUG # ifdef NDEBUG
# define EXPECT_DEBUG_DEATH(statement, regex) \ # define EXPECT_DEBUG_DEATH(statement, regex) \
do { statement; } while (::testing::internal::AlwaysFalse()) GTEST_EXECUTE_STATEMENT_(statement, regex)
# define ASSERT_DEBUG_DEATH(statement, regex) \ # define ASSERT_DEBUG_DEATH(statement, regex) \
do { statement; } while (::testing::internal::AlwaysFalse()) GTEST_EXECUTE_STATEMENT_(statement, regex)
# else # else
......
...@@ -1257,7 +1257,7 @@ inline internal::ParamGenerator<bool> Bool() { ...@@ -1257,7 +1257,7 @@ inline internal::ParamGenerator<bool> Bool() {
// Boolean flags: // Boolean flags:
// //
// class FlagDependentTest // class FlagDependentTest
// : public testing::TestWithParam<tuple(bool, bool)> > { // : public testing::TestWithParam<tuple<bool, bool> > {
// virtual void SetUp() { // virtual void SetUp() {
// // Assigns external_flag_1 and external_flag_2 values from the tuple. // // Assigns external_flag_1 and external_flag_2 values from the tuple.
// tie(external_flag_1, external_flag_2) = GetParam(); // tie(external_flag_1, external_flag_2) = GetParam();
......
...@@ -414,7 +414,7 @@ inline internal::ParamGenerator<bool> Bool() { ...@@ -414,7 +414,7 @@ inline internal::ParamGenerator<bool> Bool() {
// Boolean flags: // Boolean flags:
// //
// class FlagDependentTest // class FlagDependentTest
// : public testing::TestWithParam<tuple(bool, bool)> > { // : public testing::TestWithParam<tuple<bool, bool> > {
// virtual void SetUp() { // virtual void SetUp() {
// // Assigns external_flag_1 and external_flag_2 values from the tuple. // // Assigns external_flag_1 and external_flag_2 values from the tuple.
// tie(external_flag_1, external_flag_2) = GetParam(); // tie(external_flag_1, external_flag_2) = GetParam();
......
...@@ -1731,12 +1731,6 @@ class TestWithParam : public Test, public WithParamInterface<T> { ...@@ -1731,12 +1731,6 @@ class TestWithParam : public Test, public WithParamInterface<T> {
// usually want the fail-fast behavior of FAIL and ASSERT_*, but those // usually want the fail-fast behavior of FAIL and ASSERT_*, but those
// writing data-driven tests often find themselves using ADD_FAILURE // writing data-driven tests often find themselves using ADD_FAILURE
// and EXPECT_* more. // 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. // Generates a nonfatal failure with a generic message.
#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
......
...@@ -217,6 +217,17 @@ GTEST_API_ bool ExitedUnsuccessfully(int exit_status); ...@@ -217,6 +217,17 @@ GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
// The symbol "fail" here expands to something into which a message // The symbol "fail" here expands to something into which a message
// can be streamed. // 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 // A class representing the parsed contents of the
// --gtest_internal_run_death_test flag, as it existed when // --gtest_internal_run_death_test flag, as it existed when
// RUN_ALL_TESTS was called. // RUN_ALL_TESTS was called.
......
...@@ -530,10 +530,15 @@ class CapturedStream { ...@@ -530,10 +530,15 @@ class CapturedStream {
<< temp_file_path; << temp_file_path;
filename_ = temp_file_path; filename_ = temp_file_path;
# else # else
// There's no guarantee that a test has write access to the // There's no guarantee that a test has write access to the current
// current directory, so we create the temporary file in the /tmp // directory, so we create the temporary file in the /tmp directory instead.
// 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"; char name_template[] = "/tmp/captured_stream.XXXXXX";
# endif // GTEST_OS_LINUX_ANDROID
const int captured_fd = mkstemp(name_template); const int captured_fd = mkstemp(name_template);
filename_ = name_template; filename_ = name_template;
# endif // GTEST_OS_WINDOWS # endif // GTEST_OS_WINDOWS
......
...@@ -618,8 +618,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) { ...@@ -618,8 +618,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) {
"illegal return in test statement."); "illegal return in test statement.");
} }
// Tests that EXPECT_DEBUG_DEATH works as expected, // Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a
// that is, in debug mode, it: // message to it, and in debug mode it:
// 1. Asserts on death. // 1. Asserts on death.
// 2. Has no side effect. // 2. Has no side effect.
// //
...@@ -628,8 +628,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) { ...@@ -628,8 +628,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) {
TEST_F(TestForDeathTest, TestExpectDebugDeath) { TEST_F(TestForDeathTest, TestExpectDebugDeath) {
int sideeffect = 0; int sideeffect = 0;
EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
"death.*DieInDebugElse12"); << "Must accept a streamed message";
# ifdef NDEBUG # ifdef NDEBUG
...@@ -644,22 +644,18 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) { ...@@ -644,22 +644,18 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) {
# endif # endif
} }
// Tests that ASSERT_DEBUG_DEATH works as expected // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
// In debug mode: // message to it, and in debug mode it:
// 1. Asserts on debug death. // 1. Asserts on death.
// 2. Has no side effect. // 2. Has no side effect.
// //
// In opt mode: // And in opt mode, it:
// 1. Has side effects and returns the expected value (12). // 1. Has side effects but does not assert.
TEST_F(TestForDeathTest, TestAssertDebugDeath) { TEST_F(TestForDeathTest, TestAssertDebugDeath) {
int sideeffect = 0; int sideeffect = 0;
ASSERT_DEBUG_DEATH({ // NOLINT ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
// Tests that the return value is 12 in opt mode. << "Must accept a streamed message";
EXPECT_EQ(12, DieInDebugElse12(&sideeffect));
// Tests that the side effect occurred in opt mode.
EXPECT_EQ(12, sideeffect);
}, "death.*DieInDebugElse12");
# ifdef NDEBUG # ifdef NDEBUG
...@@ -730,7 +726,7 @@ static void TestExitMacros() { ...@@ -730,7 +726,7 @@ static void TestExitMacros() {
// Of all signals effects on the process exit code, only those of SIGABRT // Of all signals effects on the process exit code, only those of SIGABRT
// are documented on Windows. // are documented on Windows.
// See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx. // 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 # else
...@@ -739,14 +735,14 @@ static void TestExitMacros() { ...@@ -739,14 +735,14 @@ static void TestExitMacros() {
EXPECT_FATAL_FAILURE({ // NOLINT EXPECT_FATAL_FAILURE({ // NOLINT
ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "") ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
<< "This failure is expected, too."; << "This failure is expected, too.";
}, "This failure is expected, too."); }, "This failure is expected, too.");
# endif // GTEST_OS_WINDOWS # endif // GTEST_OS_WINDOWS
EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_NONFATAL_FAILURE({ // NOLINT
EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "") EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "")
<< "This failure is expected."; << "This failure is expected.";
}, "This failure is expected."); }, "This failure is expected.");
} }
......
...@@ -67,7 +67,8 @@ def GetFlag(flag): ...@@ -67,7 +67,8 @@ def GetFlag(flag):
args = [COMMAND] args = [COMMAND]
if flag is not None: if flag is not None:
args += [flag] 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): def TestFlag(flag, test_val, default_val):
......
...@@ -213,7 +213,7 @@ def GetShellCommandOutput(env_cmd): ...@@ -213,7 +213,7 @@ def GetShellCommandOutput(env_cmd):
# Set and save the environment properly. # Set and save the environment properly.
environ = os.environ.copy() environ = os.environ.copy()
environ.update(env_cmd[0]) 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 return p.output
......
...@@ -221,13 +221,13 @@ TEST(SCOPED_TRACETest, CanBeRepeated) { ...@@ -221,13 +221,13 @@ TEST(SCOPED_TRACETest, CanBeRepeated) {
{ {
SCOPED_TRACE("C"); SCOPED_TRACE("C");
ADD_FAILURE() << "This failure is expected, and should contain " ADD_FAILURE() << "This failure is expected, and should "
<< "trace point A, B, and C."; << "contain trace point A, B, and C.";
} }
SCOPED_TRACE("D"); SCOPED_TRACE("D");
ADD_FAILURE() << "This failure is expected, and should contain " ADD_FAILURE() << "This failure is expected, and should "
<< "trace point A, B, and D."; << "contain trace point A, B, and D.";
} }
#if GTEST_IS_THREADSAFE #if GTEST_IS_THREADSAFE
......
...@@ -81,7 +81,8 @@ def RunAndReturnOutput(extra_env, args): ...@@ -81,7 +81,8 @@ def RunAndReturnOutput(extra_env, args):
environ_copy = os.environ.copy() environ_copy = os.environ.copy()
environ_copy.update(extra_env) 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): def GetTestsForAllIterations(extra_env, args):
......
...@@ -2642,6 +2642,11 @@ TEST(StringAssertionTest, STREQ_Wide) { ...@@ -2642,6 +2642,11 @@ TEST(StringAssertionTest, STREQ_Wide) {
// Strings containing wide characters. // Strings containing wide characters.
EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"),
"abc"); "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. // Tests *_STRNE on wide strings.
...@@ -2668,6 +2673,9 @@ TEST(StringAssertionTest, STRNE_Wide) { ...@@ -2668,6 +2673,9 @@ TEST(StringAssertionTest, STRNE_Wide) {
// Strings containing wide characters. // Strings containing wide characters.
EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"),
"abc"); "abc");
// The streaming variation.
ASSERT_STRNE(L"abc\x8119", L"abc\x8120") << "This shouldn't happen";
} }
// Tests for ::testing::IsSubstring(). // Tests for ::testing::IsSubstring().
...@@ -4263,8 +4271,109 @@ TEST(SuccessfulAssertionTest, ASSERT_STR) { ...@@ -4263,8 +4271,109 @@ TEST(SuccessfulAssertionTest, ASSERT_STR) {
namespace { 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<const char *>(NULL)
<< static_cast<char *>(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. // Tests EXPECT_TRUE.
TEST(ExpectTest, 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_TRUE(2 > 1); // NOLINT
EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1),
"Value of: 2 < 1\n" "Value of: 2 < 1\n"
...@@ -4288,9 +4397,14 @@ TEST(ExpectTest, ExpectTrueWithAssertionResult) { ...@@ -4288,9 +4397,14 @@ TEST(ExpectTest, ExpectTrueWithAssertionResult) {
"Expected: true"); "Expected: true");
} }
// Tests EXPECT_FALSE. // Tests EXPECT_FALSE with a streamed message.
TEST(ExpectTest, EXPECT_FALSE) { TEST(ExpectTest, EXPECT_FALSE) {
EXPECT_FALSE(2 < 1); // NOLINT 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), EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1),
"Value of: 2 > 1\n" "Value of: 2 > 1\n"
" Actual: true\n" " Actual: true\n"
...@@ -4564,7 +4678,7 @@ TEST(StreamableTest, BasicIoManip) { ...@@ -4564,7 +4678,7 @@ TEST(StreamableTest, BasicIoManip) {
void AddFailureHelper(bool* aborted) { void AddFailureHelper(bool* aborted) {
*aborted = true; *aborted = true;
ADD_FAILURE() << "Failure"; ADD_FAILURE() << "Intentional failure.";
*aborted = false; *aborted = false;
} }
...@@ -4572,7 +4686,7 @@ void AddFailureHelper(bool* aborted) { ...@@ -4572,7 +4686,7 @@ void AddFailureHelper(bool* aborted) {
TEST(MacroTest, ADD_FAILURE) { TEST(MacroTest, ADD_FAILURE) {
bool aborted = true; bool aborted = true;
EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted),
"Failure"); "Intentional failure.");
EXPECT_FALSE(aborted); EXPECT_FALSE(aborted);
} }
...@@ -4605,7 +4719,6 @@ TEST(MacroTest, SUCCEED) { ...@@ -4605,7 +4719,6 @@ TEST(MacroTest, SUCCEED) {
SUCCEED() << "Explicit success."; SUCCEED() << "Explicit success.";
} }
// Tests for EXPECT_EQ() and ASSERT_EQ(). // Tests for EXPECT_EQ() and ASSERT_EQ().
// //
// These tests fail *intentionally*, s.t. the failure messages can be // These tests fail *intentionally*, s.t. the failure messages can be
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment