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 :
v[
i];
308 template <
typename E>
311 const int size =
static_cast<int>(
v->size());
313 <<
"Invalid shuffle range start " <<
begin <<
": must be in range [0, "
316 <<
"Invalid shuffle range finish " <<
end <<
": must be in range ["
321 for (
int range_width =
end -
begin; range_width >= 2; range_width--) {
322 const int last_in_range =
begin + range_width - 1;
323 const int selected =
begin + random->Generate(range_width);
329 template <
typename E>
336 template <
typename T>
344 class TestPropertyKeyIs {
352 bool operator()(
const TestProperty& test_property)
const {
353 return test_property.key() ==
key_;
393 static bool FilterMatchesTest(
const std::string& test_suite_name,
402 static int GTestShouldProcessSEH(DWORD exception_code);
403 #endif // GTEST_OS_WINDOWS
407 static bool MatchesFilter(
const std::string&
name,
const char* filter);
415 class OsStackTraceGetterInterface {
417 OsStackTraceGetterInterface() {}
418 virtual ~OsStackTraceGetterInterface() {}
426 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
431 virtual void UponLeavingGTest() = 0;
435 static const char*
const kElidedFramesMarker;
442 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
444 OsStackTraceGetter() {}
446 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
447 void UponLeavingGTest()
override;
457 void* caller_frame_ =
nullptr;
458 #endif // GTEST_HAS_ABSL
472 class DefaultGlobalTestPartResultReporter
473 :
public TestPartResultReporterInterface {
475 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
478 void ReportTestPartResult(
const TestPartResult&
result)
override;
481 UnitTestImpl*
const unit_test_;
488 class DefaultPerThreadTestPartResultReporter
489 :
public TestPartResultReporterInterface {
491 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
494 void ReportTestPartResult(
const TestPartResult&
result)
override;
497 UnitTestImpl*
const unit_test_;
508 explicit UnitTestImpl(UnitTest* parent);
509 virtual ~UnitTestImpl();
519 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
522 void SetGlobalTestPartResultReporter(
523 TestPartResultReporterInterface* reporter);
526 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
529 void SetTestPartResultReporterForCurrentThread(
530 TestPartResultReporterInterface* reporter);
533 int successful_test_suite_count()
const;
536 int failed_test_suite_count()
const;
539 int total_test_suite_count()
const;
543 int test_suite_to_run_count()
const;
546 int successful_test_count()
const;
549 int skipped_test_count()
const;
552 int failed_test_count()
const;
555 int reportable_disabled_test_count()
const;
558 int disabled_test_count()
const;
561 int reportable_test_count()
const;
564 int total_test_count()
const;
567 int test_to_run_count()
const;
571 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
574 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
577 bool Passed()
const {
return !Failed(); }
581 bool Failed()
const {
582 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
589 return index < 0 ? nullptr : test_suites_[
i];
593 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
594 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(
i); }
595 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
601 return index < 0 ? nullptr : test_suites_[
index];
605 TestEventListeners* listeners() {
return &listeners_; }
609 TestResult* current_test_result();
612 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
619 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
624 OsStackTraceGetterInterface* os_stack_trace_getter();
648 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
653 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
654 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
657 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
659 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
670 TestInfo* test_info) {
681 <<
"Failed to get the current working directory.";
684 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
685 set_up_tc, tear_down_tc)
686 ->AddTestInfo(test_info);
691 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
692 return parameterized_test_registry_;
696 void set_current_test_suite(
TestSuite* a_current_test_suite) {
697 current_test_suite_ = a_current_test_suite;
703 void set_current_test_info(TestInfo* a_current_test_info) {
704 current_test_info_ = a_current_test_info;
713 void RegisterParameterizedTests();
722 void ClearNonAdHocTestResult() {
723 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
727 void ClearAdHocTestResult() {
728 ad_hoc_test_result_.Clear();
735 void RecordProperty(
const TestProperty& test_property);
737 enum ReactionToSharding {
738 HONOR_SHARDING_PROTOCOL,
739 IGNORE_SHARDING_PROTOCOL
748 int FilterTests(ReactionToSharding shard_tests);
751 void ListTestsMatchingFilter();
753 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
754 TestInfo* current_test_info() {
return current_test_info_; }
755 const TestInfo* current_test_info()
const {
return current_test_info_; }
759 std::vector<Environment*>& environments() {
return environments_; }
762 std::vector<TraceInfo>& gtest_trace_stack() {
763 return *(gtest_trace_stack_.pointer());
765 const std::vector<TraceInfo>& gtest_trace_stack()
const {
766 return gtest_trace_stack_.get();
769 #if GTEST_HAS_DEATH_TEST
770 void InitDeathTestSubprocessControlInfo() {
771 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
777 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
778 return internal_run_death_test_flag_.get();
782 internal::DeathTestFactory* death_test_factory() {
783 return death_test_factory_.get();
786 void SuppressTestEventsIfInSubprocess();
788 friend class ReplaceDeathTestFactory;
789 #endif // GTEST_HAS_DEATH_TEST
793 void ConfigureXmlOutput();
795 #if GTEST_CAN_STREAM_RESULTS_
798 void ConfigureStreamingOutput();
806 void PostFlagParsingInit();
809 int random_seed()
const {
return random_seed_; }
819 void UnshuffleTests();
823 bool catch_exceptions()
const {
return catch_exceptions_; }
826 friend class ::testing::UnitTest;
830 void set_catch_exceptions(
bool value) { catch_exceptions_ =
value; }
840 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
841 DefaultPerThreadTestPartResultReporter
842 default_per_thread_test_part_result_reporter_;
845 TestPartResultReporterInterface* global_test_part_result_repoter_;
851 internal::ThreadLocal<TestPartResultReporterInterface*>
852 per_thread_test_part_result_reporter_;
856 std::vector<Environment*> environments_;
860 std::vector<TestSuite*> test_suites_;
866 std::vector<int> test_suite_indices_;
870 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
873 bool parameterized_tests_registered_;
876 int last_death_test_suite_;
888 TestInfo* current_test_info_;
898 TestResult ad_hoc_test_result_;
902 TestEventListeners listeners_;
908 OsStackTraceGetterInterface* os_stack_trace_getter_;
911 bool post_flag_parse_init_performed_;
926 #if GTEST_HAS_DEATH_TEST
929 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
930 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
931 #endif // GTEST_HAS_DEATH_TEST
934 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
938 bool catch_exceptions_;
946 return UnitTest::GetInstance()->impl();
949 #if GTEST_USES_SIMPLE_RE
961 GTEST_API_ bool ValidateRegex(
const char* regex);
962 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char*
str);
963 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
964 bool escaped,
char ch,
char repeat,
const char* regex,
const char*
str);
965 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char*
str);
967 #endif // GTEST_USES_SIMPLE_RE
974 #if GTEST_HAS_DEATH_TEST
984 template <
typename Integer>
998 # if GTEST_OS_WINDOWS && !defined(__GNUC__)
1001 typedef unsigned __int64 BiggestConvertible;
1002 const BiggestConvertible parsed = _strtoui64(
str.c_str(), &
end, 10);
1006 typedef unsigned long long BiggestConvertible;
1007 const BiggestConvertible parsed = strtoull(
str.c_str(), &
end, 10);
1009 # endif // GTEST_OS_WINDOWS && !defined(__GNUC__)
1011 const bool parse_success = *
end ==
'\0' && errno == 0;
1015 const Integer
result =
static_cast<Integer
>(parsed);
1016 if (parse_success &&
static_cast<BiggestConvertible
>(
result) == parsed) {
1022 #endif // GTEST_HAS_DEATH_TEST
1030 class TestResultAccessor {
1032 static void RecordProperty(TestResult*
test_result,
1034 const TestProperty& property) {
1035 test_result->RecordProperty(xml_element, property);
1038 static void ClearTestPartResults(TestResult*
test_result) {
1042 static const std::vector<testing::TestPartResult>& test_part_results(
1048 #if GTEST_CAN_STREAM_RESULTS_
1051 class StreamingListener :
public EmptyTestEventListener {
1054 class AbstractSocketWriter {
1056 virtual ~AbstractSocketWriter() {}
1062 virtual void CloseConnection() {}
1069 class SocketWriter :
public AbstractSocketWriter {
1072 : sockfd_(-1), host_name_(host), port_num_(
port) {
1076 ~SocketWriter()
override {
1084 <<
"Send() can be called only when there is a connection.";
1086 const int len =
static_cast<int>(
message.length());
1089 <<
"stream_result_to: failed to stream to "
1090 << host_name_ <<
":" << port_num_;
1096 void MakeConnection();
1099 void CloseConnection()
override {
1101 <<
"CloseConnection() can be called only when there is a connection.";
1118 : socket_writer_(
new SocketWriter(host,
port)) {
1122 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1123 : socket_writer_(socket_writer) { Start(); }
1125 void OnTestProgramStart(
const UnitTest& )
override {
1126 SendLn(
"event=TestProgramStart");
1129 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1132 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1135 socket_writer_->CloseConnection();
1138 void OnTestIterationStart(
const UnitTest& ,
1139 int iteration)
override {
1140 SendLn(
"event=TestIterationStart&iteration=" +
1144 void OnTestIterationEnd(
const UnitTest& unit_test,
1146 SendLn(
"event=TestIterationEnd&passed=" +
1147 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1153 void OnTestCaseStart(
const TestCase& test_case)
override {
1154 SendLn(
std::string(
"event=TestCaseStart&name=") + test_case.name());
1159 void OnTestCaseEnd(
const TestCase& test_case)
override {
1160 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1165 void OnTestStart(
const TestInfo& test_info)
override {
1166 SendLn(
std::string(
"event=TestStart&name=") + test_info.name());
1169 void OnTestEnd(
const TestInfo& test_info)
override {
1170 SendLn(
"event=TestEnd&passed=" +
1171 FormatBool((test_info.result())->Passed()) +
1176 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1177 const char* file_name = test_part_result.file_name();
1178 if (file_name ==
nullptr) file_name =
"";
1179 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1181 "&message=" + UrlEncode(test_part_result.message()));
1190 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1194 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1199 #endif // GTEST_CAN_STREAM_RESULTS_
1206 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_