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