34 #ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
50 #include "gtest/internal/gtest-port.h"
52 #if GTEST_CAN_STREAM_RESULTS_
53 # include <arpa/inet.h>
61 #include "gtest/gtest.h"
62 #include "gtest/gtest-spi.h"
83 const char kAlsoRunDisabledTestsFlag[] =
"also_run_disabled_tests";
84 const char kBreakOnFailureFlag[] =
"break_on_failure";
85 const char kCatchExceptionsFlag[] =
"catch_exceptions";
86 const char kColorFlag[] =
"color";
87 const char kFailFast[] =
"fail_fast";
88 const char kFilterFlag[] =
"filter";
89 const char kListTestsFlag[] =
"list_tests";
90 const char kOutputFlag[] =
"output";
91 const char kBriefFlag[] =
"brief";
92 const char kPrintTimeFlag[] =
"print_time";
93 const char kPrintUTF8Flag[] =
"print_utf8";
94 const char kRandomSeedFlag[] =
"random_seed";
95 const char kRepeatFlag[] =
"repeat";
96 const char kShuffleFlag[] =
"shuffle";
97 const char kStackTraceDepthFlag[] =
"stack_trace_depth";
98 const char kStreamResultToFlag[] =
"stream_result_to";
99 const char kThrowOnFailureFlag[] =
"throw_on_failure";
100 const char kFlagfileFlag[] =
"flagfile";
103 const int kMaxRandomSeed = 99999;
113 GTEST_API_ bool ShouldUseColor(
bool stdout_is_tty);
129 const char* str,
const char* flag, int32_t* value);
133 inline int GetRandomSeedFromFlag(int32_t random_seed_flag) {
134 const unsigned int raw_seed = (random_seed_flag == 0) ?
135 static_cast<unsigned int>(GetTimeInMillis()) :
136 static_cast<unsigned int>(random_seed_flag);
140 const int normalized_seed =
141 static_cast<int>((raw_seed - 1U) %
142 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
143 return normalized_seed;
149 inline int GetNextRandomSeed(
int seed) {
151 <<
"Invalid random seed " << seed <<
" - must be in [1, "
152 << kMaxRandomSeed <<
"].";
153 const int next_seed = seed + 1;
154 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
159 class GTestFlagSaver {
163 also_run_disabled_tests_ =
GTEST_FLAG(also_run_disabled_tests);
164 break_on_failure_ =
GTEST_FLAG(break_on_failure);
165 catch_exceptions_ =
GTEST_FLAG(catch_exceptions);
167 death_test_style_ =
GTEST_FLAG(death_test_style);
168 death_test_use_fork_ =
GTEST_FLAG(death_test_use_fork);
171 internal_run_death_test_ =
GTEST_FLAG(internal_run_death_test);
180 stack_trace_depth_ =
GTEST_FLAG(stack_trace_depth);
181 stream_result_to_ =
GTEST_FLAG(stream_result_to);
182 throw_on_failure_ =
GTEST_FLAG(throw_on_failure);
187 GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
188 GTEST_FLAG(break_on_failure) = break_on_failure_;
189 GTEST_FLAG(catch_exceptions) = catch_exceptions_;
191 GTEST_FLAG(death_test_style) = death_test_style_;
192 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
195 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
204 GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
205 GTEST_FLAG(stream_result_to) = stream_result_to_;
206 GTEST_FLAG(throw_on_failure) = throw_on_failure_;
211 bool also_run_disabled_tests_;
212 bool break_on_failure_;
213 bool catch_exceptions_;
215 std::string death_test_style_;
216 bool death_test_use_fork_;
219 std::string internal_run_death_test_;
225 int32_t random_seed_;
228 int32_t stack_trace_depth_;
229 std::string stream_result_to_;
230 bool throw_on_failure_;
239 GTEST_API_ std::string CodePointToUtf8(uint32_t code_point);
254 GTEST_API_ std::string WideStringToUtf8(
const wchar_t* str,
int num_chars);
260 void WriteToShardStatusFileIfNeeded();
268 GTEST_API_ bool ShouldShard(
const char* total_shards_str,
269 const char* shard_index_str,
270 bool in_subprocess_for_death_test);
275 GTEST_API_ int32_t Int32FromEnvOrDie(
const char* env_var, int32_t default_val);
282 int total_shards,
int shard_index,
int test_id);
288 template <
class Container,
typename Predicate>
289 inline int CountIf(
const Container& c, Predicate predicate) {
293 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
301 template <
class Container,
typename Functor>
302 void ForEach(
const Container& c, Functor functor) {
303 std::for_each(c.begin(), c.end(), functor);
308 template <
typename E>
309 inline E GetElementOr(
const std::vector<E>& v,
int i, E default_value) {
310 return (i < 0 || i >=
static_cast<int>(v.size())) ? default_value
311 : v[
static_cast<size_t>(i)];
318 template <
typename E>
319 void ShuffleRange(internal::Random* random,
int begin,
int end,
321 const int size =
static_cast<int>(v->size());
323 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, "
326 <<
"Invalid shuffle range finish " << end <<
": must be in range ["
327 << begin <<
", " << size <<
"].";
331 for (
int range_width = end - begin; range_width >= 2; range_width--) {
332 const int last_in_range = begin + range_width - 1;
335 static_cast<int>(random->Generate(
static_cast<uint32_t
>(range_width)));
336 std::swap((*v)[
static_cast<size_t>(selected)],
337 (*v)[
static_cast<size_t>(last_in_range)]);
342 template <
typename E>
343 inline void Shuffle(internal::Random* random, std::vector<E>* v) {
344 ShuffleRange(random, 0,
static_cast<int>(v->size()), v);
349 template <
typename T>
350 static void Delete(T* x) {
357 class TestPropertyKeyIs {
362 explicit TestPropertyKeyIs(
const std::string& key) : key_(key) {}
365 bool operator()(
const TestProperty& test_property)
const {
366 return test_property.key() == key_;
388 static std::string GetOutputFormat();
393 static std::string GetAbsolutePathToOutputFile();
399 static bool FilterMatchesTest(
const std::string& test_suite_name,
400 const std::string& test_name);
408 static int GTestShouldProcessSEH(DWORD exception_code);
413 static bool MatchesFilter(
const std::string& name,
const char* filter);
418 GTEST_API_ FilePath GetCurrentExecutableName();
421 class OsStackTraceGetterInterface {
423 OsStackTraceGetterInterface() {}
424 virtual ~OsStackTraceGetterInterface() {}
432 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
437 virtual void UponLeavingGTest() = 0;
441 static const char*
const kElidedFramesMarker;
448 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
450 OsStackTraceGetter() {}
452 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
453 void UponLeavingGTest()
override;
463 void* caller_frame_ =
nullptr;
478 class DefaultGlobalTestPartResultReporter
479 :
public TestPartResultReporterInterface {
481 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
484 void ReportTestPartResult(
const TestPartResult& result)
override;
487 UnitTestImpl*
const unit_test_;
494 class DefaultPerThreadTestPartResultReporter
495 :
public TestPartResultReporterInterface {
497 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
500 void ReportTestPartResult(
const TestPartResult& result)
override;
503 UnitTestImpl*
const unit_test_;
514 explicit UnitTestImpl(UnitTest* parent);
515 virtual ~UnitTestImpl();
525 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
528 void SetGlobalTestPartResultReporter(
529 TestPartResultReporterInterface* reporter);
532 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
535 void SetTestPartResultReporterForCurrentThread(
536 TestPartResultReporterInterface* reporter);
539 int successful_test_suite_count()
const;
542 int failed_test_suite_count()
const;
545 int total_test_suite_count()
const;
549 int test_suite_to_run_count()
const;
552 int successful_test_count()
const;
555 int skipped_test_count()
const;
558 int failed_test_count()
const;
561 int reportable_disabled_test_count()
const;
564 int disabled_test_count()
const;
567 int reportable_test_count()
const;
570 int total_test_count()
const;
573 int test_to_run_count()
const;
577 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
580 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
584 bool Passed()
const {
return !Failed(); }
588 bool Failed()
const {
589 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
594 const TestSuite* GetTestSuite(
int i)
const {
595 const int index = GetElementOr(test_suite_indices_, i, -1);
596 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)];
600 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
601 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(i); }
606 TestSuite* GetMutableSuiteCase(
int i) {
607 const int index = GetElementOr(test_suite_indices_, i, -1);
608 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
612 TestEventListeners* listeners() {
return &listeners_; }
616 TestResult* current_test_result();
619 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
626 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
631 OsStackTraceGetterInterface* os_stack_trace_getter();
655 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
660 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
661 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
664 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
677 TestInfo* test_info) {
678 #if GTEST_HAS_DEATH_TEST
686 if (original_working_dir_.IsEmpty()) {
687 original_working_dir_.Set(FilePath::GetCurrentDir());
689 <<
"Failed to get the current working directory.";
693 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
694 set_up_tc, tear_down_tc)
695 ->AddTestInfo(test_info);
700 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
701 return parameterized_test_registry_;
704 std::set<std::string>* ignored_parameterized_test_suites() {
705 return &ignored_parameterized_test_suites_;
710 internal::TypeParameterizedTestSuiteRegistry&
711 type_parameterized_test_registry() {
712 return type_parameterized_test_registry_;
716 void set_current_test_suite(TestSuite* a_current_test_suite) {
717 current_test_suite_ = a_current_test_suite;
723 void set_current_test_info(TestInfo* a_current_test_info) {
724 current_test_info_ = a_current_test_info;
733 void RegisterParameterizedTests();
742 void ClearNonAdHocTestResult() {
743 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
747 void ClearAdHocTestResult() {
748 ad_hoc_test_result_.Clear();
755 void RecordProperty(
const TestProperty& test_property);
757 enum ReactionToSharding {
758 HONOR_SHARDING_PROTOCOL,
759 IGNORE_SHARDING_PROTOCOL
768 int FilterTests(ReactionToSharding shard_tests);
771 void ListTestsMatchingFilter();
773 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
774 TestInfo* current_test_info() {
return current_test_info_; }
775 const TestInfo* current_test_info()
const {
return current_test_info_; }
779 std::vector<Environment*>& environments() {
return environments_; }
782 std::vector<TraceInfo>& gtest_trace_stack() {
783 return *(gtest_trace_stack_.pointer());
785 const std::vector<TraceInfo>& gtest_trace_stack()
const {
786 return gtest_trace_stack_.get();
789 #if GTEST_HAS_DEATH_TEST
790 void InitDeathTestSubprocessControlInfo() {
791 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
797 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
798 return internal_run_death_test_flag_.get();
802 internal::DeathTestFactory* death_test_factory() {
803 return death_test_factory_.get();
806 void SuppressTestEventsIfInSubprocess();
808 friend class ReplaceDeathTestFactory;
813 void ConfigureXmlOutput();
815 #if GTEST_CAN_STREAM_RESULTS_
818 void ConfigureStreamingOutput();
826 void PostFlagParsingInit();
829 int random_seed()
const {
return random_seed_; }
832 internal::Random* random() {
return &random_; }
839 void UnshuffleTests();
843 bool catch_exceptions()
const {
return catch_exceptions_; }
846 friend class ::testing::UnitTest;
850 void set_catch_exceptions(
bool value) { catch_exceptions_ = value; }
853 UnitTest*
const parent_;
857 internal::FilePath original_working_dir_;
860 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
861 DefaultPerThreadTestPartResultReporter
862 default_per_thread_test_part_result_reporter_;
865 TestPartResultReporterInterface* global_test_part_result_repoter_;
868 internal::Mutex global_test_part_result_reporter_mutex_;
871 internal::ThreadLocal<TestPartResultReporterInterface*>
872 per_thread_test_part_result_reporter_;
876 std::vector<Environment*> environments_;
880 std::vector<TestSuite*> test_suites_;
886 std::vector<int> test_suite_indices_;
890 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
891 internal::TypeParameterizedTestSuiteRegistry
892 type_parameterized_test_registry_;
896 std::set<std::string> ignored_parameterized_test_suites_;
899 bool parameterized_tests_registered_;
902 int last_death_test_suite_;
908 TestSuite* current_test_suite_;
914 TestInfo* current_test_info_;
924 TestResult ad_hoc_test_result_;
928 TestEventListeners listeners_;
934 OsStackTraceGetterInterface* os_stack_trace_getter_;
937 bool post_flag_parse_init_performed_;
943 internal::Random random_;
952 #if GTEST_HAS_DEATH_TEST
955 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
956 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
960 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
964 bool catch_exceptions_;
971 inline UnitTestImpl* GetUnitTestImpl() {
972 return UnitTest::GetInstance()->impl();
975 #if GTEST_USES_SIMPLE_RE
979 GTEST_API_ bool IsInSet(
char ch,
const char* str);
986 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
987 GTEST_API_ bool ValidateRegex(
const char* regex);
988 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
989 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
990 bool escaped,
char ch,
char repeat,
const char* regex,
const char* str);
991 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
997 GTEST_API_ void ParseGoogleTestFlagsOnly(
int* argc,
char** argv);
998 GTEST_API_ void ParseGoogleTestFlagsOnly(
int* argc,
wchar_t** argv);
1000 #if GTEST_HAS_DEATH_TEST
1004 GTEST_API_ std::string GetLastErrnoDescription();
1010 template <
typename Integer>
1011 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
1015 if (str.empty() || !
IsDigit(str[0])) {
1023 using BiggestConvertible =
unsigned long long;
1025 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1026 const bool parse_success = *end ==
'\0' && errno == 0;
1030 const Integer result =
static_cast<Integer
>(parsed);
1031 if (parse_success &&
static_cast<BiggestConvertible
>(result) == parsed) {
1045 class TestResultAccessor {
1047 static void RecordProperty(TestResult* test_result,
1048 const std::string& xml_element,
1049 const TestProperty& property) {
1050 test_result->RecordProperty(xml_element, property);
1053 static void ClearTestPartResults(TestResult* test_result) {
1054 test_result->ClearTestPartResults();
1057 static const std::vector<testing::TestPartResult>& test_part_results(
1058 const TestResult& test_result) {
1059 return test_result.test_part_results();
1063 #if GTEST_CAN_STREAM_RESULTS_
1066 class StreamingListener :
public EmptyTestEventListener {
1069 class AbstractSocketWriter {
1071 virtual ~AbstractSocketWriter() {}
1074 virtual void Send(
const std::string& message) = 0;
1077 virtual void CloseConnection() {}
1080 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1084 class SocketWriter :
public AbstractSocketWriter {
1086 SocketWriter(
const std::string& host,
const std::string& port)
1087 : sockfd_(-1), host_name_(host), port_num_(port) {
1091 ~SocketWriter()
override {
1097 void Send(
const std::string& message)
override {
1099 <<
"Send() can be called only when there is a connection.";
1101 const auto len =
static_cast<size_t>(message.length());
1102 if (write(sockfd_, message.c_str(), len) !=
static_cast<ssize_t
>(len)) {
1104 <<
"stream_result_to: failed to stream to "
1105 << host_name_ <<
":" << port_num_;
1111 void MakeConnection();
1114 void CloseConnection()
override {
1116 <<
"CloseConnection() can be called only when there is a connection.";
1123 const std::string host_name_;
1124 const std::string port_num_;
1130 static std::string UrlEncode(
const char* str);
1132 StreamingListener(
const std::string& host,
const std::string& port)
1133 : socket_writer_(
new SocketWriter(host, port)) {
1137 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1138 : socket_writer_(socket_writer) { Start(); }
1140 void OnTestProgramStart(
const UnitTest& )
override {
1141 SendLn(
"event=TestProgramStart");
1144 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1147 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1150 socket_writer_->CloseConnection();
1153 void OnTestIterationStart(
const UnitTest& ,
1154 int iteration)
override {
1155 SendLn(
"event=TestIterationStart&iteration=" +
1159 void OnTestIterationEnd(
const UnitTest& unit_test,
1161 SendLn(
"event=TestIterationEnd&passed=" +
1162 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1168 void OnTestCaseStart(
const TestCase& test_case)
override {
1169 SendLn(std::string(
"event=TestCaseStart&name=") + test_case.name());
1174 void OnTestCaseEnd(
const TestCase& test_case)
override {
1175 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1180 void OnTestStart(
const TestInfo& test_info)
override {
1181 SendLn(std::string(
"event=TestStart&name=") + test_info.name());
1184 void OnTestEnd(
const TestInfo& test_info)
override {
1185 SendLn(
"event=TestEnd&passed=" +
1186 FormatBool((test_info.result())->Passed()) +
1191 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1192 const char* file_name = test_part_result.file_name();
1193 if (file_name ==
nullptr) file_name =
"";
1194 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1196 "&message=" + UrlEncode(test_part_result.message()));
1201 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1205 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1207 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1209 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
#define GTEST_DECLARE_bool_(name)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:2198
#define GTEST_FLAG(name)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:2187
#define GTEST_LOG_(severity)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:980
#define GTEST_NO_INLINE_
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:786
#define GTEST_API_
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:775
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:324
#define GTEST_CHECK_(condition)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:1004
#define GTEST_ATTRIBUTE_UNUSED_
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:665
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:693
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) namespace testing
Definition: cmake-build-release/googletest-src/googletest/src/gtest-internal-inl.h:64
void(*)() SetUpTestSuiteFunc
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:489
const void * TypeId
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:417
bool IsDigit(char ch)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-port.h:1924
std::string StreamableToString(const T &streamable)
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest-message.h:210
void(*)() TearDownTestSuiteFunc
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/internal/gtest-internal.h:490
Definition: cmake-build-debug/googletest-src/googlemock/include/gmock/gmock-actions.h:154
internal::TimeInMillis TimeInMillis
Definition: cmake-build-debug/googletest-src/googletest/include/gtest/gtest.h:528