36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
55 #if GTEST_HAS_DEATH_TEST
83 static bool Create(
const char* statement, Matcher<const std::string&> matcher,
84 const char* file,
int line, DeathTest**
test);
86 virtual ~DeathTest() { }
89 class ReturnSentinel {
91 explicit ReturnSentinel(DeathTest*
test) : test_(
test) { }
92 ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
94 DeathTest*
const test_;
103 enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
107 TEST_ENCOUNTERED_RETURN_STATEMENT,
108 TEST_THREW_EXCEPTION,
113 virtual TestRole AssumeRole() = 0;
116 virtual int Wait() = 0;
125 virtual bool Passed(
bool exit_status_ok) = 0;
128 virtual void Abort(AbortReason reason) = 0;
132 static const char* LastMessage();
134 static void set_last_death_test_message(
const std::string& message);
146 class DeathTestFactory {
148 virtual ~DeathTestFactory() { }
149 virtual bool Create(
const char* statement,
150 Matcher<const std::string&> matcher,
const char* file,
151 int line, DeathTest**
test) = 0;
155 class DefaultDeathTestFactory :
public DeathTestFactory {
157 bool Create(
const char* statement, Matcher<const std::string&> matcher,
158 const char* file,
int line, DeathTest**
test)
override;
163 GTEST_API_ bool ExitedUnsuccessfully(
int exit_status);
168 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
170 return ContainsRegex(regex.
pattern());
172 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
const char* regex) {
173 return ContainsRegex(regex);
175 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
177 return ContainsRegex(regex);
179 #if GTEST_HAS_GLOBAL_STRING
180 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
182 return ContainsRegex(regex);
188 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
189 Matcher<const ::std::string&> matcher) {
195 # if GTEST_HAS_EXCEPTIONS
196 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
198 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
199 } catch (const ::std::exception& gtest_exception) { \
202 "\n%s: Caught std::exception-derived exception escaping the " \
203 "death test statement. Exception message: %s\n", \
204 ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
205 gtest_exception.what()); \
207 death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
209 death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
213 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
214 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
220 #define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \
221 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
222 if (::testing::internal::AlwaysTrue()) { \
223 ::testing::internal::DeathTest* gtest_dt; \
224 if (!::testing::internal::DeathTest::Create( \
226 ::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \
227 __FILE__, __LINE__, >est_dt)) { \
228 goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
230 if (gtest_dt != nullptr) { \
231 std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \
232 switch (gtest_dt->AssumeRole()) { \
233 case ::testing::internal::DeathTest::OVERSEE_TEST: \
234 if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
235 goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
238 case ::testing::internal::DeathTest::EXECUTE_TEST: { \
239 ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \
241 GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
242 gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
250 GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \
251 : fail(::testing::internal::DeathTest::LastMessage())
260 #define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \
261 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
262 if (::testing::internal::AlwaysTrue()) { \
263 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
264 } else if (!::testing::internal::AlwaysTrue()) { \
265 ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
272 class InternalRunDeathTestFlag {
274 InternalRunDeathTestFlag(
const std::string& a_file,
278 : file_(a_file), line_(a_line), index_(an_index),
279 write_fd_(a_write_fd) {}
281 ~InternalRunDeathTestFlag() {
287 int line()
const {
return line_; }
288 int index()
const {
return index_; }
289 int write_fd()
const {
return write_fd_; }
303 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
double test(int data_type)
const char * pattern() const
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
const char kDeathTestStyleFlag[]
const char kDeathTestUseFork[]
const char kInternalRunDeathTestFlag[]
GTEST_DECLARE_string_(internal_run_death_test)
class GTEST_API_ testing::ScopedTrace GTEST_ATTRIBUTE_UNUSED_