| 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 |
#ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ |
| 35 |
#define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ |
| 36 |
|
| 37 |
#ifndef _WIN32_WCE |
| 38 |
#include <errno.h> |
| 39 |
#endif |
| 40 |
#include <stddef.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
#include <algorithm> |
| 45 |
#include <cstdint> |
| 46 |
#include <memory> |
| 47 |
#include <string> |
| 48 |
#include <vector> |
| 49 |
|
| 50 |
#include "gtest/internal/gtest-port.h" |
| 51 |
|
| 52 |
#if GTEST_CAN_STREAM_RESULTS_ |
| 53 |
#include <arpa/inet.h> |
| 54 |
#include <netdb.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#if GTEST_OS_WINDOWS |
| 58 |
#include <windows.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#include "gtest/gtest-spi.h" |
| 62 |
#include "gtest/gtest.h" |
| 63 |
|
| 64 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
GTEST_DECLARE_bool_(death_test_use_fork); |
| 73 |
|
| 74 |
namespace testing { |
| 75 |
namespace internal { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest; |
| 80 |
|
| 81 |
|
| 82 |
const int kMaxRandomSeed = 99999; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
GTEST_API_ extern bool g_help_flag; |
| 87 |
|
| 88 |
|
| 89 |
GTEST_API_ TimeInMillis GetTimeInMillis(); |
| 90 |
|
| 91 |
|
| 92 |
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty); |
| 93 |
|
| 94 |
|
| 95 |
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
GTEST_API_ bool ParseFlag(const char* str, const char* flag, int32_t* value); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
inline int GetRandomSeedFromFlag(int32_t random_seed_flag) { |
| 112 |
const unsigned int raw_seed = |
| 113 |
(random_seed_flag == 0) ? static_cast<unsigned int>(GetTimeInMillis()) |
| 114 |
: static_cast<unsigned int>(random_seed_flag); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
const int normalized_seed = |
| 119 |
static_cast<int>((raw_seed - 1U) % |
| 120 |
static_cast<unsigned int>(kMaxRandomSeed)) + |
| 121 |
1; |
| 122 |
return normalized_seed; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
inline int GetNextRandomSeed(int seed) { |
| 129 |
GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed) |
| 130 |
<< "Invalid random seed " << seed << " - must be in [1, " |
| 131 |
<< kMaxRandomSeed << "]."; |
| 132 |
const int next_seed = seed + 1; |
| 133 |
return (next_seed > kMaxRandomSeed) ? 1 : next_seed; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
class GTestFlagSaver { |
| 139 |
public: |
| 140 |
|
| 141 |
GTestFlagSaver() { |
| 142 |
also_run_disabled_tests_ = GTEST_FLAG_GET(also_run_disabled_tests); |
| 143 |
break_on_failure_ = GTEST_FLAG_GET(break_on_failure); |
| 144 |
catch_exceptions_ = GTEST_FLAG_GET(catch_exceptions); |
| 145 |
color_ = GTEST_FLAG_GET(color); |
| 146 |
death_test_style_ = GTEST_FLAG_GET(death_test_style); |
| 147 |
death_test_use_fork_ = GTEST_FLAG_GET(death_test_use_fork); |
| 148 |
fail_fast_ = GTEST_FLAG_GET(fail_fast); |
| 149 |
filter_ = GTEST_FLAG_GET(filter); |
| 150 |
internal_run_death_test_ = GTEST_FLAG_GET(internal_run_death_test); |
| 151 |
list_tests_ = GTEST_FLAG_GET(list_tests); |
| 152 |
output_ = GTEST_FLAG_GET(output); |
| 153 |
brief_ = GTEST_FLAG_GET(brief); |
| 154 |
print_time_ = GTEST_FLAG_GET(print_time); |
| 155 |
print_utf8_ = GTEST_FLAG_GET(print_utf8); |
| 156 |
random_seed_ = GTEST_FLAG_GET(random_seed); |
| 157 |
repeat_ = GTEST_FLAG_GET(repeat); |
| 158 |
recreate_environments_when_repeating_ = |
| 159 |
GTEST_FLAG_GET(recreate_environments_when_repeating); |
| 160 |
shuffle_ = GTEST_FLAG_GET(shuffle); |
| 161 |
stack_trace_depth_ = GTEST_FLAG_GET(stack_trace_depth); |
| 162 |
stream_result_to_ = GTEST_FLAG_GET(stream_result_to); |
| 163 |
throw_on_failure_ = GTEST_FLAG_GET(throw_on_failure); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
~GTestFlagSaver() { |
| 168 |
GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_); |
| 169 |
GTEST_FLAG_SET(break_on_failure, break_on_failure_); |
| 170 |
GTEST_FLAG_SET(catch_exceptions, catch_exceptions_); |
| 171 |
GTEST_FLAG_SET(color, color_); |
| 172 |
GTEST_FLAG_SET(death_test_style, death_test_style_); |
| 173 |
GTEST_FLAG_SET(death_test_use_fork, death_test_use_fork_); |
| 174 |
GTEST_FLAG_SET(filter, filter_); |
| 175 |
GTEST_FLAG_SET(fail_fast, fail_fast_); |
| 176 |
GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_); |
| 177 |
GTEST_FLAG_SET(list_tests, list_tests_); |
| 178 |
GTEST_FLAG_SET(output, output_); |
| 179 |
GTEST_FLAG_SET(brief, brief_); |
| 180 |
GTEST_FLAG_SET(print_time, print_time_); |
| 181 |
GTEST_FLAG_SET(print_utf8, print_utf8_); |
| 182 |
GTEST_FLAG_SET(random_seed, random_seed_); |
| 183 |
GTEST_FLAG_SET(repeat, repeat_); |
| 184 |
GTEST_FLAG_SET(recreate_environments_when_repeating, |
| 185 |
recreate_environments_when_repeating_); |
| 186 |
GTEST_FLAG_SET(shuffle, shuffle_); |
| 187 |
GTEST_FLAG_SET(stack_trace_depth, stack_trace_depth_); |
| 188 |
GTEST_FLAG_SET(stream_result_to, stream_result_to_); |
| 189 |
GTEST_FLAG_SET(throw_on_failure, throw_on_failure_); |
| 190 |
} |
| 191 |
|
| 192 |
private: |
| 193 |
|
| 194 |
bool also_run_disabled_tests_; |
| 195 |
bool break_on_failure_; |
| 196 |
bool catch_exceptions_; |
| 197 |
std::string color_; |
| 198 |
std::string death_test_style_; |
| 199 |
bool death_test_use_fork_; |
| 200 |
bool fail_fast_; |
| 201 |
std::string filter_; |
| 202 |
std::string internal_run_death_test_; |
| 203 |
bool list_tests_; |
| 204 |
std::string output_; |
| 205 |
bool brief_; |
| 206 |
bool print_time_; |
| 207 |
bool print_utf8_; |
| 208 |
int32_t random_seed_; |
| 209 |
int32_t repeat_; |
| 210 |
bool recreate_environments_when_repeating_; |
| 211 |
bool shuffle_; |
| 212 |
int32_t stack_trace_depth_; |
| 213 |
std::string stream_result_to_; |
| 214 |
bool throw_on_failure_; |
| 215 |
} GTEST_ATTRIBUTE_UNUSED_; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
GTEST_API_ std::string CodePointToUtf8(uint32_t code_point); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
void WriteToShardStatusFileIfNeeded(); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
GTEST_API_ bool ShouldShard(const char* total_shards_str, |
| 253 |
const char* shard_index_str, |
| 254 |
bool in_subprocess_for_death_test); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, |
| 266 |
int test_id); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
template <class Container, typename Predicate> |
| 273 |
inline int CountIf(const Container& c, Predicate predicate) { |
| 274 |
|
| 275 |
|
| 276 |
int count = 0; |
| 277 |
for (auto it = c.begin(); it != c.end(); ++it) { |
| 278 |
if (predicate(*it)) ++count; |
| 279 |
} |
| 280 |
return count; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
template <class Container, typename Functor> |
| 285 |
void ForEach(const Container& c, Functor functor) { |
| 286 |
std::for_each(c.begin(), c.end(), functor); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
template <typename E> |
| 292 |
inline E GetElementOr(const std::vector<E>& v, int i, E default_value) { |
| 293 |
return (i < 0 || i >= static_cast<int>(v.size())) ? default_value |
| 294 |
: v[static_cast<size_t>(i)]; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
template <typename E> |
| 302 |
void ShuffleRange(internal::Random* random, int begin, int end, |
| 303 |
std::vector<E>* v) { |
| 304 |
const int size = static_cast<int>(v->size()); |
| 305 |
GTEST_CHECK_(0 <= begin && begin <= size) |
| 306 |
<< "Invalid shuffle range start " << begin << ": must be in range [0, " |
| 307 |
<< size << "]."; |
| 308 |
GTEST_CHECK_(begin <= end && end <= size) |
| 309 |
<< "Invalid shuffle range finish " << end << ": must be in range [" |
| 310 |
<< begin << ", " << size << "]."; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
for (int range_width = end - begin; range_width >= 2; range_width--) { |
| 315 |
const int last_in_range = begin + range_width - 1; |
| 316 |
const int selected = |
| 317 |
begin + |
| 318 |
static_cast<int>(random->Generate(static_cast<uint32_t>(range_width))); |
| 319 |
std::swap((*v)[static_cast<size_t>(selected)], |
| 320 |
(*v)[static_cast<size_t>(last_in_range)]); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
template <typename E> |
| 326 |
inline void Shuffle(internal::Random* random, std::vector<E>* v) { |
| 327 |
ShuffleRange(random, 0, static_cast<int>(v->size()), v); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
template <typename T> |
| 333 |
static void Delete(T* x) { |
| 334 |
delete x; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
class TestPropertyKeyIs { |
| 341 |
public: |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
explicit TestPropertyKeyIs(const std::string& key) : key_(key) {} |
| 346 |
|
| 347 |
|
| 348 |
bool operator()(const TestProperty& test_property) const { |
| 349 |
return test_property.key() == key_; |
| 350 |
} |
| 351 |
|
| 352 |
private: |
| 353 |
std::string key_; |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
class GTEST_API_ UnitTestOptions { |
| 367 |
public: |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static std::string GetOutputFormat(); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
static std::string GetAbsolutePathToOutputFile(); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
static bool FilterMatchesTest(const std::string& test_suite_name, |
| 383 |
const std::string& test_name); |
| 384 |
|
| 385 |
#if GTEST_OS_WINDOWS |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
static int GTestShouldProcessSEH(DWORD exception_code); |
| 392 |
#endif |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
static bool MatchesFilter(const std::string& name, const char* filter); |
| 397 |
}; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
GTEST_API_ FilePath GetCurrentExecutableName(); |
| 402 |
|
| 403 |
|
| 404 |
class OsStackTraceGetterInterface { |
| 405 |
public: |
| 406 |
OsStackTraceGetterInterface() {} |
| 407 |
virtual ~OsStackTraceGetterInterface() {} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
virtual void UponLeavingGTest() = 0; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
static const char* const kElidedFramesMarker; |
| 425 |
|
| 426 |
private: |
| 427 |
OsStackTraceGetterInterface(const OsStackTraceGetterInterface&) = delete; |
| 428 |
OsStackTraceGetterInterface& operator=(const OsStackTraceGetterInterface&) = |
| 429 |
delete; |
| 430 |
}; |
| 431 |
|
| 432 |
|
| 433 |
class OsStackTraceGetter : public OsStackTraceGetterInterface { |
| 434 |
public: |
| 435 |
OsStackTraceGetter() {} |
| 436 |
|
| 437 |
std::string CurrentStackTrace(int max_depth, int skip_count) override; |
| 438 |
void UponLeavingGTest() override; |
| 439 |
|
| 440 |
private: |
| 441 |
#if GTEST_HAS_ABSL |
| 442 |
Mutex mutex_; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
void* caller_frame_ = nullptr; |
| 449 |
#endif |
| 450 |
|
| 451 |
OsStackTraceGetter(const OsStackTraceGetter&) = delete; |
| 452 |
OsStackTraceGetter& operator=(const OsStackTraceGetter&) = delete; |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
struct TraceInfo { |
| 457 |
const char* file; |
| 458 |
int line; |
| 459 |
std::string message; |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
class DefaultGlobalTestPartResultReporter |
| 465 |
: public TestPartResultReporterInterface { |
| 466 |
public: |
| 467 |
explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test); |
| 468 |
|
| 469 |
|
| 470 |
void ReportTestPartResult(const TestPartResult& result) override; |
| 471 |
|
| 472 |
private: |
| 473 |
UnitTestImpl* const unit_test_; |
| 474 |
|
| 475 |
DefaultGlobalTestPartResultReporter( |
| 476 |
const DefaultGlobalTestPartResultReporter&) = delete; |
| 477 |
DefaultGlobalTestPartResultReporter& operator=( |
| 478 |
const DefaultGlobalTestPartResultReporter&) = delete; |
| 479 |
}; |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
class DefaultPerThreadTestPartResultReporter |
| 484 |
: public TestPartResultReporterInterface { |
| 485 |
public: |
| 486 |
explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test); |
| 487 |
|
| 488 |
|
| 489 |
void ReportTestPartResult(const TestPartResult& result) override; |
| 490 |
|
| 491 |
private: |
| 492 |
UnitTestImpl* const unit_test_; |
| 493 |
|
| 494 |
DefaultPerThreadTestPartResultReporter( |
| 495 |
const DefaultPerThreadTestPartResultReporter&) = delete; |
| 496 |
DefaultPerThreadTestPartResultReporter& operator=( |
| 497 |
const DefaultPerThreadTestPartResultReporter&) = delete; |
| 498 |
}; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
class GTEST_API_ UnitTestImpl { |
| 505 |
public: |
| 506 |
explicit UnitTestImpl(UnitTest* parent); |
| 507 |
virtual ~UnitTestImpl(); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
TestPartResultReporterInterface* GetGlobalTestPartResultReporter(); |
| 518 |
|
| 519 |
|
| 520 |
void SetGlobalTestPartResultReporter( |
| 521 |
TestPartResultReporterInterface* reporter); |
| 522 |
|
| 523 |
|
| 524 |
TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread(); |
| 525 |
|
| 526 |
|
| 527 |
void SetTestPartResultReporterForCurrentThread( |
| 528 |
TestPartResultReporterInterface* reporter); |
| 529 |
|
| 530 |
|
| 531 |
int successful_test_suite_count() const; |
| 532 |
|
| 533 |
|
| 534 |
int failed_test_suite_count() const; |
| 535 |
|
| 536 |
|
| 537 |
int total_test_suite_count() const; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
int test_suite_to_run_count() const; |
| 542 |
|
| 543 |
|
| 544 |
int successful_test_count() const; |
| 545 |
|
| 546 |
|
| 547 |
int skipped_test_count() const; |
| 548 |
|
| 549 |
|
| 550 |
int failed_test_count() const; |
| 551 |
|
| 552 |
|
| 553 |
int reportable_disabled_test_count() const; |
| 554 |
|
| 555 |
|
| 556 |
int disabled_test_count() const; |
| 557 |
|
| 558 |
|
| 559 |
int reportable_test_count() const; |
| 560 |
|
| 561 |
|
| 562 |
int total_test_count() const; |
| 563 |
|
| 564 |
|
| 565 |
int test_to_run_count() const; |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
TimeInMillis start_timestamp() const { return start_timestamp_; } |
| 570 |
|
| 571 |
|
| 572 |
TimeInMillis elapsed_time() const { return elapsed_time_; } |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
bool Passed() const { return !Failed(); } |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
bool Failed() const { |
| 581 |
return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed(); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
const TestSuite* GetTestSuite(int i) const { |
| 587 |
const int index = GetElementOr(test_suite_indices_, i, -1); |
| 588 |
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)]; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 593 |
const TestCase* GetTestCase(int i) const { return GetTestSuite(i); } |
| 594 |
#endif |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
TestSuite* GetMutableSuiteCase(int i) { |
| 599 |
const int index = GetElementOr(test_suite_indices_, i, -1); |
| 600 |
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)]; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
TestEventListeners* listeners() { return &listeners_; } |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
TestResult* current_test_result(); |
| 609 |
|
| 610 |
|
| 611 |
const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter); |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
OsStackTraceGetterInterface* os_stack_trace_getter(); |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
std::string CurrentOsStackTraceExceptTop(int skip_count) |
| 636 |
GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_; |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param, |
| 649 |
internal::SetUpTestSuiteFunc set_up_tc, |
| 650 |
internal::TearDownTestSuiteFunc tear_down_tc); |
| 651 |
|
| 652 |
|
| 653 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 654 |
TestCase* GetTestCase(const char* test_case_name, const char* type_param, |
| 655 |
internal::SetUpTestSuiteFunc set_up_tc, |
| 656 |
internal::TearDownTestSuiteFunc tear_down_tc) { |
| 657 |
return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc); |
| 658 |
} |
| 659 |
#endif |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, |
| 669 |
internal::TearDownTestSuiteFunc tear_down_tc, |
| 670 |
TestInfo* test_info) { |
| 671 |
#if GTEST_HAS_DEATH_TEST |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
if (original_working_dir_.IsEmpty()) { |
| 680 |
original_working_dir_.Set(FilePath::GetCurrentDir()); |
| 681 |
GTEST_CHECK_(!original_working_dir_.IsEmpty()) |
| 682 |
<< "Failed to get the current working directory."; |
| 683 |
} |
| 684 |
#endif |
| 685 |
|
| 686 |
GetTestSuite(test_info->test_suite_name(), test_info->type_param(), |
| 687 |
set_up_tc, tear_down_tc) |
| 688 |
->AddTestInfo(test_info); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() { |
| 694 |
return parameterized_test_registry_; |
| 695 |
} |
| 696 |
|
| 697 |
std::set<std::string>* ignored_parameterized_test_suites() { |
| 698 |
return &ignored_parameterized_test_suites_; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
internal::TypeParameterizedTestSuiteRegistry& |
| 704 |
type_parameterized_test_registry() { |
| 705 |
return type_parameterized_test_registry_; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
void set_current_test_suite(TestSuite* a_current_test_suite) { |
| 710 |
current_test_suite_ = a_current_test_suite; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
void set_current_test_info(TestInfo* a_current_test_info) { |
| 717 |
current_test_info_ = a_current_test_info; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
void RegisterParameterizedTests(); |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
bool RunAllTests(); |
| 733 |
|
| 734 |
|
| 735 |
void ClearNonAdHocTestResult() { |
| 736 |
ForEach(test_suites_, TestSuite::ClearTestSuiteResult); |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
void ClearAdHocTestResult() { ad_hoc_test_result_.Clear(); } |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
void RecordProperty(const TestProperty& test_property); |
| 747 |
|
| 748 |
enum ReactionToSharding { HONOR_SHARDING_PROTOCOL, IGNORE_SHARDING_PROTOCOL }; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
int FilterTests(ReactionToSharding shard_tests); |
| 757 |
|
| 758 |
|
| 759 |
void ListTestsMatchingFilter(); |
| 760 |
|
| 761 |
const TestSuite* current_test_suite() const { return current_test_suite_; } |
| 762 |
TestInfo* current_test_info() { return current_test_info_; } |
| 763 |
const TestInfo* current_test_info() const { return current_test_info_; } |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
std::vector<Environment*>& environments() { return environments_; } |
| 768 |
|
| 769 |
|
| 770 |
std::vector<TraceInfo>& gtest_trace_stack() { |
| 771 |
return *(gtest_trace_stack_.pointer()); |
| 772 |
} |
| 773 |
const std::vector<TraceInfo>& gtest_trace_stack() const { |
| 774 |
return gtest_trace_stack_.get(); |
| 775 |
} |
| 776 |
|
| 777 |
#if GTEST_HAS_DEATH_TEST |
| 778 |
void InitDeathTestSubprocessControlInfo() { |
| 779 |
internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
const InternalRunDeathTestFlag* internal_run_death_test_flag() const { |
| 786 |
return internal_run_death_test_flag_.get(); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
internal::DeathTestFactory* death_test_factory() { |
| 791 |
return death_test_factory_.get(); |
| 792 |
} |
| 793 |
|
| 794 |
void SuppressTestEventsIfInSubprocess(); |
| 795 |
|
| 796 |
friend class ReplaceDeathTestFactory; |
| 797 |
#endif |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
void ConfigureXmlOutput(); |
| 802 |
|
| 803 |
#if GTEST_CAN_STREAM_RESULTS_ |
| 804 |
|
| 805 |
|
| 806 |
void ConfigureStreamingOutput(); |
| 807 |
#endif |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
void PostFlagParsingInit(); |
| 815 |
|
| 816 |
|
| 817 |
int random_seed() const { return random_seed_; } |
| 818 |
|
| 819 |
|
| 820 |
internal::Random* random() { return &random_; } |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
void ShuffleTests(); |
| 825 |
|
| 826 |
|
| 827 |
void UnshuffleTests(); |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
bool catch_exceptions() const { return catch_exceptions_; } |
| 832 |
|
| 833 |
private: |
| 834 |
friend class ::testing::UnitTest; |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
void set_catch_exceptions(bool value) { catch_exceptions_ = value; } |
| 839 |
|
| 840 |
|
| 841 |
UnitTest* const parent_; |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
internal::FilePath original_working_dir_; |
| 846 |
|
| 847 |
|
| 848 |
DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_; |
| 849 |
DefaultPerThreadTestPartResultReporter |
| 850 |
default_per_thread_test_part_result_reporter_; |
| 851 |
|
| 852 |
|
| 853 |
TestPartResultReporterInterface* global_test_part_result_repoter_; |
| 854 |
|
| 855 |
|
| 856 |
internal::Mutex global_test_part_result_reporter_mutex_; |
| 857 |
|
| 858 |
|
| 859 |
internal::ThreadLocal<TestPartResultReporterInterface*> |
| 860 |
per_thread_test_part_result_reporter_; |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
std::vector<Environment*> environments_; |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
std::vector<TestSuite*> test_suites_; |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
std::vector<int> test_suite_indices_; |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
internal::ParameterizedTestSuiteRegistry parameterized_test_registry_; |
| 879 |
internal::TypeParameterizedTestSuiteRegistry |
| 880 |
type_parameterized_test_registry_; |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
std::set<std::string> ignored_parameterized_test_suites_; |
| 885 |
|
| 886 |
|
| 887 |
bool parameterized_tests_registered_; |
| 888 |
|
| 889 |
|
| 890 |
int last_death_test_suite_; |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
TestSuite* current_test_suite_; |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
TestInfo* current_test_info_; |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
TestResult ad_hoc_test_result_; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
TestEventListeners listeners_; |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
OsStackTraceGetterInterface* os_stack_trace_getter_; |
| 923 |
|
| 924 |
|
| 925 |
bool post_flag_parse_init_performed_; |
| 926 |
|
| 927 |
|
| 928 |
int random_seed_; |
| 929 |
|
| 930 |
|
| 931 |
internal::Random random_; |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
TimeInMillis start_timestamp_; |
| 936 |
|
| 937 |
|
| 938 |
TimeInMillis elapsed_time_; |
| 939 |
|
| 940 |
#if GTEST_HAS_DEATH_TEST |
| 941 |
|
| 942 |
|
| 943 |
std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_; |
| 944 |
std::unique_ptr<internal::DeathTestFactory> death_test_factory_; |
| 945 |
#endif |
| 946 |
|
| 947 |
|
| 948 |
internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_; |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
bool catch_exceptions_; |
| 953 |
|
| 954 |
UnitTestImpl(const UnitTestImpl&) = delete; |
| 955 |
UnitTestImpl& operator=(const UnitTestImpl&) = delete; |
| 956 |
}; |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
inline UnitTestImpl* GetUnitTestImpl() { |
| 961 |
return UnitTest::GetInstance()->impl(); |
| 962 |
} |
| 963 |
|
| 964 |
#if GTEST_USES_SIMPLE_RE |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
GTEST_API_ bool IsInSet(char ch, const char* str); |
| 969 |
GTEST_API_ bool IsAsciiDigit(char ch); |
| 970 |
GTEST_API_ bool IsAsciiPunct(char ch); |
| 971 |
GTEST_API_ bool IsRepeat(char ch); |
| 972 |
GTEST_API_ bool IsAsciiWhiteSpace(char ch); |
| 973 |
GTEST_API_ bool IsAsciiWordChar(char ch); |
| 974 |
GTEST_API_ bool IsValidEscape(char ch); |
| 975 |
GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch); |
| 976 |
GTEST_API_ bool ValidateRegex(const char* regex); |
| 977 |
GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str); |
| 978 |
GTEST_API_ bool MatchRepetitionAndRegexAtHead(bool escaped, char ch, |
| 979 |
char repeat, const char* regex, |
| 980 |
const char* str); |
| 981 |
GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str); |
| 982 |
|
| 983 |
#endif |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); |
| 988 |
GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); |
| 989 |
|
| 990 |
#if GTEST_HAS_DEATH_TEST |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
GTEST_API_ std::string GetLastErrnoDescription(); |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
template <typename Integer> |
| 1001 |
bool ParseNaturalNumber(const ::std::string& str, Integer* number) { |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
if (str.empty() || !IsDigit(str[0])) { |
| 1006 |
return false; |
| 1007 |
} |
| 1008 |
errno = 0; |
| 1009 |
|
| 1010 |
char* end; |
| 1011 |
|
| 1012 |
|
| 1013 |
using BiggestConvertible = unsigned long long; |
| 1014 |
|
| 1015 |
const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); |
| 1016 |
const bool parse_success = *end == '\0' && errno == 0; |
| 1017 |
|
| 1018 |
GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed)); |
| 1019 |
|
| 1020 |
const Integer result = static_cast<Integer>(parsed); |
| 1021 |
if (parse_success && static_cast<BiggestConvertible>(result) == parsed) { |
| 1022 |
*number = result; |
| 1023 |
return true; |
| 1024 |
} |
| 1025 |
return false; |
| 1026 |
} |
| 1027 |
#endif |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
class TestResultAccessor { |
| 1036 |
public: |
| 1037 |
static void RecordProperty(TestResult* test_result, |
| 1038 |
const std::string& xml_element, |
| 1039 |
const TestProperty& property) { |
| 1040 |
test_result->RecordProperty(xml_element, property); |
| 1041 |
} |
| 1042 |
|
| 1043 |
static void ClearTestPartResults(TestResult* test_result) { |
| 1044 |
test_result->ClearTestPartResults(); |
| 1045 |
} |
| 1046 |
|
| 1047 |
static const std::vector<testing::TestPartResult>& test_part_results( |
| 1048 |
const TestResult& test_result) { |
| 1049 |
return test_result.test_part_results(); |
| 1050 |
} |
| 1051 |
}; |
| 1052 |
|
| 1053 |
#if GTEST_CAN_STREAM_RESULTS_ |
| 1054 |
|
| 1055 |
|
| 1056 |
class StreamingListener : public EmptyTestEventListener { |
| 1057 |
public: |
| 1058 |
|
| 1059 |
class AbstractSocketWriter { |
| 1060 |
public: |
| 1061 |
virtual ~AbstractSocketWriter() {} |
| 1062 |
|
| 1063 |
|
| 1064 |
virtual void Send(const std::string& message) = 0; |
| 1065 |
|
| 1066 |
|
| 1067 |
virtual void CloseConnection() {} |
| 1068 |
|
| 1069 |
|
| 1070 |
void SendLn(const std::string& message) { Send(message + "\n"); } |
| 1071 |
}; |
| 1072 |
|
| 1073 |
|
| 1074 |
class SocketWriter : public AbstractSocketWriter { |
| 1075 |
public: |
| 1076 |
SocketWriter(const std::string& host, const std::string& port) |
| 1077 |
: sockfd_(-1), host_name_(host), port_num_(port) { |
| 1078 |
MakeConnection(); |
| 1079 |
} |
| 1080 |
|
| 1081 |
~SocketWriter() override { |
| 1082 |
if (sockfd_ != -1) CloseConnection(); |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
void Send(const std::string& message) override { |
| 1087 |
GTEST_CHECK_(sockfd_ != -1) |
| 1088 |
<< "Send() can be called only when there is a connection."; |
| 1089 |
|
| 1090 |
const auto len = static_cast<size_t>(message.length()); |
| 1091 |
if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) { |
| 1092 |
GTEST_LOG_(WARNING) << "stream_result_to: failed to stream to " |
| 1093 |
<< host_name_ << ":" << port_num_; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
private: |
| 1098 |
|
| 1099 |
void MakeConnection(); |
| 1100 |
|
| 1101 |
|
| 1102 |
void CloseConnection() override { |
| 1103 |
GTEST_CHECK_(sockfd_ != -1) |
| 1104 |
<< "CloseConnection() can be called only when there is a connection."; |
| 1105 |
|
| 1106 |
close(sockfd_); |
| 1107 |
sockfd_ = -1; |
| 1108 |
} |
| 1109 |
|
| 1110 |
int sockfd_; |
| 1111 |
const std::string host_name_; |
| 1112 |
const std::string port_num_; |
| 1113 |
|
| 1114 |
SocketWriter(const SocketWriter&) = delete; |
| 1115 |
SocketWriter& operator=(const SocketWriter&) = delete; |
| 1116 |
}; |
| 1117 |
|
| 1118 |
|
| 1119 |
static std::string UrlEncode(const char* str); |
| 1120 |
|
| 1121 |
StreamingListener(const std::string& host, const std::string& port) |
| 1122 |
: socket_writer_(new SocketWriter(host, port)) { |
| 1123 |
Start(); |
| 1124 |
} |
| 1125 |
|
| 1126 |
explicit StreamingListener(AbstractSocketWriter* socket_writer) |
| 1127 |
: socket_writer_(socket_writer) { |
| 1128 |
Start(); |
| 1129 |
} |
| 1130 |
|
| 1131 |
void OnTestProgramStart(const UnitTest& ) override { |
| 1132 |
SendLn("event=TestProgramStart"); |
| 1133 |
} |
| 1134 |
|
| 1135 |
void OnTestProgramEnd(const UnitTest& unit_test) override { |
| 1136 |
|
| 1137 |
|
| 1138 |
SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed())); |
| 1139 |
|
| 1140 |
|
| 1141 |
socket_writer_->CloseConnection(); |
| 1142 |
} |
| 1143 |
|
| 1144 |
void OnTestIterationStart(const UnitTest& , |
| 1145 |
int iteration) override { |
| 1146 |
SendLn("event=TestIterationStart&iteration=" + |
| 1147 |
StreamableToString(iteration)); |
| 1148 |
} |
| 1149 |
|
| 1150 |
void OnTestIterationEnd(const UnitTest& unit_test, |
| 1151 |
int ) override { |
| 1152 |
SendLn("event=TestIterationEnd&passed=" + FormatBool(unit_test.Passed()) + |
| 1153 |
"&elapsed_time=" + StreamableToString(unit_test.elapsed_time()) + |
| 1154 |
"ms"); |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
void OnTestSuiteStart(const TestSuite& test_suite) override { |
| 1160 |
SendLn(std::string("event=TestCaseStart&name=") + test_suite.name()); |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
void OnTestSuiteEnd(const TestSuite& test_suite) override { |
| 1166 |
SendLn("event=TestCaseEnd&passed=" + FormatBool(test_suite.Passed()) + |
| 1167 |
"&elapsed_time=" + StreamableToString(test_suite.elapsed_time()) + |
| 1168 |
"ms"); |
| 1169 |
} |
| 1170 |
|
| 1171 |
void OnTestStart(const TestInfo& test_info) override { |
| 1172 |
SendLn(std::string("event=TestStart&name=") + test_info.name()); |
| 1173 |
} |
| 1174 |
|
| 1175 |
void OnTestEnd(const TestInfo& test_info) override { |
| 1176 |
SendLn("event=TestEnd&passed=" + |
| 1177 |
FormatBool((test_info.result())->Passed()) + "&elapsed_time=" + |
| 1178 |
StreamableToString((test_info.result())->elapsed_time()) + "ms"); |
| 1179 |
} |
| 1180 |
|
| 1181 |
void OnTestPartResult(const TestPartResult& test_part_result) override { |
| 1182 |
const char* file_name = test_part_result.file_name(); |
| 1183 |
if (file_name == nullptr) file_name = ""; |
| 1184 |
SendLn("event=TestPartResult&file=" + UrlEncode(file_name) + |
| 1185 |
"&line=" + StreamableToString(test_part_result.line_number()) + |
| 1186 |
"&message=" + UrlEncode(test_part_result.message())); |
| 1187 |
} |
| 1188 |
|
| 1189 |
private: |
| 1190 |
|
| 1191 |
void SendLn(const std::string& message) { socket_writer_->SendLn(message); } |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
void Start() { SendLn("gtest_streaming_protocol_version=1.0"); } |
| 1196 |
|
| 1197 |
std::string FormatBool(bool value) { return value ? "1" : "0"; } |
| 1198 |
|
| 1199 |
const std::unique_ptr<AbstractSocketWriter> socket_writer_; |
| 1200 |
|
| 1201 |
StreamingListener(const StreamingListener&) = delete; |
| 1202 |
StreamingListener& operator=(const StreamingListener&) = delete; |
| 1203 |
}; |
| 1204 |
|
| 1205 |
#endif |
| 1206 |
|
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
GTEST_DISABLE_MSC_WARNINGS_POP_() |
| 1211 |
|
| 1212 |
#endif |