~ubuntu-branches/ubuntu/raring/protobuf/raring

« back to all changes in this revision

Viewing changes to gtest/test/gtest-death-test_test.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
72
73
 
77
78
// single UnitTest object during their lifetimes.
78
79
class ReplaceDeathTestFactory {
79
80
 public:
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);
84
85
  }
85
86
 
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_);
89
90
  }
90
91
 private:
91
92
  // Prevents copying ReplaceDeathTestFactory objects.
92
93
  ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
93
94
  void operator=(const ReplaceDeathTestFactory&);
94
95
 
95
 
  UnitTestImpl* parent_impl_;
 
96
  UnitTestImpl* unit_test_impl_;
96
97
  DeathTestFactory* old_factory_;
97
98
};
98
99
 
846
847
 
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_);
851
851
  }
852
852
 
853
853
  static void TearDownTestCase() {
957
957
  EXPECT_TRUE(factory_->TestDeleted());
958
958
}
959
959
 
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();
964
 
}
965
 
 
966
960
// Tests that a successful death test does not register a successful
967
961
// test part.
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());
971
965
}
972
966
 
973
967
TEST(StreamingAssertionsDeathTest, DeathTest) {
1063
1057
 
1064
1058
  result = 0;
1065
1059
  ASSERT_TRUE(ParseNaturalNumber(String("123"), &result));
1066
 
  EXPECT_EQ(123, result);
 
1060
  EXPECT_EQ(123U, result);
1067
1061
 
1068
1062
  // Check 0 as an edge case.
1069
1063
  result = 1;
1070
1064
  ASSERT_TRUE(ParseNaturalNumber(String("0"), &result));
1071
 
  EXPECT_EQ(0, result);
 
1065
  EXPECT_EQ(0U, result);
1072
1066
 
1073
1067
  result = 1;
1074
1068
  ASSERT_TRUE(ParseNaturalNumber(String("00000"), &result));
1075
 
  EXPECT_EQ(0, result);
 
1069
  EXPECT_EQ(0U, result);
1076
1070
}
1077
1071
 
1078
1072
TEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
1124
1118
}
1125
1119
#endif  // GTEST_OS_WINDOWS
1126
1120
 
 
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");
 
1126
 
 
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(;, ""), "");
 
1130
}
 
1131
 
 
1132
#else
 
1133
 
 
1134
using testing::internal::CaptureStderr;
 
1135
using testing::internal::GetCapturedStderr;
 
1136
using testing::internal::String;
 
1137
 
 
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
 
1140
// the system.
 
1141
TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
 
1142
  // Empty statement will not crash, but that should not trigger a failure
 
1143
  // when death tests are not supported.
 
1144
  CaptureStderr();
 
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(), ";"));
 
1150
 
 
1151
  CaptureStderr();
 
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(), ";"));
 
1157
}
 
1158
 
1127
1159
#endif  // GTEST_HAS_DEATH_TEST
1128
1160
 
1129
1161
// Tests that a test case whose name ends with "DeathTest" works fine