1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Utility functions and classes used by the Google C++ testing framework.//
31 // This file contains purely Google Test's internal implementation. Please
32 // DO NOT #INCLUDE IT IN A USER PROGRAM.
34 #ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
41 #include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
42 #include <string.h> // For memmove.
50 #include "gtest/internal/gtest-port.h"
52 #if GTEST_CAN_STREAM_RESULTS_
53 # include <arpa/inet.h> // NOLINT
54 # include <netdb.h> // NOLINT
58 # include <windows.h> // NOLINT
59 #endif // GTEST_OS_WINDOWS
61 #include "gtest/gtest.h"
62 #include "gtest/gtest-spi.h"
64 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
65 /* class A needs to have dll-interface to be used by clients of class B */)
67 // Declares the flags.
69 // We don't want the users to modify this flag in the code, but want
70 // Google Test's own unit tests to be able to access it. Therefore we
71 // declare it here as opposed to in gtest.h.
72 GTEST_DECLARE_bool_(death_test_use_fork);
77 // The value of GetTestTypeId() as seen from within the Google Test
78 // library. This is solely for testing GetTestTypeId().
79 GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
81 // A valid random seed must be in [1, kMaxRandomSeed].
82 const int kMaxRandomSeed = 99999;
84 // g_help_flag is true if and only if the --help flag or an equivalent form
85 // is specified on the command line.
86 GTEST_API_ extern bool g_help_flag;
88 // Returns the current time in milliseconds.
89 GTEST_API_ TimeInMillis GetTimeInMillis();
91 // Returns true if and only if Google Test should use colors in the output.
92 GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
94 // Formats the given time in milliseconds as seconds.
95 GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
97 // Converts the given time in milliseconds to a date string in the ISO 8601
98 // format, without the timezone information. N.B.: due to the use the
99 // non-reentrant localtime() function, this function is not thread safe. Do
100 // not use it in any code that can be called from multiple threads.
101 GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms);
103 // Parses a string for an Int32 flag, in the form of "--flag=value".
105 // On success, stores the value of the flag in *value, and returns
106 // true. On failure, returns false without changing *value.
107 GTEST_API_ bool ParseFlag(const char* str, const char* flag, int32_t* value);
109 // Returns a random seed in range [1, kMaxRandomSeed] based on the
110 // given --gtest_random_seed flag value.
111 inline int GetRandomSeedFromFlag(int32_t random_seed_flag) {
112 const unsigned int raw_seed = (random_seed_flag == 0) ?
113 static_cast<unsigned int>(GetTimeInMillis()) :
114 static_cast<unsigned int>(random_seed_flag);
116 // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
117 // it's easy to type.
118 const int normalized_seed =
119 static_cast<int>((raw_seed - 1U) %
120 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
121 return normalized_seed;
124 // Returns the first valid random seed after 'seed'. The behavior is
125 // undefined if 'seed' is invalid. The seed after kMaxRandomSeed is
126 // considered to be 1.
127 inline int GetNextRandomSeed(int seed) {
128 GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
129 << "Invalid random seed " << seed << " - must be in [1, "
130 << kMaxRandomSeed << "].";
131 const int next_seed = seed + 1;
132 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
135 // This class saves the values of all Google Test flags in its c'tor, and
136 // restores them in its d'tor.
137 class GTestFlagSaver {
141 also_run_disabled_tests_ = GTEST_FLAG_GET(also_run_disabled_tests);
142 break_on_failure_ = GTEST_FLAG_GET(break_on_failure);
143 catch_exceptions_ = GTEST_FLAG_GET(catch_exceptions);
144 color_ = GTEST_FLAG_GET(color);
145 death_test_style_ = GTEST_FLAG_GET(death_test_style);
146 death_test_use_fork_ = GTEST_FLAG_GET(death_test_use_fork);
147 fail_fast_ = GTEST_FLAG_GET(fail_fast);
148 filter_ = GTEST_FLAG_GET(filter);
149 internal_run_death_test_ = GTEST_FLAG_GET(internal_run_death_test);
150 list_tests_ = GTEST_FLAG_GET(list_tests);
151 output_ = GTEST_FLAG_GET(output);
152 brief_ = GTEST_FLAG_GET(brief);
153 print_time_ = GTEST_FLAG_GET(print_time);
154 print_utf8_ = GTEST_FLAG_GET(print_utf8);
155 random_seed_ = GTEST_FLAG_GET(random_seed);
156 repeat_ = GTEST_FLAG_GET(repeat);
157 recreate_environments_when_repeating_ =
158 GTEST_FLAG_GET(recreate_environments_when_repeating);
159 shuffle_ = GTEST_FLAG_GET(shuffle);
160 stack_trace_depth_ = GTEST_FLAG_GET(stack_trace_depth);
161 stream_result_to_ = GTEST_FLAG_GET(stream_result_to);
162 throw_on_failure_ = GTEST_FLAG_GET(throw_on_failure);
165 // The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS.
167 GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_);
168 GTEST_FLAG_SET(break_on_failure, break_on_failure_);
169 GTEST_FLAG_SET(catch_exceptions, catch_exceptions_);
170 GTEST_FLAG_SET(color, color_);
171 GTEST_FLAG_SET(death_test_style, death_test_style_);
172 GTEST_FLAG_SET(death_test_use_fork, death_test_use_fork_);
173 GTEST_FLAG_SET(filter, filter_);
174 GTEST_FLAG_SET(fail_fast, fail_fast_);
175 GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_);
176 GTEST_FLAG_SET(list_tests, list_tests_);
177 GTEST_FLAG_SET(output, output_);
178 GTEST_FLAG_SET(brief, brief_);
179 GTEST_FLAG_SET(print_time, print_time_);
180 GTEST_FLAG_SET(print_utf8, print_utf8_);
181 GTEST_FLAG_SET(random_seed, random_seed_);
182 GTEST_FLAG_SET(repeat, repeat_);
183 GTEST_FLAG_SET(recreate_environments_when_repeating,
184 recreate_environments_when_repeating_);
185 GTEST_FLAG_SET(shuffle, shuffle_);
186 GTEST_FLAG_SET(stack_trace_depth, stack_trace_depth_);
187 GTEST_FLAG_SET(stream_result_to, stream_result_to_);
188 GTEST_FLAG_SET(throw_on_failure, throw_on_failure_);
192 // Fields for saving the original values of flags.
193 bool also_run_disabled_tests_;
194 bool break_on_failure_;
195 bool catch_exceptions_;
197 std::string death_test_style_;
198 bool death_test_use_fork_;
201 std::string internal_run_death_test_;
207 int32_t random_seed_;
209 bool recreate_environments_when_repeating_;
211 int32_t stack_trace_depth_;
212 std::string stream_result_to_;
213 bool throw_on_failure_;
214 } GTEST_ATTRIBUTE_UNUSED_;
216 // Converts a Unicode code point to a narrow string in UTF-8 encoding.
217 // code_point parameter is of type UInt32 because wchar_t may not be
218 // wide enough to contain a code point.
219 // If the code_point is not a valid Unicode code point
220 // (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted
221 // to "(Invalid Unicode 0xXXXXXXXX)".
222 GTEST_API_ std::string CodePointToUtf8(uint32_t code_point);
224 // Converts a wide string to a narrow string in UTF-8 encoding.
225 // The wide string is assumed to have the following encoding:
226 // UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin)
227 // UTF-32 if sizeof(wchar_t) == 4 (on Linux)
228 // Parameter str points to a null-terminated wide string.
229 // Parameter num_chars may additionally limit the number
230 // of wchar_t characters processed. -1 is used when the entire string
231 // should be processed.
232 // If the string contains code points that are not valid Unicode code points
233 // (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
234 // as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
235 // and contains invalid UTF-16 surrogate pairs, values in those pairs
236 // will be encoded as individual Unicode characters from Basic Normal Plane.
237 GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars);
239 // Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
240 // if the variable is present. If a file already exists at this location, this
241 // function will write over it. If the variable is present, but the file cannot
242 // be created, prints an error and exits.
243 void WriteToShardStatusFileIfNeeded();
245 // Checks whether sharding is enabled by examining the relevant
246 // environment variable values. If the variables are present,
247 // but inconsistent (e.g., shard_index >= total_shards), prints
248 // an error and exits. If in_subprocess_for_death_test, sharding is
249 // disabled because it must only be applied to the original test
250 // process. Otherwise, we could filter out death tests we intended to execute.
251 GTEST_API_ bool ShouldShard(const char* total_shards_str,
252 const char* shard_index_str,
253 bool in_subprocess_for_death_test);
255 // Parses the environment variable var as a 32-bit integer. If it is unset,
256 // returns default_val. If it is not a 32-bit integer, prints an error and
258 GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val);
260 // Given the total number of shards, the shard index, and the test id,
261 // returns true if and only if the test should be run on this shard. The test id
262 // is some arbitrary but unique non-negative integer assigned to each test
263 // method. Assumes that 0 <= shard_index < total_shards.
264 GTEST_API_ bool ShouldRunTestOnShard(
265 int total_shards, int shard_index, int test_id);
267 // STL container utilities.
269 // Returns the number of elements in the given container that satisfy
270 // the given predicate.
271 template <class Container, typename Predicate>
272 inline int CountIf(const Container& c, Predicate predicate) {
273 // Implemented as an explicit loop since std::count_if() in libCstd on
274 // Solaris has a non-standard signature.
276 for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
283 // Applies a function/functor to each element in the container.
284 template <class Container, typename Functor>
285 void ForEach(const Container& c, Functor functor) {
286 std::for_each(c.begin(), c.end(), functor);
289 // Returns the i-th element of the vector, or default_value if i is not
290 // in range [0, v.size()).
291 template <typename E>
292 inline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
293 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value
294 : v[static_cast<size_t>(i)];
297 // Performs an in-place shuffle of a range of the vector's elements.
298 // 'begin' and 'end' are element indices as an STL-style range;
299 // i.e. [begin, end) are shuffled, where 'end' == size() means to
300 // shuffle to the end of the vector.
301 template <typename E>
302 void ShuffleRange(internal::Random* random, int begin, int end,
304 const int size = static_cast<int>(v->size());
305 GTEST_CHECK_(0 <= begin && begin <= size)
306 << "Invalid shuffle range start " << begin << ": must be in range [0, "
308 GTEST_CHECK_(begin <= end && end <= size)
309 << "Invalid shuffle range finish " << end << ": must be in range ["
310 << begin << ", " << size << "].";
312 // Fisher-Yates shuffle, from
313 // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
314 for (int range_width = end - begin; range_width >= 2; range_width--) {
315 const int last_in_range = begin + range_width - 1;
318 static_cast<int>(random->Generate(static_cast<uint32_t>(range_width)));
319 std::swap((*v)[static_cast<size_t>(selected)],
320 (*v)[static_cast<size_t>(last_in_range)]);
324 // Performs an in-place shuffle of the vector's elements.
325 template <typename E>
326 inline void Shuffle(internal::Random* random, std::vector<E>* v) {
327 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
330 // A function for deleting an object. Handy for being used as a
332 template <typename T>
333 static void Delete(T* x) {
337 // A predicate that checks the key of a TestProperty against a known key.
339 // TestPropertyKeyIs is copyable.
340 class TestPropertyKeyIs {
344 // TestPropertyKeyIs has NO default constructor.
345 explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}
347 // Returns true if and only if the test name of test property matches on key_.
348 bool operator()(const TestProperty& test_property) const {
349 return test_property.key() == key_;
356 // Class UnitTestOptions.
358 // This class contains functions for processing options the user
359 // specifies when running the tests. It has only static members.
361 // In most cases, the user can specify an option using either an
362 // environment variable or a command line flag. E.g. you can set the
363 // test filter using either GTEST_FILTER or --gtest_filter. If both
364 // the variable and the flag are present, the latter overrides the
366 class GTEST_API_ UnitTestOptions {
368 // Functions for processing the gtest_output flag.
370 // Returns the output format, or "" for normal printed output.
371 static std::string GetOutputFormat();
373 // Returns the absolute path of the requested output file, or the
374 // default (test_detail.xml in the original working directory) if
375 // none was explicitly specified.
376 static std::string GetAbsolutePathToOutputFile();
378 // Functions for processing the gtest_filter flag.
380 // Returns true if and only if the user-specified filter matches the test
381 // suite name and the test name.
382 static bool FilterMatchesTest(const std::string& test_suite_name,
383 const std::string& test_name);
386 // Function for supporting the gtest_catch_exception flag.
388 // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
389 // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
390 // This function is useful as an __except condition.
391 static int GTestShouldProcessSEH(DWORD exception_code);
392 #endif // GTEST_OS_WINDOWS
394 // Returns true if "name" matches the ':' separated list of glob-style
395 // filters in "filter".
396 static bool MatchesFilter(const std::string& name, const char* filter);
399 // Returns the current application's name, removing directory path if that
400 // is present. Used by UnitTestOptions::GetOutputFile.
401 GTEST_API_ FilePath GetCurrentExecutableName();
403 // The role interface for getting the OS stack trace as a string.
404 class OsStackTraceGetterInterface {
406 OsStackTraceGetterInterface() {}
407 virtual ~OsStackTraceGetterInterface() {}
409 // Returns the current OS stack trace as an std::string. Parameters:
411 // max_depth - the maximum number of stack frames to be included
413 // skip_count - the number of top frames to be skipped; doesn't count
414 // against max_depth.
415 virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0;
417 // UponLeavingGTest() should be called immediately before Google Test calls
418 // user code. It saves some information about the current stack that
419 // CurrentStackTrace() will use to find and hide Google Test stack frames.
420 virtual void UponLeavingGTest() = 0;
422 // This string is inserted in place of stack frames that are part of
423 // Google Test's implementation.
424 static const char* const kElidedFramesMarker;
427 GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
430 // A working implementation of the OsStackTraceGetterInterface interface.
431 class OsStackTraceGetter : public OsStackTraceGetterInterface {
433 OsStackTraceGetter() {}
435 std::string CurrentStackTrace(int max_depth, int skip_count) override;
436 void UponLeavingGTest() override;
440 Mutex mutex_; // Protects all internal state.
442 // We save the stack frame below the frame that calls user code.
443 // We do this because the address of the frame immediately below
444 // the user code changes between the call to UponLeavingGTest()
445 // and any calls to the stack trace code from within the user code.
446 void* caller_frame_ = nullptr;
447 #endif // GTEST_HAS_ABSL
449 GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
452 // Information about a Google Test trace point.
459 // This is the default global test part result reporter used in UnitTestImpl.
460 // This class should only be used by UnitTestImpl.
461 class DefaultGlobalTestPartResultReporter
462 : public TestPartResultReporterInterface {
464 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
465 // Implements the TestPartResultReporterInterface. Reports the test part
466 // result in the current test.
467 void ReportTestPartResult(const TestPartResult& result) override;
470 UnitTestImpl* const unit_test_;
472 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
475 // This is the default per thread test part result reporter used in
476 // UnitTestImpl. This class should only be used by UnitTestImpl.
477 class DefaultPerThreadTestPartResultReporter
478 : public TestPartResultReporterInterface {
480 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
481 // Implements the TestPartResultReporterInterface. The implementation just
482 // delegates to the current global test part result reporter of *unit_test_.
483 void ReportTestPartResult(const TestPartResult& result) override;
486 UnitTestImpl* const unit_test_;
488 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
491 // The private implementation of the UnitTest class. We don't protect
492 // the methods under a mutex, as this class is not accessible by a
493 // user and the UnitTest class that delegates work to this class does
495 class GTEST_API_ UnitTestImpl {
497 explicit UnitTestImpl(UnitTest* parent);
498 virtual ~UnitTestImpl();
500 // There are two different ways to register your own TestPartResultReporter.
501 // You can register your own repoter to listen either only for test results
502 // from the current thread or for results from all threads.
503 // By default, each per-thread test result repoter just passes a new
504 // TestPartResult to the global test result reporter, which registers the
505 // test part result for the currently running test.
507 // Returns the global test part result reporter.
508 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
510 // Sets the global test part result reporter.
511 void SetGlobalTestPartResultReporter(
512 TestPartResultReporterInterface* reporter);
514 // Returns the test part result reporter for the current thread.
515 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
517 // Sets the test part result reporter for the current thread.
518 void SetTestPartResultReporterForCurrentThread(
519 TestPartResultReporterInterface* reporter);
521 // Gets the number of successful test suites.
522 int successful_test_suite_count() const;
524 // Gets the number of failed test suites.
525 int failed_test_suite_count() const;
527 // Gets the number of all test suites.
528 int total_test_suite_count() const;
530 // Gets the number of all test suites that contain at least one test
532 int test_suite_to_run_count() const;
534 // Gets the number of successful tests.
535 int successful_test_count() const;
537 // Gets the number of skipped tests.
538 int skipped_test_count() const;
540 // Gets the number of failed tests.
541 int failed_test_count() const;
543 // Gets the number of disabled tests that will be reported in the XML report.
544 int reportable_disabled_test_count() const;
546 // Gets the number of disabled tests.
547 int disabled_test_count() const;
549 // Gets the number of tests to be printed in the XML report.
550 int reportable_test_count() const;
552 // Gets the number of all tests.
553 int total_test_count() const;
555 // Gets the number of tests that should run.
556 int test_to_run_count() const;
558 // Gets the time of the test program start, in ms from the start of the
560 TimeInMillis start_timestamp() const { return start_timestamp_; }
562 // Gets the elapsed time, in milliseconds.
563 TimeInMillis elapsed_time() const { return elapsed_time_; }
565 // Returns true if and only if the unit test passed (i.e. all test suites
567 bool Passed() const { return !Failed(); }
569 // Returns true if and only if the unit test failed (i.e. some test suite
570 // failed or something outside of all tests failed).
571 bool Failed() const {
572 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
575 // Gets the i-th test suite among all the test suites. i can range from 0 to
576 // total_test_suite_count() - 1. If i is not in that range, returns NULL.
577 const TestSuite* GetTestSuite(int i) const {
578 const int index = GetElementOr(test_suite_indices_, i, -1);
579 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)];
582 // Legacy API is deprecated but still available
583 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
584 const TestCase* GetTestCase(int i) const { return GetTestSuite(i); }
585 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
587 // Gets the i-th test suite among all the test suites. i can range from 0 to
588 // total_test_suite_count() - 1. If i is not in that range, returns NULL.
589 TestSuite* GetMutableSuiteCase(int i) {
590 const int index = GetElementOr(test_suite_indices_, i, -1);
591 return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
594 // Provides access to the event listener list.
595 TestEventListeners* listeners() { return &listeners_; }
597 // Returns the TestResult for the test that's currently running, or
598 // the TestResult for the ad hoc test if no test is running.
599 TestResult* current_test_result();
601 // Returns the TestResult for the ad hoc test.
602 const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
604 // Sets the OS stack trace getter.
606 // Does nothing if the input and the current OS stack trace getter
607 // are the same; otherwise, deletes the old getter and makes the
608 // input the current getter.
609 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
611 // Returns the current OS stack trace getter if it is not NULL;
612 // otherwise, creates an OsStackTraceGetter, makes it the current
613 // getter, and returns it.
614 OsStackTraceGetterInterface* os_stack_trace_getter();
616 // Returns the current OS stack trace as an std::string.
618 // The maximum number of stack frames to be included is specified by
619 // the gtest_stack_trace_depth flag. The skip_count parameter
620 // specifies the number of top frames to be skipped, which doesn't
621 // count against the number of frames to be included.
623 // For example, if Foo() calls Bar(), which in turn calls
624 // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
625 // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
626 std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;
628 // Finds and returns a TestSuite with the given name. If one doesn't
629 // exist, creates one and returns it.
633 // test_suite_name: name of the test suite
634 // type_param: the name of the test's type parameter, or NULL if
635 // this is not a typed or a type-parameterized test.
636 // set_up_tc: pointer to the function that sets up the test suite
637 // tear_down_tc: pointer to the function that tears down the test suite
638 TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param,
639 internal::SetUpTestSuiteFunc set_up_tc,
640 internal::TearDownTestSuiteFunc tear_down_tc);
642 // Legacy API is deprecated but still available
643 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
644 TestCase* GetTestCase(const char* test_case_name, const char* type_param,
645 internal::SetUpTestSuiteFunc set_up_tc,
646 internal::TearDownTestSuiteFunc tear_down_tc) {
647 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
649 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
651 // Adds a TestInfo to the unit test.
655 // set_up_tc: pointer to the function that sets up the test suite
656 // tear_down_tc: pointer to the function that tears down the test suite
657 // test_info: the TestInfo object
658 void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc,
659 internal::TearDownTestSuiteFunc tear_down_tc,
660 TestInfo* test_info) {
661 #if GTEST_HAS_DEATH_TEST
662 // In order to support thread-safe death tests, we need to
663 // remember the original working directory when the test program
664 // was first invoked. We cannot do this in RUN_ALL_TESTS(), as
665 // the user may have changed the current directory before calling
666 // RUN_ALL_TESTS(). Therefore we capture the current directory in
667 // AddTestInfo(), which is called to register a TEST or TEST_F
668 // before main() is reached.
669 if (original_working_dir_.IsEmpty()) {
670 original_working_dir_.Set(FilePath::GetCurrentDir());
671 GTEST_CHECK_(!original_working_dir_.IsEmpty())
672 << "Failed to get the current working directory.";
674 #endif // GTEST_HAS_DEATH_TEST
676 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
677 set_up_tc, tear_down_tc)
678 ->AddTestInfo(test_info);
681 // Returns ParameterizedTestSuiteRegistry object used to keep track of
682 // value-parameterized tests and instantiate and register them.
683 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
684 return parameterized_test_registry_;
687 std::set<std::string>* ignored_parameterized_test_suites() {
688 return &ignored_parameterized_test_suites_;
691 // Returns TypeParameterizedTestSuiteRegistry object used to keep track of
692 // type-parameterized tests and instantiations of them.
693 internal::TypeParameterizedTestSuiteRegistry&
694 type_parameterized_test_registry() {
695 return type_parameterized_test_registry_;
698 // Sets the TestSuite object for the test that's currently running.
699 void set_current_test_suite(TestSuite* a_current_test_suite) {
700 current_test_suite_ = a_current_test_suite;
703 // Sets the TestInfo object for the test that's currently running. If
704 // current_test_info is NULL, the assertion results will be stored in
705 // ad_hoc_test_result_.
706 void set_current_test_info(TestInfo* a_current_test_info) {
707 current_test_info_ = a_current_test_info;
710 // Registers all parameterized tests defined using TEST_P and
711 // INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter
712 // combination. This method can be called more then once; it has guards
713 // protecting from registering the tests more then once. If
714 // value-parameterized tests are disabled, RegisterParameterizedTests is
715 // present but does nothing.
716 void RegisterParameterizedTests();
718 // Runs all tests in this UnitTest object, prints the result, and
719 // returns true if all tests are successful. If any exception is
720 // thrown during a test, this test is considered to be failed, but
721 // the rest of the tests will still be run.
724 // Clears the results of all tests, except the ad hoc tests.
725 void ClearNonAdHocTestResult() {
726 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
729 // Clears the results of ad-hoc test assertions.
730 void ClearAdHocTestResult() {
731 ad_hoc_test_result_.Clear();
734 // Adds a TestProperty to the current TestResult object when invoked in a
735 // context of a test or a test suite, or to the global property set. If the
736 // result already contains a property with the same key, the value will be
738 void RecordProperty(const TestProperty& test_property);
740 enum ReactionToSharding {
741 HONOR_SHARDING_PROTOCOL,
742 IGNORE_SHARDING_PROTOCOL
745 // Matches the full name of each test against the user-specified
746 // filter to decide whether the test should run, then records the
747 // result in each TestSuite and TestInfo object.
748 // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
749 // based on sharding variables in the environment.
750 // Returns the number of tests that should run.
751 int FilterTests(ReactionToSharding shard_tests);
753 // Prints the names of the tests matching the user-specified filter flag.
754 void ListTestsMatchingFilter();
756 const TestSuite* current_test_suite() const { return current_test_suite_; }
757 TestInfo* current_test_info() { return current_test_info_; }
758 const TestInfo* current_test_info() const { return current_test_info_; }
760 // Returns the vector of environments that need to be set-up/torn-down
761 // before/after the tests are run.
762 std::vector<Environment*>& environments() { return environments_; }
764 // Getters for the per-thread Google Test trace stack.
765 std::vector<TraceInfo>& gtest_trace_stack() {
766 return *(gtest_trace_stack_.pointer());
768 const std::vector<TraceInfo>& gtest_trace_stack() const {
769 return gtest_trace_stack_.get();
772 #if GTEST_HAS_DEATH_TEST
773 void InitDeathTestSubprocessControlInfo() {
774 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
776 // Returns a pointer to the parsed --gtest_internal_run_death_test
777 // flag, or NULL if that flag was not specified.
778 // This information is useful only in a death test child process.
779 // Must not be called before a call to InitGoogleTest.
780 const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
781 return internal_run_death_test_flag_.get();
784 // Returns a pointer to the current death test factory.
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
794 // Initializes the event listener performing XML output as specified by
795 // UnitTestOptions. Must not be called before InitGoogleTest.
796 void ConfigureXmlOutput();
798 #if GTEST_CAN_STREAM_RESULTS_
799 // Initializes the event listener for streaming test results to a socket.
800 // Must not be called before InitGoogleTest.
801 void ConfigureStreamingOutput();
804 // Performs initialization dependent upon flag values obtained in
805 // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to
806 // ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest
807 // this function is also called from RunAllTests. Since this function can be
808 // called more than once, it has to be idempotent.
809 void PostFlagParsingInit();
811 // Gets the random seed used at the start of the current test iteration.
812 int random_seed() const { return random_seed_; }
814 // Gets the random number generator.
815 internal::Random* random() { return &random_; }
817 // Shuffles all test suites, and the tests within each test suite,
818 // making sure that death tests are still run first.
821 // Restores the test suites and tests to their order before the first shuffle.
822 void UnshuffleTests();
824 // Returns the value of GTEST_FLAG(catch_exceptions) at the moment
825 // UnitTest::Run() starts.
826 bool catch_exceptions() const { return catch_exceptions_; }
829 friend class ::testing::UnitTest;
831 // Used by UnitTest::Run() to capture the state of
832 // GTEST_FLAG(catch_exceptions) at the moment it starts.
833 void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
835 // The UnitTest object that owns this implementation object.
836 UnitTest* const parent_;
838 // The working directory when the first TEST() or TEST_F() was
840 internal::FilePath original_working_dir_;
842 // The default test part result reporters.
843 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
844 DefaultPerThreadTestPartResultReporter
845 default_per_thread_test_part_result_reporter_;
847 // Points to (but doesn't own) the global test part result reporter.
848 TestPartResultReporterInterface* global_test_part_result_repoter_;
850 // Protects read and write access to global_test_part_result_reporter_.
851 internal::Mutex global_test_part_result_reporter_mutex_;
853 // Points to (but doesn't own) the per-thread test part result reporter.
854 internal::ThreadLocal<TestPartResultReporterInterface*>
855 per_thread_test_part_result_reporter_;
857 // The vector of environments that need to be set-up/torn-down
858 // before/after the tests are run.
859 std::vector<Environment*> environments_;
861 // The vector of TestSuites in their original order. It owns the
862 // elements in the vector.
863 std::vector<TestSuite*> test_suites_;
865 // Provides a level of indirection for the test suite list to allow
866 // easy shuffling and restoring the test suite order. The i-th
867 // element of this vector is the index of the i-th test suite in the
869 std::vector<int> test_suite_indices_;
871 // ParameterizedTestRegistry object used to register value-parameterized
873 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
874 internal::TypeParameterizedTestSuiteRegistry
875 type_parameterized_test_registry_;
877 // The set holding the name of parameterized
878 // test suites that may go uninstantiated.
879 std::set<std::string> ignored_parameterized_test_suites_;
881 // Indicates whether RegisterParameterizedTests() has been called already.
882 bool parameterized_tests_registered_;
884 // Index of the last death test suite registered. Initially -1.
885 int last_death_test_suite_;
887 // This points to the TestSuite for the currently running test. It
888 // changes as Google Test goes through one test suite after another.
889 // When no test is running, this is set to NULL and Google Test
890 // stores assertion results in ad_hoc_test_result_. Initially NULL.
891 TestSuite* current_test_suite_;
893 // This points to the TestInfo for the currently running test. It
894 // changes as Google Test goes through one test after another. When
895 // no test is running, this is set to NULL and Google Test stores
896 // assertion results in ad_hoc_test_result_. Initially NULL.
897 TestInfo* current_test_info_;
899 // Normally, a user only writes assertions inside a TEST or TEST_F,
900 // or inside a function called by a TEST or TEST_F. Since Google
901 // Test keeps track of which test is current running, it can
902 // associate such an assertion with the test it belongs to.
904 // If an assertion is encountered when no TEST or TEST_F is running,
905 // Google Test attributes the assertion result to an imaginary "ad hoc"
906 // test, and records the result in ad_hoc_test_result_.
907 TestResult ad_hoc_test_result_;
909 // The list of event listeners that can be used to track events inside
911 TestEventListeners listeners_;
913 // The OS stack trace getter. Will be deleted when the UnitTest
914 // object is destructed. By default, an OsStackTraceGetter is used,
915 // but the user can set this field to use a custom getter if that is
917 OsStackTraceGetterInterface* os_stack_trace_getter_;
919 // True if and only if PostFlagParsingInit() has been called.
920 bool post_flag_parse_init_performed_;
922 // The random number seed used at the beginning of the test run.
925 // Our random number generator.
926 internal::Random random_;
928 // The time of the test program start, in ms from the start of the
930 TimeInMillis start_timestamp_;
932 // How long the test took to run, in milliseconds.
933 TimeInMillis elapsed_time_;
935 #if GTEST_HAS_DEATH_TEST
936 // The decomposed components of the gtest_internal_run_death_test flag,
937 // parsed when RUN_ALL_TESTS is called.
938 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
939 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
940 #endif // GTEST_HAS_DEATH_TEST
942 // A per-thread stack of traces created by the SCOPED_TRACE() macro.
943 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
945 // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests()
947 bool catch_exceptions_;
949 GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
950 }; // class UnitTestImpl
952 // Convenience function for accessing the global UnitTest
953 // implementation object.
954 inline UnitTestImpl* GetUnitTestImpl() {
955 return UnitTest::GetInstance()->impl();
958 #if GTEST_USES_SIMPLE_RE
960 // Internal helper functions for implementing the simple regular
961 // expression matcher.
962 GTEST_API_ bool IsInSet(char ch, const char* str);
963 GTEST_API_ bool IsAsciiDigit(char ch);
964 GTEST_API_ bool IsAsciiPunct(char ch);
965 GTEST_API_ bool IsRepeat(char ch);
966 GTEST_API_ bool IsAsciiWhiteSpace(char ch);
967 GTEST_API_ bool IsAsciiWordChar(char ch);
968 GTEST_API_ bool IsValidEscape(char ch);
969 GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
970 GTEST_API_ bool ValidateRegex(const char* regex);
971 GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
972 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
973 bool escaped, char ch, char repeat, const char* regex, const char* str);
974 GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
976 #endif // GTEST_USES_SIMPLE_RE
978 // Parses the command line for Google Test flags, without initializing
979 // other parts of Google Test.
980 GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
981 GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
983 #if GTEST_HAS_DEATH_TEST
985 // Returns the message describing the last system error, regardless of the
987 GTEST_API_ std::string GetLastErrnoDescription();
989 // Attempts to parse a string into a positive integer pointed to by the
990 // number parameter. Returns true if that is possible.
991 // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
993 template <typename Integer>
994 bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
995 // Fail fast if the given string does not begin with a digit;
996 // this bypasses strtoXXX's "optional leading whitespace and plus
997 // or minus sign" semantics, which are undesirable here.
998 if (str.empty() || !IsDigit(str[0])) {
1004 // BiggestConvertible is the largest integer type that system-provided
1005 // string-to-number conversion routines can return.
1006 using BiggestConvertible = unsigned long long; // NOLINT
1008 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); // NOLINT
1009 const bool parse_success = *end == '\0' && errno == 0;
1011 GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
1013 const Integer result = static_cast<Integer>(parsed);
1014 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1020 #endif // GTEST_HAS_DEATH_TEST
1022 // TestResult contains some private methods that should be hidden from
1023 // Google Test user but are required for testing. This class allow our tests
1026 // This class is supplied only for the purpose of testing Google Test's own
1027 // constructs. Do not use it in user tests, either directly or indirectly.
1028 class TestResultAccessor {
1030 static void RecordProperty(TestResult* test_result,
1031 const std::string& xml_element,
1032 const TestProperty& property) {
1033 test_result->RecordProperty(xml_element, property);
1036 static void ClearTestPartResults(TestResult* test_result) {
1037 test_result->ClearTestPartResults();
1040 static const std::vector<testing::TestPartResult>& test_part_results(
1041 const TestResult& test_result) {
1042 return test_result.test_part_results();
1046 #if GTEST_CAN_STREAM_RESULTS_
1048 // Streams test results to the given port on the given host machine.
1049 class StreamingListener : public EmptyTestEventListener {
1051 // Abstract base class for writing strings to a socket.
1052 class AbstractSocketWriter {
1054 virtual ~AbstractSocketWriter() {}
1056 // Sends a string to the socket.
1057 virtual void Send(const std::string& message) = 0;
1059 // Closes the socket.
1060 virtual void CloseConnection() {}
1062 // Sends a string and a newline to the socket.
1063 void SendLn(const std::string& message) { Send(message + "\n"); }
1066 // Concrete class for actually writing strings to a socket.
1067 class SocketWriter : public AbstractSocketWriter {
1069 SocketWriter(const std::string& host, const std::string& port)
1070 : sockfd_(-1), host_name_(host), port_num_(port) {
1074 ~SocketWriter() override {
1079 // Sends a string to the socket.
1080 void Send(const std::string& message) override {
1081 GTEST_CHECK_(sockfd_ != -1)
1082 << "Send() can be called only when there is a connection.";
1084 const auto len = static_cast<size_t>(message.length());
1085 if (write(sockfd_, message.c_str(), len) != static_cast<ssize_t>(len)) {
1087 << "stream_result_to: failed to stream to "
1088 << host_name_ << ":" << port_num_;
1093 // Creates a client socket and connects to the server.
1094 void MakeConnection();
1096 // Closes the socket.
1097 void CloseConnection() override {
1098 GTEST_CHECK_(sockfd_ != -1)
1099 << "CloseConnection() can be called only when there is a connection.";
1105 int sockfd_; // socket file descriptor
1106 const std::string host_name_;
1107 const std::string port_num_;
1109 GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter);
1110 }; // class SocketWriter
1112 // Escapes '=', '&', '%', and '\n' characters in str as "%xx".
1113 static std::string UrlEncode(const char* str);
1115 StreamingListener(const std::string& host, const std::string& port)
1116 : socket_writer_(new SocketWriter(host, port)) {
1120 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1121 : socket_writer_(socket_writer) { Start(); }
1123 void OnTestProgramStart(const UnitTest& /* unit_test */) override {
1124 SendLn("event=TestProgramStart");
1127 void OnTestProgramEnd(const UnitTest& unit_test) override {
1128 // Note that Google Test current only report elapsed time for each
1129 // test iteration, not for the entire test program.
1130 SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1132 // Notify the streaming server to stop.
1133 socket_writer_->CloseConnection();
1136 void OnTestIterationStart(const UnitTest& /* unit_test */,
1137 int iteration) override {
1138 SendLn("event=TestIterationStart&iteration=" +
1139 StreamableToString(iteration));
1142 void OnTestIterationEnd(const UnitTest& unit_test,
1143 int /* iteration */) override {
1144 SendLn("event=TestIterationEnd&passed=" +
1145 FormatBool(unit_test.Passed()) + "&elapsed_time=" +
1146 StreamableToString(unit_test.elapsed_time()) + "ms");
1149 // Note that "event=TestCaseStart" is a wire format and has to remain
1150 // "case" for compatibility
1151 void OnTestCaseStart(const TestCase& test_case) override {
1152 SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
1155 // Note that "event=TestCaseEnd" is a wire format and has to remain
1156 // "case" for compatibility
1157 void OnTestCaseEnd(const TestCase& test_case) override {
1158 SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1159 "&elapsed_time=" + StreamableToString(test_case.elapsed_time()) +
1163 void OnTestStart(const TestInfo& test_info) override {
1164 SendLn(std::string("event=TestStart&name=") + test_info.name());
1167 void OnTestEnd(const TestInfo& test_info) override {
1168 SendLn("event=TestEnd&passed=" +
1169 FormatBool((test_info.result())->Passed()) +
1171 StreamableToString((test_info.result())->elapsed_time()) + "ms");
1174 void OnTestPartResult(const TestPartResult& test_part_result) override {
1175 const char* file_name = test_part_result.file_name();
1176 if (file_name == nullptr) file_name = "";
1177 SendLn("event=TestPartResult&file=" + UrlEncode(file_name) +
1178 "&line=" + StreamableToString(test_part_result.line_number()) +
1179 "&message=" + UrlEncode(test_part_result.message()));
1183 // Sends the given message and a newline to the socket.
1184 void SendLn(const std::string& message) { socket_writer_->SendLn(message); }
1186 // Called at the start of streaming to notify the receiver what
1187 // protocol we are using.
1188 void Start() { SendLn("gtest_streaming_protocol_version=1.0"); }
1190 std::string FormatBool(bool value) { return value ? "1" : "0"; }
1192 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
1194 GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);
1195 }; // class StreamingListener
1197 #endif // GTEST_CAN_STREAM_RESULTS_
1199 } // namespace internal
1200 } // namespace testing
1202 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
1204 #endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_