| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ |
| 40 |
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ |
| 41 |
|
| 42 |
#include <stdio.h> |
| 43 |
|
| 44 |
#include <memory> |
| 45 |
|
| 46 |
#include "gtest/gtest-matchers.h" |
| 47 |
#include "gtest/internal/gtest-internal.h" |
| 48 |
|
| 49 |
GTEST_DECLARE_string_(internal_run_death_test); |
| 50 |
|
| 51 |
namespace testing { |
| 52 |
namespace internal { |
| 53 |
|
| 54 |
|
| 55 |
const char kDeathTestStyleFlag[] = "death_test_style"; |
| 56 |
const char kDeathTestUseFork[] = "death_test_use_fork"; |
| 57 |
const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; |
| 58 |
|
| 59 |
#if GTEST_HAS_DEATH_TEST |
| 60 |
|
| 61 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
class GTEST_API_ DeathTest { |
| 78 |
public: |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static bool Create(const char* statement, Matcher<const std::string&> matcher, |
| 88 |
const char* file, int line, DeathTest** test); |
| 89 |
DeathTest(); |
| 90 |
virtual ~DeathTest() {} |
| 91 |
|
| 92 |
|
| 93 |
class ReturnSentinel { |
| 94 |
public: |
| 95 |
explicit ReturnSentinel(DeathTest* test) : test_(test) {} |
| 96 |
~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } |
| 97 |
|
| 98 |
private: |
| 99 |
DeathTest* const test_; |
| 100 |
ReturnSentinel(const ReturnSentinel&) = delete; |
| 101 |
ReturnSentinel& operator=(const ReturnSentinel&) = delete; |
| 102 |
} GTEST_ATTRIBUTE_UNUSED_; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; |
| 110 |
|
| 111 |
|
| 112 |
enum AbortReason { |
| 113 |
TEST_ENCOUNTERED_RETURN_STATEMENT, |
| 114 |
TEST_THREW_EXCEPTION, |
| 115 |
TEST_DID_NOT_DIE |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
virtual TestRole AssumeRole() = 0; |
| 120 |
|
| 121 |
|
| 122 |
virtual int Wait() = 0; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
virtual bool Passed(bool exit_status_ok) = 0; |
| 132 |
|
| 133 |
|
| 134 |
virtual void Abort(AbortReason reason) = 0; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static const char* LastMessage(); |
| 139 |
|
| 140 |
static void set_last_death_test_message(const std::string& message); |
| 141 |
|
| 142 |
private: |
| 143 |
|
| 144 |
static std::string last_death_test_message_; |
| 145 |
|
| 146 |
DeathTest(const DeathTest&) = delete; |
| 147 |
DeathTest& operator=(const DeathTest&) = delete; |
| 148 |
}; |
| 149 |
|
| 150 |
GTEST_DISABLE_MSC_WARNINGS_POP_() |
| 151 |
|
| 152 |
|
| 153 |
class DeathTestFactory { |
| 154 |
public: |
| 155 |
virtual ~DeathTestFactory() {} |
| 156 |
virtual bool Create(const char* statement, |
| 157 |
Matcher<const std::string&> matcher, const char* file, |
| 158 |
int line, DeathTest** test) = 0; |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
class DefaultDeathTestFactory : public DeathTestFactory { |
| 163 |
public: |
| 164 |
bool Create(const char* statement, Matcher<const std::string&> matcher, |
| 165 |
const char* file, int line, DeathTest** test) override; |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
GTEST_API_ bool ExitedUnsuccessfully(int exit_status); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
inline Matcher<const ::std::string&> MakeDeathTestMatcher( |
| 176 |
::testing::internal::RE regex) { |
| 177 |
return ContainsRegex(regex.pattern()); |
| 178 |
} |
| 179 |
inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) { |
| 180 |
return ContainsRegex(regex); |
| 181 |
} |
| 182 |
inline Matcher<const ::std::string&> MakeDeathTestMatcher( |
| 183 |
const ::std::string& regex) { |
| 184 |
return ContainsRegex(regex); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
inline Matcher<const ::std::string&> MakeDeathTestMatcher( |
| 190 |
Matcher<const ::std::string&> matcher) { |
| 191 |
return matcher; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#if GTEST_HAS_EXCEPTIONS |
| 197 |
#define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ |
| 198 |
try { \ |
| 199 |
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ |
| 200 |
} catch (const ::std::exception& gtest_exception) { \ |
| 201 |
fprintf( \ |
| 202 |
stderr, \ |
| 203 |
"\n%s: Caught std::exception-derived exception escaping the " \ |
| 204 |
"death test statement. Exception message: %s\n", \ |
| 205 |
::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \ |
| 206 |
gtest_exception.what()); \ |
| 207 |
fflush(stderr); \ |
| 208 |
death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ |
| 209 |
} catch (...) { \ |
| 210 |
death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ |
| 211 |
} |
| 212 |
|
| 213 |
#else |
| 214 |
#define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ |
| 215 |
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) |
| 216 |
|
| 217 |
#endif |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \ |
| 222 |
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
| 223 |
if (::testing::internal::AlwaysTrue()) { \ |
| 224 |
::testing::internal::DeathTest* gtest_dt; \ |
| 225 |
if (!::testing::internal::DeathTest::Create( \ |
| 226 |
#statement, \ |
| 227 |
::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \ |
| 228 |
__FILE__, __LINE__, >est_dt)) { \ |
| 229 |
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ |
| 230 |
} \ |
| 231 |
if (gtest_dt != nullptr) { \ |
| 232 |
std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \ |
| 233 |
switch (gtest_dt->AssumeRole()) { \ |
| 234 |
case ::testing::internal::DeathTest::OVERSEE_TEST: \ |
| 235 |
if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ |
| 236 |
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ |
| 237 |
} \ |
| 238 |
break; \ |
| 239 |
case ::testing::internal::DeathTest::EXECUTE_TEST: { \ |
| 240 |
::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \ |
| 241 |
gtest_dt); \ |
| 242 |
GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \ |
| 243 |
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ |
| 244 |
break; \ |
| 245 |
} \ |
| 246 |
} \ |
| 247 |
} \ |
| 248 |
} else \ |
| 249 |
GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \ |
| 250 |
: fail(::testing::internal::DeathTest::LastMessage()) |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \ |
| 260 |
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
| 261 |
if (::testing::internal::AlwaysTrue()) { \ |
| 262 |
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ |
| 263 |
} else if (!::testing::internal::AlwaysTrue()) { \ |
| 264 |
::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \ |
| 265 |
} else \ |
| 266 |
::testing::Message() |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
class InternalRunDeathTestFlag { |
| 272 |
public: |
| 273 |
InternalRunDeathTestFlag(const std::string& a_file, int a_line, int an_index, |
| 274 |
int a_write_fd) |
| 275 |
: file_(a_file), line_(a_line), index_(an_index), write_fd_(a_write_fd) {} |
| 276 |
|
| 277 |
~InternalRunDeathTestFlag() { |
| 278 |
if (write_fd_ >= 0) posix::Close(write_fd_); |
| 279 |
} |
| 280 |
|
| 281 |
const std::string& file() const { return file_; } |
| 282 |
int line() const { return line_; } |
| 283 |
int index() const { return index_; } |
| 284 |
int write_fd() const { return write_fd_; } |
| 285 |
|
| 286 |
private: |
| 287 |
std::string file_; |
| 288 |
int line_; |
| 289 |
int index_; |
| 290 |
int write_fd_; |
| 291 |
|
| 292 |
InternalRunDeathTestFlag(const InternalRunDeathTestFlag&) = delete; |
| 293 |
InternalRunDeathTestFlag& operator=(const InternalRunDeathTestFlag&) = delete; |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); |
| 300 |
|
| 301 |
#endif |
| 302 |
|
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
#endif |