00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
00038 #define GTEST_SRC_GTEST_INTERNAL_INL_H_
00039
00040
00041
00042 #if !GTEST_IMPLEMENTATION_
00043
00044 # error "gtest-internal-inl.h is part of Google Test's internal implementation."
00045 # error "It must not be included except by Google Test itself."
00046 #endif // GTEST_IMPLEMENTATION_
00047
00048 #ifndef _WIN32_WCE
00049 # include <errno.h>
00050 #endif // !_WIN32_WCE
00051 #include <stddef.h>
00052 #include <stdlib.h>
00053 #include <string.h>
00054
00055 #include <algorithm>
00056 #include <string>
00057 #include <vector>
00058
00059 #include "gtest/internal/gtest-port.h"
00060
00061 #if GTEST_CAN_STREAM_RESULTS_
00062 # include <arpa/inet.h>
00063 # include <netdb.h>
00064 #endif
00065
00066 #if GTEST_OS_WINDOWS
00067 # include <windows.h>
00068 #endif // GTEST_OS_WINDOWS
00069
00070 #include "gtest/gtest.h"
00071 #include "gtest/gtest-spi.h"
00072
00073 namespace testing {
00074
00075
00076
00077
00078
00079
00080 GTEST_DECLARE_bool_(death_test_use_fork);
00081
00082 namespace internal {
00083
00084
00085
00086 GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
00087
00088
00089 const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
00090 const char kBreakOnFailureFlag[] = "break_on_failure";
00091 const char kCatchExceptionsFlag[] = "catch_exceptions";
00092 const char kColorFlag[] = "color";
00093 const char kFilterFlag[] = "filter";
00094 const char kListTestsFlag[] = "list_tests";
00095 const char kOutputFlag[] = "output";
00096 const char kPrintTimeFlag[] = "print_time";
00097 const char kRandomSeedFlag[] = "random_seed";
00098 const char kRepeatFlag[] = "repeat";
00099 const char kShuffleFlag[] = "shuffle";
00100 const char kStackTraceDepthFlag[] = "stack_trace_depth";
00101 const char kStreamResultToFlag[] = "stream_result_to";
00102 const char kThrowOnFailureFlag[] = "throw_on_failure";
00103
00104
00105 const int kMaxRandomSeed = 99999;
00106
00107
00108
00109 GTEST_API_ extern bool g_help_flag;
00110
00111
00112 GTEST_API_ TimeInMillis GetTimeInMillis();
00113
00114
00115 GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
00116
00117
00118 GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
00119
00120
00121
00122
00123
00124 GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms);
00125
00126
00127
00128
00129
00130 GTEST_API_ bool ParseInt32Flag(
00131 const char* str, const char* flag, Int32* value);
00132
00133
00134
00135 inline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
00136 const unsigned int raw_seed = (random_seed_flag == 0) ?
00137 static_cast<unsigned int>(GetTimeInMillis()) :
00138 static_cast<unsigned int>(random_seed_flag);
00139
00140
00141
00142 const int normalized_seed =
00143 static_cast<int>((raw_seed - 1U) %
00144 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
00145 return normalized_seed;
00146 }
00147
00148
00149
00150
00151 inline int GetNextRandomSeed(int seed) {
00152 GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
00153 << "Invalid random seed " << seed << " - must be in [1, "
00154 << kMaxRandomSeed << "].";
00155 const int next_seed = seed + 1;
00156 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
00157 }
00158
00159
00160
00161 class GTestFlagSaver {
00162 public:
00163
00164 GTestFlagSaver() {
00165 also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
00166 break_on_failure_ = GTEST_FLAG(break_on_failure);
00167 catch_exceptions_ = GTEST_FLAG(catch_exceptions);
00168 color_ = GTEST_FLAG(color);
00169 death_test_style_ = GTEST_FLAG(death_test_style);
00170 death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
00171 filter_ = GTEST_FLAG(filter);
00172 internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
00173 list_tests_ = GTEST_FLAG(list_tests);
00174 output_ = GTEST_FLAG(output);
00175 print_time_ = GTEST_FLAG(print_time);
00176 random_seed_ = GTEST_FLAG(random_seed);
00177 repeat_ = GTEST_FLAG(repeat);
00178 shuffle_ = GTEST_FLAG(shuffle);
00179 stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
00180 stream_result_to_ = GTEST_FLAG(stream_result_to);
00181 throw_on_failure_ = GTEST_FLAG(throw_on_failure);
00182 }
00183
00184
00185 ~GTestFlagSaver() {
00186 GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
00187 GTEST_FLAG(break_on_failure) = break_on_failure_;
00188 GTEST_FLAG(catch_exceptions) = catch_exceptions_;
00189 GTEST_FLAG(color) = color_;
00190 GTEST_FLAG(death_test_style) = death_test_style_;
00191 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
00192 GTEST_FLAG(filter) = filter_;
00193 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
00194 GTEST_FLAG(list_tests) = list_tests_;
00195 GTEST_FLAG(output) = output_;
00196 GTEST_FLAG(print_time) = print_time_;
00197 GTEST_FLAG(random_seed) = random_seed_;
00198 GTEST_FLAG(repeat) = repeat_;
00199 GTEST_FLAG(shuffle) = shuffle_;
00200 GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
00201 GTEST_FLAG(stream_result_to) = stream_result_to_;
00202 GTEST_FLAG(throw_on_failure) = throw_on_failure_;
00203 }
00204
00205 private:
00206
00207 bool also_run_disabled_tests_;
00208 bool break_on_failure_;
00209 bool catch_exceptions_;
00210 std::string color_;
00211 std::string death_test_style_;
00212 bool death_test_use_fork_;
00213 std::string filter_;
00214 std::string internal_run_death_test_;
00215 bool list_tests_;
00216 std::string output_;
00217 bool print_time_;
00218 internal::Int32 random_seed_;
00219 internal::Int32 repeat_;
00220 bool shuffle_;
00221 internal::Int32 stack_trace_depth_;
00222 std::string stream_result_to_;
00223 bool throw_on_failure_;
00224 } GTEST_ATTRIBUTE_UNUSED_;
00225
00226
00227
00228
00229
00230
00231
00232 GTEST_API_ std::string CodePointToUtf8(UInt32 code_point);
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars);
00248
00249
00250
00251
00252
00253 void WriteToShardStatusFileIfNeeded();
00254
00255
00256
00257
00258
00259
00260
00261 GTEST_API_ bool ShouldShard(const char* total_shards_str,
00262 const char* shard_index_str,
00263 bool in_subprocess_for_death_test);
00264
00265
00266
00267
00268 GTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
00269
00270
00271
00272
00273
00274 GTEST_API_ bool ShouldRunTestOnShard(
00275 int total_shards, int shard_index, int test_id);
00276
00277
00278
00279
00280
00281 template <class Container, typename Predicate>
00282 inline int CountIf(const Container& c, Predicate predicate) {
00283
00284
00285 int count = 0;
00286 for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
00287 if (predicate(*it))
00288 ++count;
00289 }
00290 return count;
00291 }
00292
00293
00294 template <class Container, typename Functor>
00295 void ForEach(const Container& c, Functor functor) {
00296 std::for_each(c.begin(), c.end(), functor);
00297 }
00298
00299
00300
00301 template <typename E>
00302 inline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
00303 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
00304 }
00305
00306
00307
00308
00309
00310 template <typename E>
00311 void ShuffleRange(internal::Random* random, int begin, int end,
00312 std::vector<E>* v) {
00313 const int size = static_cast<int>(v->size());
00314 GTEST_CHECK_(0 <= begin && begin <= size)
00315 << "Invalid shuffle range start " << begin << ": must be in range [0, "
00316 << size << "].";
00317 GTEST_CHECK_(begin <= end && end <= size)
00318 << "Invalid shuffle range finish " << end << ": must be in range ["
00319 << begin << ", " << size << "].";
00320
00321
00322
00323 for (int range_width = end - begin; range_width >= 2; range_width--) {
00324 const int last_in_range = begin + range_width - 1;
00325 const int selected = begin + random->Generate(range_width);
00326 std::swap((*v)[selected], (*v)[last_in_range]);
00327 }
00328 }
00329
00330
00331 template <typename E>
00332 inline void Shuffle(internal::Random* random, std::vector<E>* v) {
00333 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
00334 }
00335
00336
00337
00338 template <typename T>
00339 static void Delete(T* x) {
00340 delete x;
00341 }
00342
00343
00344
00345
00346 class TestPropertyKeyIs {
00347 public:
00348
00349
00350
00351 explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}
00352
00353
00354 bool operator()(const TestProperty& test_property) const {
00355 return test_property.key() == key_;
00356 }
00357
00358 private:
00359 std::string key_;
00360 };
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 class GTEST_API_ UnitTestOptions {
00373 public:
00374
00375
00376
00377 static std::string GetOutputFormat();
00378
00379
00380
00381
00382 static std::string GetAbsolutePathToOutputFile();
00383
00384
00385
00386
00387
00388
00389
00390
00391 static bool PatternMatchesString(const char *pattern, const char *str);
00392
00393
00394
00395 static bool FilterMatchesTest(const std::string &test_case_name,
00396 const std::string &test_name);
00397
00398 #if GTEST_OS_WINDOWS
00399
00400
00401
00402
00403
00404 static int GTestShouldProcessSEH(DWORD exception_code);
00405 #endif // GTEST_OS_WINDOWS
00406
00407
00408
00409 static bool MatchesFilter(const std::string& name, const char* filter);
00410 };
00411
00412
00413
00414 GTEST_API_ FilePath GetCurrentExecutableName();
00415
00416
00417 class OsStackTraceGetterInterface {
00418 public:
00419 OsStackTraceGetterInterface() {}
00420 virtual ~OsStackTraceGetterInterface() {}
00421
00422
00423
00424
00425
00426
00427
00428 virtual string CurrentStackTrace(int max_depth, int skip_count) = 0;
00429
00430
00431
00432
00433 virtual void UponLeavingGTest() = 0;
00434
00435 private:
00436 GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
00437 };
00438
00439
00440 class OsStackTraceGetter : public OsStackTraceGetterInterface {
00441 public:
00442 OsStackTraceGetter() : caller_frame_(NULL) {}
00443
00444 virtual string CurrentStackTrace(int max_depth, int skip_count)
00445 GTEST_LOCK_EXCLUDED_(mutex_);
00446
00447 virtual void UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_);
00448
00449
00450
00451 static const char* const kElidedFramesMarker;
00452
00453 private:
00454 Mutex mutex_;
00455
00456
00457
00458
00459
00460 void* caller_frame_;
00461
00462 GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
00463 };
00464
00465
00466 struct TraceInfo {
00467 const char* file;
00468 int line;
00469 std::string message;
00470 };
00471
00472
00473
00474 class DefaultGlobalTestPartResultReporter
00475 : public TestPartResultReporterInterface {
00476 public:
00477 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
00478
00479
00480 virtual void ReportTestPartResult(const TestPartResult& result);
00481
00482 private:
00483 UnitTestImpl* const unit_test_;
00484
00485 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
00486 };
00487
00488
00489
00490 class DefaultPerThreadTestPartResultReporter
00491 : public TestPartResultReporterInterface {
00492 public:
00493 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
00494
00495
00496 virtual void ReportTestPartResult(const TestPartResult& result);
00497
00498 private:
00499 UnitTestImpl* const unit_test_;
00500
00501 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
00502 };
00503
00504
00505
00506
00507
00508 class GTEST_API_ UnitTestImpl {
00509 public:
00510 explicit UnitTestImpl(UnitTest* parent);
00511 virtual ~UnitTestImpl();
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
00522
00523
00524 void SetGlobalTestPartResultReporter(
00525 TestPartResultReporterInterface* reporter);
00526
00527
00528 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
00529
00530
00531 void SetTestPartResultReporterForCurrentThread(
00532 TestPartResultReporterInterface* reporter);
00533
00534
00535 int successful_test_case_count() const;
00536
00537
00538 int failed_test_case_count() const;
00539
00540
00541 int total_test_case_count() const;
00542
00543
00544
00545 int test_case_to_run_count() const;
00546
00547
00548 int successful_test_count() const;
00549
00550
00551 int failed_test_count() const;
00552
00553
00554 int reportable_disabled_test_count() const;
00555
00556
00557 int disabled_test_count() const;
00558
00559
00560 int reportable_test_count() const;
00561
00562
00563 int total_test_count() const;
00564
00565
00566 int test_to_run_count() const;
00567
00568
00569
00570 TimeInMillis start_timestamp() const { return start_timestamp_; }
00571
00572
00573 TimeInMillis elapsed_time() const { return elapsed_time_; }
00574
00575
00576 bool Passed() const { return !Failed(); }
00577
00578
00579
00580 bool Failed() const {
00581 return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
00582 }
00583
00584
00585
00586 const TestCase* GetTestCase(int i) const {
00587 const int index = GetElementOr(test_case_indices_, i, -1);
00588 return index < 0 ? NULL : test_cases_[i];
00589 }
00590
00591
00592
00593 TestCase* GetMutableTestCase(int i) {
00594 const int index = GetElementOr(test_case_indices_, i, -1);
00595 return index < 0 ? NULL : test_cases_[index];
00596 }
00597
00598
00599 TestEventListeners* listeners() { return &listeners_; }
00600
00601
00602
00603 TestResult* current_test_result();
00604
00605
00606 const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
00607
00608
00609
00610
00611
00612
00613 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
00614
00615
00616
00617
00618 OsStackTraceGetterInterface* os_stack_trace_getter();
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 TestCase* GetTestCase(const char* test_case_name,
00643 const char* type_param,
00644 Test::SetUpTestCaseFunc set_up_tc,
00645 Test::TearDownTestCaseFunc tear_down_tc);
00646
00647
00648
00649
00650
00651
00652
00653
00654 void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
00655 Test::TearDownTestCaseFunc tear_down_tc,
00656 TestInfo* test_info) {
00657
00658
00659
00660
00661
00662
00663
00664 if (original_working_dir_.IsEmpty()) {
00665 original_working_dir_.Set(FilePath::GetCurrentDir());
00666 GTEST_CHECK_(!original_working_dir_.IsEmpty())
00667 << "Failed to get the current working directory.";
00668 }
00669
00670 GetTestCase(test_info->test_case_name(),
00671 test_info->type_param(),
00672 set_up_tc,
00673 tear_down_tc)->AddTestInfo(test_info);
00674 }
00675
00676 #if GTEST_HAS_PARAM_TEST
00677
00678
00679 internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
00680 return parameterized_test_registry_;
00681 }
00682 #endif // GTEST_HAS_PARAM_TEST
00683
00684
00685 void set_current_test_case(TestCase* a_current_test_case) {
00686 current_test_case_ = a_current_test_case;
00687 }
00688
00689
00690
00691
00692 void set_current_test_info(TestInfo* a_current_test_info) {
00693 current_test_info_ = a_current_test_info;
00694 }
00695
00696
00697
00698
00699
00700
00701
00702 void RegisterParameterizedTests();
00703
00704
00705
00706
00707
00708 bool RunAllTests();
00709
00710
00711 void ClearNonAdHocTestResult() {
00712 ForEach(test_cases_, TestCase::ClearTestCaseResult);
00713 }
00714
00715
00716 void ClearAdHocTestResult() {
00717 ad_hoc_test_result_.Clear();
00718 }
00719
00720
00721
00722
00723
00724 void RecordProperty(const TestProperty& test_property);
00725
00726 enum ReactionToSharding {
00727 HONOR_SHARDING_PROTOCOL,
00728 IGNORE_SHARDING_PROTOCOL
00729 };
00730
00731
00732
00733
00734
00735
00736
00737 int FilterTests(ReactionToSharding shard_tests);
00738
00739
00740 void ListTestsMatchingFilter();
00741
00742 const TestCase* current_test_case() const { return current_test_case_; }
00743 TestInfo* current_test_info() { return current_test_info_; }
00744 const TestInfo* current_test_info() const { return current_test_info_; }
00745
00746
00747
00748 std::vector<Environment*>& environments() { return environments_; }
00749
00750
00751 std::vector<TraceInfo>& gtest_trace_stack() {
00752 return *(gtest_trace_stack_.pointer());
00753 }
00754 const std::vector<TraceInfo>& gtest_trace_stack() const {
00755 return gtest_trace_stack_.get();
00756 }
00757
00758 #if GTEST_HAS_DEATH_TEST
00759 void InitDeathTestSubprocessControlInfo() {
00760 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
00761 }
00762
00763
00764
00765
00766 const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
00767 return internal_run_death_test_flag_.get();
00768 }
00769
00770
00771 internal::DeathTestFactory* death_test_factory() {
00772 return death_test_factory_.get();
00773 }
00774
00775 void SuppressTestEventsIfInSubprocess();
00776
00777 friend class ReplaceDeathTestFactory;
00778 #endif // GTEST_HAS_DEATH_TEST
00779
00780
00781
00782 void ConfigureXmlOutput();
00783
00784 #if GTEST_CAN_STREAM_RESULTS_
00785
00786
00787 void ConfigureStreamingOutput();
00788 #endif
00789
00790
00791
00792
00793
00794
00795 void PostFlagParsingInit();
00796
00797
00798 int random_seed() const { return random_seed_; }
00799
00800
00801 internal::Random* random() { return &random_; }
00802
00803
00804
00805 void ShuffleTests();
00806
00807
00808 void UnshuffleTests();
00809
00810
00811
00812 bool catch_exceptions() const { return catch_exceptions_; }
00813
00814 private:
00815 friend class ::testing::UnitTest;
00816
00817
00818
00819 void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
00820
00821
00822 UnitTest* const parent_;
00823
00824
00825
00826 internal::FilePath original_working_dir_;
00827
00828
00829 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
00830 DefaultPerThreadTestPartResultReporter
00831 default_per_thread_test_part_result_reporter_;
00832
00833
00834 TestPartResultReporterInterface* global_test_part_result_repoter_;
00835
00836
00837 internal::Mutex global_test_part_result_reporter_mutex_;
00838
00839
00840 internal::ThreadLocal<TestPartResultReporterInterface*>
00841 per_thread_test_part_result_reporter_;
00842
00843
00844
00845 std::vector<Environment*> environments_;
00846
00847
00848
00849 std::vector<TestCase*> test_cases_;
00850
00851
00852
00853
00854
00855 std::vector<int> test_case_indices_;
00856
00857 #if GTEST_HAS_PARAM_TEST
00858
00859
00860 internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
00861
00862
00863 bool parameterized_tests_registered_;
00864 #endif // GTEST_HAS_PARAM_TEST
00865
00866
00867 int last_death_test_case_;
00868
00869
00870
00871
00872
00873 TestCase* current_test_case_;
00874
00875
00876
00877
00878
00879 TestInfo* current_test_info_;
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889 TestResult ad_hoc_test_result_;
00890
00891
00892
00893 TestEventListeners listeners_;
00894
00895
00896
00897
00898
00899 OsStackTraceGetterInterface* os_stack_trace_getter_;
00900
00901
00902 bool post_flag_parse_init_performed_;
00903
00904
00905 int random_seed_;
00906
00907
00908 internal::Random random_;
00909
00910
00911
00912 TimeInMillis start_timestamp_;
00913
00914
00915 TimeInMillis elapsed_time_;
00916
00917 #if GTEST_HAS_DEATH_TEST
00918
00919
00920 internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
00921 internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
00922 #endif // GTEST_HAS_DEATH_TEST
00923
00924
00925 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
00926
00927
00928
00929 bool catch_exceptions_;
00930
00931 GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
00932 };
00933
00934
00935
00936 inline UnitTestImpl* GetUnitTestImpl() {
00937 return UnitTest::GetInstance()->impl();
00938 }
00939
00940 #if GTEST_USES_SIMPLE_RE
00941
00942
00943
00944 GTEST_API_ bool IsInSet(char ch, const char* str);
00945 GTEST_API_ bool IsAsciiDigit(char ch);
00946 GTEST_API_ bool IsAsciiPunct(char ch);
00947 GTEST_API_ bool IsRepeat(char ch);
00948 GTEST_API_ bool IsAsciiWhiteSpace(char ch);
00949 GTEST_API_ bool IsAsciiWordChar(char ch);
00950 GTEST_API_ bool IsValidEscape(char ch);
00951 GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
00952 GTEST_API_ bool ValidateRegex(const char* regex);
00953 GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
00954 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
00955 bool escaped, char ch, char repeat, const char* regex, const char* str);
00956 GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
00957
00958 #endif // GTEST_USES_SIMPLE_RE
00959
00960
00961
00962 GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
00963 GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
00964
00965 #if GTEST_HAS_DEATH_TEST
00966
00967
00968
00969 GTEST_API_ std::string GetLastErrnoDescription();
00970
00971 # if GTEST_OS_WINDOWS
00972
00973 class AutoHandle {
00974 public:
00975 AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
00976 explicit AutoHandle(HANDLE handle) : handle_(handle) {}
00977
00978 ~AutoHandle() { Reset(); }
00979
00980 HANDLE Get() const { return handle_; }
00981 void Reset() { Reset(INVALID_HANDLE_VALUE); }
00982 void Reset(HANDLE handle) {
00983 if (handle != handle_) {
00984 if (handle_ != INVALID_HANDLE_VALUE)
00985 ::CloseHandle(handle_);
00986 handle_ = handle;
00987 }
00988 }
00989
00990 private:
00991 HANDLE handle_;
00992
00993 GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
00994 };
00995 # endif // GTEST_OS_WINDOWS
00996
00997
00998
00999
01000
01001 template <typename Integer>
01002 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
01003
01004
01005
01006 if (str.empty() || !IsDigit(str[0])) {
01007 return false;
01008 }
01009 errno = 0;
01010
01011 char* end;
01012
01013
01014
01015 # if GTEST_OS_WINDOWS && !defined(__GNUC__)
01016
01017
01018 typedef unsigned __int64 BiggestConvertible;
01019 const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
01020
01021 # else
01022
01023 typedef unsigned long long BiggestConvertible;
01024 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
01025
01026 # endif // GTEST_OS_WINDOWS && !defined(__GNUC__)
01027
01028 const bool parse_success = *end == '\0' && errno == 0;
01029
01030
01031
01032 GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
01033
01034 const Integer result = static_cast<Integer>(parsed);
01035 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
01036 *number = result;
01037 return true;
01038 }
01039 return false;
01040 }
01041 #endif // GTEST_HAS_DEATH_TEST
01042
01043
01044
01045
01046
01047
01048
01049 class TestResultAccessor {
01050 public:
01051 static void RecordProperty(TestResult* test_result,
01052 const std::string& xml_element,
01053 const TestProperty& property) {
01054 test_result->RecordProperty(xml_element, property);
01055 }
01056
01057 static void ClearTestPartResults(TestResult* test_result) {
01058 test_result->ClearTestPartResults();
01059 }
01060
01061 static const std::vector<testing::TestPartResult>& test_part_results(
01062 const TestResult& test_result) {
01063 return test_result.test_part_results();
01064 }
01065 };
01066
01067 #if GTEST_CAN_STREAM_RESULTS_
01068
01069
01070 class StreamingListener : public EmptyTestEventListener {
01071 public:
01072
01073 class AbstractSocketWriter {
01074 public:
01075 virtual ~AbstractSocketWriter() {}
01076
01077
01078 virtual void Send(const string& message) = 0;
01079
01080
01081 virtual void CloseConnection() {}
01082
01083
01084 void SendLn(const string& message) {
01085 Send(message + "\n");
01086 }
01087 };
01088
01089
01090 class SocketWriter : public AbstractSocketWriter {
01091 public:
01092 SocketWriter(const string& host, const string& port)
01093 : sockfd_(-1), host_name_(host), port_num_(port) {
01094 MakeConnection();
01095 }
01096
01097 virtual ~SocketWriter() {
01098 if (sockfd_ != -1)
01099 CloseConnection();
01100 }
01101
01102
01103 virtual void Send(const string& message) {
01104 GTEST_CHECK_(sockfd_ != -1)
01105 << "Send() can be called only when there is a connection.";
01106
01107 const int len = static_cast<int>(message.length());
01108 if (write(sockfd_, message.c_str(), len) != len) {
01109 GTEST_LOG_(WARNING)
01110 << "stream_result_to: failed to stream to "
01111 << host_name_ << ":" << port_num_;
01112 }
01113 }
01114
01115 private:
01116
01117 void MakeConnection();
01118
01119
01120 void CloseConnection() {
01121 GTEST_CHECK_(sockfd_ != -1)
01122 << "CloseConnection() can be called only when there is a connection.";
01123
01124 close(sockfd_);
01125 sockfd_ = -1;
01126 }
01127
01128 int sockfd_;
01129 const string host_name_;
01130 const string port_num_;
01131
01132 GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter);
01133 };
01134
01135
01136 static string UrlEncode(const char* str);
01137
01138 StreamingListener(const string& host, const string& port)
01139 : socket_writer_(new SocketWriter(host, port)) { Start(); }
01140
01141 explicit StreamingListener(AbstractSocketWriter* socket_writer)
01142 : socket_writer_(socket_writer) { Start(); }
01143
01144 void OnTestProgramStart(const UnitTest& ) {
01145 SendLn("event=TestProgramStart");
01146 }
01147
01148 void OnTestProgramEnd(const UnitTest& unit_test) {
01149
01150
01151 SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
01152
01153
01154 socket_writer_->CloseConnection();
01155 }
01156
01157 void OnTestIterationStart(const UnitTest& , int iteration) {
01158 SendLn("event=TestIterationStart&iteration=" +
01159 StreamableToString(iteration));
01160 }
01161
01162 void OnTestIterationEnd(const UnitTest& unit_test, int ) {
01163 SendLn("event=TestIterationEnd&passed=" +
01164 FormatBool(unit_test.Passed()) + "&elapsed_time=" +
01165 StreamableToString(unit_test.elapsed_time()) + "ms");
01166 }
01167
01168 void OnTestCaseStart(const TestCase& test_case) {
01169 SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
01170 }
01171
01172 void OnTestCaseEnd(const TestCase& test_case) {
01173 SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed())
01174 + "&elapsed_time=" + StreamableToString(test_case.elapsed_time())
01175 + "ms");
01176 }
01177
01178 void OnTestStart(const TestInfo& test_info) {
01179 SendLn(std::string("event=TestStart&name=") + test_info.name());
01180 }
01181
01182 void OnTestEnd(const TestInfo& test_info) {
01183 SendLn("event=TestEnd&passed=" +
01184 FormatBool((test_info.result())->Passed()) +
01185 "&elapsed_time=" +
01186 StreamableToString((test_info.result())->elapsed_time()) + "ms");
01187 }
01188
01189 void OnTestPartResult(const TestPartResult& test_part_result) {
01190 const char* file_name = test_part_result.file_name();
01191 if (file_name == NULL)
01192 file_name = "";
01193 SendLn("event=TestPartResult&file=" + UrlEncode(file_name) +
01194 "&line=" + StreamableToString(test_part_result.line_number()) +
01195 "&message=" + UrlEncode(test_part_result.message()));
01196 }
01197
01198 private:
01199
01200 void SendLn(const string& message) { socket_writer_->SendLn(message); }
01201
01202
01203
01204 void Start() { SendLn("gtest_streaming_protocol_version=1.0"); }
01205
01206 string FormatBool(bool value) { return value ? "1" : "0"; }
01207
01208 const scoped_ptr<AbstractSocketWriter> socket_writer_;
01209
01210 GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);
01211 };
01212
01213 #endif // GTEST_CAN_STREAM_RESULTS_
01214
01215 }
01216 }
01217
01218 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_