34 #ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ 35 #define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ 53 #if GTEST_CAN_STREAM_RESULTS_ 54 #include <arpa/inet.h> 60 #endif // GTEST_OS_WINDOWS 113 const unsigned int raw_seed =
115 : static_cast<unsigned int>(random_seed_flag);
119 const int normalized_seed =
120 static_cast<int>((raw_seed - 1U) %
121 static_cast<unsigned int>(kMaxRandomSeed)) +
123 return normalized_seed;
131 <<
"Invalid random seed " << seed <<
" - must be in [1, " 132 << kMaxRandomSeed <<
"].";
133 const int next_seed = seed + 1;
134 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
143 also_run_disabled_tests_ =
GTEST_FLAG_GET(also_run_disabled_tests);
151 internal_run_death_test_ =
GTEST_FLAG_GET(internal_run_death_test);
159 recreate_environments_when_repeating_ =
169 GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_);
177 GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_);
186 recreate_environments_when_repeating_);
195 bool also_run_disabled_tests_;
196 bool break_on_failure_;
197 bool catch_exceptions_;
199 std::string death_test_style_;
200 bool death_test_use_fork_;
203 std::string internal_run_death_test_;
209 int32_t random_seed_;
211 bool recreate_environments_when_repeating_;
213 int32_t stack_trace_depth_;
214 std::string stream_result_to_;
215 bool throw_on_failure_;
254 const char* shard_index_str,
255 bool in_subprocess_for_death_test);
273 template <
class Container,
typename Predicate>
274 inline int CountIf(
const Container&
c, Predicate predicate) {
278 for (
auto it = c.begin(); it != c.end(); ++it) {
279 if (predicate(*it)) ++count;
285 template <
class Container,
typename Functor>
286 void ForEach(
const Container&
c, Functor functor) {
287 std::for_each(c.begin(), c.end(), functor);
292 template <
typename E>
294 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value
295 : v[
static_cast<size_t>(
i)];
302 template <
typename E>
305 const int size =
static_cast<int>(v->size());
307 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, " 310 <<
"Invalid shuffle range finish " << end <<
": must be in range [" 311 << begin <<
", " << size <<
"].";
315 for (
int range_width = end - begin; range_width >= 2; range_width--) {
316 const int last_in_range = begin + range_width - 1;
319 static_cast<int>(random->
Generate(static_cast<uint32_t>(range_width)));
320 std::swap((*v)[static_cast<size_t>(selected)],
321 (*v)[static_cast<size_t>(last_in_range)]);
326 template <
typename E>
328 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
333 template <
typename T>
334 static void Delete(T*
x) {
350 return test_property.
key() == key_;
372 static std::string GetOutputFormat();
377 static std::string GetAbsolutePathToOutputFile();
383 static bool FilterMatchesTest(
const std::string& test_suite_name,
384 const std::string& test_name);
392 static int GTestShouldProcessSEH(DWORD exception_code);
393 #endif // GTEST_OS_WINDOWS 397 static bool MatchesFilter(
const std::string& name,
const char* filter);
400 #if GTEST_HAS_FILE_SYSTEM 403 GTEST_API_ FilePath GetCurrentExecutableName();
404 #endif // GTEST_HAS_FILE_SYSTEM 418 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
423 virtual void UponLeavingGTest() = 0;
440 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
441 void UponLeavingGTest()
override;
451 void* caller_frame_ =
nullptr;
452 #endif // GTEST_HAS_ABSL 468 :
public TestPartResultReporterInterface {
473 void ReportTestPartResult(
const TestPartResult& result)
override;
487 :
public TestPartResultReporterInterface {
492 void ReportTestPartResult(
const TestPartResult& result)
override;
520 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
523 void SetGlobalTestPartResultReporter(
524 TestPartResultReporterInterface* reporter);
527 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
530 void SetTestPartResultReporterForCurrentThread(
531 TestPartResultReporterInterface* reporter);
534 int successful_test_suite_count()
const;
537 int failed_test_suite_count()
const;
540 int total_test_suite_count()
const;
544 int test_suite_to_run_count()
const;
547 int successful_test_count()
const;
550 int skipped_test_count()
const;
553 int failed_test_count()
const;
556 int reportable_disabled_test_count()
const;
559 int disabled_test_count()
const;
562 int reportable_test_count()
const;
565 int total_test_count()
const;
568 int test_to_run_count()
const;
579 bool Passed()
const {
return !Failed(); }
584 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
591 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
i)];
595 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 597 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 603 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
index)];
638 std::string CurrentOsStackTraceExceptTop(
int skip_count)
651 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
656 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 660 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
662 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ 674 #if GTEST_HAS_DEATH_TEST 682 if (original_working_dir_.IsEmpty()) {
683 original_working_dir_.Set(FilePath::GetCurrentDir());
685 <<
"Failed to get the current working directory.";
687 #endif // GTEST_HAS_DEATH_TEST 690 set_up_tc, tear_down_tc)
691 ->AddTestInfo(test_info);
697 return parameterized_test_registry_;
701 return &ignored_parameterized_test_suites_;
708 return type_parameterized_test_registry_;
713 current_test_suite_ = a_current_test_suite;
720 current_test_info_ = a_current_test_info;
729 void RegisterParameterizedTests();
739 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
762 void ListTestsMatchingFilter();
774 return *(gtest_trace_stack_.pointer());
777 return gtest_trace_stack_.get();
780 #if GTEST_HAS_DEATH_TEST 781 void InitDeathTestSubprocessControlInfo() {
782 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
788 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
789 return internal_run_death_test_flag_.get();
793 internal::DeathTestFactory* death_test_factory() {
794 return death_test_factory_.get();
797 void SuppressTestEventsIfInSubprocess();
799 friend class ReplaceDeathTestFactory;
800 #endif // GTEST_HAS_DEATH_TEST 804 void ConfigureXmlOutput();
806 #if GTEST_CAN_STREAM_RESULTS_ 809 void ConfigureStreamingOutput();
817 void PostFlagParsingInit();
830 void UnshuffleTests();
837 friend class ::testing::UnitTest;
841 void set_catch_exceptions(
bool value) { catch_exceptions_ = value; }
846 #if GTEST_HAS_FILE_SYSTEM 849 internal::FilePath original_working_dir_;
850 #endif // GTEST_HAS_FILE_SYSTEM 855 default_per_thread_test_part_result_reporter_;
858 TestPartResultReporterInterface* global_test_part_result_reporter_;
865 per_thread_test_part_result_reporter_;
869 std::vector<Environment*> environments_;
873 std::vector<TestSuite*> test_suites_;
879 std::vector<int> test_suite_indices_;
885 type_parameterized_test_registry_;
889 std::set<std::string> ignored_parameterized_test_suites_;
892 bool parameterized_tests_registered_;
895 int last_death_test_suite_;
930 bool post_flag_parse_init_performed_;
945 #if GTEST_HAS_DEATH_TEST 948 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
949 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
950 #endif // GTEST_HAS_DEATH_TEST 957 bool catch_exceptions_;
966 return UnitTest::GetInstance()->impl();
969 #if GTEST_USES_SIMPLE_RE 973 GTEST_API_ bool IsInSet(
char ch,
const char* str);
980 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
981 GTEST_API_ bool ValidateRegex(
const char* regex);
982 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
983 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
bool escaped,
char ch,
984 char repeat,
const char* regex,
986 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
988 #endif // GTEST_USES_SIMPLE_RE 995 #if GTEST_HAS_DEATH_TEST 999 GTEST_API_ std::string GetLastErrnoDescription();
1005 template <
typename Integer>
1006 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
1010 if (str.empty() || !
IsDigit(str[0])) {
1018 using BiggestConvertible =
unsigned long long;
1020 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1021 const bool parse_success = *end ==
'\0' && errno == 0;
1025 const Integer result =
static_cast<Integer
>(parsed);
1026 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1032 #endif // GTEST_HAS_DEATH_TEST 1043 const std::string& xml_element,
1045 test_result->RecordProperty(xml_element, property);
1049 test_result->ClearTestPartResults();
1054 return test_result.test_part_results();
1058 #if GTEST_CAN_STREAM_RESULTS_ 1064 class AbstractSocketWriter {
1066 virtual ~AbstractSocketWriter() {}
1069 virtual void Send(
const std::string& message) = 0;
1072 virtual void CloseConnection() {}
1075 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1079 class SocketWriter :
public AbstractSocketWriter {
1081 SocketWriter(
const std::string& host,
const std::string& port)
1082 : sockfd_(-1), host_name_(host), port_num_(port) {
1086 ~SocketWriter()
override {
1087 if (sockfd_ != -1) CloseConnection();
1091 void Send(
const std::string& message)
override {
1093 <<
"Send() can be called only when there is a connection.";
1095 const auto len =
static_cast<size_t>(message.length());
1096 if (
write(sockfd_, message.c_str(),
len) != static_cast<ssize_t>(
len)) {
1097 GTEST_LOG_(WARNING) <<
"stream_result_to: failed to stream to " 1098 << host_name_ <<
":" << port_num_;
1104 void MakeConnection();
1107 void CloseConnection()
override {
1109 <<
"CloseConnection() can be called only when there is a connection.";
1116 const std::string host_name_;
1117 const std::string port_num_;
1119 SocketWriter(
const SocketWriter&) =
delete;
1120 SocketWriter& operator=(
const SocketWriter&) =
delete;
1124 static std::string UrlEncode(
const char* str);
1126 StreamingListener(
const std::string& host,
const std::string& port)
1127 : socket_writer_(
new SocketWriter(host, port)) {
1131 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1132 : socket_writer_(socket_writer) {
1136 void OnTestProgramStart(
const UnitTest& )
override {
1137 SendLn(
"event=TestProgramStart");
1140 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1143 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.
Passed()));
1146 socket_writer_->CloseConnection();
1149 void OnTestIterationStart(
const UnitTest& ,
1151 SendLn(
"event=TestIterationStart&iteration=" +
1155 void OnTestIterationEnd(
const UnitTest& unit_test,
1157 SendLn(
"event=TestIterationEnd&passed=" + FormatBool(unit_test.
Passed()) +
1164 void OnTestSuiteStart(
const TestSuite& test_suite)
override {
1165 SendLn(std::string(
"event=TestCaseStart&name=") + test_suite.
name());
1170 void OnTestSuiteEnd(
const TestSuite& test_suite)
override {
1171 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_suite.
Passed()) +
1176 void OnTestStart(
const TestInfo& test_info)
override {
1177 SendLn(std::string(
"event=TestStart&name=") + test_info.
name());
1180 void OnTestEnd(
const TestInfo& test_info)
override {
1181 SendLn(
"event=TestEnd&passed=" +
1182 FormatBool((test_info.
result())->Passed()) +
"&elapsed_time=" +
1186 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1187 const char* file_name = test_part_result.file_name();
1188 if (file_name ==
nullptr) file_name =
"";
1189 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1191 "&message=" + UrlEncode(test_part_result.message()));
1196 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1200 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1202 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1204 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1206 StreamingListener(
const StreamingListener&) =
delete;
1207 StreamingListener& operator=(
const StreamingListener&) =
delete;
1210 #endif // GTEST_CAN_STREAM_RESULTS_ 1217 #endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
class UnitTestImpl * GetUnitTestImpl()
void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc, TestInfo *test_info)
std::vector< Environment * > & environments()
static const std::vector< testing::TestPartResult > & test_part_results(const TestResult &test_result)
GTEST_API_ bool g_help_flag
const TestResult * result() const
void(*)() SetUpTestSuiteFunc
TimeInMillis start_timestamp() const
TestPropertyKeyIs(const std::string &key)
E GetElementOr(const std::vector< E > &v, int i, E default_value)
n The following was calculated during iteration
void write(std::ostream &os, const datablock &db)
#define GTEST_DECLARE_bool_(name)
GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
internal::ParameterizedTestSuiteRegistry & parameterized_test_registry()
void set_current_test_info(TestInfo *a_current_test_info)
TimeInMillis elapsed_time() const
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
int GetNextRandomSeed(int seed)
#define GTEST_LOG_(severity)
GTEST_API_ const TypeId kTestTypeIdInGoogleTest
TestSuite * GetMutableSuiteCase(int i)
uint32_t Generate(uint32_t range)
void ClearAdHocTestResult()
GTEST_API_ int32_t Int32FromEnvOrDie(const char *env_var, int32_t default_val)
double elapsed_time(ProcessorTimeStamp &time, bool _IN_SECS)
const TestInfo * current_test_info() const
GTEST_API_ bool ParseFlag(const char *str, const char *flag, int32_t *value)
const TestCase * GetTestCase(int i) const
static void ClearTestPartResults(TestResult *test_result)
const char * name() const
const TestSuite * GetTestSuite(int i) const
std::string StreamableToString(const T &streamable)
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty)
const char * name() const
const char * test_suite_name() const
#define GTEST_NO_TAIL_CALL_
bool operator()(const TestProperty &test_property) const
#define GTEST_CHECK_(condition)
void(*)() TearDownTestSuiteFunc
GTEST_API_ std::string WideStringToUtf8(const wchar_t *str, int num_chars)
GTEST_API_ TimeInMillis GetTimeInMillis()
const std::vector< TraceInfo > & gtest_trace_stack() const
GTEST_API_ bool ShouldShard(const char *total_shards_str, const char *shard_index_str, bool in_subprocess_for_death_test)
const TestResult * ad_hoc_test_result() const
static void RecordProperty(TestResult *test_result, const std::string &xml_element, const TestProperty &property)
const char * type_param() const
#define GTEST_FLAG_SET(name, value)
void ClearNonAdHocTestResult()
virtual ~OsStackTraceGetterInterface()
#define GTEST_FLAG_GET(name)
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) GTEST_DECLARE_bool_(death_test_use_fork)
void ShuffleRange(internal::Random *random, int begin, int end, std::vector< E > *v)
TimeInMillis elapsed_time() const
int CountIf(const Container &c, Predicate predicate)
void set_current_test_suite(TestSuite *a_current_test_suite)
TestEventListeners * listeners()
GTEST_API_ std::string CodePointToUtf8(uint32_t code_point)
TimeInMillis elapsed_time() const
void Shuffle(internal::Random *random, std::vector< E > *v)
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
static const char *const kElidedFramesMarker
const TestSuite * current_test_suite() const
internal::Random * random()
std::vector< TraceInfo > & gtest_trace_stack()
bool catch_exceptions() const
OsStackTraceGetterInterface()
int GetRandomSeedFromFlag(int32_t random_seed_flag)
void ForEach(const Container &c, Functor functor)
GTEST_API_ void ParseGoogleTestFlagsOnly(int *argc, wchar_t **argv)
void WriteToShardStatusFileIfNeeded()
TestInfo * current_test_info()
std::set< std::string > * ignored_parameterized_test_suites()
internal::TypeParameterizedTestSuiteRegistry & type_parameterized_test_registry()
TestCase * GetTestCase(const char *test_case_name, const char *type_param, internal::SetUpTestSuiteFunc set_up_tc, internal::TearDownTestSuiteFunc tear_down_tc)