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>
59 #endif // GTEST_OS_WINDOWS
61 #include "gtest/gtest.h"
62 #include "gtest/gtest-spi.h"
112 const unsigned int raw_seed = (random_seed_flag == 0) ?
114 static_cast<unsigned int>(random_seed_flag);
118 const int normalized_seed =
119 static_cast<int>((raw_seed - 1U) %
121 return normalized_seed;
129 <<
"Invalid random seed " <<
seed <<
" - must be in [1, "
131 const int next_seed =
seed + 1;
137 class GTestFlagSaver {
141 also_run_disabled_tests_ =
GTEST_FLAG_GET(also_run_disabled_tests);
149 internal_run_death_test_ =
GTEST_FLAG_GET(internal_run_death_test);
157 recreate_environments_when_repeating_ =
167 GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_);
175 GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_);
184 recreate_environments_when_repeating_);
193 bool also_run_disabled_tests_;
194 bool break_on_failure_;
195 bool catch_exceptions_;
198 bool death_test_use_fork_;
213 bool throw_on_failure_;
252 const char* shard_index_str,
253 bool in_subprocess_for_death_test);
265 int total_shards,
int shard_index,
int test_id);
271 template <
class Container,
typename Predicate>
272 inline int CountIf(
const Container& c, Predicate predicate) {
276 for (
auto it = c.begin();
it != c.end(); ++
it) {
284 template <
class Container,
typename Functor>
285 void ForEach(
const Container& c, Functor functor) {
286 std::for_each(c.begin(), c.end(), functor);
291 template <
typename E>
292 inline E GetElementOr(
const std::vector<E>&
v,
int i, E default_value) {
293 return (i < 0 || i >=
static_cast<int>(
v.size())) ? default_value
294 :
v[
static_cast<size_t>(
i)];
301 template <
typename E>
304 const int size =
static_cast<int>(
v->size());
306 <<
"Invalid shuffle range start " <<
begin <<
": must be in range [0, "
309 <<
"Invalid shuffle range finish " <<
end <<
": must be in range ["
314 for (
int range_width =
end -
begin; range_width >= 2; range_width--) {
315 const int last_in_range =
begin + range_width - 1;
318 static_cast<int>(random->Generate(
static_cast<uint32_t>(range_width)));
319 std::swap((*
v)[
static_cast<size_t>(selected)],
320 (*
v)[
static_cast<size_t>(last_in_range)]);
325 template <
typename E>
332 template <
typename T>
340 class TestPropertyKeyIs {
349 return test_property.
key() ==
key_;
382 static bool FilterMatchesTest(
const std::string& test_suite_name,
391 static int GTestShouldProcessSEH(DWORD exception_code);
392 #endif // GTEST_OS_WINDOWS
396 static bool MatchesFilter(
const std::string&
name,
const char* filter);
404 class OsStackTraceGetterInterface {
415 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
420 virtual void UponLeavingGTest() = 0;
424 static const char*
const kElidedFramesMarker;
431 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
435 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
436 void UponLeavingGTest()
override;
446 void* caller_frame_ =
nullptr;
447 #endif // GTEST_HAS_ABSL
461 class DefaultGlobalTestPartResultReporter
462 :
public TestPartResultReporterInterface {
464 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
467 void ReportTestPartResult(
const TestPartResult&
result)
override;
470 UnitTestImpl*
const unit_test_;
477 class DefaultPerThreadTestPartResultReporter
478 :
public TestPartResultReporterInterface {
480 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
483 void ReportTestPartResult(
const TestPartResult&
result)
override;
486 UnitTestImpl*
const unit_test_;
497 explicit UnitTestImpl(UnitTest* parent);
498 virtual ~UnitTestImpl();
508 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
511 void SetGlobalTestPartResultReporter(
512 TestPartResultReporterInterface* reporter);
515 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
518 void SetTestPartResultReporterForCurrentThread(
519 TestPartResultReporterInterface* reporter);
522 int successful_test_suite_count()
const;
525 int failed_test_suite_count()
const;
528 int total_test_suite_count()
const;
532 int test_suite_to_run_count()
const;
535 int successful_test_count()
const;
538 int skipped_test_count()
const;
541 int failed_test_count()
const;
544 int reportable_disabled_test_count()
const;
547 int disabled_test_count()
const;
550 int reportable_test_count()
const;
553 int total_test_count()
const;
556 int test_to_run_count()
const;
567 bool Passed()
const {
return !Failed(); }
572 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
579 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
i)];
583 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
585 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
591 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
index)];
626 std::string CurrentOsStackTraceExceptTop(
int skip_count)
639 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
644 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
648 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
650 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
662 #if GTEST_HAS_DEATH_TEST
673 <<
"Failed to get the current working directory.";
675 #endif // GTEST_HAS_DEATH_TEST
678 set_up_tc, tear_down_tc)
679 ->AddTestInfo(test_info);
685 return parameterized_test_registry_;
689 return &ignored_parameterized_test_suites_;
696 return type_parameterized_test_registry_;
701 current_test_suite_ = a_current_test_suite;
708 current_test_info_ = a_current_test_info;
717 void RegisterParameterizedTests();
727 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
732 ad_hoc_test_result_.Clear();
742 HONOR_SHARDING_PROTOCOL,
743 IGNORE_SHARDING_PROTOCOL
752 int FilterTests(ReactionToSharding shard_tests);
755 void ListTestsMatchingFilter();
767 return *(gtest_trace_stack_.pointer());
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();
823 void UnshuffleTests();
830 friend class ::testing::UnitTest;
846 default_per_thread_test_part_result_reporter_;
856 per_thread_test_part_result_reporter_;
860 std::vector<Environment*> environments_;
883 bool parameterized_tests_registered_;
921 bool post_flag_parse_init_performed_;
936 #if GTEST_HAS_DEATH_TEST
939 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
940 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
941 #endif // GTEST_HAS_DEATH_TEST
948 bool catch_exceptions_;
956 return UnitTest::GetInstance()->impl();
959 #if GTEST_USES_SIMPLE_RE
971 GTEST_API_ bool ValidateRegex(
const char* regex);
972 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char*
str);
973 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
974 bool escaped,
char ch,
char repeat,
const char* regex,
const char*
str);
975 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char*
str);
977 #endif // GTEST_USES_SIMPLE_RE
984 #if GTEST_HAS_DEATH_TEST
994 template <
typename Integer>
1007 using BiggestConvertible =
unsigned long long;
1009 const BiggestConvertible parsed = strtoull(
str.c_str(), &
end, 10);
1010 const bool parse_success = *
end ==
'\0' && errno == 0;
1014 const Integer
result =
static_cast<Integer
>(parsed);
1015 if (parse_success &&
static_cast<BiggestConvertible
>(
result) == parsed) {
1021 #endif // GTEST_HAS_DEATH_TEST
1029 class TestResultAccessor {
1034 test_result->RecordProperty(xml_element, property);
1047 #if GTEST_CAN_STREAM_RESULTS_
1053 class AbstractSocketWriter {
1055 virtual ~AbstractSocketWriter() {}
1061 virtual void CloseConnection() {}
1068 class SocketWriter :
public AbstractSocketWriter {
1071 : sockfd_(-1), host_name_(host), port_num_(
port) {
1075 ~SocketWriter()
override {
1083 <<
"Send() can be called only when there is a connection.";
1085 const auto len =
static_cast<size_t>(
message.length());
1088 <<
"stream_result_to: failed to stream to "
1089 << host_name_ <<
":" << port_num_;
1095 void MakeConnection();
1098 void CloseConnection()
override {
1100 <<
"CloseConnection() can be called only when there is a connection.";
1117 : socket_writer_(
new SocketWriter(host,
port)) {
1121 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1122 : socket_writer_(socket_writer) { Start(); }
1124 void OnTestProgramStart(
const UnitTest& )
override {
1125 SendLn(
"event=TestProgramStart");
1128 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1131 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1134 socket_writer_->CloseConnection();
1137 void OnTestIterationStart(
const UnitTest& ,
1138 int iteration)
override {
1139 SendLn(
"event=TestIterationStart&iteration=" +
1143 void OnTestIterationEnd(
const UnitTest& unit_test,
1145 SendLn(
"event=TestIterationEnd&passed=" +
1146 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1159 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(
test_suite.Passed()) +
1164 void OnTestStart(
const TestInfo& test_info)
override {
1165 SendLn(
std::string(
"event=TestStart&name=") + test_info.name());
1168 void OnTestEnd(
const TestInfo& test_info)
override {
1169 SendLn(
"event=TestEnd&passed=" +
1170 FormatBool((test_info.result())->Passed()) +
1175 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1176 const char* file_name = test_part_result.file_name();
1177 if (file_name ==
nullptr) file_name =
"";
1178 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1180 "&message=" + UrlEncode(test_part_result.message()));
1189 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1193 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1198 #endif // GTEST_CAN_STREAM_RESULTS_
1205 #endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_