34 #ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
49 #include <unordered_map>
54 #if GTEST_CAN_STREAM_RESULTS_
55 #include <arpa/inet.h>
59 #ifdef GTEST_OS_WINDOWS
61 #endif // GTEST_OS_WINDOWS
115 const unsigned int raw_seed =
117 : static_cast<unsigned int>(random_seed_flag);
121 const int normalized_seed =
122 static_cast<int>((raw_seed - 1U) %
125 return normalized_seed;
133 <<
"Invalid random seed " << seed <<
" - must be in [1, "
135 const int next_seed = seed + 1;
136 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
145 also_run_disabled_tests_ =
GTEST_FLAG_GET(also_run_disabled_tests);
153 internal_run_death_test_ =
GTEST_FLAG_GET(internal_run_death_test);
161 recreate_environments_when_repeating_ =
171 GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_);
179 GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_);
188 recreate_environments_when_repeating_);
256 const char* shard_index_str,
257 bool in_subprocess_for_death_test);
275 template <
class Container,
typename Predicate>
276 inline int CountIf(
const Container&
c, Predicate predicate) {
280 for (
auto it = c.begin(); it != c.end(); ++it) {
281 if (predicate(*it)) ++count;
287 template <
class Container,
typename Functor>
288 void ForEach(
const Container&
c, Functor functor) {
289 std::for_each(c.begin(), c.end(), functor);
294 template <
typename E>
296 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value
297 : v[
static_cast<size_t>(
i)];
304 template <
typename E>
307 const int size =
static_cast<int>(v->size());
309 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, "
312 <<
"Invalid shuffle range finish " << end <<
": must be in range ["
313 << begin <<
", " << size <<
"].";
317 for (
int range_width = end - begin; range_width >= 2; range_width--) {
318 const int last_in_range = begin + range_width - 1;
321 static_cast<int>(random->
Generate(static_cast<uint32_t>(range_width)));
322 std::swap((*v)[static_cast<size_t>(selected)],
323 (*v)[static_cast<size_t>(last_in_range)]);
328 template <
typename E>
330 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
335 template <
typename T>
352 return test_property.
key() == key_;
374 static std::string GetOutputFormat();
379 static std::string GetAbsolutePathToOutputFile();
385 static bool FilterMatchesTest(
const std::string& test_suite_name,
386 const std::string& test_name);
388 #ifdef GTEST_OS_WINDOWS
394 static int GTestProcessSEH(DWORD seh_code,
const char* location);
395 #endif // GTEST_OS_WINDOWS
399 static bool MatchesFilter(
const std::string& name,
const char* filter);
402 #if GTEST_HAS_FILE_SYSTEM
405 GTEST_API_ FilePath GetCurrentExecutableName();
406 #endif // GTEST_HAS_FILE_SYSTEM
420 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
425 virtual void UponLeavingGTest() = 0;
442 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
443 void UponLeavingGTest()
override;
446 #ifdef GTEST_HAS_ABSL
453 void* caller_frame_ =
nullptr;
454 #endif // GTEST_HAS_ABSL
470 :
public TestPartResultReporterInterface {
475 void ReportTestPartResult(
const TestPartResult& result)
override;
489 :
public TestPartResultReporterInterface {
494 void ReportTestPartResult(
const TestPartResult& result)
override;
522 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
525 void SetGlobalTestPartResultReporter(
526 TestPartResultReporterInterface* reporter);
529 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
532 void SetTestPartResultReporterForCurrentThread(
533 TestPartResultReporterInterface* reporter);
536 int successful_test_suite_count()
const;
539 int failed_test_suite_count()
const;
542 int total_test_suite_count()
const;
546 int test_suite_to_run_count()
const;
549 int successful_test_count()
const;
552 int skipped_test_count()
const;
555 int failed_test_count()
const;
558 int reportable_disabled_test_count()
const;
561 int disabled_test_count()
const;
564 int reportable_test_count()
const;
567 int total_test_count()
const;
570 int test_to_run_count()
const;
581 bool Passed()
const {
return !Failed(); }
586 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
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_
599 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
604 const int index =
GetElementOr(test_suite_indices_, i, -1);
605 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
640 std::string CurrentOsStackTraceExceptTop(
int skip_count)
653 TestSuite* GetTestSuite(
const std::string& test_suite_name,
654 const char* type_param,
659 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
661 const char* type_param,
664 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
666 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
678 #if GTEST_HAS_FILE_SYSTEM
689 <<
"Failed to get the current working directory.";
691 #endif // GTEST_HAS_FILE_SYSTEM
694 set_up_tc, tear_down_tc)
695 ->AddTestInfo(test_info);
701 return parameterized_test_registry_;
705 return &ignored_parameterized_test_suites_;
712 return type_parameterized_test_registry_;
721 void RegisterParameterizedTests();
731 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
751 int FilterTests(ReactionToSharding shard_tests);
754 void ListTestsMatchingFilter();
766 return *(gtest_trace_stack_.pointer());
769 return gtest_trace_stack_.get();
772 #ifdef GTEST_HAS_DEATH_TEST
773 void InitDeathTestSubprocessControlInfo() {
774 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
780 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
781 return internal_run_death_test_flag_.get();
785 internal::DeathTestFactory* death_test_factory() {
786 return death_test_factory_.get();
789 void SuppressTestEventsIfInSubprocess();
791 friend class ReplaceDeathTestFactory;
792 #endif // GTEST_HAS_DEATH_TEST
796 void ConfigureXmlOutput();
798 #if GTEST_CAN_STREAM_RESULTS_
801 void ConfigureStreamingOutput();
809 void PostFlagParsingInit();
822 void UnshuffleTests();
835 friend class ::testing::UnitTest;
843 current_test_suite_ = a_current_test_suite;
850 current_test_info_ = a_current_test_info;
856 #if GTEST_HAS_FILE_SYSTEM
860 #endif // GTEST_HAS_FILE_SYSTEM
958 #ifdef GTEST_HAS_DEATH_TEST
961 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
962 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
963 #endif // GTEST_HAS_DEATH_TEST
979 return UnitTest::GetInstance()->impl();
982 #ifdef GTEST_USES_SIMPLE_RE
993 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
994 GTEST_API_ bool ValidateRegex(
const char* regex);
995 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
996 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
bool escaped,
char ch,
997 char repeat,
const char* regex,
999 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
1001 #endif // GTEST_USES_SIMPLE_RE
1008 #ifdef GTEST_HAS_DEATH_TEST
1012 GTEST_API_ std::string GetLastErrnoDescription();
1018 template <
typename Integer>
1019 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
1023 if (str.empty() || !
IsDigit(str[0])) {
1031 using BiggestConvertible =
unsigned long long;
1033 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1034 const bool parse_success = *end ==
'\0' && errno == 0;
1038 const Integer result =
static_cast<Integer
>(parsed);
1039 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1045 #endif // GTEST_HAS_DEATH_TEST
1056 const std::string& xml_element,
1071 #if GTEST_CAN_STREAM_RESULTS_
1077 class AbstractSocketWriter {
1079 virtual ~AbstractSocketWriter() =
default;
1082 virtual void Send(
const std::string& message) = 0;
1085 virtual void CloseConnection() {}
1088 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1092 class SocketWriter :
public AbstractSocketWriter {
1094 SocketWriter(
const std::string& host,
const std::string& port)
1095 : sockfd_(-1), host_name_(host), port_num_(port) {
1099 ~SocketWriter()
override {
1100 if (sockfd_ != -1) CloseConnection();
1104 void Send(
const std::string& message)
override {
1106 <<
"Send() can be called only when there is a connection.";
1108 const auto len =
static_cast<size_t>(message.length());
1109 if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) {
1110 GTEST_LOG_(WARNING) <<
"stream_result_to: failed to stream to "
1111 << host_name_ <<
":" << port_num_;
1117 void MakeConnection();
1120 void CloseConnection()
override {
1122 <<
"CloseConnection() can be called only when there is a connection.";
1129 const std::string host_name_;
1130 const std::string port_num_;
1132 SocketWriter(
const SocketWriter&) =
delete;
1133 SocketWriter& operator=(
const SocketWriter&) =
delete;
1137 static std::string UrlEncode(
const char* str);
1139 StreamingListener(
const std::string& host,
const std::string& port)
1140 : socket_writer_(new SocketWriter(host, port)) {
1144 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1145 : socket_writer_(socket_writer) {
1149 void OnTestProgramStart(
const UnitTest& )
override {
1150 SendLn(
"event=TestProgramStart");
1153 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1156 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1159 socket_writer_->CloseConnection();
1162 void OnTestIterationStart(
const UnitTest& ,
1163 int iteration)
override {
1164 SendLn(
"event=TestIterationStart&iteration=" +
1168 void OnTestIterationEnd(
const UnitTest& unit_test,
1170 SendLn(
"event=TestIterationEnd&passed=" + FormatBool(unit_test.Passed()) +
1177 void OnTestSuiteStart(
const TestSuite& test_suite)
override {
1178 SendLn(std::string(
"event=TestCaseStart&name=") + test_suite.name());
1183 void OnTestSuiteEnd(
const TestSuite& test_suite)
override {
1184 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_suite.Passed()) +
1189 void OnTestStart(
const TestInfo& test_info)
override {
1190 SendLn(std::string(
"event=TestStart&name=") + test_info.name());
1193 void OnTestEnd(
const TestInfo& test_info)
override {
1194 SendLn(
"event=TestEnd&passed=" +
1195 FormatBool((test_info.result())->Passed()) +
"&elapsed_time=" +
1199 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1200 const char* file_name = test_part_result.file_name();
1201 if (file_name ==
nullptr) file_name =
"";
1202 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1204 "&message=" + UrlEncode(test_part_result.message()));
1209 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1213 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1215 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1217 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1219 StreamingListener(
const StreamingListener&) =
delete;
1220 StreamingListener& operator=(
const StreamingListener&) =
delete;
1223 #endif // GTEST_CAN_STREAM_RESULTS_
1230 #endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
GTEST_API_ bool g_help_flag
class UnitTestImpl * GetUnitTestImpl()
void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc, TestInfo *test_info)
std::vector< Environment * > & environments()
void RecordProperty(const std::string &xml_element, const TestProperty &test_property)
static const std::vector< testing::TestPartResult > & test_part_results(const TestResult &test_result)
std::vector< int > test_suite_indices_
const TestSuite * GetTestSuite(int i) const
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
const TestResult * ad_hoc_test_result() const
std::string death_test_style_
TestPropertyKeyIs(const std::string &key)
E GetElementOr(const std::vector< E > &v, int i, E default_value)
std::vector< TestSuite * > test_suites_
TimeInMillis elapsed_time_
#define GTEST_DECLARE_bool_(name)
std::vector< Environment * > environments_
bool death_test_use_fork_
GTEST_API_ int32_t Int32FromEnvOrDie(const char *env_var, int32_t default_val)
GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
internal::ParameterizedTestSuiteRegistry & parameterized_test_registry()
void set_current_test_info(TestInfo *a_current_test_info)
const std::string test_suite_name_
int GetNextRandomSeed(int seed)
const std::vector< TestPartResult > & test_part_results() const
#define GTEST_LOG_(severity)
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty)
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
TimeInMillis start_timestamp() const
std::string internal_run_death_test_
TestSuite * GetMutableSuiteCase(int i)
uint32_t Generate(uint32_t range)
internal::Mutex global_test_part_result_reporter_mutex_
void ClearAdHocTestResult()
static void ClearTestPartResults(TestResult *test_result)
GTEST_API_ bool ShouldShard(const char *total_shards_str, const char *shard_index_str, bool in_subprocess_for_death_test)
bool catch_exceptions() const
UnitTestImpl *const unit_test_
static const char *const kElidedFramesMarker
void(*)( TearDownTestSuiteFunc)
OsStackTraceGetterInterface * os_stack_trace_getter_
std::string StreamableToString(const T &streamable)
bool operator()(const TestSuite *lhs, const TestSuite *rhs) const
GTEST_API_ std::string CodePointToUtf8(uint32_t code_point)
int32_t stack_trace_depth_
bool recreate_environments_when_repeating_
int last_death_test_suite_
internal::TypeParameterizedTestSuiteRegistry type_parameterized_test_registry_
std::set< std::string > ignored_parameterized_test_suites_
internal::ThreadLocal< std::vector< TraceInfo > > gtest_trace_stack_
#define GTEST_NO_TAIL_CALL_
TimeInMillis start_timestamp_
const std::vector< TraceInfo > & gtest_trace_stack() const
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
TestResult ad_hoc_test_result_
#define GTEST_CHECK_(condition)
bool parameterized_tests_registered_
GTEST_API_ const TypeId kTestTypeIdInGoogleTest
static void RecordProperty(TestResult *test_result, const std::string &xml_element, const TestProperty &property)
const TestCase * GetTestCase(int i) const
#define GTEST_FLAG_SET(name, value)
void ClearNonAdHocTestResult()
UnitTestImpl *const unit_test_
GTEST_API_ TimeInMillis GetTimeInMillis()
TestEventListeners listeners_
#define GTEST_FLAG_GET(name)
FilePath original_working_dir_
const TestInfo * current_test_info() const
GTEST_API_ bool ParseFlag(const char *str, const char *flag, int32_t *value)
GTEST_API_ std::string WideStringToUtf8(const wchar_t *str, int num_chars)
void ShuffleRange(internal::Random *random, int begin, int end, std::vector< E > *v)
TestPartResultReporterInterface * global_test_part_result_reporter_
internal::ParameterizedTestSuiteRegistry parameterized_test_registry_
int CountIf(const Container &c, Predicate predicate)
void set_current_test_suite(TestSuite *a_current_test_suite)
bool operator()(const TestProperty &test_property) const
TestEventListeners * listeners()
DefaultPerThreadTestPartResultReporter default_per_thread_test_part_result_reporter_
TestCase * GetTestCase(const std::string &test_case_name, const char *type_param, internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc)
GTEST_API_ void ParseGoogleTestFlagsOnly(int *argc, char **argv)
bool post_flag_parse_init_performed_
void Shuffle(internal::Random *random, std::vector< E > *v)
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
const TestSuite * current_test_suite() const
internal::Random * random()
std::vector< TraceInfo > & gtest_trace_stack()
TestSuite * current_test_suite_
TestInfo * current_test_info_
bool also_run_disabled_tests_
void ClearTestPartResults()
void set_catch_exceptions(bool value)
void(*)( SetUpTestSuiteFunc)
const char * type_param() const
int GetRandomSeedFromFlag(int32_t random_seed_flag)
DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_
internal::ThreadLocal< TestPartResultReporterInterface * > per_thread_test_part_result_reporter_
void ForEach(const Container &c, Functor functor)
TimeInMillis elapsed_time() const
void WriteToShardStatusFileIfNeeded()
std::string stream_result_to_
TestInfo * current_test_info()
std::unordered_map< std::string, TestSuite * > test_suites_by_name_
std::set< std::string > * ignored_parameterized_test_suites()
internal::TypeParameterizedTestSuiteRegistry & type_parameterized_test_registry()