34 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GTEST_SRC_GTEST_INTERNAL_INL_H_
52 #if GTEST_CAN_STREAM_RESULTS_
53 # include <arpa/inet.h>
59 #endif // GTEST_OS_WINDOWS
83 const char kAlsoRunDisabledTestsFlag[] =
"also_run_disabled_tests";
84 const char kBreakOnFailureFlag[] =
"break_on_failure";
85 const char kCatchExceptionsFlag[] =
"catch_exceptions";
86 const char kColorFlag[] =
"color";
87 const char kFailFast[] =
"fail_fast";
88 const char kFilterFlag[] =
"filter";
89 const char kListTestsFlag[] =
"list_tests";
90 const char kOutputFlag[] =
"output";
91 const char kBriefFlag[] =
"brief";
92 const char kPrintTimeFlag[] =
"print_time";
93 const char kPrintUTF8Flag[] =
"print_utf8";
94 const char kRandomSeedFlag[] =
"random_seed";
95 const char kRepeatFlag[] =
"repeat";
96 const char kShuffleFlag[] =
"shuffle";
97 const char kStackTraceDepthFlag[] =
"stack_trace_depth";
98 const char kStreamResultToFlag[] =
"stream_result_to";
99 const char kThrowOnFailureFlag[] =
"throw_on_failure";
100 const char kFlagfileFlag[] =
"flagfile";
103 const int kMaxRandomSeed = 99999;
129 const char* str,
const char* flag, int32_t*
value);
133 inline int GetRandomSeedFromFlag(int32_t random_seed_flag) {
134 const unsigned int raw_seed = (random_seed_flag == 0) ?
136 static_cast<unsigned int>(random_seed_flag);
140 const int normalized_seed =
141 static_cast<int>((raw_seed - 1U) %
142 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
143 return normalized_seed;
149 inline int GetNextRandomSeed(
int seed) {
151 <<
"Invalid random seed " << seed <<
" - must be in [1, "
152 << kMaxRandomSeed <<
"].";
153 const int next_seed = seed + 1;
154 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
159 class GTestFlagSaver {
163 also_run_disabled_tests_ =
GTEST_FLAG(also_run_disabled_tests);
164 break_on_failure_ =
GTEST_FLAG(break_on_failure);
165 catch_exceptions_ =
GTEST_FLAG(catch_exceptions);
167 death_test_style_ =
GTEST_FLAG(death_test_style);
168 death_test_use_fork_ =
GTEST_FLAG(death_test_use_fork);
171 internal_run_death_test_ =
GTEST_FLAG(internal_run_death_test);
180 stack_trace_depth_ =
GTEST_FLAG(stack_trace_depth);
181 stream_result_to_ =
GTEST_FLAG(stream_result_to);
182 throw_on_failure_ =
GTEST_FLAG(throw_on_failure);
187 GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
188 GTEST_FLAG(break_on_failure) = break_on_failure_;
189 GTEST_FLAG(catch_exceptions) = catch_exceptions_;
191 GTEST_FLAG(death_test_style) = death_test_style_;
192 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
195 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
204 GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
205 GTEST_FLAG(stream_result_to) = stream_result_to_;
206 GTEST_FLAG(throw_on_failure) = throw_on_failure_;
211 bool also_run_disabled_tests_;
212 bool break_on_failure_;
213 bool catch_exceptions_;
215 std::string death_test_style_;
216 bool death_test_use_fork_;
219 std::string internal_run_death_test_;
225 int32_t random_seed_;
228 int32_t stack_trace_depth_;
229 std::string stream_result_to_;
230 bool throw_on_failure_;
269 const char* shard_index_str,
270 bool in_subprocess_for_death_test);
282 int total_shards,
int shard_index,
int test_id);
288 template <
class Container,
typename Predicate>
289 inline int CountIf(
const Container&
c, Predicate predicate) {
293 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
301 template <
class Container,
typename Functor>
302 void ForEach(
const Container& c, Functor functor) {
303 std::for_each(c.begin(), c.end(), functor);
308 template <
typename E>
309 inline E GetElementOr(
const std::vector<E>& v,
int i,
E default_value) {
310 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value
311 : v[
static_cast<size_t>(
i)];
318 template <
typename E>
319 void ShuffleRange(internal::Random* random,
int begin,
int end,
321 const int size =
static_cast<int>(v->size());
323 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, "
326 <<
"Invalid shuffle range finish " << end <<
": must be in range ["
327 << begin <<
", " << size <<
"].";
331 for (
int range_width = end - begin; range_width >= 2; range_width--) {
332 const int last_in_range = begin + range_width - 1;
335 static_cast<int>(random->Generate(static_cast<uint32_t>(range_width)));
336 std::swap((*v)[static_cast<size_t>(selected)],
337 (*v)[static_cast<size_t>(last_in_range)]);
342 template <
typename E>
343 inline void Shuffle(internal::Random* random, std::vector<E>* v) {
344 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
349 template <
typename T>
350 static void Delete(
T*
x) {
357 class TestPropertyKeyIs {
362 explicit TestPropertyKeyIs(
const std::string& key) : key_(key) {}
365 bool operator()(
const TestProperty& test_property)
const {
366 return test_property.key() == key_;
388 static std::string GetOutputFormat();
393 static std::string GetAbsolutePathToOutputFile();
402 static bool PatternMatchesString(
const char *pattern,
const char *str);
406 static bool FilterMatchesTest(
const std::string& test_suite_name,
407 const std::string& test_name);
415 static int GTestShouldProcessSEH(DWORD exception_code);
416 #endif // GTEST_OS_WINDOWS
420 static bool MatchesFilter(
const std::string& name,
const char* filter);
428 class OsStackTraceGetterInterface {
430 OsStackTraceGetterInterface() {}
431 virtual ~OsStackTraceGetterInterface() {}
439 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
444 virtual void UponLeavingGTest() = 0;
448 static const char*
const kElidedFramesMarker;
455 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
457 OsStackTraceGetter() {}
459 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
460 void UponLeavingGTest()
override;
470 void* caller_frame_ =
nullptr;
471 #endif // GTEST_HAS_ABSL
485 class DefaultGlobalTestPartResultReporter
486 :
public TestPartResultReporterInterface {
488 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
491 void ReportTestPartResult(
const TestPartResult& result)
override;
494 UnitTestImpl*
const unit_test_;
501 class DefaultPerThreadTestPartResultReporter
502 :
public TestPartResultReporterInterface {
504 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
507 void ReportTestPartResult(
const TestPartResult& result)
override;
510 UnitTestImpl*
const unit_test_;
521 explicit UnitTestImpl(UnitTest* parent);
522 virtual ~UnitTestImpl();
532 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
535 void SetGlobalTestPartResultReporter(
536 TestPartResultReporterInterface* reporter);
539 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
542 void SetTestPartResultReporterForCurrentThread(
543 TestPartResultReporterInterface* reporter);
546 int successful_test_suite_count()
const;
549 int failed_test_suite_count()
const;
552 int total_test_suite_count()
const;
556 int test_suite_to_run_count()
const;
559 int successful_test_count()
const;
562 int skipped_test_count()
const;
565 int failed_test_count()
const;
568 int reportable_disabled_test_count()
const;
571 int disabled_test_count()
const;
574 int reportable_test_count()
const;
577 int total_test_count()
const;
580 int test_to_run_count()
const;
584 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
587 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
591 bool Passed()
const {
return !Failed(); }
595 bool Failed()
const {
596 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
601 const TestSuite* GetTestSuite(
int i)
const {
602 const int index = GetElementOr(test_suite_indices_, i, -1);
603 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(
i)];
607 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
608 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(i); }
609 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
613 TestSuite* GetMutableSuiteCase(
int i) {
614 const int index = GetElementOr(test_suite_indices_, i, -1);
615 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
619 TestEventListeners* listeners() {
return &listeners_; }
623 TestResult* current_test_result();
626 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
633 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
638 OsStackTraceGetterInterface* os_stack_trace_getter();
662 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
667 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
668 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
671 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
673 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
684 TestInfo* test_info) {
695 <<
"Failed to get the current working directory.";
698 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
699 set_up_tc, tear_down_tc)
700 ->AddTestInfo(test_info);
705 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
706 return parameterized_test_registry_;
709 std::set<std::string>* ignored_parameterized_test_suites() {
710 return &ignored_parameterized_test_suites_;
715 internal::TypeParameterizedTestSuiteRegistry&
716 type_parameterized_test_registry() {
717 return type_parameterized_test_registry_;
721 void set_current_test_suite(TestSuite* a_current_test_suite) {
722 current_test_suite_ = a_current_test_suite;
728 void set_current_test_info(TestInfo* a_current_test_info) {
729 current_test_info_ = a_current_test_info;
738 void RegisterParameterizedTests();
747 void ClearNonAdHocTestResult() {
748 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
752 void ClearAdHocTestResult() {
753 ad_hoc_test_result_.Clear();
760 void RecordProperty(
const TestProperty& test_property);
762 enum ReactionToSharding {
763 HONOR_SHARDING_PROTOCOL,
764 IGNORE_SHARDING_PROTOCOL
773 int FilterTests(ReactionToSharding shard_tests);
776 void ListTestsMatchingFilter();
778 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
779 TestInfo* current_test_info() {
return current_test_info_; }
780 const TestInfo* current_test_info()
const {
return current_test_info_; }
784 std::vector<Environment*>& environments() {
return environments_; }
787 std::vector<TraceInfo>& gtest_trace_stack() {
788 return *(gtest_trace_stack_.pointer());
790 const std::vector<TraceInfo>& gtest_trace_stack()
const {
791 return gtest_trace_stack_.get();
794 #if GTEST_HAS_DEATH_TEST
795 void InitDeathTestSubprocessControlInfo() {
796 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
802 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
803 return internal_run_death_test_flag_.get();
807 internal::DeathTestFactory* death_test_factory() {
808 return death_test_factory_.get();
811 void SuppressTestEventsIfInSubprocess();
813 friend class ReplaceDeathTestFactory;
814 #endif // GTEST_HAS_DEATH_TEST
818 void ConfigureXmlOutput();
820 #if GTEST_CAN_STREAM_RESULTS_
823 void ConfigureStreamingOutput();
831 void PostFlagParsingInit();
834 int random_seed()
const {
return random_seed_; }
837 internal::Random* random() {
return &random_; }
844 void UnshuffleTests();
848 bool catch_exceptions()
const {
return catch_exceptions_; }
851 friend class ::testing::UnitTest;
855 void set_catch_exceptions(
bool value) { catch_exceptions_ =
value; }
858 UnitTest*
const parent_;
865 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
866 DefaultPerThreadTestPartResultReporter
867 default_per_thread_test_part_result_reporter_;
870 TestPartResultReporterInterface* global_test_part_result_repoter_;
873 internal::Mutex global_test_part_result_reporter_mutex_;
876 internal::ThreadLocal<TestPartResultReporterInterface*>
877 per_thread_test_part_result_reporter_;
881 std::vector<Environment*> environments_;
885 std::vector<TestSuite*> test_suites_;
891 std::vector<int> test_suite_indices_;
895 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
896 internal::TypeParameterizedTestSuiteRegistry
897 type_parameterized_test_registry_;
901 std::set<std::string> ignored_parameterized_test_suites_;
904 bool parameterized_tests_registered_;
907 int last_death_test_suite_;
913 TestSuite* current_test_suite_;
919 TestInfo* current_test_info_;
929 TestResult ad_hoc_test_result_;
933 TestEventListeners listeners_;
939 OsStackTraceGetterInterface* os_stack_trace_getter_;
942 bool post_flag_parse_init_performed_;
948 internal::Random random_;
957 #if GTEST_HAS_DEATH_TEST
960 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
961 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
962 #endif // GTEST_HAS_DEATH_TEST
965 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
969 bool catch_exceptions_;
976 inline UnitTestImpl* GetUnitTestImpl() {
977 return UnitTest::GetInstance()->impl();
980 #if GTEST_USES_SIMPLE_RE
991 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
992 GTEST_API_ bool ValidateRegex(
const char* regex);
993 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
994 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
995 bool escaped,
char ch,
char repeat,
const char* regex,
const char* str);
996 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
998 #endif // GTEST_USES_SIMPLE_RE
1005 #if GTEST_HAS_DEATH_TEST
1009 GTEST_API_ std::string GetLastErrnoDescription();
1015 template <
typename Integer>
1016 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
1020 if (str.empty() || !
IsDigit(str[0])) {
1028 using BiggestConvertible =
unsigned long long;
1030 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1031 const bool parse_success = *end ==
'\0' && errno == 0;
1035 const Integer result =
static_cast<Integer
>(parsed);
1036 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1042 #endif // GTEST_HAS_DEATH_TEST
1050 class TestResultAccessor {
1052 static void RecordProperty(TestResult* test_result,
1053 const std::string& xml_element,
1054 const TestProperty& property) {
1055 test_result->RecordProperty(xml_element, property);
1058 static void ClearTestPartResults(TestResult* test_result) {
1059 test_result->ClearTestPartResults();
1062 static const std::vector<testing::TestPartResult>& test_part_results(
1063 const TestResult& test_result) {
1064 return test_result.test_part_results();
1068 #if GTEST_CAN_STREAM_RESULTS_
1071 class StreamingListener :
public EmptyTestEventListener {
1074 class AbstractSocketWriter {
1076 virtual ~AbstractSocketWriter() {}
1079 virtual void Send(
const std::string& message) = 0;
1082 virtual void CloseConnection() {}
1085 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1089 class SocketWriter :
public AbstractSocketWriter {
1091 SocketWriter(
const std::string& host,
const std::string& port)
1092 : sockfd_(-1), host_name_(host), port_num_(port) {
1096 ~SocketWriter()
override {
1102 void Send(
const std::string& message)
override {
1104 <<
"Send() can be called only when there is a connection.";
1106 const auto len =
static_cast<size_t>(message.length());
1107 if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) {
1109 <<
"stream_result_to: failed to stream to "
1110 << host_name_ <<
":" << port_num_;
1116 void MakeConnection();
1119 void CloseConnection()
override {
1121 <<
"CloseConnection() can be called only when there is a connection.";
1128 const std::string host_name_;
1129 const std::string port_num_;
1135 static std::string UrlEncode(
const char* str);
1137 StreamingListener(
const std::string& host,
const std::string& port)
1138 : socket_writer_(
new SocketWriter(host, port)) {
1142 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1143 : socket_writer_(socket_writer) { Start(); }
1145 void OnTestProgramStart(
const UnitTest& )
override {
1146 SendLn(
"event=TestProgramStart");
1149 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1152 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1155 socket_writer_->CloseConnection();
1158 void OnTestIterationStart(
const UnitTest& ,
1159 int iteration)
override {
1160 SendLn(
"event=TestIterationStart&iteration=" +
1164 void OnTestIterationEnd(
const UnitTest& unit_test,
1166 SendLn(
"event=TestIterationEnd&passed=" +
1167 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1173 void OnTestCaseStart(
const TestCase& test_case)
override {
1174 SendLn(std::string(
"event=TestCaseStart&name=") + test_case.name());
1179 void OnTestCaseEnd(
const TestCase& test_case)
override {
1180 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1185 void OnTestStart(
const TestInfo& test_info)
override {
1186 SendLn(std::string(
"event=TestStart&name=") + test_info.name());
1189 void OnTestEnd(
const TestInfo& test_info)
override {
1190 SendLn(
"event=TestEnd&passed=" +
1191 FormatBool((test_info.result())->Passed()) +
1196 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1197 const char* file_name = test_part_result.file_name();
1198 if (file_name ==
nullptr) file_name =
"";
1199 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1201 "&message=" + UrlEncode(test_part_result.message()));
1206 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1210 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1212 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1214 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1219 #endif // GTEST_CAN_STREAM_RESULTS_
1226 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_
std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
#define GTEST_DECLARE_bool_(name)
#define GTEST_ATTRIBUTE_UNUSED_
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
#define GTEST_LOG_(severity)
bool ShouldUseColor(bool stdout_is_tty)
bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
void(*)( TearDownTestSuiteFunc)
std::string StreamableToString(const T &streamable)
std::string CodePointToUtf8(uint32_t code_point)
internal::TimeInMillis TimeInMillis
bool ShouldShard(const char *total_shards_env, const char *shard_index_env, bool in_subprocess_for_death_test)
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)
#define GTEST_CHECK_(condition)
const TypeId kTestTypeIdInGoogleTest
FilePath GetCurrentExecutableName()
TimeInMillis GetTimeInMillis()
int32_t Int32FromEnvOrDie(const char *var, int32_t default_val)
FilePath original_working_dir_
bool ParseInt32Flag(const char *str, const char *flag, int32_t *value)
std::string WideStringToUtf8(const wchar_t *str, int num_chars)
void ParseGoogleTestFlagsOnly(int *argc, char **argv)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
void(*)( SetUpTestSuiteFunc)
void WriteToShardStatusFileIfNeeded()