123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221 |
- #ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
- #define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
- #ifndef _WIN32_WCE
- # include <errno.h>
- #endif
- #include <stddef.h>
- #include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
- #include <string.h> // For memmove.
- #include <algorithm>
- #include <cstdint>
- #include <memory>
- #include <string>
- #include <vector>
- #include "gtest/internal/gtest-port.h"
- #if GTEST_CAN_STREAM_RESULTS_
- # include <arpa/inet.h> // NOLINT
- # include <netdb.h> // NOLINT
- #endif
- #if GTEST_OS_WINDOWS
- # include <windows.h> // NOLINT
- #endif
- #include "gtest/gtest.h"
- #include "gtest/gtest-spi.h"
- GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
- )
- namespace testing {
- GTEST_DECLARE_bool_(death_test_use_fork);
- namespace internal {
- GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
- const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
- const char kBreakOnFailureFlag[] = "break_on_failure";
- const char kCatchExceptionsFlag[] = "catch_exceptions";
- const char kColorFlag[] = "color";
- const char kFailFast[] = "fail_fast";
- const char kFilterFlag[] = "filter";
- const char kListTestsFlag[] = "list_tests";
- const char kOutputFlag[] = "output";
- const char kBriefFlag[] = "brief";
- const char kPrintTimeFlag[] = "print_time";
- const char kPrintUTF8Flag[] = "print_utf8";
- const char kRandomSeedFlag[] = "random_seed";
- const char kRepeatFlag[] = "repeat";
- const char kShuffleFlag[] = "shuffle";
- const char kStackTraceDepthFlag[] = "stack_trace_depth";
- const char kStreamResultToFlag[] = "stream_result_to";
- const char kThrowOnFailureFlag[] = "throw_on_failure";
- const char kFlagfileFlag[] = "flagfile";
- const int kMaxRandomSeed = 99999;
- GTEST_API_ extern bool g_help_flag;
- GTEST_API_ TimeInMillis GetTimeInMillis();
- GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
- GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
- GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms);
- GTEST_API_ bool ParseInt32Flag(
- const char* str, const char* flag, int32_t* value);
- inline int GetRandomSeedFromFlag(int32_t random_seed_flag) {
- const unsigned int raw_seed = (random_seed_flag == 0) ?
- static_cast<unsigned int>(GetTimeInMillis()) :
- static_cast<unsigned int>(random_seed_flag);
-
-
- const int normalized_seed =
- static_cast<int>((raw_seed - 1U) %
- static_cast<unsigned int>(kMaxRandomSeed)) + 1;
- return normalized_seed;
- }
- inline int GetNextRandomSeed(int seed) {
- GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
- << "Invalid random seed " << seed << " - must be in [1, "
- << kMaxRandomSeed << "].";
- const int next_seed = seed + 1;
- return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
- }
- class GTestFlagSaver {
- public:
-
- GTestFlagSaver() {
- also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
- break_on_failure_ = GTEST_FLAG(break_on_failure);
- catch_exceptions_ = GTEST_FLAG(catch_exceptions);
- color_ = GTEST_FLAG(color);
- death_test_style_ = GTEST_FLAG(death_test_style);
- death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
- fail_fast_ = GTEST_FLAG(fail_fast);
- filter_ = GTEST_FLAG(filter);
- internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
- list_tests_ = GTEST_FLAG(list_tests);
- output_ = GTEST_FLAG(output);
- brief_ = GTEST_FLAG(brief);
- print_time_ = GTEST_FLAG(print_time);
- print_utf8_ = GTEST_FLAG(print_utf8);
- random_seed_ = GTEST_FLAG(random_seed);
- repeat_ = GTEST_FLAG(repeat);
- shuffle_ = GTEST_FLAG(shuffle);
- stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
- stream_result_to_ = GTEST_FLAG(stream_result_to);
- throw_on_failure_ = GTEST_FLAG(throw_on_failure);
- }
-
- ~GTestFlagSaver() {
- GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
- GTEST_FLAG(break_on_failure) = break_on_failure_;
- GTEST_FLAG(catch_exceptions) = catch_exceptions_;
- GTEST_FLAG(color) = color_;
- GTEST_FLAG(death_test_style) = death_test_style_;
- GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
- GTEST_FLAG(filter) = filter_;
- GTEST_FLAG(fail_fast) = fail_fast_;
- GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
- GTEST_FLAG(list_tests) = list_tests_;
- GTEST_FLAG(output) = output_;
- GTEST_FLAG(brief) = brief_;
- GTEST_FLAG(print_time) = print_time_;
- GTEST_FLAG(print_utf8) = print_utf8_;
- GTEST_FLAG(random_seed) = random_seed_;
- GTEST_FLAG(repeat) = repeat_;
- GTEST_FLAG(shuffle) = shuffle_;
- GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
- GTEST_FLAG(stream_result_to) = stream_result_to_;
- GTEST_FLAG(throw_on_failure) = throw_on_failure_;
- }
- private:
-
- bool also_run_disabled_tests_;
- bool break_on_failure_;
- bool catch_exceptions_;
- std::string color_;
- std::string death_test_style_;
- bool death_test_use_fork_;
- bool fail_fast_;
- std::string filter_;
- std::string internal_run_death_test_;
- bool list_tests_;
- std::string output_;
- bool brief_;
- bool print_time_;
- bool print_utf8_;
- int32_t random_seed_;
- int32_t repeat_;
- bool shuffle_;
- int32_t stack_trace_depth_;
- std::string stream_result_to_;
- bool throw_on_failure_;
- } GTEST_ATTRIBUTE_UNUSED_;
- GTEST_API_ std::string CodePointToUtf8(uint32_t code_point);
- GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars);
- void WriteToShardStatusFileIfNeeded();
- GTEST_API_ bool ShouldShard(const char* total_shards_str,
- const char* shard_index_str,
- bool in_subprocess_for_death_test);
- GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val);
- GTEST_API_ bool ShouldRunTestOnShard(
- int total_shards, int shard_index, int test_id);
- template <class Container, typename Predicate>
- inline int CountIf(const Container& c, Predicate predicate) {
-
-
- int count = 0;
- for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
- if (predicate(*it))
- ++count;
- }
- return count;
- }
- template <class Container, typename Functor>
- void ForEach(const Container& c, Functor functor) {
- std::for_each(c.begin(), c.end(), functor);
- }
- template <typename E>
- inline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
- return (i < 0 || i >= static_cast<int>(v.size())) ? default_value
- : v[static_cast<size_t>(i)];
- }
- template <typename E>
- void ShuffleRange(internal::Random* random, int begin, int end,
- std::vector<E>* v) {
- const int size = static_cast<int>(v->size());
- GTEST_CHECK_(0 <= begin && begin <= size)
- << "Invalid shuffle range start " << begin << ": must be in range [0, "
- << size << "].";
- GTEST_CHECK_(begin <= end && end <= size)
- << "Invalid shuffle range finish " << end << ": must be in range ["
- << begin << ", " << size << "].";
-
-
- for (int range_width = end - begin; range_width >= 2; range_width--) {
- const int last_in_range = begin + range_width - 1;
- const int selected =
- begin +
- static_cast<int>(random->Generate(static_cast<uint32_t>(range_width)));
- std::swap((*v)[static_cast<size_t>(selected)],
- (*v)[static_cast<size_t>(last_in_range)]);
- }
- }
- template <typename E>
- inline void Shuffle(internal::Random* random, std::vector<E>* v) {
- ShuffleRange(random, 0, static_cast<int>(v->size()), v);
- }
- template <typename T>
- static void Delete(T* x) {
- delete x;
- }
- class TestPropertyKeyIs {
- public:
-
-
-
- explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}
-
- bool operator()(const TestProperty& test_property) const {
- return test_property.key() == key_;
- }
- private:
- std::string key_;
- };
- class GTEST_API_ UnitTestOptions {
- public:
-
-
- static std::string GetOutputFormat();
-
-
-
- static std::string GetAbsolutePathToOutputFile();
-
-
-
- static bool FilterMatchesTest(const std::string& test_suite_name,
- const std::string& test_name);
- #if GTEST_OS_WINDOWS
-
-
-
-
- static int GTestShouldProcessSEH(DWORD exception_code);
- #endif
-
-
- static bool MatchesFilter(const std::string& name, const char* filter);
- };
- GTEST_API_ FilePath GetCurrentExecutableName();
- class OsStackTraceGetterInterface {
- public:
- OsStackTraceGetterInterface() {}
- virtual ~OsStackTraceGetterInterface() {}
-
-
-
-
-
-
- virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0;
-
-
-
- virtual void UponLeavingGTest() = 0;
-
-
- static const char* const kElidedFramesMarker;
- private:
- GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
- };
- class OsStackTraceGetter : public OsStackTraceGetterInterface {
- public:
- OsStackTraceGetter() {}
- std::string CurrentStackTrace(int max_depth, int skip_count) override;
- void UponLeavingGTest() override;
- private:
- #if GTEST_HAS_ABSL
- Mutex mutex_;
-
-
-
-
- void* caller_frame_ = nullptr;
- #endif
- GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
- };
- struct TraceInfo {
- const char* file;
- int line;
- std::string message;
- };
- class DefaultGlobalTestPartResultReporter
- : public TestPartResultReporterInterface {
- public:
- explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
-
-
- void ReportTestPartResult(const TestPartResult& result) override;
- private:
- UnitTestImpl* const unit_test_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
- };
- class DefaultPerThreadTestPartResultReporter
- : public TestPartResultReporterInterface {
- public:
- explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
-
-
- void ReportTestPartResult(const TestPartResult& result) override;
- private:
- UnitTestImpl* const unit_test_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
- };
- class GTEST_API_ UnitTestImpl {
- public:
- explicit UnitTestImpl(UnitTest* parent);
- virtual ~UnitTestImpl();
-
-
-
-
-
-
-
- TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
-
- void SetGlobalTestPartResultReporter(
- TestPartResultReporterInterface* reporter);
-
- TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
-
- void SetTestPartResultReporterForCurrentThread(
- TestPartResultReporterInterface* reporter);
-
- int successful_test_suite_count() const;
-
- int failed_test_suite_count() const;
-
- int total_test_suite_count() const;
-
-
- int test_suite_to_run_count() const;
-
- int successful_test_count() const;
-
- int skipped_test_count() const;
-
- int failed_test_count() const;
-
- int reportable_disabled_test_count() const;
-
- int disabled_test_count() const;
-
- int reportable_test_count() const;
-
- int total_test_count() const;
-
- int test_to_run_count() const;
-
-
- TimeInMillis start_timestamp() const { return start_timestamp_; }
-
- TimeInMillis elapsed_time() const { return elapsed_time_; }
-
-
- bool Passed() const { return !Failed(); }
-
-
- bool Failed() const {
- return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
- }
-
-
- const TestSuite* GetTestSuite(int i) const {
- const int index = GetElementOr(test_suite_indices_, i, -1);
- return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)];
- }
-
- #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
- const TestCase* GetTestCase(int i) const { return GetTestSuite(i); }
- #endif
-
-
- TestSuite* GetMutableSuiteCase(int i) {
- const int index = GetElementOr(test_suite_indices_, i, -1);
- return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
- }
-
- TestEventListeners* listeners() { return &listeners_; }
-
-
- TestResult* current_test_result();
-
- const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
-
-
-
-
-
- void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
-
-
-
- OsStackTraceGetterInterface* os_stack_trace_getter();
-
-
-
-
-
-
-
-
-
-
- std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;
-
-
-
-
-
-
-
-
-
-
- TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param,
- internal::SetUpTestSuiteFunc set_up_tc,
- internal::TearDownTestSuiteFunc tear_down_tc);
- #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
- TestCase* GetTestCase(const char* test_case_name, const char* type_param,
- internal::SetUpTestSuiteFunc set_up_tc,
- internal::TearDownTestSuiteFunc tear_down_tc) {
- return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
- }
- #endif
-
-
-
-
-
-
-
- void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc,
- internal::TearDownTestSuiteFunc tear_down_tc,
- TestInfo* test_info) {
- #if GTEST_HAS_DEATH_TEST
-
-
-
-
-
-
-
- if (original_working_dir_.IsEmpty()) {
- original_working_dir_.Set(FilePath::GetCurrentDir());
- GTEST_CHECK_(!original_working_dir_.IsEmpty())
- << "Failed to get the current working directory.";
- }
- #endif
- GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
- set_up_tc, tear_down_tc)
- ->AddTestInfo(test_info);
- }
-
-
- internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
- return parameterized_test_registry_;
- }
- std::set<std::string>* ignored_parameterized_test_suites() {
- return &ignored_parameterized_test_suites_;
- }
-
-
- internal::TypeParameterizedTestSuiteRegistry&
- type_parameterized_test_registry() {
- return type_parameterized_test_registry_;
- }
-
- void set_current_test_suite(TestSuite* a_current_test_suite) {
- current_test_suite_ = a_current_test_suite;
- }
-
-
-
- void set_current_test_info(TestInfo* a_current_test_info) {
- current_test_info_ = a_current_test_info;
- }
-
-
-
-
-
-
- void RegisterParameterizedTests();
-
-
-
-
- bool RunAllTests();
-
- void ClearNonAdHocTestResult() {
- ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
- }
-
- void ClearAdHocTestResult() {
- ad_hoc_test_result_.Clear();
- }
-
-
-
-
- void RecordProperty(const TestProperty& test_property);
- enum ReactionToSharding {
- HONOR_SHARDING_PROTOCOL,
- IGNORE_SHARDING_PROTOCOL
- };
-
-
-
-
-
-
- int FilterTests(ReactionToSharding shard_tests);
-
- void ListTestsMatchingFilter();
- const TestSuite* current_test_suite() const { return current_test_suite_; }
- TestInfo* current_test_info() { return current_test_info_; }
- const TestInfo* current_test_info() const { return current_test_info_; }
-
-
- std::vector<Environment*>& environments() { return environments_; }
-
- std::vector<TraceInfo>& gtest_trace_stack() {
- return *(gtest_trace_stack_.pointer());
- }
- const std::vector<TraceInfo>& gtest_trace_stack() const {
- return gtest_trace_stack_.get();
- }
- #if GTEST_HAS_DEATH_TEST
- void InitDeathTestSubprocessControlInfo() {
- internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
- }
-
-
-
-
- const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
- return internal_run_death_test_flag_.get();
- }
-
- internal::DeathTestFactory* death_test_factory() {
- return death_test_factory_.get();
- }
- void SuppressTestEventsIfInSubprocess();
- friend class ReplaceDeathTestFactory;
- #endif
-
-
- void ConfigureXmlOutput();
- #if GTEST_CAN_STREAM_RESULTS_
-
-
- void ConfigureStreamingOutput();
- #endif
-
-
-
-
-
- void PostFlagParsingInit();
-
- int random_seed() const { return random_seed_; }
-
- internal::Random* random() { return &random_; }
-
-
- void ShuffleTests();
-
- void UnshuffleTests();
-
-
- bool catch_exceptions() const { return catch_exceptions_; }
- private:
- friend class ::testing::UnitTest;
-
-
- void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
-
- UnitTest* const parent_;
-
-
- internal::FilePath original_working_dir_;
-
- DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
- DefaultPerThreadTestPartResultReporter
- default_per_thread_test_part_result_reporter_;
-
- TestPartResultReporterInterface* global_test_part_result_repoter_;
-
- internal::Mutex global_test_part_result_reporter_mutex_;
-
- internal::ThreadLocal<TestPartResultReporterInterface*>
- per_thread_test_part_result_reporter_;
-
-
- std::vector<Environment*> environments_;
-
-
- std::vector<TestSuite*> test_suites_;
-
-
-
-
- std::vector<int> test_suite_indices_;
-
-
- internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
- internal::TypeParameterizedTestSuiteRegistry
- type_parameterized_test_registry_;
-
-
- std::set<std::string> ignored_parameterized_test_suites_;
-
- bool parameterized_tests_registered_;
-
- int last_death_test_suite_;
-
-
-
-
- TestSuite* current_test_suite_;
-
-
-
-
- TestInfo* current_test_info_;
-
-
-
-
-
-
-
-
- TestResult ad_hoc_test_result_;
-
-
- TestEventListeners listeners_;
-
-
-
-
- OsStackTraceGetterInterface* os_stack_trace_getter_;
-
- bool post_flag_parse_init_performed_;
-
- int random_seed_;
-
- internal::Random random_;
-
-
- TimeInMillis start_timestamp_;
-
- TimeInMillis elapsed_time_;
- #if GTEST_HAS_DEATH_TEST
-
-
- std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
- std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
- #endif
-
- internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
-
-
- bool catch_exceptions_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
- };
- inline UnitTestImpl* GetUnitTestImpl() {
- return UnitTest::GetInstance()->impl();
- }
- #if GTEST_USES_SIMPLE_RE
- GTEST_API_ bool IsInSet(char ch, const char* str);
- GTEST_API_ bool IsAsciiDigit(char ch);
- GTEST_API_ bool IsAsciiPunct(char ch);
- GTEST_API_ bool IsRepeat(char ch);
- GTEST_API_ bool IsAsciiWhiteSpace(char ch);
- GTEST_API_ bool IsAsciiWordChar(char ch);
- GTEST_API_ bool IsValidEscape(char ch);
- GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
- GTEST_API_ bool ValidateRegex(const char* regex);
- GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
- GTEST_API_ bool MatchRepetitionAndRegexAtHead(
- bool escaped, char ch, char repeat, const char* regex, const char* str);
- GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
- #endif
- GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
- GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
- #if GTEST_HAS_DEATH_TEST
- GTEST_API_ std::string GetLastErrnoDescription();
- template <typename Integer>
- bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
-
-
-
- if (str.empty() || !IsDigit(str[0])) {
- return false;
- }
- errno = 0;
- char* end;
-
-
- using BiggestConvertible = unsigned long long;
- const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
- const bool parse_success = *end == '\0' && errno == 0;
- GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
- const Integer result = static_cast<Integer>(parsed);
- if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
- *number = result;
- return true;
- }
- return false;
- }
- #endif
- class TestResultAccessor {
- public:
- static void RecordProperty(TestResult* test_result,
- const std::string& xml_element,
- const TestProperty& property) {
- test_result->RecordProperty(xml_element, property);
- }
- static void ClearTestPartResults(TestResult* test_result) {
- test_result->ClearTestPartResults();
- }
- static const std::vector<testing::TestPartResult>& test_part_results(
- const TestResult& test_result) {
- return test_result.test_part_results();
- }
- };
- #if GTEST_CAN_STREAM_RESULTS_
- class StreamingListener : public EmptyTestEventListener {
- public:
-
- class AbstractSocketWriter {
- public:
- virtual ~AbstractSocketWriter() {}
-
- virtual void Send(const std::string& message) = 0;
-
- virtual void CloseConnection() {}
-
- void SendLn(const std::string& message) { Send(message + "\n"); }
- };
-
- class SocketWriter : public AbstractSocketWriter {
- public:
- SocketWriter(const std::string& host, const std::string& port)
- : sockfd_(-1), host_name_(host), port_num_(port) {
- MakeConnection();
- }
- ~SocketWriter() override {
- if (sockfd_ != -1)
- CloseConnection();
- }
-
- void Send(const std::string& message) override {
- GTEST_CHECK_(sockfd_ != -1)
- << "Send() can be called only when there is a connection.";
- const auto len = static_cast<size_t>(message.length());
- if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) {
- GTEST_LOG_(WARNING)
- << "stream_result_to: failed to stream to "
- << host_name_ << ":" << port_num_;
- }
- }
- private:
-
- void MakeConnection();
-
- void CloseConnection() override {
- GTEST_CHECK_(sockfd_ != -1)
- << "CloseConnection() can be called only when there is a connection.";
- close(sockfd_);
- sockfd_ = -1;
- }
- int sockfd_;
- const std::string host_name_;
- const std::string port_num_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter);
- };
-
- static std::string UrlEncode(const char* str);
- StreamingListener(const std::string& host, const std::string& port)
- : socket_writer_(new SocketWriter(host, port)) {
- Start();
- }
- explicit StreamingListener(AbstractSocketWriter* socket_writer)
- : socket_writer_(socket_writer) { Start(); }
- void OnTestProgramStart(const UnitTest& ) override {
- SendLn("event=TestProgramStart");
- }
- void OnTestProgramEnd(const UnitTest& unit_test) override {
-
-
- SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
-
- socket_writer_->CloseConnection();
- }
- void OnTestIterationStart(const UnitTest& ,
- int iteration) override {
- SendLn("event=TestIterationStart&iteration=" +
- StreamableToString(iteration));
- }
- void OnTestIterationEnd(const UnitTest& unit_test,
- int ) override {
- SendLn("event=TestIterationEnd&passed=" +
- FormatBool(unit_test.Passed()) + "&elapsed_time=" +
- StreamableToString(unit_test.elapsed_time()) + "ms");
- }
-
-
- void OnTestCaseStart(const TestCase& test_case) override {
- SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
- }
-
-
- void OnTestCaseEnd(const TestCase& test_case) override {
- SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
- "&elapsed_time=" + StreamableToString(test_case.elapsed_time()) +
- "ms");
- }
- void OnTestStart(const TestInfo& test_info) override {
- SendLn(std::string("event=TestStart&name=") + test_info.name());
- }
- void OnTestEnd(const TestInfo& test_info) override {
- SendLn("event=TestEnd&passed=" +
- FormatBool((test_info.result())->Passed()) +
- "&elapsed_time=" +
- StreamableToString((test_info.result())->elapsed_time()) + "ms");
- }
- void OnTestPartResult(const TestPartResult& test_part_result) override {
- const char* file_name = test_part_result.file_name();
- if (file_name == nullptr) file_name = "";
- SendLn("event=TestPartResult&file=" + UrlEncode(file_name) +
- "&line=" + StreamableToString(test_part_result.line_number()) +
- "&message=" + UrlEncode(test_part_result.message()));
- }
- private:
-
- void SendLn(const std::string& message) { socket_writer_->SendLn(message); }
-
-
- void Start() { SendLn("gtest_streaming_protocol_version=1.0"); }
- std::string FormatBool(bool value) { return value ? "1" : "0"; }
- const std::unique_ptr<AbstractSocketWriter> socket_writer_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);
- };
- #endif
- }
- }
- GTEST_DISABLE_MSC_WARNINGS_POP_()
- #endif
|