googletest-death-test-test.cc (googletest-release-1.11.0) | : | googletest-death-test-test.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 34 | skipping to change at line 34 | |||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// | // | |||
// Tests for death tests. | // Tests for death tests. | |||
#include "gtest/gtest-death-test.h" | #include "gtest/gtest-death-test.h" | |||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
#include "gtest/internal/gtest-filepath.h" | #include "gtest/internal/gtest-filepath.h" | |||
using testing::internal::AlwaysFalse; | using testing::internal::AlwaysFalse; | |||
using testing::internal::AlwaysTrue; | using testing::internal::AlwaysTrue; | |||
#if GTEST_HAS_DEATH_TEST | #if GTEST_HAS_DEATH_TEST | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
# include <fcntl.h> // For O_BINARY | #include <direct.h> // For chdir(). | |||
# include <direct.h> // For chdir(). | #include <fcntl.h> // For O_BINARY | |||
# include <io.h> | #include <io.h> | |||
# else | #else | |||
# include <unistd.h> | #include <sys/wait.h> // For waitpid. | |||
# include <sys/wait.h> // For waitpid. | #include <unistd.h> | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
# include <limits.h> | #include <limits.h> | |||
# include <signal.h> | #include <signal.h> | |||
# include <stdio.h> | #include <stdio.h> | |||
# if GTEST_OS_LINUX | #if GTEST_OS_LINUX | |||
# include <sys/time.h> | #include <sys/time.h> | |||
# endif // GTEST_OS_LINUX | #endif // GTEST_OS_LINUX | |||
# include "gtest/gtest-spi.h" | #include "gtest/gtest-spi.h" | |||
# include "src/gtest-internal-inl.h" | #include "src/gtest-internal-inl.h" | |||
namespace posix = ::testing::internal::posix; | namespace posix = ::testing::internal::posix; | |||
using testing::ContainsRegex; | using testing::ContainsRegex; | |||
using testing::Matcher; | using testing::Matcher; | |||
using testing::Message; | using testing::Message; | |||
using testing::internal::DeathTest; | using testing::internal::DeathTest; | |||
using testing::internal::DeathTestFactory; | using testing::internal::DeathTestFactory; | |||
using testing::internal::FilePath; | using testing::internal::FilePath; | |||
using testing::internal::GetLastErrnoDescription; | using testing::internal::GetLastErrnoDescription; | |||
skipping to change at line 93 | skipping to change at line 92 | |||
explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory) | explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory) | |||
: unit_test_impl_(GetUnitTestImpl()) { | : unit_test_impl_(GetUnitTestImpl()) { | |||
old_factory_ = unit_test_impl_->death_test_factory_.release(); | old_factory_ = unit_test_impl_->death_test_factory_.release(); | |||
unit_test_impl_->death_test_factory_.reset(new_factory); | unit_test_impl_->death_test_factory_.reset(new_factory); | |||
} | } | |||
~ReplaceDeathTestFactory() { | ~ReplaceDeathTestFactory() { | |||
unit_test_impl_->death_test_factory_.release(); | unit_test_impl_->death_test_factory_.release(); | |||
unit_test_impl_->death_test_factory_.reset(old_factory_); | unit_test_impl_->death_test_factory_.reset(old_factory_); | |||
} | } | |||
private: | private: | |||
// Prevents copying ReplaceDeathTestFactory objects. | // Prevents copying ReplaceDeathTestFactory objects. | |||
ReplaceDeathTestFactory(const ReplaceDeathTestFactory&); | ReplaceDeathTestFactory(const ReplaceDeathTestFactory&); | |||
void operator=(const ReplaceDeathTestFactory&); | void operator=(const ReplaceDeathTestFactory&); | |||
UnitTestImpl* unit_test_impl_; | UnitTestImpl* unit_test_impl_; | |||
DeathTestFactory* old_factory_; | DeathTestFactory* old_factory_; | |||
}; | }; | |||
} // namespace internal | } // namespace internal | |||
skipping to change at line 119 | skipping to change at line 119 | |||
fflush(stderr); // Make sure the text is printed before the process exits. | fflush(stderr); // Make sure the text is printed before the process exits. | |||
// We call _exit() instead of exit(), as the former is a direct | // We call _exit() instead of exit(), as the former is a direct | |||
// system call and thus safer in the presence of threads. exit() | // system call and thus safer in the presence of threads. exit() | |||
// will invoke user-defined exit-hooks, which may do dangerous | // will invoke user-defined exit-hooks, which may do dangerous | |||
// things that conflict with death tests. | // things that conflict with death tests. | |||
// | // | |||
// Some compilers can recognize that _exit() never returns and issue the | // Some compilers can recognize that _exit() never returns and issue the | |||
// 'unreachable code' warning for code following this function, unless | // 'unreachable code' warning for code following this function, unless | |||
// fooled by a fake condition. | // fooled by a fake condition. | |||
if (AlwaysTrue()) | if (AlwaysTrue()) _exit(1); | |||
_exit(1); | ||||
} | } | |||
void DieInside(const ::std::string& function) { | void DieInside(const ::std::string& function) { | |||
DieWithMessage("death inside " + function + "()."); | DieWithMessage("death inside " + function + "()."); | |||
} | } | |||
// Tests that death tests work. | // Tests that death tests work. | |||
class TestForDeathTest : public testing::Test { | class TestForDeathTest : public testing::Test { | |||
protected: | protected: | |||
TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {} | TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {} | |||
~TestForDeathTest() override { posix::ChDir(original_dir_.c_str()); } | ~TestForDeathTest() override { posix::ChDir(original_dir_.c_str()); } | |||
// A static member function that's expected to die. | // A static member function that's expected to die. | |||
static void StaticMemberFunction() { DieInside("StaticMemberFunction"); } | static void StaticMemberFunction() { DieInside("StaticMemberFunction"); } | |||
// A method of the test fixture that may die. | // A method of the test fixture that may die. | |||
void MemberFunction() { | void MemberFunction() { | |||
if (should_die_) | if (should_die_) DieInside("MemberFunction"); | |||
DieInside("MemberFunction"); | ||||
} | } | |||
// True if and only if MemberFunction() should die. | // True if and only if MemberFunction() should die. | |||
bool should_die_; | bool should_die_; | |||
const FilePath original_dir_; | const FilePath original_dir_; | |||
}; | }; | |||
// A class with a member function that may die. | // A class with a member function that may die. | |||
class MayDie { | class MayDie { | |||
public: | public: | |||
explicit MayDie(bool should_die) : should_die_(should_die) {} | explicit MayDie(bool should_die) : should_die_(should_die) {} | |||
// A member function that may die. | // A member function that may die. | |||
void MemberFunction() const { | void MemberFunction() const { | |||
if (should_die_) | if (should_die_) DieInside("MayDie::MemberFunction"); | |||
DieInside("MayDie::MemberFunction"); | ||||
} | } | |||
private: | private: | |||
// True if and only if MemberFunction() should die. | // True if and only if MemberFunction() should die. | |||
bool should_die_; | bool should_die_; | |||
}; | }; | |||
// A global function that's expected to die. | // A global function that's expected to die. | |||
void GlobalFunction() { DieInside("GlobalFunction"); } | void GlobalFunction() { DieInside("GlobalFunction"); } | |||
// A non-void function that's expected to die. | // A non-void function that's expected to die. | |||
int NonVoidFunction() { | int NonVoidFunction() { | |||
DieInside("NonVoidFunction"); | DieInside("NonVoidFunction"); | |||
return 1; | return 1; | |||
} | } | |||
// A unary function that may die. | // A unary function that may die. | |||
void DieIf(bool should_die) { | void DieIf(bool should_die) { | |||
if (should_die) | if (should_die) DieInside("DieIf"); | |||
DieInside("DieIf"); | ||||
} | } | |||
// A binary function that may die. | // A binary function that may die. | |||
bool DieIfLessThan(int x, int y) { | bool DieIfLessThan(int x, int y) { | |||
if (x < y) { | if (x < y) { | |||
DieInside("DieIfLessThan"); | DieInside("DieIfLessThan"); | |||
} | } | |||
return true; | return true; | |||
} | } | |||
// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. | // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. | |||
void DeathTestSubroutine() { | void DeathTestSubroutine() { | |||
EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction"); | EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction"); | |||
ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction"); | ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction"); | |||
} | } | |||
// Death in dbg, not opt. | // Death in dbg, not opt. | |||
int DieInDebugElse12(int* sideeffect) { | int DieInDebugElse12(int* sideeffect) { | |||
if (sideeffect) *sideeffect = 12; | if (sideeffect) *sideeffect = 12; | |||
# ifndef NDEBUG | #ifndef NDEBUG | |||
DieInside("DieInDebugElse12"); | DieInside("DieInDebugElse12"); | |||
# endif // NDEBUG | #endif // NDEBUG | |||
return 12; | return 12; | |||
} | } | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
// Death in dbg due to Windows CRT assertion failure, not opt. | // Death in dbg due to Windows CRT assertion failure, not opt. | |||
int DieInCRTDebugElse12(int* sideeffect) { | int DieInCRTDebugElse12(int* sideeffect) { | |||
if (sideeffect) *sideeffect = 12; | if (sideeffect) *sideeffect = 12; | |||
// Create an invalid fd by closing a valid one | // Create an invalid fd by closing a valid one | |||
int fdpipe[2]; | int fdpipe[2]; | |||
EXPECT_EQ(_pipe(fdpipe, 256, O_BINARY), 0); | EXPECT_EQ(_pipe(fdpipe, 256, O_BINARY), 0); | |||
EXPECT_EQ(_close(fdpipe[0]), 0); | EXPECT_EQ(_close(fdpipe[0]), 0); | |||
EXPECT_EQ(_close(fdpipe[1]), 0); | EXPECT_EQ(_close(fdpipe[1]), 0); | |||
// _dup() should crash in debug mode | // _dup() should crash in debug mode | |||
EXPECT_EQ(_dup(fdpipe[0]), -1); | EXPECT_EQ(_dup(fdpipe[0]), -1); | |||
return 12; | return 12; | |||
} | } | |||
#endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA | #if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA | |||
// Tests the ExitedWithCode predicate. | // Tests the ExitedWithCode predicate. | |||
TEST(ExitStatusPredicateTest, ExitedWithCode) { | TEST(ExitStatusPredicateTest, ExitedWithCode) { | |||
// On Windows, the process's exit code is the same as its exit status, | // On Windows, the process's exit code is the same as its exit status, | |||
// so the predicate just compares the its input with its parameter. | // so the predicate just compares the its input with its parameter. | |||
EXPECT_TRUE(testing::ExitedWithCode(0)(0)); | EXPECT_TRUE(testing::ExitedWithCode(0)(0)); | |||
EXPECT_TRUE(testing::ExitedWithCode(1)(1)); | EXPECT_TRUE(testing::ExitedWithCode(1)(1)); | |||
EXPECT_TRUE(testing::ExitedWithCode(42)(42)); | EXPECT_TRUE(testing::ExitedWithCode(42)(42)); | |||
EXPECT_FALSE(testing::ExitedWithCode(0)(1)); | EXPECT_FALSE(testing::ExitedWithCode(0)(1)); | |||
EXPECT_FALSE(testing::ExitedWithCode(1)(0)); | EXPECT_FALSE(testing::ExitedWithCode(1)(0)); | |||
} | } | |||
# else | #else | |||
// Returns the exit status of a process that calls _exit(2) with a | // Returns the exit status of a process that calls _exit(2) with a | |||
// given exit code. This is a helper function for the | // given exit code. This is a helper function for the | |||
// ExitStatusPredicateTest test suite. | // ExitStatusPredicateTest test suite. | |||
static int NormalExitStatus(int exit_code) { | static int NormalExitStatus(int exit_code) { | |||
pid_t child_pid = fork(); | pid_t child_pid = fork(); | |||
if (child_pid == 0) { | if (child_pid == 0) { | |||
_exit(exit_code); | _exit(exit_code); | |||
} | } | |||
int status; | int status; | |||
skipping to change at line 273 | skipping to change at line 269 | |||
raise(signum); | raise(signum); | |||
_exit(1); | _exit(1); | |||
} | } | |||
int status; | int status; | |||
waitpid(child_pid, &status, 0); | waitpid(child_pid, &status, 0); | |||
return status; | return status; | |||
} | } | |||
// Tests the ExitedWithCode predicate. | // Tests the ExitedWithCode predicate. | |||
TEST(ExitStatusPredicateTest, ExitedWithCode) { | TEST(ExitStatusPredicateTest, ExitedWithCode) { | |||
const int status0 = NormalExitStatus(0); | const int status0 = NormalExitStatus(0); | |||
const int status1 = NormalExitStatus(1); | const int status1 = NormalExitStatus(1); | |||
const int status42 = NormalExitStatus(42); | const int status42 = NormalExitStatus(42); | |||
const testing::ExitedWithCode pred0(0); | const testing::ExitedWithCode pred0(0); | |||
const testing::ExitedWithCode pred1(1); | const testing::ExitedWithCode pred1(1); | |||
const testing::ExitedWithCode pred42(42); | const testing::ExitedWithCode pred42(42); | |||
EXPECT_PRED1(pred0, status0); | EXPECT_PRED1(pred0, status0); | |||
EXPECT_PRED1(pred1, status1); | EXPECT_PRED1(pred1, status1); | |||
EXPECT_PRED1(pred42, status42); | EXPECT_PRED1(pred42, status42); | |||
EXPECT_FALSE(pred0(status1)); | EXPECT_FALSE(pred0(status1)); | |||
EXPECT_FALSE(pred42(status0)); | EXPECT_FALSE(pred42(status0)); | |||
EXPECT_FALSE(pred1(status42)); | EXPECT_FALSE(pred1(status42)); | |||
} | } | |||
// Tests the KilledBySignal predicate. | // Tests the KilledBySignal predicate. | |||
TEST(ExitStatusPredicateTest, KilledBySignal) { | TEST(ExitStatusPredicateTest, KilledBySignal) { | |||
const int status_segv = KilledExitStatus(SIGSEGV); | const int status_segv = KilledExitStatus(SIGSEGV); | |||
const int status_kill = KilledExitStatus(SIGKILL); | const int status_kill = KilledExitStatus(SIGKILL); | |||
const testing::KilledBySignal pred_segv(SIGSEGV); | const testing::KilledBySignal pred_segv(SIGSEGV); | |||
const testing::KilledBySignal pred_kill(SIGKILL); | const testing::KilledBySignal pred_kill(SIGKILL); | |||
EXPECT_PRED1(pred_segv, status_segv); | EXPECT_PRED1(pred_segv, status_segv); | |||
EXPECT_PRED1(pred_kill, status_kill); | EXPECT_PRED1(pred_kill, status_kill); | |||
EXPECT_FALSE(pred_segv(status_kill)); | EXPECT_FALSE(pred_segv(status_kill)); | |||
EXPECT_FALSE(pred_kill(status_segv)); | EXPECT_FALSE(pred_kill(status_segv)); | |||
} | } | |||
# endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA | #endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA | |||
// The following code intentionally tests a suboptimal syntax. | // The following code intentionally tests a suboptimal syntax. | |||
#ifdef __GNUC__ | #ifdef __GNUC__ | |||
#pragma GCC diagnostic push | #pragma GCC diagnostic push | |||
#pragma GCC diagnostic ignored "-Wdangling-else" | #pragma GCC diagnostic ignored "-Wdangling-else" | |||
#pragma GCC diagnostic ignored "-Wempty-body" | #pragma GCC diagnostic ignored "-Wempty-body" | |||
#pragma GCC diagnostic ignored "-Wpragmas" | #pragma GCC diagnostic ignored "-Wpragmas" | |||
#endif | #endif | |||
// Tests that the death test macros expand to code which may or may not | // 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 | // be followed by operator<<, and that in either case the complete text | |||
skipping to change at line 323 | skipping to change at line 319 | |||
// This would fail if executed; this is a compilation test only | // This would fail if executed; this is a compilation test only | |||
ASSERT_DEATH(return, ""); | ASSERT_DEATH(return, ""); | |||
if (AlwaysTrue()) | if (AlwaysTrue()) | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
else | else | |||
// This empty "else" branch is meant to ensure that EXPECT_DEATH | // This empty "else" branch is meant to ensure that EXPECT_DEATH | |||
// doesn't expand into an "if" statement without an "else" | // doesn't expand into an "if" statement without an "else" | |||
; | ; | |||
if (AlwaysFalse()) | if (AlwaysFalse()) ASSERT_DEATH(return, "") << "did not die"; | |||
ASSERT_DEATH(return, "") << "did not die"; | ||||
if (AlwaysFalse()) | if (AlwaysFalse()) | |||
; | ; | |||
else | else | |||
EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3; | EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3; | |||
} | } | |||
#ifdef __GNUC__ | #ifdef __GNUC__ | |||
#pragma GCC diagnostic pop | #pragma GCC diagnostic pop | |||
#endif | #endif | |||
# if GTEST_USES_PCRE | #if GTEST_USES_PCRE | |||
void DieWithEmbeddedNul() { | void DieWithEmbeddedNul() { | |||
fprintf(stderr, "Hello%cmy null world.\n", '\0'); | fprintf(stderr, "Hello%cmy null world.\n", '\0'); | |||
fflush(stderr); | fflush(stderr); | |||
_exit(1); | _exit(1); | |||
} | } | |||
// Tests that EXPECT_DEATH and ASSERT_DEATH work when the error | // Tests that EXPECT_DEATH and ASSERT_DEATH work when the error | |||
// message has a NUL character in it. | // message has a NUL character in it. | |||
TEST_F(TestForDeathTest, EmbeddedNulInMessage) { | TEST_F(TestForDeathTest, EmbeddedNulInMessage) { | |||
EXPECT_DEATH(DieWithEmbeddedNul(), "my null world"); | EXPECT_DEATH(DieWithEmbeddedNul(), "my null world"); | |||
ASSERT_DEATH(DieWithEmbeddedNul(), "my null world"); | ASSERT_DEATH(DieWithEmbeddedNul(), "my null world"); | |||
} | } | |||
# endif // GTEST_USES_PCRE | #endif // GTEST_USES_PCRE | |||
// Tests that death test macros expand to code which interacts well with switch | // Tests that death test macros expand to code which interacts well with switch | |||
// statements. | // statements. | |||
TEST_F(TestForDeathTest, SwitchStatement) { | TEST_F(TestForDeathTest, SwitchStatement) { | |||
// Microsoft compiler usually complains about switch statements without | // Microsoft compiler usually complains about switch statements without | |||
// case labels. We suppress that warning for this test. | // case labels. We suppress that warning for this test. | |||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065) | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065) | |||
switch (0) | switch (0) | |||
default: | default: | |||
ASSERT_DEATH(_exit(1), "") << "exit in default switch handler"; | ASSERT_DEATH(_exit(1), "") << "exit in default switch handler"; | |||
switch (0) | switch (0) | |||
case 0: | case 0: | |||
EXPECT_DEATH(_exit(1), "") << "exit in switch case"; | EXPECT_DEATH(_exit(1), "") << "exit in switch case"; | |||
GTEST_DISABLE_MSC_WARNINGS_POP_() | GTEST_DISABLE_MSC_WARNINGS_POP_() | |||
} | } | |||
// Tests that a static member function can be used in a "fast" style | // Tests that a static member function can be used in a "fast" style | |||
// death test. | // death test. | |||
TEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) { | TEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); | ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); | |||
} | } | |||
// Tests that a method of the test fixture can be used in a "fast" | // Tests that a method of the test fixture can be used in a "fast" | |||
// style death test. | // style death test. | |||
TEST_F(TestForDeathTest, MemberFunctionFastStyle) { | TEST_F(TestForDeathTest, MemberFunctionFastStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
should_die_ = true; | should_die_ = true; | |||
EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); | EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); | |||
} | } | |||
void ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); } | void ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); } | |||
// Tests that death tests work even if the current directory has been | // Tests that death tests work even if the current directory has been | |||
// changed. | // changed. | |||
TEST_F(TestForDeathTest, FastDeathTestInChangedDir) { | TEST_F(TestForDeathTest, FastDeathTestInChangedDir) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
ChangeToRootDir(); | ChangeToRootDir(); | |||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | |||
ChangeToRootDir(); | ChangeToRootDir(); | |||
ASSERT_DEATH(_exit(1), ""); | ASSERT_DEATH(_exit(1), ""); | |||
} | } | |||
# if GTEST_OS_LINUX | #if GTEST_OS_LINUX | |||
void SigprofAction(int, siginfo_t*, void*) { /* no op */ } | void SigprofAction(int, siginfo_t*, void*) { /* no op */ | |||
} | ||||
// Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms). | // Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms). | |||
void SetSigprofActionAndTimer() { | void SetSigprofActionAndTimer() { | |||
struct sigaction signal_action; | struct sigaction signal_action; | |||
memset(&signal_action, 0, sizeof(signal_action)); | memset(&signal_action, 0, sizeof(signal_action)); | |||
sigemptyset(&signal_action.sa_mask); | sigemptyset(&signal_action.sa_mask); | |||
signal_action.sa_sigaction = SigprofAction; | signal_action.sa_sigaction = SigprofAction; | |||
signal_action.sa_flags = SA_RESTART | SA_SIGINFO; | signal_action.sa_flags = SA_RESTART | SA_SIGINFO; | |||
ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, nullptr)); | ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, nullptr)); | |||
// timer comes second, to avoid SIGPROF premature delivery, as suggested at | // timer comes second, to avoid SIGPROF premature delivery, as suggested at | |||
skipping to change at line 435 | skipping to change at line 431 | |||
ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, nullptr)); | ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer, nullptr)); | |||
struct sigaction signal_action; | struct sigaction signal_action; | |||
memset(&signal_action, 0, sizeof(signal_action)); | memset(&signal_action, 0, sizeof(signal_action)); | |||
sigemptyset(&signal_action.sa_mask); | sigemptyset(&signal_action.sa_mask); | |||
signal_action.sa_handler = SIG_IGN; | signal_action.sa_handler = SIG_IGN; | |||
ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action)); | ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action)); | |||
} | } | |||
// Tests that death tests work when SIGPROF handler and timer are set. | // Tests that death tests work when SIGPROF handler and timer are set. | |||
TEST_F(TestForDeathTest, FastSigprofActionSet) { | TEST_F(TestForDeathTest, FastSigprofActionSet) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
SetSigprofActionAndTimer(); | SetSigprofActionAndTimer(); | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
struct sigaction old_signal_action; | struct sigaction old_signal_action; | |||
DisableSigprofActionAndTimer(&old_signal_action); | DisableSigprofActionAndTimer(&old_signal_action); | |||
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); | EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); | |||
} | } | |||
TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) { | TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
SetSigprofActionAndTimer(); | SetSigprofActionAndTimer(); | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
struct sigaction old_signal_action; | struct sigaction old_signal_action; | |||
DisableSigprofActionAndTimer(&old_signal_action); | DisableSigprofActionAndTimer(&old_signal_action); | |||
EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); | EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction); | |||
} | } | |||
# endif // GTEST_OS_LINUX | #endif // GTEST_OS_LINUX | |||
// Repeats a representative sample of death tests in the "threadsafe" style: | // Repeats a representative sample of death tests in the "threadsafe" style: | |||
TEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) { | TEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); | ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember"); | |||
} | } | |||
TEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) { | TEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
should_die_ = true; | should_die_ = true; | |||
EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); | EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction"); | |||
} | } | |||
TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) { | TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
for (int i = 0; i < 3; ++i) | for (int i = 0; i < 3; ++i) | |||
EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i; | EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i; | |||
} | } | |||
TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) { | TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
ChangeToRootDir(); | ChangeToRootDir(); | |||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | |||
ChangeToRootDir(); | ChangeToRootDir(); | |||
ASSERT_DEATH(_exit(1), ""); | ASSERT_DEATH(_exit(1), ""); | |||
} | } | |||
TEST_F(TestForDeathTest, MixedStyles) { | TEST_F(TestForDeathTest, MixedStyles) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
} | } | |||
# if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD | #if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD | |||
bool pthread_flag; | bool pthread_flag; | |||
void SetPthreadFlag() { | void SetPthreadFlag() { pthread_flag = true; } | |||
pthread_flag = true; | ||||
} | ||||
TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) { | TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) { | |||
if (!testing::GTEST_FLAG(death_test_use_fork)) { | if (!GTEST_FLAG_GET(death_test_use_fork)) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
pthread_flag = false; | pthread_flag = false; | |||
ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr)); | ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, nullptr, nullptr)); | |||
ASSERT_DEATH(_exit(1), ""); | ASSERT_DEATH(_exit(1), ""); | |||
ASSERT_FALSE(pthread_flag); | ASSERT_FALSE(pthread_flag); | |||
} | } | |||
} | } | |||
# endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD | #endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD | |||
// Tests that a method of another class can be used in a death test. | // Tests that a method of another class can be used in a death test. | |||
TEST_F(TestForDeathTest, MethodOfAnotherClass) { | TEST_F(TestForDeathTest, MethodOfAnotherClass) { | |||
const MayDie x(true); | const MayDie x(true); | |||
ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction"); | ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction"); | |||
} | } | |||
// Tests that a global function can be used in a death test. | // Tests that a global function can be used in a death test. | |||
TEST_F(TestForDeathTest, GlobalFunction) { | TEST_F(TestForDeathTest, GlobalFunction) { | |||
EXPECT_DEATH(GlobalFunction(), "GlobalFunction"); | EXPECT_DEATH(GlobalFunction(), "GlobalFunction"); | |||
skipping to change at line 530 | skipping to change at line 524 | |||
// Tests that any value convertible to an RE works as a second | // Tests that any value convertible to an RE works as a second | |||
// argument to EXPECT_DEATH. | // argument to EXPECT_DEATH. | |||
TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) { | TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) { | |||
static const char regex_c_str[] = "GlobalFunction"; | static const char regex_c_str[] = "GlobalFunction"; | |||
EXPECT_DEATH(GlobalFunction(), regex_c_str); | EXPECT_DEATH(GlobalFunction(), regex_c_str); | |||
const testing::internal::RE regex(regex_c_str); | const testing::internal::RE regex(regex_c_str); | |||
EXPECT_DEATH(GlobalFunction(), regex); | EXPECT_DEATH(GlobalFunction(), regex); | |||
# if !GTEST_USES_PCRE | #if !GTEST_USES_PCRE | |||
const ::std::string regex_std_str(regex_c_str); | const ::std::string regex_std_str(regex_c_str); | |||
EXPECT_DEATH(GlobalFunction(), regex_std_str); | EXPECT_DEATH(GlobalFunction(), regex_std_str); | |||
// This one is tricky; a temporary pointer into another temporary. Reference | // This one is tricky; a temporary pointer into another temporary. Reference | |||
// lifetime extension of the pointer is not sufficient. | // lifetime extension of the pointer is not sufficient. | |||
EXPECT_DEATH(GlobalFunction(), ::std::string(regex_c_str).c_str()); | EXPECT_DEATH(GlobalFunction(), ::std::string(regex_c_str).c_str()); | |||
# endif // !GTEST_USES_PCRE | #endif // !GTEST_USES_PCRE | |||
} | } | |||
// Tests that a non-void function can be used in a death test. | // Tests that a non-void function can be used in a death test. | |||
TEST_F(TestForDeathTest, NonVoidFunction) { | TEST_F(TestForDeathTest, NonVoidFunction) { | |||
ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction"); | ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction"); | |||
} | } | |||
// Tests that functions that take parameter(s) can be used in a death test. | // Tests that functions that take parameter(s) can be used in a death test. | |||
TEST_F(TestForDeathTest, FunctionWithParameter) { | TEST_F(TestForDeathTest, FunctionWithParameter) { | |||
EXPECT_DEATH(DieIf(true), "DieIf\\(\\)"); | EXPECT_DEATH(DieIf(true), "DieIf\\(\\)"); | |||
EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan"); | EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan"); | |||
} | } | |||
// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. | // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture. | |||
TEST_F(TestForDeathTest, OutsideFixture) { | TEST_F(TestForDeathTest, OutsideFixture) { DeathTestSubroutine(); } | |||
DeathTestSubroutine(); | ||||
} | ||||
// Tests that death tests can be done inside a loop. | // Tests that death tests can be done inside a loop. | |||
TEST_F(TestForDeathTest, InsideLoop) { | TEST_F(TestForDeathTest, InsideLoop) { | |||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | |||
EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i; | EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i; | |||
} | } | |||
} | } | |||
// Tests that a compound statement can be used in a death test. | // Tests that a compound statement can be used in a death test. | |||
TEST_F(TestForDeathTest, CompoundStatement) { | TEST_F(TestForDeathTest, CompoundStatement) { | |||
EXPECT_DEATH({ // NOLINT | EXPECT_DEATH( | |||
const int x = 2; | { // NOLINT | |||
const int y = x + 1; | const int x = 2; | |||
DieIfLessThan(x, y); | const int y = x + 1; | |||
}, | DieIfLessThan(x, y); | |||
"DieIfLessThan"); | }, | |||
"DieIfLessThan"); | ||||
} | } | |||
// Tests that code that doesn't die causes a death test to fail. | // Tests that code that doesn't die causes a death test to fail. | |||
TEST_F(TestForDeathTest, DoesNotDie) { | TEST_F(TestForDeathTest, DoesNotDie) { | |||
EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"), | EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"), "failed to die"); | |||
"failed to die"); | ||||
} | } | |||
// Tests that a death test fails when the error message isn't expected. | // Tests that a death test fails when the error message isn't expected. | |||
TEST_F(TestForDeathTest, ErrorMessageMismatch) { | TEST_F(TestForDeathTest, ErrorMessageMismatch) { | |||
EXPECT_NONFATAL_FAILURE({ // NOLINT | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_DEATH(DieIf(true), "DieIfLessThan") << "End of death test message."; | { // NOLINT | |||
}, "died but not with expected error"); | EXPECT_DEATH(DieIf(true), "DieIfLessThan") | |||
<< "End of death test message."; | ||||
}, | ||||
"died but not with expected error"); | ||||
} | } | |||
// On exit, *aborted will be true if and only if the EXPECT_DEATH() | // On exit, *aborted will be true if and only if the EXPECT_DEATH() | |||
// statement aborted the function. | // statement aborted the function. | |||
void ExpectDeathTestHelper(bool* aborted) { | void ExpectDeathTestHelper(bool* aborted) { | |||
*aborted = true; | *aborted = true; | |||
EXPECT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. | EXPECT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. | |||
*aborted = false; | *aborted = false; | |||
} | } | |||
// Tests that EXPECT_DEATH doesn't abort the test on failure. | // Tests that EXPECT_DEATH doesn't abort the test on failure. | |||
TEST_F(TestForDeathTest, EXPECT_DEATH) { | TEST_F(TestForDeathTest, EXPECT_DEATH) { | |||
bool aborted = true; | bool aborted = true; | |||
EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted), | EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted), "failed to die"); | |||
"failed to die"); | ||||
EXPECT_FALSE(aborted); | EXPECT_FALSE(aborted); | |||
} | } | |||
// Tests that ASSERT_DEATH does abort the test on failure. | // Tests that ASSERT_DEATH does abort the test on failure. | |||
TEST_F(TestForDeathTest, ASSERT_DEATH) { | TEST_F(TestForDeathTest, ASSERT_DEATH) { | |||
static bool aborted; | static bool aborted; | |||
EXPECT_FATAL_FAILURE({ // NOLINT | EXPECT_FATAL_FAILURE( | |||
aborted = true; | { // NOLINT | |||
ASSERT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. | aborted = true; | |||
aborted = false; | ASSERT_DEATH(DieIf(false), "DieIf"); // This assertion should fail. | |||
}, "failed to die"); | aborted = false; | |||
}, | ||||
"failed to die"); | ||||
EXPECT_TRUE(aborted); | EXPECT_TRUE(aborted); | |||
} | } | |||
// Tests that EXPECT_DEATH evaluates the arguments exactly once. | // Tests that EXPECT_DEATH evaluates the arguments exactly once. | |||
TEST_F(TestForDeathTest, SingleEvaluation) { | TEST_F(TestForDeathTest, SingleEvaluation) { | |||
int x = 3; | int x = 3; | |||
EXPECT_DEATH(DieIf((++x) == 4), "DieIf"); | EXPECT_DEATH(DieIf((++x) == 4), "DieIf"); | |||
const char* regex = "DieIf"; | const char* regex = "DieIf"; | |||
const char* regex_save = regex; | const char* regex_save = regex; | |||
skipping to change at line 656 | skipping to change at line 652 | |||
TEST_F(TestForDeathTest, TestExpectDebugDeath) { | TEST_F(TestForDeathTest, TestExpectDebugDeath) { | |||
int sideeffect = 0; | int sideeffect = 0; | |||
// Put the regex in a local variable to make sure we don't get an "unused" | // Put the regex in a local variable to make sure we don't get an "unused" | |||
// warning in opt mode. | // warning in opt mode. | |||
const char* regex = "death.*DieInDebugElse12"; | const char* regex = "death.*DieInDebugElse12"; | |||
EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), regex) | EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), regex) | |||
<< "Must accept a streamed message"; | << "Must accept a streamed message"; | |||
# ifdef NDEBUG | #ifdef NDEBUG | |||
// Checks that the assignment occurs in opt mode (sideeffect). | // Checks that the assignment occurs in opt mode (sideeffect). | |||
EXPECT_EQ(12, sideeffect); | EXPECT_EQ(12, sideeffect); | |||
# else | #else | |||
// Checks that the assignment does not occur in dbg mode (no sideeffect). | // Checks that the assignment does not occur in dbg mode (no sideeffect). | |||
EXPECT_EQ(0, sideeffect); | EXPECT_EQ(0, sideeffect); | |||
# endif | #endif | |||
} | } | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
// Tests that EXPECT_DEBUG_DEATH works as expected when in debug mode | // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetreport | |||
// the Windows CRT crashes the process with an assertion failure. | mode | |||
// In debug mode, the calls to _CrtSetReportMode and _CrtSetReportFile enable | ||||
// the dumping of assertions to stderr. Tests that EXPECT_DEATH works as | ||||
// expected when in CRT debug mode (compiled with /MTd or /MDd, which defines | ||||
// _DEBUG) the Windows CRT crashes the process with an assertion failure. | ||||
// 1. Asserts on death. | // 1. Asserts on death. | |||
// 2. Has no side effect (doesn't pop up a window or wait for user input). | // 2. Has no side effect (doesn't pop up a window or wait for user input). | |||
// | #ifdef _DEBUG | |||
// And in opt mode, it: | ||||
// 1. Has side effects but does not assert. | ||||
TEST_F(TestForDeathTest, CRTDebugDeath) { | TEST_F(TestForDeathTest, CRTDebugDeath) { | |||
int sideeffect = 0; | EXPECT_DEATH(DieInCRTDebugElse12(nullptr), "dup.* : Assertion failed") | |||
// Put the regex in a local variable to make sure we don't get an "unused" | ||||
// warning in opt mode. | ||||
const char* regex = "dup.* : Assertion failed"; | ||||
EXPECT_DEBUG_DEATH(DieInCRTDebugElse12(&sideeffect), regex) | ||||
<< "Must accept a streamed message"; | << "Must accept a streamed message"; | |||
# ifdef NDEBUG | ||||
// Checks that the assignment occurs in opt mode (sideeffect). | ||||
EXPECT_EQ(12, sideeffect); | ||||
# else | ||||
// Checks that the assignment does not occur in dbg mode (no sideeffect). | ||||
EXPECT_EQ(0, sideeffect); | ||||
# endif | ||||
} | } | |||
#endif // _DEBUG | ||||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
// Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a | // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a | |||
// message to it, and 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. | |||
// | // | |||
// And in opt mode, it: | // And in opt mode, it: | |||
// 1. Has side effects but does not assert. | // 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(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") | ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death.*DieInDebugElse12") | |||
<< "Must accept a streamed message"; | << "Must accept a streamed message"; | |||
# ifdef NDEBUG | #ifdef NDEBUG | |||
// Checks that the assignment occurs in opt mode (sideeffect). | // Checks that the assignment occurs in opt mode (sideeffect). | |||
EXPECT_EQ(12, sideeffect); | EXPECT_EQ(12, sideeffect); | |||
# else | #else | |||
// Checks that the assignment does not occur in dbg mode (no sideeffect). | // Checks that the assignment does not occur in dbg mode (no sideeffect). | |||
EXPECT_EQ(0, sideeffect); | EXPECT_EQ(0, sideeffect); | |||
# endif | #endif | |||
} | } | |||
# ifndef NDEBUG | #ifndef NDEBUG | |||
void ExpectDebugDeathHelper(bool* aborted) { | void ExpectDebugDeathHelper(bool* aborted) { | |||
*aborted = true; | *aborted = true; | |||
EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail."; | EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail."; | |||
*aborted = false; | *aborted = false; | |||
} | } | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
TEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) { | TEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) { | |||
printf("This test should be considered failing if it shows " | printf( | |||
"any pop-up dialogs.\n"); | "This test should be considered failing if it shows " | |||
"any pop-up dialogs.\n"); | ||||
fflush(stdout); | fflush(stdout); | |||
EXPECT_DEATH({ | EXPECT_DEATH( | |||
testing::GTEST_FLAG(catch_exceptions) = false; | { | |||
abort(); | GTEST_FLAG_SET(catch_exceptions, false); | |||
}, ""); | abort(); | |||
}, | ||||
""); | ||||
} | } | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
// Tests that EXPECT_DEBUG_DEATH in debug mode does not abort | // Tests that EXPECT_DEBUG_DEATH in debug mode does not abort | |||
// the function. | // the function. | |||
TEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) { | TEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) { | |||
bool aborted = true; | bool aborted = true; | |||
EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), ""); | EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), ""); | |||
EXPECT_FALSE(aborted); | EXPECT_FALSE(aborted); | |||
} | } | |||
void AssertDebugDeathHelper(bool* aborted) { | void AssertDebugDeathHelper(bool* aborted) { | |||
skipping to change at line 839 | skipping to change at line 822 | |||
EXPECT_TRUE(aborted); | EXPECT_TRUE(aborted); | |||
} | } | |||
TEST_F(TestForDeathTest, AssertDebugDeathAborts10) { | TEST_F(TestForDeathTest, AssertDebugDeathAborts10) { | |||
static bool aborted; | static bool aborted; | |||
aborted = false; | aborted = false; | |||
EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), ""); | EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), ""); | |||
EXPECT_TRUE(aborted); | EXPECT_TRUE(aborted); | |||
} | } | |||
# endif // _NDEBUG | #endif // _NDEBUG | |||
// Tests the *_EXIT family of macros, using a variety of predicates. | // Tests the *_EXIT family of macros, using a variety of predicates. | |||
static void TestExitMacros() { | static void TestExitMacros() { | |||
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), ""); | |||
ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), ""); | ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), ""); | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
// 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 https://msdn.microsoft.com/en-us/query-bi/m/dwwzkt4c. | // See https://msdn.microsoft.com/en-us/query-bi/m/dwwzkt4c. | |||
EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar"; | EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "") << "b_ar"; | |||
# elif !GTEST_OS_FUCHSIA | #elif !GTEST_OS_FUCHSIA | |||
// Fuchsia has no unix signals. | // Fuchsia has no unix signals. | |||
EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo"; | EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo"; | |||
ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar"; | ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar"; | |||
EXPECT_FATAL_FAILURE({ // NOLINT | EXPECT_FATAL_FAILURE( | |||
ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "") | { // NOLINT | |||
<< "This failure is expected, too."; | 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 | #endif // GTEST_OS_WINDOWS | |||
EXPECT_NONFATAL_FAILURE({ // NOLINT | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "") | { // NOLINT | |||
<< "This failure is expected."; | EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "") | |||
}, "This failure is expected."); | << "This failure is expected."; | |||
}, | ||||
"This failure is expected."); | ||||
} | } | |||
TEST_F(TestForDeathTest, ExitMacros) { | TEST_F(TestForDeathTest, ExitMacros) { TestExitMacros(); } | |||
TestExitMacros(); | ||||
} | ||||
TEST_F(TestForDeathTest, ExitMacrosUsingFork) { | TEST_F(TestForDeathTest, ExitMacrosUsingFork) { | |||
testing::GTEST_FLAG(death_test_use_fork) = true; | GTEST_FLAG_SET(death_test_use_fork, true); | |||
TestExitMacros(); | TestExitMacros(); | |||
} | } | |||
TEST_F(TestForDeathTest, InvalidStyle) { | TEST_F(TestForDeathTest, InvalidStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "rococo"; | GTEST_FLAG_SET(death_test_style, "rococo"); | |||
EXPECT_NONFATAL_FAILURE({ // NOLINT | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_DEATH(_exit(0), "") << "This failure is expected."; | { // NOLINT | |||
}, "This failure is expected."); | EXPECT_DEATH(_exit(0), "") << "This failure is expected."; | |||
}, | ||||
"This failure is expected."); | ||||
} | } | |||
TEST_F(TestForDeathTest, DeathTestFailedOutput) { | TEST_F(TestForDeathTest, DeathTestFailedOutput) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_NONFATAL_FAILURE( | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_DEATH(DieWithMessage("death\n"), | EXPECT_DEATH(DieWithMessage("death\n"), "expected message"), | |||
"expected message"), | ||||
"Actual msg:\n" | "Actual msg:\n" | |||
"[ DEATH ] death\n"); | "[ DEATH ] death\n"); | |||
} | } | |||
TEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) { | TEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_NONFATAL_FAILURE( | EXPECT_NONFATAL_FAILURE(EXPECT_DEATH( | |||
EXPECT_DEATH({ | { | |||
fprintf(stderr, "returning\n"); | fprintf(stderr, "returning\n"); | |||
fflush(stderr); | fflush(stderr); | |||
return; | return; | |||
}, ""), | }, | |||
" Result: illegal return in test statement.\n" | ""), | |||
" Error msg:\n" | " Result: illegal return in test statement.\n" | |||
"[ DEATH ] returning\n"); | " Error msg:\n" | |||
"[ DEATH ] returning\n"); | ||||
} | } | |||
TEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) { | TEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_NONFATAL_FAILURE( | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"), | EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"), | |||
testing::ExitedWithCode(3), | testing::ExitedWithCode(3), "expected message"), | |||
"expected message"), | ||||
" Result: died but not with expected exit code:\n" | " Result: died but not with expected exit code:\n" | |||
" Exited with exit status 1\n" | " Exited with exit status 1\n" | |||
"Actual msg:\n" | "Actual msg:\n" | |||
"[ DEATH ] exiting with rc 1\n"); | "[ DEATH ] exiting with rc 1\n"); | |||
} | } | |||
TEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) { | TEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_NONFATAL_FAILURE( | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), | EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), | |||
"line 1\nxyz\nline 3\n"), | "line 1\nxyz\nline 3\n"), | |||
"Actual msg:\n" | "Actual msg:\n" | |||
"[ DEATH ] line 1\n" | "[ DEATH ] line 1\n" | |||
"[ DEATH ] line 2\n" | "[ DEATH ] line 2\n" | |||
"[ DEATH ] line 3\n"); | "[ DEATH ] line 3\n"); | |||
} | } | |||
TEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) { | TEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), | EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"), | |||
"line 1\nline 2\nline 3\n"); | "line 1\nline 2\nline 3\n"); | |||
} | } | |||
// A DeathTestFactory that returns MockDeathTests. | // A DeathTestFactory that returns MockDeathTests. | |||
class MockDeathTestFactory : public DeathTestFactory { | class MockDeathTestFactory : public DeathTestFactory { | |||
public: | public: | |||
MockDeathTestFactory(); | MockDeathTestFactory(); | |||
bool Create(const char* statement, | bool Create(const char* statement, | |||
testing::Matcher<const std::string&> matcher, const char* file, | testing::Matcher<const std::string&> matcher, const char* file, | |||
int line, DeathTest** test) override; | int line, DeathTest** test) override; | |||
// Sets the parameters for subsequent calls to Create. | // Sets the parameters for subsequent calls to Create. | |||
void SetParameters(bool create, DeathTest::TestRole role, | void SetParameters(bool create, DeathTest::TestRole role, int status, | |||
int status, bool passed); | bool passed); | |||
// Accessors. | // Accessors. | |||
int AssumeRoleCalls() const { return assume_role_calls_; } | int AssumeRoleCalls() const { return assume_role_calls_; } | |||
int WaitCalls() const { return wait_calls_; } | int WaitCalls() const { return wait_calls_; } | |||
size_t PassedCalls() const { return passed_args_.size(); } | size_t PassedCalls() const { return passed_args_.size(); } | |||
bool PassedArgument(int n) const { | bool PassedArgument(int n) const { | |||
return passed_args_[static_cast<size_t>(n)]; | return passed_args_[static_cast<size_t>(n)]; | |||
} | } | |||
size_t AbortCalls() const { return abort_args_.size(); } | size_t AbortCalls() const { return abort_args_.size(); } | |||
DeathTest::AbortReason AbortArgument(int n) const { | DeathTest::AbortReason AbortArgument(int n) const { | |||
skipping to change at line 997 | skipping to change at line 983 | |||
// deleted. | // deleted. | |||
bool test_deleted_; | bool test_deleted_; | |||
}; | }; | |||
// A DeathTest implementation useful in testing. It returns values set | // A DeathTest implementation useful in testing. It returns values set | |||
// at its creation from its various inherited DeathTest methods, and | // at its creation from its various inherited DeathTest methods, and | |||
// reports calls to those methods to its parent MockDeathTestFactory | // reports calls to those methods to its parent MockDeathTestFactory | |||
// object. | // object. | |||
class MockDeathTest : public DeathTest { | class MockDeathTest : public DeathTest { | |||
public: | public: | |||
MockDeathTest(MockDeathTestFactory *parent, | MockDeathTest(MockDeathTestFactory* parent, TestRole role, int status, | |||
TestRole role, int status, bool passed) : | bool passed) | |||
parent_(parent), role_(role), status_(status), passed_(passed) { | : parent_(parent), role_(role), status_(status), passed_(passed) {} | |||
} | ||||
~MockDeathTest() override { parent_->test_deleted_ = true; } | ~MockDeathTest() override { parent_->test_deleted_ = true; } | |||
TestRole AssumeRole() override { | TestRole AssumeRole() override { | |||
++parent_->assume_role_calls_; | ++parent_->assume_role_calls_; | |||
return role_; | return role_; | |||
} | } | |||
int Wait() override { | int Wait() override { | |||
++parent_->wait_calls_; | ++parent_->wait_calls_; | |||
return status_; | return status_; | |||
} | } | |||
bool Passed(bool exit_status_ok) override { | bool Passed(bool exit_status_ok) override { | |||
skipping to change at line 1034 | skipping to change at line 1019 | |||
// MockDeathTestFactory constructor. | // MockDeathTestFactory constructor. | |||
MockDeathTestFactory::MockDeathTestFactory() | MockDeathTestFactory::MockDeathTestFactory() | |||
: create_(true), | : create_(true), | |||
role_(DeathTest::OVERSEE_TEST), | role_(DeathTest::OVERSEE_TEST), | |||
status_(0), | status_(0), | |||
passed_(true), | passed_(true), | |||
assume_role_calls_(0), | assume_role_calls_(0), | |||
wait_calls_(0), | wait_calls_(0), | |||
passed_args_(), | passed_args_(), | |||
abort_args_() { | abort_args_() {} | |||
} | ||||
// Sets the parameters for subsequent calls to Create. | // Sets the parameters for subsequent calls to Create. | |||
void MockDeathTestFactory::SetParameters(bool create, | void MockDeathTestFactory::SetParameters(bool create, DeathTest::TestRole role, | |||
DeathTest::TestRole role, | ||||
int status, bool passed) { | int status, bool passed) { | |||
create_ = create; | create_ = create; | |||
role_ = role; | role_ = role; | |||
status_ = status; | status_ = status; | |||
passed_ = passed; | passed_ = passed; | |||
assume_role_calls_ = 0; | assume_role_calls_ = 0; | |||
wait_calls_ = 0; | wait_calls_ = 0; | |||
passed_args_.clear(); | passed_args_.clear(); | |||
abort_args_.clear(); | abort_args_.clear(); | |||
skipping to change at line 1091 | skipping to change at line 1074 | |||
delete replacer_; | delete replacer_; | |||
replacer_ = nullptr; | replacer_ = nullptr; | |||
delete factory_; | delete factory_; | |||
factory_ = nullptr; | factory_ = nullptr; | |||
} | } | |||
// Runs a death test that breaks the rules by returning. Such a death | // Runs a death test that breaks the rules by returning. Such a death | |||
// test cannot be run directly from a test routine that uses a | // test cannot be run directly from a test routine that uses a | |||
// MockDeathTest, or the remainder of the routine will not be executed. | // MockDeathTest, or the remainder of the routine will not be executed. | |||
static void RunReturningDeathTest(bool* flag) { | static void RunReturningDeathTest(bool* flag) { | |||
ASSERT_DEATH({ // NOLINT | ASSERT_DEATH( | |||
*flag = true; | { // NOLINT | |||
return; | *flag = true; | |||
}, ""); | return; | |||
}, | ||||
""); | ||||
} | } | |||
}; | }; | |||
testing::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_ = | testing::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_ = | |||
nullptr; | nullptr; | |||
MockDeathTestFactory* MacroLogicDeathTest::factory_ = nullptr; | MockDeathTestFactory* MacroLogicDeathTest::factory_ = nullptr; | |||
// Test that nothing happens when the factory doesn't return a DeathTest: | // Test that nothing happens when the factory doesn't return a DeathTest: | |||
TEST_F(MacroLogicDeathTest, NothingHappens) { | TEST_F(MacroLogicDeathTest, NothingHappens) { | |||
bool flag = false; | bool flag = false; | |||
skipping to change at line 1179 | skipping to change at line 1164 | |||
EXPECT_TRUE(flag); | EXPECT_TRUE(flag); | |||
EXPECT_EQ(1, factory_->AssumeRoleCalls()); | EXPECT_EQ(1, factory_->AssumeRoleCalls()); | |||
EXPECT_EQ(0, factory_->WaitCalls()); | EXPECT_EQ(0, factory_->WaitCalls()); | |||
EXPECT_EQ(0U, factory_->PassedCalls()); | EXPECT_EQ(0U, factory_->PassedCalls()); | |||
// This time there are two calls to Abort: one since the test didn't | // This time there are two calls to Abort: one since the test didn't | |||
// die, and another from the ReturnSentinel when it's destroyed. The | // die, and another from the ReturnSentinel when it's destroyed. The | |||
// sentinel normally isn't destroyed if a test doesn't die, since | // sentinel normally isn't destroyed if a test doesn't die, since | |||
// _exit(2) is called in that case by ForkingDeathTest, but not by | // _exit(2) is called in that case by ForkingDeathTest, but not by | |||
// our MockDeathTest. | // our MockDeathTest. | |||
ASSERT_EQ(2U, factory_->AbortCalls()); | ASSERT_EQ(2U, factory_->AbortCalls()); | |||
EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, | EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0)); | |||
factory_->AbortArgument(0)); | ||||
EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT, | EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT, | |||
factory_->AbortArgument(1)); | factory_->AbortArgument(1)); | |||
EXPECT_TRUE(factory_->TestDeleted()); | EXPECT_TRUE(factory_->TestDeleted()); | |||
} | } | |||
// Tests that a successful death test does not register a successful | // Tests that a successful death test does not register a successful | |||
// test part. | // test part. | |||
TEST(SuccessRegistrationDeathTest, NoSuccessPart) { | TEST(SuccessRegistrationDeathTest, NoSuccessPart) { | |||
EXPECT_DEATH(_exit(1), ""); | EXPECT_DEATH(_exit(1), ""); | |||
EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); | |||
} | } | |||
TEST(StreamingAssertionsDeathTest, DeathTest) { | TEST(StreamingAssertionsDeathTest, DeathTest) { | |||
EXPECT_DEATH(_exit(1), "") << "unexpected failure"; | EXPECT_DEATH(_exit(1), "") << "unexpected failure"; | |||
ASSERT_DEATH(_exit(1), "") << "unexpected failure"; | ASSERT_DEATH(_exit(1), "") << "unexpected failure"; | |||
EXPECT_NONFATAL_FAILURE({ // NOLINT | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_DEATH(_exit(0), "") << "expected failure"; | { // NOLINT | |||
}, "expected failure"); | EXPECT_DEATH(_exit(0), "") << "expected failure"; | |||
EXPECT_FATAL_FAILURE({ // NOLINT | }, | |||
ASSERT_DEATH(_exit(0), "") << "expected failure"; | "expected failure"); | |||
}, "expected failure"); | EXPECT_FATAL_FAILURE( | |||
{ // NOLINT | ||||
ASSERT_DEATH(_exit(0), "") << "expected failure"; | ||||
}, | ||||
"expected failure"); | ||||
} | } | |||
// Tests that GetLastErrnoDescription returns an empty string when the | // Tests that GetLastErrnoDescription returns an empty string when the | |||
// last error is 0 and non-empty string when it is non-zero. | // last error is 0 and non-empty string when it is non-zero. | |||
TEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) { | TEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) { | |||
errno = ENOENT; | errno = ENOENT; | |||
EXPECT_STRNE("", GetLastErrnoDescription().c_str()); | EXPECT_STRNE("", GetLastErrnoDescription().c_str()); | |||
errno = 0; | errno = 0; | |||
EXPECT_STREQ("", GetLastErrnoDescription().c_str()); | EXPECT_STREQ("", GetLastErrnoDescription().c_str()); | |||
} | } | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
TEST(AutoHandleTest, AutoHandleWorks) { | TEST(AutoHandleTest, AutoHandleWorks) { | |||
HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); | HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); | |||
ASSERT_NE(INVALID_HANDLE_VALUE, handle); | ASSERT_NE(INVALID_HANDLE_VALUE, handle); | |||
// Tests that the AutoHandle is correctly initialized with a handle. | // Tests that the AutoHandle is correctly initialized with a handle. | |||
testing::internal::AutoHandle auto_handle(handle); | testing::internal::AutoHandle auto_handle(handle); | |||
EXPECT_EQ(handle, auto_handle.Get()); | EXPECT_EQ(handle, auto_handle.Get()); | |||
// Tests that Reset assigns INVALID_HANDLE_VALUE. | // Tests that Reset assigns INVALID_HANDLE_VALUE. | |||
// Note that this cannot verify whether the original handle is closed. | // Note that this cannot verify whether the original handle is closed. | |||
skipping to change at line 1238 | skipping to change at line 1226 | |||
// Note that this cannot verify whether the original handle is closed. | // Note that this cannot verify whether the original handle is closed. | |||
handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); | handle = ::CreateEvent(NULL, FALSE, FALSE, NULL); | |||
ASSERT_NE(INVALID_HANDLE_VALUE, handle); | ASSERT_NE(INVALID_HANDLE_VALUE, handle); | |||
auto_handle.Reset(handle); | auto_handle.Reset(handle); | |||
EXPECT_EQ(handle, auto_handle.Get()); | EXPECT_EQ(handle, auto_handle.Get()); | |||
// Tests that AutoHandle contains INVALID_HANDLE_VALUE by default. | // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default. | |||
testing::internal::AutoHandle auto_handle2; | testing::internal::AutoHandle auto_handle2; | |||
EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get()); | EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get()); | |||
} | } | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
typedef unsigned __int64 BiggestParsable; | typedef unsigned __int64 BiggestParsable; | |||
typedef signed __int64 BiggestSignedParsable; | typedef signed __int64 BiggestSignedParsable; | |||
# else | #else | |||
typedef unsigned long long BiggestParsable; | typedef unsigned long long BiggestParsable; | |||
typedef signed long long BiggestSignedParsable; | typedef signed long long BiggestSignedParsable; | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
// We cannot use std::numeric_limits<T>::max() as it clashes with the | // We cannot use std::numeric_limits<T>::max() as it clashes with the | |||
// max() macro defined by <windows.h>. | // max() macro defined by <windows.h>. | |||
const BiggestParsable kBiggestParsableMax = ULLONG_MAX; | const BiggestParsable kBiggestParsableMax = ULLONG_MAX; | |||
const BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX; | const BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX; | |||
TEST(ParseNaturalNumberTest, RejectsInvalidFormat) { | TEST(ParseNaturalNumberTest, RejectsInvalidFormat) { | |||
BiggestParsable result = 0; | BiggestParsable result = 0; | |||
// Rejects non-numbers. | // Rejects non-numbers. | |||
skipping to change at line 1337 | skipping to change at line 1325 | |||
TEST(ParseNaturalNumberTest, WorksForShorterIntegers) { | TEST(ParseNaturalNumberTest, WorksForShorterIntegers) { | |||
short short_result = 0; | short short_result = 0; | |||
ASSERT_TRUE(ParseNaturalNumber("123", &short_result)); | ASSERT_TRUE(ParseNaturalNumber("123", &short_result)); | |||
EXPECT_EQ(123, short_result); | EXPECT_EQ(123, short_result); | |||
signed char char_result = 0; | signed char char_result = 0; | |||
ASSERT_TRUE(ParseNaturalNumber("123", &char_result)); | ASSERT_TRUE(ParseNaturalNumber("123", &char_result)); | |||
EXPECT_EQ(123, char_result); | EXPECT_EQ(123, char_result); | |||
} | } | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
TEST(EnvironmentTest, HandleFitsIntoSizeT) { | TEST(EnvironmentTest, HandleFitsIntoSizeT) { | |||
ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t)); | ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t)); | |||
} | } | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger | // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger | |||
// failures when death tests are available on the system. | // failures when death tests are available on the system. | |||
TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) { | TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) { | |||
EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"), | EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"), | |||
"death inside CondDeathTestExpectMacro"); | "death inside CondDeathTestExpectMacro"); | |||
ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"), | ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"), | |||
"death inside CondDeathTestAssertMacro"); | "death inside CondDeathTestAssertMacro"); | |||
// Empty statement will not crash, which must trigger a failure. | // Empty statement will not crash, which must trigger a failure. | |||
EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), ""); | EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), ""); | |||
EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), ""); | EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), ""); | |||
} | } | |||
TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) { | TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "fast"; | GTEST_FLAG_SET(death_test_style, "fast"); | |||
EXPECT_FALSE(InDeathTestChild()); | EXPECT_FALSE(InDeathTestChild()); | |||
EXPECT_DEATH({ | EXPECT_DEATH( | |||
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); | { | |||
fflush(stderr); | fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); | |||
_exit(1); | fflush(stderr); | |||
}, "Inside"); | _exit(1); | |||
}, | ||||
"Inside"); | ||||
} | } | |||
TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) { | TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) { | |||
testing::GTEST_FLAG(death_test_style) = "threadsafe"; | GTEST_FLAG_SET(death_test_style, "threadsafe"); | |||
EXPECT_FALSE(InDeathTestChild()); | EXPECT_FALSE(InDeathTestChild()); | |||
EXPECT_DEATH({ | EXPECT_DEATH( | |||
fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); | { | |||
fflush(stderr); | fprintf(stderr, InDeathTestChild() ? "Inside" : "Outside"); | |||
_exit(1); | fflush(stderr); | |||
}, "Inside"); | _exit(1); | |||
}, | ||||
"Inside"); | ||||
} | } | |||
void DieWithMessage(const char* message) { | void DieWithMessage(const char* message) { | |||
fputs(message, stderr); | fputs(message, stderr); | |||
fflush(stderr); // Make sure the text is printed before the process exits. | fflush(stderr); // Make sure the text is printed before the process exits. | |||
_exit(1); | _exit(1); | |||
} | } | |||
TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) { | TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) { | |||
// googletest tests this, of course; here we ensure that including googlemock | // googletest tests this, of course; here we ensure that including googlemock | |||
skipping to change at line 1501 | skipping to change at line 1493 | |||
// This would fail if executed; this is a compilation test only | // This would fail if executed; this is a compilation test only | |||
ASSERT_DEATH_IF_SUPPORTED(return, ""); | ASSERT_DEATH_IF_SUPPORTED(return, ""); | |||
if (AlwaysTrue()) | if (AlwaysTrue()) | |||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), ""); | EXPECT_DEATH_IF_SUPPORTED(_exit(1), ""); | |||
else | else | |||
// This empty "else" branch is meant to ensure that EXPECT_DEATH | // This empty "else" branch is meant to ensure that EXPECT_DEATH | |||
// doesn't expand into an "if" statement without an "else" | // doesn't expand into an "if" statement without an "else" | |||
; // NOLINT | ; // NOLINT | |||
if (AlwaysFalse()) | if (AlwaysFalse()) ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die"; | |||
ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die"; | ||||
if (AlwaysFalse()) | if (AlwaysFalse()) | |||
; // NOLINT | ; // NOLINT | |||
else | else | |||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3; | EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3; | |||
} | } | |||
#ifdef __GNUC__ | #ifdef __GNUC__ | |||
#pragma GCC diagnostic pop | #pragma GCC diagnostic pop | |||
#endif | #endif | |||
// Tests that conditional death test macros expand to code which interacts | // Tests that conditional death test macros expand to code which interacts | |||
// well with switch statements. | // well with switch statements. | |||
TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) { | TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) { | |||
// Microsoft compiler usually complains about switch statements without | // Microsoft compiler usually complains about switch statements without | |||
// case labels. We suppress that warning for this test. | // case labels. We suppress that warning for this test. | |||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065) | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065) | |||
switch (0) | switch (0) | |||
default: | default: | |||
ASSERT_DEATH_IF_SUPPORTED(_exit(1), "") | ASSERT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in default switch handler"; | |||
<< "exit in default switch handler"; | ||||
switch (0) | switch (0) | |||
case 0: | case 0: | |||
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case"; | EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case"; | |||
GTEST_DISABLE_MSC_WARNINGS_POP_() | GTEST_DISABLE_MSC_WARNINGS_POP_() | |||
} | } | |||
// Tests that a test case whose name ends with "DeathTest" works fine | // Tests that a test case whose name ends with "DeathTest" works fine | |||
// on Windows. | // on Windows. | |||
TEST(NotADeathTest, Test) { | TEST(NotADeathTest, Test) { SUCCEED(); } | |||
SUCCEED(); | ||||
} | ||||
} // namespace | } // namespace | |||
End of changes. 103 change blocks. | ||||
220 lines changed or deleted | 209 lines changed or added |