34 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GTEST_SRC_GTEST_INTERNAL_INL_H_
50 #include "gtest/internal/gtest-port.h"
52 #if GTEST_CAN_STREAM_RESULTS_
53 # include <arpa/inet.h>
59 #endif // GTEST_OS_WINDOWS
61 #include "gtest/gtest.h"
62 #include "gtest/gtest-spi.h"
87 const char kFailFast[] =
"fail_fast";
91 const char kBriefFlag[] =
"brief";
93 const char kPrintUTF8Flag[] =
"print_utf8";
134 const unsigned int raw_seed = (random_seed_flag == 0) ?
136 static_cast<unsigned int>(random_seed_flag);
140 const int normalized_seed =
141 static_cast<int>((raw_seed - 1U) %
143 return normalized_seed;
151 <<
"Invalid random seed " <<
seed <<
" - must be in [1, "
153 const int next_seed =
seed + 1;
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_;
216 bool death_test_use_fork_;
230 bool throw_on_failure_;
269 const char* shard_index_str,
270 bool in_subprocess_for_death_test);
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>
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 ["
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>
349 template <
typename T>
357 class TestPropertyKeyIs {
365 bool operator()(
const TestProperty& test_property)
const {
366 return test_property.key() ==
key_;
406 static bool FilterMatchesTest(
const std::string& test_suite_name,
415 static int GTestShouldProcessSEH(DWORD exception_code);
416 #endif // GTEST_OS_WINDOWS
420 static bool MatchesFilter(
const std::string&
name,
const char* filter);
428 class OsStackTraceGetterInterface {
430 OsStackTraceGetterInterface() {}
431 virtual ~OsStackTraceGetterInterface() {}
439 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
444 virtual void UponLeavingGTest() = 0;
448 static const char*
const kElidedFramesMarker;
455 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
457 OsStackTraceGetter() {}
459 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
460 void UponLeavingGTest()
override;
470 void* caller_frame_ =
nullptr;
471 #endif // GTEST_HAS_ABSL
485 class DefaultGlobalTestPartResultReporter
486 :
public TestPartResultReporterInterface {
488 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
491 void ReportTestPartResult(
const TestPartResult&
result)
override;
494 UnitTestImpl*
const unit_test_;
501 class DefaultPerThreadTestPartResultReporter
502 :
public TestPartResultReporterInterface {
504 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
507 void ReportTestPartResult(
const TestPartResult&
result)
override;
510 UnitTestImpl*
const unit_test_;
521 explicit UnitTestImpl(UnitTest* parent);
522 virtual ~UnitTestImpl();
532 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
535 void SetGlobalTestPartResultReporter(
536 TestPartResultReporterInterface* reporter);
539 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
542 void SetTestPartResultReporterForCurrentThread(
543 TestPartResultReporterInterface* reporter);
546 int successful_test_suite_count()
const;
549 int failed_test_suite_count()
const;
552 int total_test_suite_count()
const;
556 int test_suite_to_run_count()
const;
559 int successful_test_count()
const;
562 int skipped_test_count()
const;
565 int failed_test_count()
const;
568 int reportable_disabled_test_count()
const;
571 int disabled_test_count()
const;
574 int reportable_test_count()
const;
577 int total_test_count()
const;
580 int test_to_run_count()
const;
584 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
587 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
591 bool Passed()
const {
return !Failed(); }
595 bool Failed()
const {
596 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
603 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
i)];
607 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
608 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(
i); }
609 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
615 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
index)];
619 TestEventListeners* listeners() {
return &listeners_; }
623 TestResult* current_test_result();
626 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
633 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
638 OsStackTraceGetterInterface* os_stack_trace_getter();
662 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
667 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
668 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
671 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
673 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
684 TestInfo* test_info) {
685 #if GTEST_HAS_DEATH_TEST
696 <<
"Failed to get the current working directory.";
698 #endif // GTEST_HAS_DEATH_TEST
700 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
701 set_up_tc, tear_down_tc)
702 ->AddTestInfo(test_info);
707 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
708 return parameterized_test_registry_;
711 std::set<std::string>* ignored_parameterized_test_suites() {
712 return &ignored_parameterized_test_suites_;
717 internal::TypeParameterizedTestSuiteRegistry&
718 type_parameterized_test_registry() {
719 return type_parameterized_test_registry_;
723 void set_current_test_suite(
TestSuite* a_current_test_suite) {
724 current_test_suite_ = a_current_test_suite;
730 void set_current_test_info(TestInfo* a_current_test_info) {
731 current_test_info_ = a_current_test_info;
740 void RegisterParameterizedTests();
749 void ClearNonAdHocTestResult() {
750 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
754 void ClearAdHocTestResult() {
755 ad_hoc_test_result_.Clear();
762 void RecordProperty(
const TestProperty& test_property);
764 enum ReactionToSharding {
765 HONOR_SHARDING_PROTOCOL,
766 IGNORE_SHARDING_PROTOCOL
775 int FilterTests(ReactionToSharding shard_tests);
778 void ListTestsMatchingFilter();
780 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
781 TestInfo* current_test_info() {
return current_test_info_; }
782 const TestInfo* current_test_info()
const {
return current_test_info_; }
786 std::vector<Environment*>& environments() {
return environments_; }
789 std::vector<TraceInfo>& gtest_trace_stack() {
790 return *(gtest_trace_stack_.pointer());
792 const std::vector<TraceInfo>& gtest_trace_stack()
const {
793 return gtest_trace_stack_.get();
796 #if GTEST_HAS_DEATH_TEST
797 void InitDeathTestSubprocessControlInfo() {
798 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
804 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
805 return internal_run_death_test_flag_.get();
809 internal::DeathTestFactory* death_test_factory() {
810 return death_test_factory_.get();
813 void SuppressTestEventsIfInSubprocess();
815 friend class ReplaceDeathTestFactory;
816 #endif // GTEST_HAS_DEATH_TEST
820 void ConfigureXmlOutput();
822 #if GTEST_CAN_STREAM_RESULTS_
825 void ConfigureStreamingOutput();
833 void PostFlagParsingInit();
836 int random_seed()
const {
return random_seed_; }
846 void UnshuffleTests();
850 bool catch_exceptions()
const {
return catch_exceptions_; }
853 friend class ::testing::UnitTest;
857 void set_catch_exceptions(
bool value) { catch_exceptions_ =
value; }
867 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
868 DefaultPerThreadTestPartResultReporter
869 default_per_thread_test_part_result_reporter_;
872 TestPartResultReporterInterface* global_test_part_result_repoter_;
878 internal::ThreadLocal<TestPartResultReporterInterface*>
879 per_thread_test_part_result_reporter_;
883 std::vector<Environment*> environments_;
887 std::vector<TestSuite*> test_suites_;
893 std::vector<int> test_suite_indices_;
897 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
898 internal::TypeParameterizedTestSuiteRegistry
899 type_parameterized_test_registry_;
903 std::set<std::string> ignored_parameterized_test_suites_;
906 bool parameterized_tests_registered_;
909 int last_death_test_suite_;
921 TestInfo* current_test_info_;
931 TestResult ad_hoc_test_result_;
935 TestEventListeners listeners_;
941 OsStackTraceGetterInterface* os_stack_trace_getter_;
944 bool post_flag_parse_init_performed_;
959 #if GTEST_HAS_DEATH_TEST
962 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
963 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
964 #endif // GTEST_HAS_DEATH_TEST
967 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
971 bool catch_exceptions_;
979 return UnitTest::GetInstance()->impl();
982 #if GTEST_USES_SIMPLE_RE
994 GTEST_API_ bool ValidateRegex(
const char* regex);
995 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char*
str);
996 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
997 bool escaped,
char ch,
char repeat,
const char* regex,
const char*
str);
998 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char*
str);
1000 #endif // GTEST_USES_SIMPLE_RE
1007 #if GTEST_HAS_DEATH_TEST
1017 template <
typename Integer>
1030 using BiggestConvertible =
unsigned long long;
1032 const BiggestConvertible parsed = strtoull(
str.c_str(), &
end, 10);
1033 const bool parse_success = *
end ==
'\0' && errno == 0;
1037 const Integer
result =
static_cast<Integer
>(parsed);
1038 if (parse_success &&
static_cast<BiggestConvertible
>(
result) == parsed) {
1044 #endif // GTEST_HAS_DEATH_TEST
1052 class TestResultAccessor {
1054 static void RecordProperty(TestResult*
test_result,
1056 const TestProperty& property) {
1057 test_result->RecordProperty(xml_element, property);
1060 static void ClearTestPartResults(TestResult*
test_result) {
1064 static const std::vector<testing::TestPartResult>& test_part_results(
1070 #if GTEST_CAN_STREAM_RESULTS_
1073 class StreamingListener :
public EmptyTestEventListener {
1076 class AbstractSocketWriter {
1078 virtual ~AbstractSocketWriter() {}
1084 virtual void CloseConnection() {}
1091 class SocketWriter :
public AbstractSocketWriter {
1094 : sockfd_(-1), host_name_(host), port_num_(
port) {
1098 ~SocketWriter()
override {
1106 <<
"Send() can be called only when there is a connection.";
1108 const auto len =
static_cast<size_t>(
message.length());
1111 <<
"stream_result_to: failed to stream to "
1112 << host_name_ <<
":" << port_num_;
1118 void MakeConnection();
1121 void CloseConnection()
override {
1123 <<
"CloseConnection() can be called only when there is a connection.";
1140 : socket_writer_(
new SocketWriter(host,
port)) {
1144 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1145 : socket_writer_(socket_writer) { Start(); }
1147 void OnTestProgramStart(
const UnitTest& )
override {
1148 SendLn(
"event=TestProgramStart");
1151 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1154 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1157 socket_writer_->CloseConnection();
1160 void OnTestIterationStart(
const UnitTest& ,
1161 int iteration)
override {
1162 SendLn(
"event=TestIterationStart&iteration=" +
1166 void OnTestIterationEnd(
const UnitTest& unit_test,
1168 SendLn(
"event=TestIterationEnd&passed=" +
1169 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1175 void OnTestCaseStart(
const TestCase& test_case)
override {
1176 SendLn(
std::string(
"event=TestCaseStart&name=") + test_case.name());
1181 void OnTestCaseEnd(
const TestCase& test_case)
override {
1182 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1187 void OnTestStart(
const TestInfo& test_info)
override {
1188 SendLn(
std::string(
"event=TestStart&name=") + test_info.name());
1191 void OnTestEnd(
const TestInfo& test_info)
override {
1192 SendLn(
"event=TestEnd&passed=" +
1193 FormatBool((test_info.result())->Passed()) +
1198 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1199 const char* file_name = test_part_result.file_name();
1200 if (file_name ==
nullptr) file_name =
"";
1201 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1203 "&message=" + UrlEncode(test_part_result.message()));
1212 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1216 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1221 #endif // GTEST_CAN_STREAM_RESULTS_
1228 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_