34 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GTEST_SRC_GTEST_INTERNAL_INL_H_
49 #include "gtest/internal/gtest-port.h"
51 #if GTEST_CAN_STREAM_RESULTS_
52 # include <arpa/inet.h>
58 #endif // GTEST_OS_WINDOWS
60 #include "gtest/gtest.h"
61 #include "gtest/gtest-spi.h"
90 const char kPrintUTF8Flag[] =
"print_utf8";
131 const unsigned int raw_seed = (random_seed_flag == 0) ?
133 static_cast<unsigned int>(random_seed_flag);
137 const int normalized_seed =
138 static_cast<int>((raw_seed - 1U) %
140 return normalized_seed;
148 <<
"Invalid random seed " <<
seed <<
" - must be in [1, "
150 const int next_seed =
seed + 1;
156 class GTestFlagSaver {
160 also_run_disabled_tests_ =
GTEST_FLAG(also_run_disabled_tests);
161 break_on_failure_ =
GTEST_FLAG(break_on_failure);
162 catch_exceptions_ =
GTEST_FLAG(catch_exceptions);
164 death_test_style_ =
GTEST_FLAG(death_test_style);
165 death_test_use_fork_ =
GTEST_FLAG(death_test_use_fork);
167 internal_run_death_test_ =
GTEST_FLAG(internal_run_death_test);
175 stack_trace_depth_ =
GTEST_FLAG(stack_trace_depth);
176 stream_result_to_ =
GTEST_FLAG(stream_result_to);
177 throw_on_failure_ =
GTEST_FLAG(throw_on_failure);
182 GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
183 GTEST_FLAG(break_on_failure) = break_on_failure_;
184 GTEST_FLAG(catch_exceptions) = catch_exceptions_;
186 GTEST_FLAG(death_test_style) = death_test_style_;
187 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
189 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
197 GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
198 GTEST_FLAG(stream_result_to) = stream_result_to_;
199 GTEST_FLAG(throw_on_failure) = throw_on_failure_;
204 bool also_run_disabled_tests_;
205 bool break_on_failure_;
206 bool catch_exceptions_;
209 bool death_test_use_fork_;
221 bool throw_on_failure_;
260 const char* shard_index_str,
261 bool in_subprocess_for_death_test);
273 int total_shards,
int shard_index,
int test_id);
279 template <
class Container,
typename Predicate>
280 inline int CountIf(
const Container& c, Predicate predicate) {
284 for (
typename Container::const_iterator
it = c.begin();
it != c.end(); ++
it) {
292 template <
class Container,
typename Functor>
293 void ForEach(
const Container& c, Functor functor) {
294 std::for_each(c.begin(), c.end(), functor);
299 template <
typename E>
300 inline E
GetElementOr(
const std::vector<E>&
v,
int i, E default_value) {
301 return (i < 0 || i >=
static_cast<int>(
v.size())) ? default_value
302 :
v[
static_cast<size_t>(
i)];
309 template <
typename E>
312 const int size =
static_cast<int>(
v->size());
314 <<
"Invalid shuffle range start " <<
begin <<
": must be in range [0, "
317 <<
"Invalid shuffle range finish " <<
end <<
": must be in range ["
322 for (
int range_width =
end -
begin; range_width >= 2; range_width--) {
323 const int last_in_range =
begin + range_width - 1;
326 static_cast<int>(random->Generate(
static_cast<UInt32>(range_width)));
327 std::swap((*
v)[
static_cast<size_t>(selected)],
328 (*
v)[
static_cast<size_t>(last_in_range)]);
333 template <
typename E>
340 template <
typename T>
348 class TestPropertyKeyIs {
356 bool operator()(
const TestProperty& test_property)
const {
357 return test_property.key() ==
key_;
397 static bool FilterMatchesTest(
const std::string& test_suite_name,
406 static int GTestShouldProcessSEH(DWORD exception_code);
407 #endif // GTEST_OS_WINDOWS
411 static bool MatchesFilter(
const std::string&
name,
const char* filter);
419 class OsStackTraceGetterInterface {
421 OsStackTraceGetterInterface() {}
422 virtual ~OsStackTraceGetterInterface() {}
430 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
435 virtual void UponLeavingGTest() = 0;
439 static const char*
const kElidedFramesMarker;
446 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
448 OsStackTraceGetter() {}
450 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
451 void UponLeavingGTest()
override;
461 void* caller_frame_ =
nullptr;
462 #endif // GTEST_HAS_ABSL
476 class DefaultGlobalTestPartResultReporter
477 :
public TestPartResultReporterInterface {
479 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
482 void ReportTestPartResult(
const TestPartResult&
result)
override;
485 UnitTestImpl*
const unit_test_;
492 class DefaultPerThreadTestPartResultReporter
493 :
public TestPartResultReporterInterface {
495 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
498 void ReportTestPartResult(
const TestPartResult&
result)
override;
501 UnitTestImpl*
const unit_test_;
512 explicit UnitTestImpl(UnitTest* parent);
513 virtual ~UnitTestImpl();
523 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
526 void SetGlobalTestPartResultReporter(
527 TestPartResultReporterInterface* reporter);
530 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
533 void SetTestPartResultReporterForCurrentThread(
534 TestPartResultReporterInterface* reporter);
537 int successful_test_suite_count()
const;
540 int failed_test_suite_count()
const;
543 int total_test_suite_count()
const;
547 int test_suite_to_run_count()
const;
550 int successful_test_count()
const;
553 int skipped_test_count()
const;
556 int failed_test_count()
const;
559 int reportable_disabled_test_count()
const;
562 int disabled_test_count()
const;
565 int reportable_test_count()
const;
568 int total_test_count()
const;
571 int test_to_run_count()
const;
575 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
578 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
581 bool Passed()
const {
return !Failed(); }
585 bool Failed()
const {
586 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
593 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
i)];
597 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
598 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(
i); }
599 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
605 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
index)];
609 TestEventListeners* listeners() {
return &listeners_; }
613 TestResult* current_test_result();
616 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
623 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
628 OsStackTraceGetterInterface* os_stack_trace_getter();
652 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
657 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
658 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
661 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
663 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
674 TestInfo* test_info) {
685 <<
"Failed to get the current working directory.";
688 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
689 set_up_tc, tear_down_tc)
690 ->AddTestInfo(test_info);
695 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
696 return parameterized_test_registry_;
700 void set_current_test_suite(
TestSuite* a_current_test_suite) {
701 current_test_suite_ = a_current_test_suite;
707 void set_current_test_info(TestInfo* a_current_test_info) {
708 current_test_info_ = a_current_test_info;
717 void RegisterParameterizedTests();
726 void ClearNonAdHocTestResult() {
727 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
731 void ClearAdHocTestResult() {
732 ad_hoc_test_result_.Clear();
739 void RecordProperty(
const TestProperty& test_property);
741 enum ReactionToSharding {
742 HONOR_SHARDING_PROTOCOL,
743 IGNORE_SHARDING_PROTOCOL
752 int FilterTests(ReactionToSharding shard_tests);
755 void ListTestsMatchingFilter();
757 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
758 TestInfo* current_test_info() {
return current_test_info_; }
759 const TestInfo* current_test_info()
const {
return current_test_info_; }
763 std::vector<Environment*>& environments() {
return environments_; }
766 std::vector<TraceInfo>& gtest_trace_stack() {
767 return *(gtest_trace_stack_.pointer());
769 const std::vector<TraceInfo>& gtest_trace_stack()
const {
770 return gtest_trace_stack_.get();
773 #if GTEST_HAS_DEATH_TEST
774 void InitDeathTestSubprocessControlInfo() {
775 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
781 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
782 return internal_run_death_test_flag_.get();
786 internal::DeathTestFactory* death_test_factory() {
787 return death_test_factory_.get();
790 void SuppressTestEventsIfInSubprocess();
792 friend class ReplaceDeathTestFactory;
793 #endif // GTEST_HAS_DEATH_TEST
797 void ConfigureXmlOutput();
799 #if GTEST_CAN_STREAM_RESULTS_
802 void ConfigureStreamingOutput();
810 void PostFlagParsingInit();
813 int random_seed()
const {
return random_seed_; }
823 void UnshuffleTests();
827 bool catch_exceptions()
const {
return catch_exceptions_; }
830 friend class ::testing::UnitTest;
834 void set_catch_exceptions(
bool value) { catch_exceptions_ =
value; }
844 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
845 DefaultPerThreadTestPartResultReporter
846 default_per_thread_test_part_result_reporter_;
849 TestPartResultReporterInterface* global_test_part_result_repoter_;
855 internal::ThreadLocal<TestPartResultReporterInterface*>
856 per_thread_test_part_result_reporter_;
860 std::vector<Environment*> environments_;
864 std::vector<TestSuite*> test_suites_;
870 std::vector<int> test_suite_indices_;
874 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
877 bool parameterized_tests_registered_;
880 int last_death_test_suite_;
892 TestInfo* current_test_info_;
902 TestResult ad_hoc_test_result_;
906 TestEventListeners listeners_;
912 OsStackTraceGetterInterface* os_stack_trace_getter_;
915 bool post_flag_parse_init_performed_;
930 #if GTEST_HAS_DEATH_TEST
933 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
934 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
935 #endif // GTEST_HAS_DEATH_TEST
938 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
942 bool catch_exceptions_;
950 return UnitTest::GetInstance()->impl();
953 #if GTEST_USES_SIMPLE_RE
965 GTEST_API_ bool ValidateRegex(
const char* regex);
966 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char*
str);
967 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
968 bool escaped,
char ch,
char repeat,
const char* regex,
const char*
str);
969 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char*
str);
971 #endif // GTEST_USES_SIMPLE_RE
978 #if GTEST_HAS_DEATH_TEST
988 template <
typename Integer>
1002 # if GTEST_OS_WINDOWS && !defined(__GNUC__)
1005 typedef unsigned __int64 BiggestConvertible;
1006 const BiggestConvertible parsed = _strtoui64(
str.c_str(), &
end, 10);
1010 typedef unsigned long long BiggestConvertible;
1011 const BiggestConvertible parsed = strtoull(
str.c_str(), &
end, 10);
1013 # endif // GTEST_OS_WINDOWS && !defined(__GNUC__)
1015 const bool parse_success = *
end ==
'\0' && errno == 0;
1019 const Integer
result =
static_cast<Integer
>(parsed);
1020 if (parse_success &&
static_cast<BiggestConvertible
>(
result) == parsed) {
1026 #endif // GTEST_HAS_DEATH_TEST
1034 class TestResultAccessor {
1036 static void RecordProperty(TestResult*
test_result,
1038 const TestProperty& property) {
1039 test_result->RecordProperty(xml_element, property);
1042 static void ClearTestPartResults(TestResult*
test_result) {
1046 static const std::vector<testing::TestPartResult>& test_part_results(
1052 #if GTEST_CAN_STREAM_RESULTS_
1055 class StreamingListener :
public EmptyTestEventListener {
1058 class AbstractSocketWriter {
1060 virtual ~AbstractSocketWriter() {}
1066 virtual void CloseConnection() {}
1073 class SocketWriter :
public AbstractSocketWriter {
1076 : sockfd_(-1), host_name_(host), port_num_(
port) {
1080 ~SocketWriter()
override {
1088 <<
"Send() can be called only when there is a connection.";
1090 const auto len =
static_cast<size_t>(
message.length());
1093 <<
"stream_result_to: failed to stream to "
1094 << host_name_ <<
":" << port_num_;
1100 void MakeConnection();
1103 void CloseConnection()
override {
1105 <<
"CloseConnection() can be called only when there is a connection.";
1122 : socket_writer_(
new SocketWriter(host,
port)) {
1126 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1127 : socket_writer_(socket_writer) { Start(); }
1129 void OnTestProgramStart(
const UnitTest& )
override {
1130 SendLn(
"event=TestProgramStart");
1133 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1136 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1139 socket_writer_->CloseConnection();
1142 void OnTestIterationStart(
const UnitTest& ,
1143 int iteration)
override {
1144 SendLn(
"event=TestIterationStart&iteration=" +
1148 void OnTestIterationEnd(
const UnitTest& unit_test,
1150 SendLn(
"event=TestIterationEnd&passed=" +
1151 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1157 void OnTestCaseStart(
const TestCase& test_case)
override {
1158 SendLn(
std::string(
"event=TestCaseStart&name=") + test_case.name());
1163 void OnTestCaseEnd(
const TestCase& test_case)
override {
1164 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1169 void OnTestStart(
const TestInfo& test_info)
override {
1170 SendLn(
std::string(
"event=TestStart&name=") + test_info.name());
1173 void OnTestEnd(
const TestInfo& test_info)
override {
1174 SendLn(
"event=TestEnd&passed=" +
1175 FormatBool((test_info.result())->Passed()) +
1180 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1181 const char* file_name = test_part_result.file_name();
1182 if (file_name ==
nullptr) file_name =
"";
1183 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1185 "&message=" + UrlEncode(test_part_result.message()));
1194 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1198 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1203 #endif // GTEST_CAN_STREAM_RESULTS_
1210 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_