Skip to content

Commit 7a574c9

Browse files
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
1 parent 62576d5 commit 7a574c9

12 files changed

+164
-43
lines changed

include/gtest/gtest-death-test.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ GTEST_API_ bool InDeathTestChild();
8686
// for (int i = 0; i < 5; i++) {
8787
// EXPECT_DEATH(server.ProcessRequest(i),
8888
// "Invalid request .* in ProcessRequest()")
89-
// << "Failed to die on request " << i);
89+
// << "Failed to die on request " << i;
9090
// }
9191
//
9292
// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
@@ -256,10 +256,10 @@ class GTEST_API_ KilledBySignal {
256256
# ifdef NDEBUG
257257

258258
# define EXPECT_DEBUG_DEATH(statement, regex) \
259-
do { statement; } while (::testing::internal::AlwaysFalse())
259+
GTEST_EXECUTE_STATEMENT_(statement, regex)
260260

261261
# define ASSERT_DEBUG_DEATH(statement, regex) \
262-
do { statement; } while (::testing::internal::AlwaysFalse())
262+
GTEST_EXECUTE_STATEMENT_(statement, regex)
263263

264264
# else
265265

include/gtest/gtest-param-test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ inline internal::ParamGenerator<bool> Bool() {
12571257
// Boolean flags:
12581258
//
12591259
// class FlagDependentTest
1260-
// : public testing::TestWithParam<tuple(bool, bool)> > {
1260+
// : public testing::TestWithParam<tuple<bool, bool> > {
12611261
// virtual void SetUp() {
12621262
// // Assigns external_flag_1 and external_flag_2 values from the tuple.
12631263
// tie(external_flag_1, external_flag_2) = GetParam();

include/gtest/gtest-param-test.h.pump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ inline internal::ParamGenerator<bool> Bool() {
414414
// Boolean flags:
415415
//
416416
// class FlagDependentTest
417-
// : public testing::TestWithParam<tuple(bool, bool)> > {
417+
// : public testing::TestWithParam<tuple<bool, bool> > {
418418
// virtual void SetUp() {
419419
// // Assigns external_flag_1 and external_flag_2 values from the tuple.
420420
// tie(external_flag_1, external_flag_2) = GetParam();

include/gtest/gtest.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,12 +1731,6 @@ class TestWithParam : public Test, public WithParamInterface<T> {
17311731
// usually want the fail-fast behavior of FAIL and ASSERT_*, but those
17321732
// writing data-driven tests often find themselves using ADD_FAILURE
17331733
// and EXPECT_* more.
1734-
//
1735-
// Examples:
1736-
//
1737-
// EXPECT_TRUE(server.StatusIsOK());
1738-
// ASSERT_FALSE(server.HasPendingRequest(port))
1739-
// << "There are still pending requests " << "on port " << port;
17401734

17411735
// Generates a nonfatal failure with a generic message.
17421736
#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")

include/gtest/internal/gtest-death-test-internal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
217217
// The symbol "fail" here expands to something into which a message
218218
// can be streamed.
219219

220+
// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
221+
// NDEBUG mode. In this case we need the statements to be executed, the regex is
222+
// ignored, and the macro must accept a streamed message even though the message
223+
// is never printed.
224+
# define GTEST_EXECUTE_STATEMENT_(statement, regex) \
225+
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
226+
if (::testing::internal::AlwaysTrue()) { \
227+
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
228+
} else \
229+
::testing::Message()
230+
220231
// A class representing the parsed contents of the
221232
// --gtest_internal_run_death_test flag, as it existed when
222233
// RUN_ALL_TESTS was called.

src/gtest-port.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,15 @@ class CapturedStream {
530530
<< temp_file_path;
531531
filename_ = temp_file_path;
532532
# else
533-
// There's no guarantee that a test has write access to the
534-
// current directory, so we create the temporary file in the /tmp
535-
// directory instead.
533+
// There's no guarantee that a test has write access to the current
534+
// directory, so we create the temporary file in the /tmp directory instead.
535+
// We use /tmp on most systems, and /mnt/sdcard on Android. That's because
536+
// Android doesn't have /tmp.
537+
# if GTEST_OS_LINUX_ANDROID
538+
char name_template[] = "/mnt/sdcard/gtest_captured_stream.XXXXXX";
539+
# else
536540
char name_template[] = "/tmp/captured_stream.XXXXXX";
541+
# endif // GTEST_OS_LINUX_ANDROID
537542
const int captured_fd = mkstemp(name_template);
538543
filename_ = name_template;
539544
# endif // GTEST_OS_WINDOWS

test/gtest-death-test_test.cc

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) {
618618
"illegal return in test statement.");
619619
}
620620

621-
// Tests that EXPECT_DEBUG_DEATH works as expected,
622-
// that is, in debug mode, it:
621+
// Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a
622+
// message to it, and in debug mode it:
623623
// 1. Asserts on death.
624624
// 2. Has no side effect.
625625
//
@@ -628,8 +628,8 @@ TEST_F(TestForDeathTest, ReturnIsFailure) {
628628
TEST_F(TestForDeathTest, TestExpectDebugDeath) {
629629
int sideeffect = 0;
630630

631-
EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect),
632-
"death.*DieInDebugElse12");
631+
EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
632+
<< "Must accept a streamed message";
633633

634634
# ifdef NDEBUG
635635

@@ -644,22 +644,18 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) {
644644
# endif
645645
}
646646

647-
// Tests that ASSERT_DEBUG_DEATH works as expected
648-
// In debug mode:
649-
// 1. Asserts on debug death.
647+
// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
648+
// message to it, and in debug mode it:
649+
// 1. Asserts on death.
650650
// 2. Has no side effect.
651651
//
652-
// In opt mode:
653-
// 1. Has side effects and returns the expected value (12).
652+
// And in opt mode, it:
653+
// 1. Has side effects but does not assert.
654654
TEST_F(TestForDeathTest, TestAssertDebugDeath) {
655655
int sideeffect = 0;
656656

657-
ASSERT_DEBUG_DEATH({ // NOLINT
658-
// Tests that the return value is 12 in opt mode.
659-
EXPECT_EQ(12, DieInDebugElse12(&sideeffect));
660-
// Tests that the side effect occurred in opt mode.
661-
EXPECT_EQ(12, sideeffect);
662-
}, "death.*DieInDebugElse12");
657+
ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12")
658+
<< "Must accept a streamed message";
663659

664660
# ifdef NDEBUG
665661

@@ -730,7 +726,7 @@ static void TestExitMacros() {
730726
// Of all signals effects on the process exit code, only those of SIGABRT
731727
// are documented on Windows.
732728
// See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx.
733-
EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "");
729+
EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar";
734730

735731
# else
736732

@@ -739,14 +735,14 @@ static void TestExitMacros() {
739735

740736
EXPECT_FATAL_FAILURE({ // NOLINT
741737
ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
742-
<< "This failure is expected, too.";
738+
<< "This failure is expected, too.";
743739
}, "This failure is expected, too.");
744740

745741
# endif // GTEST_OS_WINDOWS
746742

747743
EXPECT_NONFATAL_FAILURE({ // NOLINT
748744
EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "")
749-
<< "This failure is expected.";
745+
<< "This failure is expected.";
750746
}, "This failure is expected.");
751747
}
752748

test/gtest_env_var_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def GetFlag(flag):
6767
args = [COMMAND]
6868
if flag is not None:
6969
args += [flag]
70-
return gtest_test_utils.Subprocess(args, env=environ).output
70+
return gtest_test_utils.Subprocess(args, env=environ,
71+
capture_stderr=False).output
7172

7273

7374
def TestFlag(flag, test_val, default_val):

test/gtest_output_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def GetShellCommandOutput(env_cmd):
213213
# Set and save the environment properly.
214214
environ = os.environ.copy()
215215
environ.update(env_cmd[0])
216-
p = gtest_test_utils.Subprocess(env_cmd[1], env=environ)
216+
p = gtest_test_utils.Subprocess(env_cmd[1], env=environ, capture_stderr=False)
217217

218218
return p.output
219219

test/gtest_output_test_.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ TEST(SCOPED_TRACETest, CanBeRepeated) {
221221

222222
{
223223
SCOPED_TRACE("C");
224-
ADD_FAILURE() << "This failure is expected, and should contain "
225-
<< "trace point A, B, and C.";
224+
ADD_FAILURE() << "This failure is expected, and should "
225+
<< "contain trace point A, B, and C.";
226226
}
227227

228228
SCOPED_TRACE("D");
229-
ADD_FAILURE() << "This failure is expected, and should contain "
230-
<< "trace point A, B, and D.";
229+
ADD_FAILURE() << "This failure is expected, and should "
230+
<< "contain trace point A, B, and D.";
231231
}
232232

233233
#if GTEST_IS_THREADSAFE

test/gtest_shuffle_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def RunAndReturnOutput(extra_env, args):
8181
environ_copy = os.environ.copy()
8282
environ_copy.update(extra_env)
8383

84-
return gtest_test_utils.Subprocess([COMMAND] + args, env=environ_copy).output
84+
return gtest_test_utils.Subprocess([COMMAND] + args, env=environ_copy,
85+
capture_stderr=False).output
8586

8687

8788
def GetTestsForAllIterations(extra_env, args):

test/gtest_unittest.cc

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,11 @@ TEST(StringAssertionTest, STREQ_Wide) {
26422642
// Strings containing wide characters.
26432643
EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"),
26442644
"abc");
2645+
2646+
// The streaming variation.
2647+
EXPECT_NONFATAL_FAILURE({ // NOLINT
2648+
EXPECT_STREQ(L"abc\x8119", L"abc\x8121") << "Expected failure";
2649+
}, "Expected failure");
26452650
}
26462651

26472652
// Tests *_STRNE on wide strings.
@@ -2668,6 +2673,9 @@ TEST(StringAssertionTest, STRNE_Wide) {
26682673
// Strings containing wide characters.
26692674
EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"),
26702675
"abc");
2676+
2677+
// The streaming variation.
2678+
ASSERT_STRNE(L"abc\x8119", L"abc\x8120") << "This shouldn't happen";
26712679
}
26722680

26732681
// Tests for ::testing::IsSubstring().
@@ -4263,8 +4271,109 @@ TEST(SuccessfulAssertionTest, ASSERT_STR) {
42634271

42644272
namespace {
42654273

4274+
// Tests the message streaming variation of assertions.
4275+
4276+
TEST(AssertionWithMessageTest, EXPECT) {
4277+
EXPECT_EQ(1, 1) << "This should succeed.";
4278+
EXPECT_NONFATAL_FAILURE(EXPECT_NE(1, 1) << "Expected failure #1.",
4279+
"Expected failure #1");
4280+
EXPECT_LE(1, 2) << "This should succeed.";
4281+
EXPECT_NONFATAL_FAILURE(EXPECT_LT(1, 0) << "Expected failure #2.",
4282+
"Expected failure #2.");
4283+
EXPECT_GE(1, 0) << "This should succeed.";
4284+
EXPECT_NONFATAL_FAILURE(EXPECT_GT(1, 2) << "Expected failure #3.",
4285+
"Expected failure #3.");
4286+
4287+
EXPECT_STREQ("1", "1") << "This should succeed.";
4288+
EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("1", "1") << "Expected failure #4.",
4289+
"Expected failure #4.");
4290+
EXPECT_STRCASEEQ("a", "A") << "This should succeed.";
4291+
EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("a", "A") << "Expected failure #5.",
4292+
"Expected failure #5.");
4293+
4294+
EXPECT_FLOAT_EQ(1, 1) << "This should succeed.";
4295+
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1, 1.2) << "Expected failure #6.",
4296+
"Expected failure #6.");
4297+
EXPECT_NEAR(1, 1.1, 0.2) << "This should succeed.";
4298+
}
4299+
4300+
TEST(AssertionWithMessageTest, ASSERT) {
4301+
ASSERT_EQ(1, 1) << "This should succeed.";
4302+
ASSERT_NE(1, 2) << "This should succeed.";
4303+
ASSERT_LE(1, 2) << "This should succeed.";
4304+
ASSERT_LT(1, 2) << "This should succeed.";
4305+
ASSERT_GE(1, 0) << "This should succeed.";
4306+
EXPECT_FATAL_FAILURE(ASSERT_GT(1, 2) << "Expected failure.",
4307+
"Expected failure.");
4308+
}
4309+
4310+
TEST(AssertionWithMessageTest, ASSERT_STR) {
4311+
ASSERT_STREQ("1", "1") << "This should succeed.";
4312+
ASSERT_STRNE("1", "2") << "This should succeed.";
4313+
ASSERT_STRCASEEQ("a", "A") << "This should succeed.";
4314+
EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("a", "A") << "Expected failure.",
4315+
"Expected failure.");
4316+
}
4317+
4318+
TEST(AssertionWithMessageTest, ASSERT_FLOATING) {
4319+
ASSERT_FLOAT_EQ(1, 1) << "This should succeed.";
4320+
ASSERT_DOUBLE_EQ(1, 1) << "This should succeed.";
4321+
EXPECT_FATAL_FAILURE(ASSERT_NEAR(1,1.2, 0.1) << "Expect failure.", // NOLINT
4322+
"Expect failure.");
4323+
// To work around a bug in gcc 2.95.0, there is intentionally no
4324+
// space after the first comma in the previous statement.
4325+
}
4326+
4327+
// Tests using ASSERT_FALSE with a streamed message.
4328+
TEST(AssertionWithMessageTest, ASSERT_FALSE) {
4329+
ASSERT_FALSE(false) << "This shouldn't fail.";
4330+
EXPECT_FATAL_FAILURE({ // NOLINT
4331+
ASSERT_FALSE(true) << "Expected failure: " << 2 << " > " << 1
4332+
<< " evaluates to " << true;
4333+
}, "Expected failure");
4334+
}
4335+
4336+
// Tests using FAIL with a streamed message.
4337+
TEST(AssertionWithMessageTest, FAIL) {
4338+
EXPECT_FATAL_FAILURE(FAIL() << 0,
4339+
"0");
4340+
}
4341+
4342+
// Tests using SUCCEED with a streamed message.
4343+
TEST(AssertionWithMessageTest, SUCCEED) {
4344+
SUCCEED() << "Success == " << 1;
4345+
}
4346+
4347+
// Tests using ASSERT_TRUE with a streamed message.
4348+
TEST(AssertionWithMessageTest, ASSERT_TRUE) {
4349+
ASSERT_TRUE(true) << "This should succeed.";
4350+
ASSERT_TRUE(true) << true;
4351+
EXPECT_FATAL_FAILURE({ // NOLINT
4352+
ASSERT_TRUE(false) << static_cast<const char *>(NULL)
4353+
<< static_cast<char *>(NULL);
4354+
}, "(null)(null)");
4355+
}
4356+
4357+
#if GTEST_OS_WINDOWS
4358+
// Tests using wide strings in assertion messages.
4359+
TEST(AssertionWithMessageTest, WideStringMessage) {
4360+
EXPECT_NONFATAL_FAILURE({ // NOLINT
4361+
EXPECT_TRUE(false) << L"This failure is expected.\x8119";
4362+
}, "This failure is expected.");
4363+
EXPECT_FATAL_FAILURE({ // NOLINT
4364+
ASSERT_EQ(1, 2) << "This failure is "
4365+
<< L"expected too.\x8120";
4366+
}, "This failure is expected too.");
4367+
}
4368+
#endif // GTEST_OS_WINDOWS
4369+
42664370
// Tests EXPECT_TRUE.
42674371
TEST(ExpectTest, EXPECT_TRUE) {
4372+
EXPECT_TRUE(true) << "Intentional success";
4373+
EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #1.",
4374+
"Intentional failure #1.");
4375+
EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #2.",
4376+
"Intentional failure #2.");
42684377
EXPECT_TRUE(2 > 1); // NOLINT
42694378
EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1),
42704379
"Value of: 2 < 1\n"
@@ -4288,9 +4397,14 @@ TEST(ExpectTest, ExpectTrueWithAssertionResult) {
42884397
"Expected: true");
42894398
}
42904399

4291-
// Tests EXPECT_FALSE.
4400+
// Tests EXPECT_FALSE with a streamed message.
42924401
TEST(ExpectTest, EXPECT_FALSE) {
42934402
EXPECT_FALSE(2 < 1); // NOLINT
4403+
EXPECT_FALSE(false) << "Intentional success";
4404+
EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #1.",
4405+
"Intentional failure #1.");
4406+
EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #2.",
4407+
"Intentional failure #2.");
42944408
EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1),
42954409
"Value of: 2 > 1\n"
42964410
" Actual: true\n"
@@ -4564,15 +4678,15 @@ TEST(StreamableTest, BasicIoManip) {
45644678

45654679
void AddFailureHelper(bool* aborted) {
45664680
*aborted = true;
4567-
ADD_FAILURE() << "Failure";
4681+
ADD_FAILURE() << "Intentional failure.";
45684682
*aborted = false;
45694683
}
45704684

45714685
// Tests ADD_FAILURE.
45724686
TEST(MacroTest, ADD_FAILURE) {
45734687
bool aborted = true;
45744688
EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted),
4575-
"Failure");
4689+
"Intentional failure.");
45764690
EXPECT_FALSE(aborted);
45774691
}
45784692

@@ -4605,7 +4719,6 @@ TEST(MacroTest, SUCCEED) {
46054719
SUCCEED() << "Explicit success.";
46064720
}
46074721

4608-
46094722
// Tests for EXPECT_EQ() and ASSERT_EQ().
46104723
//
46114724
// These tests fail *intentionally*, s.t. the failure messages can be

0 commit comments

Comments
 (0)