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