Improves EXPECT_DEATH_IF_SUPPORTED to allow streaming of messages and enforcing the validity of arguments (by Vlad Losev); adds samples for the event listener API (by Vlad Losev); simplifies the tests using EXPECT_DEATH_IF_SUPPORTED (by Zhanyong Wan).
This commit is contained in:
parent
f6dd67a155
commit
b2ee82ebf9
@ -260,7 +260,7 @@ class KilledBySignal {
|
||||
|
||||
// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
|
||||
// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
|
||||
// death tests are supported; otherwise they expand to empty. This is
|
||||
// death tests are supported; otherwise they just issue a warning. This is
|
||||
// useful when you are combining death test assertions with normal test
|
||||
// assertions in one test.
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
@ -270,13 +270,9 @@ class KilledBySignal {
|
||||
ASSERT_DEATH(statement, regex)
|
||||
#else
|
||||
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
|
||||
GTEST_LOG_(WARNING, \
|
||||
"Death tests are not supported on this platform. The statement" \
|
||||
" '" #statement "' can not be verified")
|
||||
GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
|
||||
#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
|
||||
GTEST_LOG_(WARNING, \
|
||||
"Death tests are not supported on this platform. The statement" \
|
||||
" '" #statement "' can not be verified")
|
||||
GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
|
||||
#endif
|
||||
|
||||
} // namespace testing
|
||||
|
@ -219,6 +219,55 @@ class InternalRunDeathTestFlag {
|
||||
// the flag is specified; otherwise returns NULL.
|
||||
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
|
||||
|
||||
#else // GTEST_HAS_DEATH_TEST
|
||||
|
||||
// This macro is used for implementing macros such as
|
||||
// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
|
||||
// death tests are not supported. Those macros must compile on such systems
|
||||
// iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on
|
||||
// systems that support death tests. This allows one to write such a macro
|
||||
// on a system that does not support death tests and be sure that it will
|
||||
// compile on a death-test supporting system.
|
||||
//
|
||||
// Parameters:
|
||||
// statement - A statement that a macro such as EXPECT_DEATH would test
|
||||
// for program termination. This macro has to make sure this
|
||||
// statement is compiled but not executed, to ensure that
|
||||
// EXPECT_DEATH_IF_SUPPORTED compiles with a certain
|
||||
// parameter iff EXPECT_DEATH compiles with it.
|
||||
// regex - A regex that a macro such as EXPECT_DEATH would use to test
|
||||
// the output of statement. This parameter has to be
|
||||
// compiled but not evaluated by this macro, to ensure that
|
||||
// this macro only accepts expressions that a macro such as
|
||||
// EXPECT_DEATH would accept.
|
||||
// terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
|
||||
// and a return statement for ASSERT_DEATH_IF_SUPPORTED.
|
||||
// This ensures that ASSERT_DEATH_IF_SUPPORTED will not
|
||||
// compile inside functions where ASSERT_DEATH doesn't
|
||||
// compile.
|
||||
//
|
||||
// The branch that has an always false condition is used to ensure that
|
||||
// statement and regex are compiled (and thus syntactically correct) but
|
||||
// never executed. The unreachable code macro protects the terminator
|
||||
// statement from generating an 'unreachable code' warning in case
|
||||
// statement unconditionally returns or throws. The Message constructor at
|
||||
// the end allows the syntax of streaming additional messages into the
|
||||
// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
|
||||
// TODO(vladl@google.com): rename the GTEST_HIDE_UNREACHABLE_CODE_ macro to
|
||||
// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_.
|
||||
#define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
|
||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||
if (::testing::internal::AlwaysTrue()) { \
|
||||
GTEST_LOG_(WARNING, \
|
||||
"Death tests are not supported on this platform.\n" \
|
||||
"Statement '" #statement "' cannot be verified."); \
|
||||
} else if (!::testing::internal::AlwaysTrue()) { \
|
||||
::testing::internal::RE::PartialMatch(".*", (regex)); \
|
||||
GTEST_HIDE_UNREACHABLE_CODE_(statement); \
|
||||
terminator; \
|
||||
} else \
|
||||
::testing::Message()
|
||||
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
} // namespace internal
|
||||
|
165
samples/sample10_unittest.cc
Normal file
165
samples/sample10_unittest.cc
Normal file
@ -0,0 +1,165 @@
|
||||
// Copyright 2009 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Author: vladl@google.com (Vlad Losev)
|
||||
|
||||
// This sample shows how to use Google Test listener API to implement
|
||||
// a primitive leak checker.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using ::testing::InitGoogleTest;
|
||||
using ::testing::Test;
|
||||
using ::testing::TestInfo;
|
||||
using ::testing::TestPartResult;
|
||||
using ::testing::UnitTest;
|
||||
using ::testing::internal::EmptyTestEventListener;
|
||||
using ::testing::internal::EventListeners;
|
||||
using ::testing::internal::TestCase;
|
||||
|
||||
namespace testing {
|
||||
namespace internal {
|
||||
|
||||
// TODO(vladl@google.com): Get rid of the accessor class once the API is
|
||||
// published.
|
||||
class UnitTestAccessor {
|
||||
public:
|
||||
static bool Passed(const UnitTest& unit_test) { return unit_test.Passed(); }
|
||||
static EventListeners& listeners(UnitTest* unit_test) {
|
||||
return unit_test->listeners();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace testing
|
||||
|
||||
using ::testing::internal::UnitTestAccessor;
|
||||
|
||||
namespace {
|
||||
|
||||
// We will track memory used by this class.
|
||||
class Water {
|
||||
public:
|
||||
// Normal Water declarations go here.
|
||||
|
||||
// operator new and operator delete help us control water allocation.
|
||||
void* operator new(size_t allocation_size) {
|
||||
allocated_++;
|
||||
return malloc(allocation_size);
|
||||
}
|
||||
|
||||
void operator delete(void* block, size_t allocation_size) {
|
||||
allocated_--;
|
||||
free(block);
|
||||
}
|
||||
|
||||
static int allocated() { return allocated_; }
|
||||
|
||||
private:
|
||||
static int allocated_;
|
||||
};
|
||||
|
||||
int Water::allocated_ = 0;
|
||||
|
||||
// This event listener monitors how many Water objects are created and
|
||||
// destroyed by each test, and reports a failure if a test leaks some Water
|
||||
// objects. It does this by comparing the number of live Water objects at
|
||||
// the beginning of a test and at the end of a test.
|
||||
class LeakChecker : public EmptyTestEventListener {
|
||||
private:
|
||||
// Called before a test starts.
|
||||
virtual void OnTestStart(const TestInfo& test_info) {
|
||||
initially_allocated_ = Water::allocated();
|
||||
}
|
||||
|
||||
// Called after a test ends.
|
||||
virtual void OnTestEnd(const TestInfo& test_info) {
|
||||
int difference = Water::allocated() - initially_allocated_;
|
||||
|
||||
// You can generate a failure in any event handler except
|
||||
// OnTestPartResult. Just use an appropriate Google Test assertion to do
|
||||
// it.
|
||||
EXPECT_TRUE(difference <= 0)
|
||||
<< "Leaked " << difference << " unit(s) of Water!";
|
||||
}
|
||||
|
||||
int initially_allocated_;
|
||||
};
|
||||
|
||||
TEST(ListenersTest, DoesNotLeak) {
|
||||
Water* water = new Water;
|
||||
delete water;
|
||||
}
|
||||
|
||||
// This should fail when the --check_for_leaks command line flag is
|
||||
// specified.
|
||||
TEST(ListenersTest, LeaksWater) {
|
||||
Water* water = new Water;
|
||||
EXPECT_TRUE(water != NULL);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
InitGoogleTest(&argc, argv);
|
||||
|
||||
bool check_for_leaks = false;
|
||||
if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
|
||||
check_for_leaks = true;
|
||||
else
|
||||
printf("%s\n", "Run this program with --check_for_leaks to enable "
|
||||
"custom leak checking in the tests.");
|
||||
|
||||
// If we are given the --check_for_leaks command line flag, installs the
|
||||
// leak checker.
|
||||
if (check_for_leaks) {
|
||||
EventListeners& listeners = UnitTestAccessor::listeners(
|
||||
UnitTest::GetInstance());
|
||||
|
||||
// Adds the leak checker to the end of the test event listener list,
|
||||
// after the default text output printer and the default XML report
|
||||
// generator.
|
||||
//
|
||||
// The order is important - it ensures that failures generated in the
|
||||
// leak checker's OnTestEnd() method are processed by the text and XML
|
||||
// printers *before* their OnTestEnd() methods are called, such that
|
||||
// they are attributed to the right test. Remember that a listener
|
||||
// receives an OnXyzStart event *after* listeners preceding it in the
|
||||
// list received that event, and receives an OnXyzEnd event *before*
|
||||
// listeners preceding it.
|
||||
//
|
||||
// We don't need to worry about deleting the new listener later, as
|
||||
// Google Test will do it.
|
||||
listeners.Append(new LeakChecker);
|
||||
}
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
188
samples/sample9_unittest.cc
Normal file
188
samples/sample9_unittest.cc
Normal file
@ -0,0 +1,188 @@
|
||||
// Copyright 2009 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Author: vladl@google.com (Vlad Losev)
|
||||
|
||||
// This sample shows how to use Google Test listener API to implement
|
||||
// an alternative console output and how to use the UnitTest reflection API
|
||||
// to enumerate test cases and tests and to inspect their results.
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using ::testing::InitGoogleTest;
|
||||
using ::testing::Test;
|
||||
using ::testing::TestInfo;
|
||||
using ::testing::TestPartResult;
|
||||
using ::testing::UnitTest;
|
||||
using ::testing::internal::EmptyTestEventListener;
|
||||
using ::testing::internal::EventListeners;
|
||||
using ::testing::internal::TestCase;
|
||||
|
||||
namespace testing {
|
||||
namespace internal {
|
||||
|
||||
// TODO(vladl@google.com): Get rid of the accessor class once the API is
|
||||
// published.
|
||||
class UnitTestAccessor {
|
||||
public:
|
||||
static bool Passed(const UnitTest& unit_test) { return unit_test.Passed(); }
|
||||
static EventListeners& listeners(UnitTest* unit_test) {
|
||||
return unit_test->listeners();
|
||||
}
|
||||
static int GetTotalTestCaseCount(const UnitTest& unit_test) {
|
||||
return unit_test.total_test_case_count();
|
||||
}
|
||||
static const TestCase* GetTestCase(const UnitTest& unit_test, int index) {
|
||||
return unit_test.GetTestCase(index);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace testing
|
||||
|
||||
using ::testing::internal::UnitTestAccessor;
|
||||
|
||||
namespace {
|
||||
|
||||
// Provides alternative output mode which produces minimal amount of
|
||||
// information about tests.
|
||||
class TersePrinter : public EmptyTestEventListener {
|
||||
private:
|
||||
// Called before any test activity starts.
|
||||
virtual void OnTestProgramStart(const UnitTest& unit_test) {}
|
||||
|
||||
// Called after all test activities have ended.
|
||||
virtual void OnTestProgramEnd(const UnitTest& unit_test) {
|
||||
fprintf(stdout,
|
||||
"TEST %s\n",
|
||||
UnitTestAccessor::Passed(unit_test) ? "PASSED" : "FAILED");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// Called before a test starts.
|
||||
virtual void OnTestStart(const TestInfo& test_info) {
|
||||
fprintf(stdout,
|
||||
"*** Test %s.%s starting.\n",
|
||||
test_info.test_case_name(),
|
||||
test_info.name());
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// Called after a test ends.
|
||||
virtual void OnTestEnd(const TestInfo& test_info) {
|
||||
fprintf(stdout,
|
||||
"*** Test %s.%s ending.\n",
|
||||
test_info.test_case_name(),
|
||||
test_info.name());
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// Called after a failed assertion or a SUCCESS().
|
||||
virtual void OnNewTestPartResult(const TestPartResult& test_part_result) {
|
||||
fprintf(stdout,
|
||||
"%s in %s:%d\n%s\n",
|
||||
test_part_result.failed() ? "*** Failure" : "Success",
|
||||
test_part_result.file_name(),
|
||||
test_part_result.line_number(),
|
||||
test_part_result.summary());
|
||||
fflush(stdout);
|
||||
}
|
||||
}; // class TersePrinter
|
||||
|
||||
TEST(CustomOutputTest, PrintsMessage) {
|
||||
printf("Printing something from the test body...\n");
|
||||
}
|
||||
|
||||
TEST(CustomOutputTest, Succeeds) {
|
||||
SUCCEED() << "SUCCEED() has been invoked from here";
|
||||
}
|
||||
|
||||
TEST(CustomOutputTest, Fails) {
|
||||
EXPECT_EQ(1, 2)
|
||||
<< "This test fails in order to demonstrate alternative failure messages";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
InitGoogleTest(&argc, argv);
|
||||
|
||||
bool terse_output = false;
|
||||
if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
|
||||
terse_output = true;
|
||||
else
|
||||
printf("%s\n", "Run this program with --terse_output to change the way "
|
||||
"it prints its output.");
|
||||
|
||||
// If we are given the --terse_output command line flag, suppresses the
|
||||
// standard output and attaches own result printer.
|
||||
if (terse_output) {
|
||||
EventListeners& listeners = UnitTestAccessor::listeners(
|
||||
UnitTest::GetInstance());
|
||||
|
||||
// Removes the default console output listener from the list so it will
|
||||
// not receive events from Google Test and won't print any output. Since
|
||||
// this operation transfers ownership of the listener to the caller we
|
||||
// have to delete it as well.
|
||||
delete listeners.Release(listeners.default_result_printer());
|
||||
|
||||
// Adds the custom output listener to the list. It will now receive
|
||||
// events from Google Test and print the alternative output. We don't
|
||||
// have to worry about deleting it since Google Test assumes ownership
|
||||
// over it after adding it to the list.
|
||||
listeners.Append(new TersePrinter);
|
||||
}
|
||||
int ret_val = RUN_ALL_TESTS();
|
||||
|
||||
// This is an example of using the UnitTest reflection API to inspect test
|
||||
// results. Here we discount failures from the tests we expected to fail.
|
||||
int unexpectedly_failed_tests = 0;
|
||||
for (int i = 0;
|
||||
i < UnitTestAccessor::GetTotalTestCaseCount(*UnitTest::GetInstance());
|
||||
++i) {
|
||||
const TestCase* test_case = UnitTestAccessor::GetTestCase(
|
||||
*UnitTest::GetInstance(), i);
|
||||
for (int j = 0; j < test_case->total_test_count(); ++j) {
|
||||
const TestInfo* test_info = test_case->GetTestInfo(j);
|
||||
// Counts failed tests that were not meant to fail (those without
|
||||
// 'Fails' in the name).
|
||||
if (test_info->result()->Failed() &&
|
||||
strcmp(test_info->name(), "Fails") != 0) {
|
||||
unexpectedly_failed_tests++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test that were meant to fail should not affect the test program outcome.
|
||||
if (unexpectedly_failed_tests == 0)
|
||||
ret_val = 0;
|
||||
|
||||
return ret_val;
|
||||
}
|
@ -364,6 +364,8 @@ if env.get('GTEST_BUILD_SAMPLES', False):
|
||||
GtestSample(env, 'sample6_unittest')
|
||||
GtestSample(env, 'sample7_unittest')
|
||||
GtestSample(env, 'sample8_unittest')
|
||||
GtestSample(env, 'sample9_unittest')
|
||||
GtestSample(env, 'sample10_unittest')
|
||||
|
||||
# These exports are used by Google Mock.
|
||||
gtest_exports = {'gtest': gtest,
|
||||
|
@ -1136,7 +1136,7 @@ using testing::internal::GetCapturedStderr;
|
||||
using testing::internal::String;
|
||||
|
||||
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
|
||||
// defined but do not rigger failures when death tests are not available on
|
||||
// defined but do not trigger failures when death tests are not available on
|
||||
// the system.
|
||||
TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
|
||||
// Empty statement will not crash, but that should not trigger a failure
|
||||
@ -1148,16 +1148,89 @@ TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
|
||||
"Death tests are not supported on this platform"));
|
||||
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
|
||||
|
||||
// The streamed message should not be printed as there is no test failure.
|
||||
CaptureStderr();
|
||||
ASSERT_DEATH_IF_SUPPORTED(;, "");
|
||||
EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
|
||||
output = GetCapturedStderr();
|
||||
ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
|
||||
|
||||
CaptureStderr();
|
||||
ASSERT_DEATH_IF_SUPPORTED(;, ""); // NOLINT
|
||||
output = GetCapturedStderr();
|
||||
ASSERT_TRUE(NULL != strstr(output.c_str(),
|
||||
"Death tests are not supported on this platform"));
|
||||
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
|
||||
|
||||
CaptureStderr();
|
||||
ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message"; // NOLINT
|
||||
output = GetCapturedStderr();
|
||||
ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
|
||||
}
|
||||
|
||||
void FuncWithAssert(int* n) {
|
||||
ASSERT_DEATH_IF_SUPPORTED(return;, "");
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
// Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
|
||||
// function (as ASSERT_DEATH does) if death tests are not supported.
|
||||
TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
|
||||
int n = 0;
|
||||
FuncWithAssert(&n);
|
||||
EXPECT_EQ(1, n);
|
||||
}
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
// Tests that the death test macros expand to code which may or may not
|
||||
// be followed by operator<<, and that in either case the complete text
|
||||
// comprises only a single C++ statement.
|
||||
//
|
||||
// The syntax should work whether death tests are available or not.
|
||||
TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
|
||||
if (false)
|
||||
// This would fail if executed; this is a compilation test only
|
||||
ASSERT_DEATH_IF_SUPPORTED(return, "");
|
||||
|
||||
if (true)
|
||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
|
||||
else
|
||||
// This empty "else" branch is meant to ensure that EXPECT_DEATH
|
||||
// doesn't expand into an "if" statement without an "else"
|
||||
; // NOLINT
|
||||
|
||||
if (false)
|
||||
ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
|
||||
|
||||
if (false)
|
||||
; // NOLINT
|
||||
else
|
||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
|
||||
}
|
||||
|
||||
// Tests that conditional death test macros expand to code which interacts
|
||||
// well with switch statements.
|
||||
TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
|
||||
// Microsoft compiler usually complains about switch statements without
|
||||
// case labels. We suppress that warning for this test.
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4065)
|
||||
#endif // _MSC_VER
|
||||
|
||||
switch (0)
|
||||
default:
|
||||
ASSERT_DEATH_IF_SUPPORTED(_exit(1), "")
|
||||
<< "exit in default switch handler";
|
||||
|
||||
switch (0)
|
||||
case 0:
|
||||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif // _MSC_VER
|
||||
}
|
||||
|
||||
// Tests that a test case whose name ends with "DeathTest" works fine
|
||||
// on Windows.
|
||||
TEST(NotADeathTest, Test) {
|
||||
|
@ -133,8 +133,6 @@ TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
|
||||
}
|
||||
#endif // GTEST_OS_MAC
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
|
||||
TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
|
||||
const bool a_false_condition = false;
|
||||
const char regex[] =
|
||||
@ -145,9 +143,12 @@ TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
|
||||
#endif // _MSC_VER
|
||||
".*a_false_condition.*Extra info.*";
|
||||
|
||||
EXPECT_DEATH(GTEST_CHECK_(a_false_condition) << "Extra info", regex);
|
||||
EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
|
||||
regex);
|
||||
}
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
|
||||
TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
|
||||
EXPECT_EXIT({
|
||||
GTEST_CHECK_(true) << "Extra info";
|
||||
|
@ -146,8 +146,6 @@ TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
|
||||
EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
|
||||
}
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
|
||||
typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
|
||||
|
||||
// Tests that the program dies when GetTestPartResult() is called with
|
||||
@ -156,12 +154,10 @@ TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
|
||||
TestPartResultArray results;
|
||||
results.Append(r1_);
|
||||
|
||||
EXPECT_DEATH(results.GetTestPartResult(-1), "");
|
||||
EXPECT_DEATH(results.GetTestPartResult(1), "");
|
||||
EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
|
||||
EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
|
||||
}
|
||||
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
// TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper.
|
||||
|
||||
} // namespace
|
||||
|
@ -198,24 +198,22 @@ TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
|
||||
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
|
||||
}
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
|
||||
typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
|
||||
|
||||
TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
|
||||
EXPECT_DEATH(
|
||||
EXPECT_DEATH_IF_SUPPORTED(
|
||||
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
|
||||
"foo\\.cc.1.?: Test A is listed more than once\\.");
|
||||
}
|
||||
|
||||
TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
|
||||
EXPECT_DEATH(
|
||||
EXPECT_DEATH_IF_SUPPORTED(
|
||||
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
|
||||
"foo\\.cc.1.?: No test named D can be found in this test case\\.");
|
||||
}
|
||||
|
||||
TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
|
||||
EXPECT_DEATH(
|
||||
EXPECT_DEATH_IF_SUPPORTED(
|
||||
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
|
||||
"foo\\.cc.1.?: You forgot to list test B\\.");
|
||||
}
|
||||
@ -224,14 +222,12 @@ TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
|
||||
// a run-time error if the test case has been registered.
|
||||
TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
|
||||
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
|
||||
EXPECT_DEATH(
|
||||
EXPECT_DEATH_IF_SUPPORTED(
|
||||
state_.AddTestName("foo.cc", 2, "FooTest", "D"),
|
||||
"foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
|
||||
"\\(FooTest, \\.\\.\\.\\)\\.");
|
||||
}
|
||||
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
|
||||
// and SetUp()/TearDown() work correctly in type-parameterized tests.
|
||||
|
||||
|
@ -92,19 +92,13 @@ TEST(BazTest, DISABLED_TestC) {
|
||||
// Test case HasDeathTest
|
||||
|
||||
TEST(HasDeathTest, Test1) {
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
EXPECT_DEATH({exit(1);},
|
||||
".*");
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
EXPECT_DEATH_IF_SUPPORTED(exit(1), ".*");
|
||||
}
|
||||
|
||||
// We need at least two death tests to make sure that the all death tests
|
||||
// aren't on the first shard.
|
||||
TEST(HasDeathTest, Test2) {
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
EXPECT_DEATH({exit(1);},
|
||||
".*");
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
EXPECT_DEATH_IF_SUPPORTED(exit(1), ".*");
|
||||
}
|
||||
|
||||
// Test case FoobarTest
|
||||
|
@ -112,13 +112,11 @@ int g_death_test_count = 0;
|
||||
TEST(BarDeathTest, ThreadSafeAndFast) {
|
||||
g_death_test_count++;
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
GTEST_FLAG(death_test_style) = "threadsafe";
|
||||
EXPECT_DEATH(abort(), "");
|
||||
EXPECT_DEATH_IF_SUPPORTED(abort(), "");
|
||||
|
||||
GTEST_FLAG(death_test_style) = "fast";
|
||||
EXPECT_DEATH(abort(), "");
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
EXPECT_DEATH_IF_SUPPORTED(abort(), "");
|
||||
}
|
||||
|
||||
#if GTEST_HAS_PARAM_TEST
|
||||
|
@ -6345,16 +6345,14 @@ TEST(EventListenerTest, SuppressEventForwarding) {
|
||||
EXPECT_EQ(0, on_start_counter);
|
||||
}
|
||||
|
||||
#if GTEST_HAS_DEATH_TEST
|
||||
// Tests that events generated by Google Test are not forwarded in
|
||||
// death test subprocesses.
|
||||
TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) {
|
||||
EXPECT_DEATH({ // NOLINT
|
||||
EXPECT_DEATH_IF_SUPPORTED({
|
||||
GTEST_CHECK_(EventListenersAccessor::EventForwardingEnabled(
|
||||
*GetUnitTestImpl()->listeners())) << "expected failure";},
|
||||
"expected failure");
|
||||
}
|
||||
#endif // GTEST_HAS_DEATH_TEST
|
||||
|
||||
// Tests that a listener installed via SetDefaultResultPrinter() starts
|
||||
// receiving events and is returned via default_result_printer() and that
|
||||
|
Loading…
Reference in New Issue
Block a user