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