34 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_ 35 #define GTEST_SRC_GTEST_INTERNAL_INL_H_ 51 #if GTEST_CAN_STREAM_RESULTS_ 52 # include <arpa/inet.h> 58 #endif // GTEST_OS_WINDOWS 82 const char kAlsoRunDisabledTestsFlag[] =
"also_run_disabled_tests";
83 const char kBreakOnFailureFlag[] =
"break_on_failure";
84 const char kCatchExceptionsFlag[] =
"catch_exceptions";
85 const char kColorFlag[] =
"color";
86 const char kFilterFlag[] =
"filter";
87 const char kListTestsFlag[] =
"list_tests";
88 const char kOutputFlag[] =
"output";
89 const char kPrintTimeFlag[] =
"print_time";
90 const char kPrintUTF8Flag[] =
"print_utf8";
91 const char kRandomSeedFlag[] =
"random_seed";
92 const char kRepeatFlag[] =
"repeat";
93 const char kShuffleFlag[] =
"shuffle";
94 const char kStackTraceDepthFlag[] =
"stack_trace_depth";
95 const char kStreamResultToFlag[] =
"stream_result_to";
96 const char kThrowOnFailureFlag[] =
"throw_on_failure";
97 const char kFlagfileFlag[] =
"flagfile";
100 const int kMaxRandomSeed = 99999;
126 const char* str,
const char* flag,
Int32*
value);
130 inline int GetRandomSeedFromFlag(
Int32 random_seed_flag) {
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) %
139 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
140 return normalized_seed;
146 inline int GetNextRandomSeed(
int seed) {
148 <<
"Invalid random seed " << seed <<
" - must be in [1, " 149 << kMaxRandomSeed <<
"].";
150 const int next_seed = seed + 1;
151 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
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_;
208 std::string death_test_style_;
209 bool death_test_use_fork_;
211 std::string internal_run_death_test_;
220 std::string stream_result_to_;
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>
310 void ShuffleRange(internal::Random* random,
int begin,
int end,
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 [" 318 << begin <<
", " << size <<
"].";
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>
334 inline void Shuffle(internal::Random* random, std::vector<E>* v) {
335 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
340 template <
typename T>
341 static void Delete(T*
x) {
348 class TestPropertyKeyIs {
353 explicit TestPropertyKeyIs(
const std::string& key) : key_(key) {}
356 bool operator()(
const TestProperty& test_property)
const {
357 return test_property.key() == key_;
379 static std::string GetOutputFormat();
384 static std::string GetAbsolutePathToOutputFile();
393 static bool PatternMatchesString(
const char *pattern,
const char *str);
397 static bool FilterMatchesTest(
const std::string& test_suite_name,
398 const std::string& test_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();
591 const TestSuite* GetTestSuite(
int i)
const {
592 const int index = GetElementOr(test_suite_indices_, i, -1);
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_ 603 TestSuite* GetMutableSuiteCase(
int i) {
604 const int index = GetElementOr(test_suite_indices_, i, -1);
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_; }
816 internal::Random* random() {
return &random_; }
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; }
837 UnitTest*
const parent_;
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_;
852 internal::Mutex global_test_part_result_reporter_mutex_;
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_;
886 TestSuite* current_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_;
921 internal::Random random_;
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_;
949 inline UnitTestImpl* GetUnitTestImpl() {
950 return UnitTest::GetInstance()->impl();
953 #if GTEST_USES_SIMPLE_RE 964 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
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 982 GTEST_API_ std::string GetLastErrnoDescription();
988 template <
typename Integer>
989 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
993 if (str.empty() || !
IsDigit(str[0])) {
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,
1037 const std::string& xml_element,
1038 const TestProperty& property) {
1039 test_result->RecordProperty(xml_element, property);
1042 static void ClearTestPartResults(TestResult* test_result) {
1043 test_result->ClearTestPartResults();
1046 static const std::vector<testing::TestPartResult>& test_part_results(
1047 const TestResult& test_result) {
1048 return test_result.test_part_results();
1052 #if GTEST_CAN_STREAM_RESULTS_ 1055 class StreamingListener :
public EmptyTestEventListener {
1058 class AbstractSocketWriter {
1060 virtual ~AbstractSocketWriter() {}
1063 virtual void Send(
const std::string&
message) = 0;
1066 virtual void CloseConnection() {}
1069 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1073 class SocketWriter :
public AbstractSocketWriter {
1075 SocketWriter(
const std::string& host,
const std::string& port)
1076 : sockfd_(-1), host_name_(host), port_num_(port) {
1080 ~SocketWriter()
override {
1086 void Send(
const std::string&
message)
override {
1088 <<
"Send() can be called only when there is a connection.";
1090 const auto len =
static_cast<size_t>(message.length());
1091 if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) {
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.";
1112 const std::string host_name_;
1113 const std::string port_num_;
1119 static std::string UrlEncode(
const char* str);
1121 StreamingListener(
const std::string& host,
const std::string& port)
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()));
1190 void SendLn(
const std::string&
message) { socket_writer_->SendLn(message); }
1194 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1196 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1198 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1203 #endif // GTEST_CAN_STREAM_RESULTS_ 1210 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_ bool g_help_flag
Definition: gtest.cc:182
Definition: gmock-actions.h:59
std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
Definition: gtest.cc:3704
int * count
Definition: gmock_stress_test.cc:96
#define GTEST_DECLARE_bool_(name)
Definition: gtest-port.h:2237
#define GTEST_ATTRIBUTE_UNUSED_
Definition: gtest-port.h:660
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:314
tuple message
Definition: googletest-output-test.py:337
#define GTEST_LOG_(severity)
Definition: gtest-port.h:990
bool ShouldUseColor(bool stdout_is_tty)
Definition: gtest.cc:3002
bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
Definition: gtest.cc:5428
bool IsDigit(char ch)
Definition: gtest-port.h:1980
std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
Definition: gtest.cc:3681
#define GTEST_API_
Definition: gtest-port.h:759
std::string CodePointToUtf8(UInt32 code_point)
Definition: gtest.cc:1770
int i
Definition: gmock-matchers_test.cc:711
#define GTEST_FLAG(name)
Definition: gtest-port.h:2226
void(*)( TearDownTestSuiteFunc)
Definition: gtest-internal.h:478
std::string StreamableToString(const T &streamable)
Definition: gtest-message.h:209
internal::TimeInMillis TimeInMillis
Definition: gtest.h:515
TypeWithSize< 4 >::UInt UInt32
Definition: gtest-port.h:2217
const void * TypeId
Definition: gtest-internal.h:405
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2216
TestCase
Definition: gmock_test_utils.py:97
char ch
Definition: gmock-matchers_test.cc:3621
bool ShouldShard(const char *total_shards_env, const char *shard_index_env, bool in_subprocess_for_death_test)
Definition: gtest.cc:5365
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:1014
Int32 Int32FromEnvOrDie(const char *var, Int32 default_val)
Definition: gtest.cc:5410
#define GTEST_NO_INLINE_
Definition: gtest-port.h:770
const TypeId kTestTypeIdInGoogleTest
FilePath GetCurrentExecutableName()
Definition: gtest.cc:429
TimeInMillis GetTimeInMillis()
Definition: gtest.cc:836
FilePath original_working_dir_
Definition: googletest-options-test.cc:140
std::string WideStringToUtf8(const wchar_t *str, int num_chars)
Definition: gtest.cc:1837
int value
Definition: gmock-matchers_test.cc:657
output
Definition: gmock_output_test.py:173
int x
Definition: gmock-matchers_test.cc:3610
int RunAllTests()
Definition: googletest-output-test_.cc:354
void ParseGoogleTestFlagsOnly(int *argc, char **argv)
Definition: gtest.cc:5996
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:688
void(*)( SetUpTestSuiteFunc)
Definition: gtest-internal.h:477
void WriteToShardStatusFileIfNeeded()
Definition: gtest.cc:5343
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) namespace testing
Definition: gtest-internal-inl.h:63
bool ParseInt32Flag(const char *str, const char *flag, Int32 *value)
Definition: gtest.cc:5736