File indexing completed on 2026-11-25 10:04:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
0040 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
0041
0042 #include <stdio.h>
0043
0044 #include <memory>
0045 #include <string>
0046 #include <string_view>
0047
0048 #include "gtest/gtest-matchers.h"
0049 #include "gtest/internal/gtest-internal.h"
0050 #include "gtest/internal/gtest-port.h"
0051
0052 GTEST_DECLARE_string_(internal_run_death_test);
0053
0054 namespace testing {
0055 namespace internal {
0056
0057
0058 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
0059
0060
0061
0062
0063 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0064 ::testing::internal::RE regex) {
0065 return ContainsRegex(regex.pattern());
0066 }
0067 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0068 std::string_view regex) {
0069 return ContainsRegex(regex);
0070 }
0071 inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
0072 return ContainsRegex(regex);
0073 }
0074 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0075 const ::std::string& regex) {
0076 return ContainsRegex(regex);
0077 }
0078
0079
0080
0081 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0082 Matcher<const ::std::string&> matcher) {
0083 return matcher;
0084 }
0085
0086 #ifdef GTEST_HAS_DEATH_TEST
0087
0088 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0089 )
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 class GTEST_API_ [[nodiscard]] DeathTest {
0105 public:
0106
0107
0108
0109
0110
0111
0112
0113
0114 static bool Create(const char* statement, Matcher<const std::string&> matcher,
0115 const char* file, int line, DeathTest** test);
0116 DeathTest();
0117 virtual ~DeathTest() = default;
0118
0119
0120 class ReturnSentinel {
0121 public:
0122 explicit ReturnSentinel(DeathTest* test) : test_(test) {}
0123 ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
0124
0125 private:
0126 DeathTest* const test_;
0127 ReturnSentinel(const ReturnSentinel&) = delete;
0128 ReturnSentinel& operator=(const ReturnSentinel&) = delete;
0129 };
0130
0131
0132
0133
0134
0135
0136 enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
0137
0138
0139 enum AbortReason {
0140 TEST_ENCOUNTERED_RETURN_STATEMENT,
0141 TEST_THREW_EXCEPTION,
0142 TEST_DID_NOT_DIE
0143 };
0144
0145
0146 virtual TestRole AssumeRole() = 0;
0147
0148
0149 virtual int Wait() = 0;
0150
0151
0152
0153
0154
0155
0156
0157
0158 virtual bool Passed(bool exit_status_ok) = 0;
0159
0160
0161 virtual void Abort(AbortReason reason) = 0;
0162
0163
0164
0165 static const char* LastMessage();
0166
0167 static void set_last_death_test_message(const std::string& message);
0168
0169 private:
0170
0171 static std::string last_death_test_message_;
0172
0173 DeathTest(const DeathTest&) = delete;
0174 DeathTest& operator=(const DeathTest&) = delete;
0175 };
0176
0177 GTEST_DISABLE_MSC_WARNINGS_POP_()
0178
0179
0180 class [[nodiscard]] DeathTestFactory {
0181 public:
0182 virtual ~DeathTestFactory() = default;
0183 virtual bool Create(const char* statement,
0184 Matcher<const std::string&> matcher, const char* file,
0185 int line, DeathTest** test) = 0;
0186 };
0187
0188
0189 class [[nodiscard]] DefaultDeathTestFactory : public DeathTestFactory {
0190 public:
0191 bool Create(const char* statement, Matcher<const std::string&> matcher,
0192 const char* file, int line, DeathTest** test) override;
0193 };
0194
0195
0196
0197 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
0198
0199
0200
0201 #if GTEST_HAS_EXCEPTIONS
0202 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
0203 try { \
0204 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
0205 } catch (const ::std::exception& gtest_exception) { \
0206 fprintf( \
0207 stderr, \
0208 "\n%s: Caught std::exception-derived exception escaping the " \
0209 "death test statement. Exception message: %s\n", \
0210 ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
0211 gtest_exception.what()); \
0212 fflush(stderr); \
0213 death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0214 } catch (...) { \
0215 death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0216 }
0217
0218 #else
0219 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
0220 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
0221
0222 #endif
0223
0224
0225
0226 #define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \
0227 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
0228 if (::testing::internal::AlwaysTrue()) { \
0229 ::testing::internal::DeathTest* gtest_dt; \
0230 if (!::testing::internal::DeathTest::Create( \
0231 #statement, \
0232 ::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \
0233 __FILE__, __LINE__, >est_dt)) { \
0234 goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
0235 } \
0236 if (gtest_dt != nullptr) { \
0237 const std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr( \
0238 gtest_dt); \
0239 switch (gtest_dt->AssumeRole()) { \
0240 case ::testing::internal::DeathTest::OVERSEE_TEST: \
0241 if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
0242 goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
0243 } \
0244 break; \
0245 case ::testing::internal::DeathTest::EXECUTE_TEST: { \
0246 const ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \
0247 gtest_dt); \
0248 GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
0249 gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
0250 break; \
0251 } \
0252 } \
0253 } \
0254 } else \
0255 GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \
0256 : fail(::testing::internal::DeathTest::LastMessage())
0257
0258
0259
0260
0261
0262
0263
0264
0265 #define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \
0266 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
0267 if (::testing::internal::AlwaysTrue()) { \
0268 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
0269 } else if (!::testing::internal::AlwaysTrue()) { \
0270 (void)::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
0271 } else \
0272 ::testing::Message()
0273
0274
0275
0276
0277 class [[nodiscard]] InternalRunDeathTestFlag {
0278 public:
0279 InternalRunDeathTestFlag(const std::string& a_file, int a_line, int an_index,
0280 int a_write_fd)
0281 : file_(a_file), line_(a_line), index_(an_index), write_fd_(a_write_fd) {}
0282
0283 ~InternalRunDeathTestFlag() {
0284 if (write_fd_ >= 0) posix::Close(write_fd_);
0285 }
0286
0287 const std::string& file() const { return file_; }
0288 int line() const { return line_; }
0289 int index() const { return index_; }
0290 int write_fd() const { return write_fd_; }
0291
0292 private:
0293 std::string file_;
0294 int line_;
0295 int index_;
0296 int write_fd_;
0297
0298 InternalRunDeathTestFlag(const InternalRunDeathTestFlag&) = delete;
0299 InternalRunDeathTestFlag& operator=(const InternalRunDeathTestFlag&) = delete;
0300 };
0301
0302
0303
0304
0305 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
0306
0307 #endif
0308
0309 }
0310 }
0311
0312 #endif