67
67
using testing::internal::DeathTestFactory;
68
68
using testing::internal::FilePath;
69
69
using testing::internal::GetLastErrnoDescription;
70
using testing::internal::GetUnitTestImpl;
70
71
using testing::internal::ParseNaturalNumber;
71
72
using testing::internal::String;
77
78
// single UnitTest object during their lifetimes.
78
79
class ReplaceDeathTestFactory {
80
ReplaceDeathTestFactory(UnitTest* parent, DeathTestFactory* new_factory)
81
: parent_impl_(parent->impl()) {
82
old_factory_ = parent_impl_->death_test_factory_.release();
83
parent_impl_->death_test_factory_.reset(new_factory);
81
explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
82
: unit_test_impl_(GetUnitTestImpl()) {
83
old_factory_ = unit_test_impl_->death_test_factory_.release();
84
unit_test_impl_->death_test_factory_.reset(new_factory);
86
87
~ReplaceDeathTestFactory() {
87
parent_impl_->death_test_factory_.release();
88
parent_impl_->death_test_factory_.reset(old_factory_);
88
unit_test_impl_->death_test_factory_.release();
89
unit_test_impl_->death_test_factory_.reset(old_factory_);
91
92
// Prevents copying ReplaceDeathTestFactory objects.
92
93
ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
93
94
void operator=(const ReplaceDeathTestFactory&);
95
UnitTestImpl* parent_impl_;
96
UnitTestImpl* unit_test_impl_;
96
97
DeathTestFactory* old_factory_;
847
848
static void SetUpTestCase() {
848
849
factory_ = new MockDeathTestFactory;
849
replacer_ = new testing::internal::ReplaceDeathTestFactory(
850
testing::UnitTest::GetInstance(), factory_);
850
replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_);
853
853
static void TearDownTestCase() {
957
957
EXPECT_TRUE(factory_->TestDeleted());
960
// Returns the number of successful parts in the current test.
961
static size_t GetSuccessfulTestPartCount() {
962
return testing::UnitTest::GetInstance()->impl()->current_test_result()->
963
successful_part_count();
966
960
// Tests that a successful death test does not register a successful
968
962
TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
969
963
EXPECT_DEATH(_exit(1), "");
970
EXPECT_EQ(0u, GetSuccessfulTestPartCount());
964
EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
973
967
TEST(StreamingAssertionsDeathTest, DeathTest) {
1065
1059
ASSERT_TRUE(ParseNaturalNumber(String("123"), &result));
1066
EXPECT_EQ(123, result);
1060
EXPECT_EQ(123U, result);
1068
1062
// Check 0 as an edge case.
1070
1064
ASSERT_TRUE(ParseNaturalNumber(String("0"), &result));
1071
EXPECT_EQ(0, result);
1065
EXPECT_EQ(0U, result);
1074
1068
ASSERT_TRUE(ParseNaturalNumber(String("00000"), &result));
1075
EXPECT_EQ(0, result);
1069
EXPECT_EQ(0U, result);
1078
1072
TEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
1125
1119
#endif // GTEST_OS_WINDOWS
1121
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
1122
// failures when death tests are available on the system.
1123
TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
1124
EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(false) << "failure", "false.*failure");
1125
ASSERT_DEATH_IF_SUPPORTED(GTEST_CHECK_(false) << "failure", "false.*failure");
1127
// Empty statement will not crash, which must trigger a failure.
1128
EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");
1129
EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), "");
1134
using testing::internal::CaptureStderr;
1135
using testing::internal::GetCapturedStderr;
1136
using testing::internal::String;
1138
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
1139
// defined but do not rigger failures when death tests are not available on
1141
TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
1142
// Empty statement will not crash, but that should not trigger a failure
1143
// when death tests are not supported.
1145
EXPECT_DEATH_IF_SUPPORTED(;, "");
1146
String output = GetCapturedStderr();
1147
ASSERT_TRUE(NULL != strstr(output.c_str(),
1148
"Death tests are not supported on this platform"));
1149
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1152
ASSERT_DEATH_IF_SUPPORTED(;, "");
1153
output = GetCapturedStderr();
1154
ASSERT_TRUE(NULL != strstr(output.c_str(),
1155
"Death tests are not supported on this platform"));
1156
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
1127
1159
#endif // GTEST_HAS_DEATH_TEST
1129
1161
// Tests that a test case whose name ends with "DeathTest" works fine