From 84d986531e8df9fb6ea4a7bae8fb130c05b3782c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 22 Apr 2019 18:23:35 -0400 Subject: [PATCH] Googletest export Add GTEST_FAIL_AT, an equivalent to ADD_FAILURE_AT but that spawns a fatal failure rather than a non-fatal, eg. the equivalent of an ASSERT* failure rather than an EXPECT* failure. PiperOrigin-RevId: 244746609 --- googletest/include/gtest/gtest.h | 5 +++++ .../googletest-output-test-golden-lin.txt | 19 ++++++++++++++----- googletest/test/googletest-output-test_.cc | 6 +++++- googletest/test/gtest_unittest.cc | 13 +++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 76f4098b..73824428 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -1900,6 +1900,11 @@ class TestWithParam : public Test, public WithParamInterface { // Generates a fatal failure with a generic message. #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") +// Like GTEST_FAIL(), but at the given source file location. +#define GTEST_FAIL_AT(file, line) \ + GTEST_MESSAGE_AT_(file, line, "Failed", \ + ::testing::TestPartResult::kFatalFailure) + // Define this macro to 1 to omit the definition of FAIL(), which is a // generic name and clashes with some other libraries. #if !GTEST_DONT_DEFINE_FAIL diff --git a/googletest/test/googletest-output-test-golden-lin.txt b/googletest/test/googletest-output-test-golden-lin.txt index 29507fc0..270b15ae 100644 --- a/googletest/test/googletest-output-test-golden-lin.txt +++ b/googletest/test/googletest-output-test-golden-lin.txt @@ -12,7 +12,7 @@ Expected equality of these values: 3 Stack trace: (omitted) -[==========] Running 83 tests from 38 test suites. +[==========] Running 84 tests from 39 test suites. [----------] Global test environment set-up. FooEnvironment::SetUp() called. BarEnvironment::SetUp() called. @@ -380,10 +380,18 @@ Stack trace: (omitted) [ RUN ] AddFailureAtTest.MessageContainsSpecifiedFileAndLineNumber foo.cc:42: Failure Failed -Expected failure in foo.cc +Expected nonfatal failure in foo.cc Stack trace: (omitted) [ FAILED ] AddFailureAtTest.MessageContainsSpecifiedFileAndLineNumber +[----------] 1 test from GtestFailAtTest +[ RUN ] GtestFailAtTest.MessageContainsSpecifiedFileAndLineNumber +foo.cc:42: Failure +Failed +Expected fatal failure in foo.cc +Stack trace: (omitted) + +[ FAILED ] GtestFailAtTest.MessageContainsSpecifiedFileAndLineNumber [----------] 4 tests from MixedUpTestSuiteTest [ RUN ] MixedUpTestSuiteTest.FirstTestFromNamespaceFoo [ OK ] MixedUpTestSuiteTest.FirstTestFromNamespaceFoo @@ -984,9 +992,9 @@ Failed Expected fatal failure. Stack trace: (omitted) -[==========] 83 tests from 38 test suites ran. +[==========] 84 tests from 39 test suites ran. [ PASSED ] 30 tests. -[ FAILED ] 53 tests, listed below: +[ FAILED ] 54 tests, listed below: [ FAILED ] NonfatalFailureTest.EscapesStringOperands [ FAILED ] NonfatalFailureTest.DiffForLongStrings [ FAILED ] FatalFailureTest.FatalFailureInSubroutine @@ -1006,6 +1014,7 @@ Stack trace: (omitted) [ FAILED ] NonFatalFailureInSetUpTest.FailureInSetUp [ FAILED ] FatalFailureInSetUpTest.FailureInSetUp [ FAILED ] AddFailureAtTest.MessageContainsSpecifiedFileAndLineNumber +[ FAILED ] GtestFailAtTest.MessageContainsSpecifiedFileAndLineNumber [ FAILED ] MixedUpTestSuiteTest.ThisShouldFail [ FAILED ] MixedUpTestSuiteTest.ThisShouldFailToo [ FAILED ] MixedUpTestSuiteWithSameTestNameTest.TheSecondTestWithThisNameShouldFail @@ -1041,7 +1050,7 @@ Stack trace: (omitted) [ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 [ FAILED ] PrintingStrings/ParamTest.Failure/a, where GetParam() = "a" -53 FAILED TESTS +54 FAILED TESTS  YOU HAVE 1 DISABLED TEST Note: Google Test filter = FatalFailureTest.*:LoggingTest.* diff --git a/googletest/test/googletest-output-test_.cc b/googletest/test/googletest-output-test_.cc index c6ce59e7..f1222b6a 100644 --- a/googletest/test/googletest-output-test_.cc +++ b/googletest/test/googletest-output-test_.cc @@ -461,7 +461,11 @@ TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { } TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { - ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc"; + ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc"; +} + +TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) { + GTEST_FAIL_AT("foo.cc", 42) << "Expected fatal failure in foo.cc"; } #if GTEST_IS_THREADSAFE diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index 28ced738..d4810567 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -4708,6 +4708,19 @@ TEST(MacroTest, FAIL) { "Intentional failure."); } +// Tests GTEST_FAIL_AT. +TEST(MacroTest, GTEST_FAIL_AT) { + // Verifies that GTEST_FAIL_AT does generate a fatal failure and + // the failure message contains the user-streamed part. + EXPECT_FATAL_FAILURE(GTEST_FAIL_AT("foo.cc", 42) << "Wrong!", "Wrong!"); + + // Verifies that the user-streamed part is optional. + EXPECT_FATAL_FAILURE(GTEST_FAIL_AT("foo.cc", 42), "Failed"); + + // See the ADD_FAIL_AT test above to see how we test that the failure message + // contains the right filename and line number -- the same applies here. +} + // Tests SUCCEED TEST(MacroTest, SUCCEED) { SUCCEED();