12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187 |
- #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
- #define GTEST_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 <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 kFilterFlag[] = "filter";
- const char kListTestsFlag[] = "list_tests";
- const char kOutputFlag[] = "output";
- 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* value);
- inline int GetRandomSeedFromFlag(Int32 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);
- filter_ = GTEST_FLAG(filter);
- internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
- list_tests_ = GTEST_FLAG(list_tests);
- output_ = GTEST_FLAG(output);
- 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(internal_run_death_test) = internal_run_death_test_;
- GTEST_FLAG(list_tests) = list_tests_;
- GTEST_FLAG(output) = output_;
- 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_;
- std::string filter_;
- std::string internal_run_death_test_;
- bool list_tests_;
- std::string output_;
- bool print_time_;
- bool print_utf8_;
- internal::Int32 random_seed_;
- internal::Int32 repeat_;
- bool shuffle_;
- internal::Int32 stack_trace_depth_;
- std::string stream_result_to_;
- bool throw_on_failure_;
- } GTEST_ATTRIBUTE_UNUSED_;
- GTEST_API_ std::string CodePointToUtf8(UInt32 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 Int32FromEnvOrDie(const char* env_var, Int32 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[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 + random->Generate(range_width);
- std::swap((*v)[selected], (*v)[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 PatternMatchesString(const char *pattern, const char *str);
-
-
- static bool FilterMatchesTest(const std::string &test_case_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() {}
- virtual std::string CurrentStackTrace(int max_depth, int skip_count);
- virtual void UponLeavingGTest();
- 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);
-
-
- virtual void ReportTestPartResult(const TestPartResult& result);
- private:
- UnitTestImpl* const unit_test_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
- };
- class DefaultPerThreadTestPartResultReporter
- : public TestPartResultReporterInterface {
- public:
- explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
-
-
- virtual void ReportTestPartResult(const TestPartResult& result);
- 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_case_count() const;
-
- int failed_test_case_count() const;
-
- int total_test_case_count() const;
-
-
- int test_case_to_run_count() const;
-
- int successful_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_case_count() > 0 || ad_hoc_test_result()->Failed();
- }
-
-
- const TestCase* GetTestCase(int i) const {
- const int index = GetElementOr(test_case_indices_, i, -1);
- return index < 0 ? NULL : test_cases_[i];
- }
-
-
- TestCase* GetMutableTestCase(int i) {
- const int index = GetElementOr(test_case_indices_, i, -1);
- return index < 0 ? NULL : test_cases_[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_;
-
-
-
-
-
-
-
-
-
-
- TestCase* GetTestCase(const char* test_case_name,
- const char* type_param,
- Test::SetUpTestCaseFunc set_up_tc,
- Test::TearDownTestCaseFunc tear_down_tc);
-
-
-
-
-
-
-
- void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
- Test::TearDownTestCaseFunc tear_down_tc,
- TestInfo* test_info) {
-
-
-
-
-
-
-
- if (original_working_dir_.IsEmpty()) {
- original_working_dir_.Set(FilePath::GetCurrentDir());
- GTEST_CHECK_(!original_working_dir_.IsEmpty())
- << "Failed to get the current working directory.";
- }
- GetTestCase(test_info->test_case_name(),
- test_info->type_param(),
- set_up_tc,
- tear_down_tc)->AddTestInfo(test_info);
- }
-
-
- internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
- return parameterized_test_registry_;
- }
-
- void set_current_test_case(TestCase* a_current_test_case) {
- current_test_case_ = a_current_test_case;
- }
-
-
-
- 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_cases_, TestCase::ClearTestCaseResult);
- }
-
- 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 TestCase* current_test_case() const { return current_test_case_; }
- 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<TestCase*> test_cases_;
-
-
-
-
- std::vector<int> test_case_indices_;
-
-
- internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
-
- bool parameterized_tests_registered_;
-
- int last_death_test_case_;
-
-
-
-
- TestCase* current_test_case_;
-
-
-
-
- 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
-
-
- internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
- internal::scoped_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;
-
-
- # if GTEST_OS_WINDOWS && !defined(__GNUC__)
-
- typedef unsigned __int64 BiggestConvertible;
- const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
- # else
- typedef unsigned long long BiggestConvertible;
- const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
- # endif
- 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();
- }
- virtual ~SocketWriter() {
- if (sockfd_ != -1)
- CloseConnection();
- }
-
- virtual void Send(const std::string& message) {
- GTEST_CHECK_(sockfd_ != -1)
- << "Send() can be called only when there is a connection.";
- const int len = static_cast<int>(message.length());
- if (write(sockfd_, message.c_str(), len) != len) {
- GTEST_LOG_(WARNING)
- << "stream_result_to: failed to stream to "
- << host_name_ << ":" << port_num_;
- }
- }
- private:
-
- void MakeConnection();
-
- void CloseConnection() {
- 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& ) {
- SendLn("event=TestProgramStart");
- }
- void OnTestProgramEnd(const UnitTest& unit_test) {
-
-
- SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
-
- socket_writer_->CloseConnection();
- }
- void OnTestIterationStart(const UnitTest& , int iteration) {
- SendLn("event=TestIterationStart&iteration=" +
- StreamableToString(iteration));
- }
- void OnTestIterationEnd(const UnitTest& unit_test, int ) {
- SendLn("event=TestIterationEnd&passed=" +
- FormatBool(unit_test.Passed()) + "&elapsed_time=" +
- StreamableToString(unit_test.elapsed_time()) + "ms");
- }
- void OnTestCaseStart(const TestCase& test_case) {
- SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
- }
- void OnTestCaseEnd(const TestCase& test_case) {
- SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed())
- + "&elapsed_time=" + StreamableToString(test_case.elapsed_time())
- + "ms");
- }
- void OnTestStart(const TestInfo& test_info) {
- SendLn(std::string("event=TestStart&name=") + test_info.name());
- }
- void OnTestEnd(const TestInfo& test_info) {
- 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) {
- const char* file_name = test_part_result.file_name();
- if (file_name == NULL)
- 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 scoped_ptr<AbstractSocketWriter> socket_writer_;
- GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);
- };
- #endif
- }
- }
- GTEST_DISABLE_MSC_WARNINGS_POP_()
- #endif
|