"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "googletest/src/gtest.cc" between
googletest-release-1.11.0.tar.gz and googletest-release-1.12.0.tar.gz

About: GoogleTest is Google's (unit) testing and mocking framework for C++ tests.

gtest.cc  (googletest-release-1.11.0):gtest.cc  (googletest-release-1.12.0)
skipping to change at line 34 skipping to change at line 34
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// The Google C++ Testing and Mocking Framework (Google Test) // The Google C++ Testing and Mocking Framework (Google Test)
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "gtest/internal/custom/gtest.h"
#include "gtest/gtest-spi.h"
#include <ctype.h> #include <ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#include <algorithm> #include <algorithm>
#include <chrono> // NOLINT #include <chrono> // NOLINT
#include <cmath> #include <cmath>
#include <cstdint> #include <cstdint>
#include <initializer_list>
#include <iomanip> #include <iomanip>
#include <iterator>
#include <limits> #include <limits>
#include <list> #include <list>
#include <map> #include <map>
#include <ostream> // NOLINT #include <ostream> // NOLINT
#include <sstream> #include <sstream>
#include <unordered_set>
#include <vector> #include <vector>
#include "gtest/gtest-assertion-result.h"
#include "gtest/gtest-spi.h"
#include "gtest/internal/custom/gtest.h"
#if GTEST_OS_LINUX #if GTEST_OS_LINUX
# include <fcntl.h> // NOLINT #include <fcntl.h> // NOLINT
# include <limits.h> // NOLINT #include <limits.h> // NOLINT
# include <sched.h> // NOLINT #include <sched.h> // NOLINT
// Declares vsnprintf(). This header is not available on Windows. // Declares vsnprintf(). This header is not available on Windows.
# include <strings.h> // NOLINT #include <strings.h> // NOLINT
# include <sys/mman.h> // NOLINT #include <sys/mman.h> // NOLINT
# include <sys/time.h> // NOLINT #include <sys/time.h> // NOLINT
# include <unistd.h> // NOLINT #include <unistd.h> // NOLINT
# include <string>
#include <string>
#elif GTEST_OS_ZOS #elif GTEST_OS_ZOS
# include <sys/time.h> // NOLINT #include <sys/time.h> // NOLINT
// On z/OS we additionally need strings.h for strcasecmp. // On z/OS we additionally need strings.h for strcasecmp.
# include <strings.h> // NOLINT #include <strings.h> // NOLINT
#elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE. #elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE.
# include <windows.h> // NOLINT #include <windows.h> // NOLINT
# undef min #undef min
#elif GTEST_OS_WINDOWS // We are on Windows proper. #elif GTEST_OS_WINDOWS // We are on Windows proper.
# include <windows.h> // NOLINT #include <windows.h> // NOLINT
# undef min #undef min
#ifdef _MSC_VER #ifdef _MSC_VER
# include <crtdbg.h> // NOLINT #include <crtdbg.h> // NOLINT
#endif #endif
# include <io.h> // NOLINT #include <io.h> // NOLINT
# include <sys/timeb.h> // NOLINT #include <sys/stat.h> // NOLINT
# include <sys/types.h> // NOLINT #include <sys/timeb.h> // NOLINT
# include <sys/stat.h> // NOLINT #include <sys/types.h> // NOLINT
# if GTEST_OS_WINDOWS_MINGW #if GTEST_OS_WINDOWS_MINGW
# include <sys/time.h> // NOLINT #include <sys/time.h> // NOLINT
# endif // GTEST_OS_WINDOWS_MINGW #endif // GTEST_OS_WINDOWS_MINGW
#else #else
// cpplint thinks that the header is already included, so we want to // cpplint thinks that the header is already included, so we want to
// silence it. // silence it.
# include <sys/time.h> // NOLINT #include <sys/time.h> // NOLINT
# include <unistd.h> // NOLINT #include <unistd.h> // NOLINT
#endif // GTEST_OS_LINUX #endif // GTEST_OS_LINUX
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
# include <stdexcept> #include <stdexcept>
#endif #endif
#if GTEST_CAN_STREAM_RESULTS_ #if GTEST_CAN_STREAM_RESULTS_
# include <arpa/inet.h> // NOLINT #include <arpa/inet.h> // NOLINT
# include <netdb.h> // NOLINT #include <netdb.h> // NOLINT
# include <sys/socket.h> // NOLINT #include <sys/socket.h> // NOLINT
# include <sys/types.h> // NOLINT #include <sys/types.h> // NOLINT
#endif #endif
#include "src/gtest-internal-inl.h" #include "src/gtest-internal-inl.h"
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
# define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#if GTEST_OS_MAC #if GTEST_OS_MAC
#ifndef GTEST_OS_IOS #ifndef GTEST_OS_IOS
#include <crt_externs.h> #include <crt_externs.h>
#endif #endif
#endif #endif
#if GTEST_HAS_ABSL #if GTEST_HAS_ABSL
#include "absl/debugging/failure_signal_handler.h" #include "absl/debugging/failure_signal_handler.h"
#include "absl/debugging/stacktrace.h" #include "absl/debugging/stacktrace.h"
#include "absl/debugging/symbolize.h" #include "absl/debugging/symbolize.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h"
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
namespace testing { namespace testing {
using internal::CountIf; using internal::CountIf;
using internal::ForEach; using internal::ForEach;
using internal::GetElementOr; using internal::GetElementOr;
using internal::Shuffle; using internal::Shuffle;
// Constants. // Constants.
skipping to change at line 180 skipping to change at line 189
namespace internal { namespace internal {
// The text used in failure messages to indicate the start of the // The text used in failure messages to indicate the start of the
// stack trace. // stack trace.
const char kStackTraceMarker[] = "\nStack trace:\n"; const char kStackTraceMarker[] = "\nStack trace:\n";
// g_help_flag is true if and only if the --help flag or an equivalent form // g_help_flag is true if and only if the --help flag or an equivalent form
// is specified on the command line. // is specified on the command line.
bool g_help_flag = false; bool g_help_flag = false;
// Utilty function to Open File for Writing // Utility function to Open File for Writing
static FILE* OpenFileForWriting(const std::string& output_file) { static FILE* OpenFileForWriting(const std::string& output_file) {
FILE* fileout = nullptr; FILE* fileout = nullptr;
FilePath output_file_path(output_file); FilePath output_file_path(output_file);
FilePath output_dir(output_file_path.RemoveFileName()); FilePath output_dir(output_file_path.RemoveFileName());
if (output_dir.CreateDirectoriesRecursively()) { if (output_dir.CreateDirectoriesRecursively()) {
fileout = posix::FOpen(output_file.c_str(), "w"); fileout = posix::FOpen(output_file.c_str(), "w");
} }
if (fileout == nullptr) { if (fileout == nullptr) {
GTEST_LOG_(FATAL) << "Unable to open file \"" << output_file << "\""; GTEST_LOG_(FATAL) << "Unable to open file \"" << output_file << "\"";
skipping to change at line 219 skipping to change at line 228
// TESTBRIDGE_TEST_RUNNER_FAIL_FAST environment variable. // TESTBRIDGE_TEST_RUNNER_FAIL_FAST environment variable.
static bool GetDefaultFailFast() { static bool GetDefaultFailFast() {
const char* const testbridge_test_runner_fail_fast = const char* const testbridge_test_runner_fail_fast =
internal::posix::GetEnv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST"); internal::posix::GetEnv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST");
if (testbridge_test_runner_fail_fast != nullptr) { if (testbridge_test_runner_fail_fast != nullptr) {
return strcmp(testbridge_test_runner_fail_fast, "1") == 0; return strcmp(testbridge_test_runner_fail_fast, "1") == 0;
} }
return false; return false;
} }
} // namespace testing
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
fail_fast, internal::BoolFromGTestEnv("fail_fast", GetDefaultFailFast()), fail_fast,
testing::internal::BoolFromGTestEnv("fail_fast",
testing::GetDefaultFailFast()),
"True if and only if a test failure should stop further test execution."); "True if and only if a test failure should stop further test execution.");
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
also_run_disabled_tests, also_run_disabled_tests,
internal::BoolFromGTestEnv("also_run_disabled_tests", false), testing::internal::BoolFromGTestEnv("also_run_disabled_tests", false),
"Run disabled tests too, in addition to the tests normally being run."); "Run disabled tests too, in addition to the tests normally being run.");
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
break_on_failure, internal::BoolFromGTestEnv("break_on_failure", false), break_on_failure,
testing::internal::BoolFromGTestEnv("break_on_failure", false),
"True if and only if a failed assertion should be a debugger " "True if and only if a failed assertion should be a debugger "
"break-point."); "break-point.");
GTEST_DEFINE_bool_(catch_exceptions, GTEST_DEFINE_bool_(catch_exceptions,
internal::BoolFromGTestEnv("catch_exceptions", true), testing::internal::BoolFromGTestEnv("catch_exceptions",
true),
"True if and only if " GTEST_NAME_ "True if and only if " GTEST_NAME_
" should catch exceptions and treat them as test failures."); " should catch exceptions and treat them as test failures.");
GTEST_DEFINE_string_( GTEST_DEFINE_string_(
color, color, testing::internal::StringFromGTestEnv("color", "auto"),
internal::StringFromGTestEnv("color", "auto"),
"Whether to use colors in the output. Valid values: yes, no, " "Whether to use colors in the output. Valid values: yes, no, "
"and auto. 'auto' means to use colors if the output is " "and auto. 'auto' means to use colors if the output is "
"being sent to a terminal and the TERM environment variable " "being sent to a terminal and the TERM environment variable "
"is set to a terminal type that supports colors."); "is set to a terminal type that supports colors.");
GTEST_DEFINE_string_( GTEST_DEFINE_string_(
filter, filter,
internal::StringFromGTestEnv("filter", GetDefaultFilter()), testing::internal::StringFromGTestEnv("filter",
testing::GetDefaultFilter()),
"A colon-separated list of glob (not regex) patterns " "A colon-separated list of glob (not regex) patterns "
"for filtering the tests to run, optionally followed by a " "for filtering the tests to run, optionally followed by a "
"'-' and a : separated list of negative patterns (tests to " "'-' and a : separated list of negative patterns (tests to "
"exclude). A test is run if it matches one of the positive " "exclude). A test is run if it matches one of the positive "
"patterns and does not match any of the negative patterns."); "patterns and does not match any of the negative patterns.");
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
install_failure_signal_handler, install_failure_signal_handler,
internal::BoolFromGTestEnv("install_failure_signal_handler", false), testing::internal::BoolFromGTestEnv("install_failure_signal_handler",
"If true and supported on the current platform, " GTEST_NAME_ " should " false),
"If true and supported on the current platform, " GTEST_NAME_
" should "
"install a signal handler that dumps debugging information when fatal " "install a signal handler that dumps debugging information when fatal "
"signals are raised."); "signals are raised.");
GTEST_DEFINE_bool_(list_tests, false, GTEST_DEFINE_bool_(list_tests, false, "List all tests without running them.");
"List all tests without running them.");
// The net priority order after flag processing is thus: // The net priority order after flag processing is thus:
// --gtest_output command line flag // --gtest_output command line flag
// GTEST_OUTPUT environment variable // GTEST_OUTPUT environment variable
// XML_OUTPUT_FILE environment variable // XML_OUTPUT_FILE environment variable
// '' // ''
GTEST_DEFINE_string_( GTEST_DEFINE_string_(
output, output,
internal::StringFromGTestEnv("output", testing::internal::StringFromGTestEnv(
internal::OutputFlagAlsoCheckEnvVar().c_str()), "output", testing::internal::OutputFlagAlsoCheckEnvVar().c_str()),
"A format (defaults to \"xml\" but can be specified to be \"json\"), " "A format (defaults to \"xml\" but can be specified to be \"json\"), "
"optionally followed by a colon and an output file name or directory. " "optionally followed by a colon and an output file name or directory. "
"A directory is indicated by a trailing pathname separator. " "A directory is indicated by a trailing pathname separator. "
"Examples: \"xml:filename.xml\", \"xml::directoryname/\". " "Examples: \"xml:filename.xml\", \"xml::directoryname/\". "
"If a directory is specified, output files will be created " "If a directory is specified, output files will be created "
"within that directory, with file-names based on the test " "within that directory, with file-names based on the test "
"executable's name and, if necessary, made unique by adding " "executable's name and, if necessary, made unique by adding "
"digits."); "digits.");
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
brief, internal::BoolFromGTestEnv("brief", false), brief, testing::internal::BoolFromGTestEnv("brief", false),
"True if only test failures should be displayed in text output."); "True if only test failures should be displayed in text output.");
GTEST_DEFINE_bool_(print_time, internal::BoolFromGTestEnv("print_time", true), GTEST_DEFINE_bool_(print_time,
testing::internal::BoolFromGTestEnv("print_time", true),
"True if and only if " GTEST_NAME_ "True if and only if " GTEST_NAME_
" should display elapsed time in text output."); " should display elapsed time in text output.");
GTEST_DEFINE_bool_(print_utf8, internal::BoolFromGTestEnv("print_utf8", true), GTEST_DEFINE_bool_(print_utf8,
testing::internal::BoolFromGTestEnv("print_utf8", true),
"True if and only if " GTEST_NAME_ "True if and only if " GTEST_NAME_
" prints UTF8 characters as text."); " prints UTF8 characters as text.");
GTEST_DEFINE_int32_( GTEST_DEFINE_int32_(
random_seed, random_seed, testing::internal::Int32FromGTestEnv("random_seed", 0),
internal::Int32FromGTestEnv("random_seed", 0),
"Random number seed to use when shuffling test orders. Must be in range " "Random number seed to use when shuffling test orders. Must be in range "
"[1, 99999], or 0 to use a seed based on the current time."); "[1, 99999], or 0 to use a seed based on the current time.");
GTEST_DEFINE_int32_( GTEST_DEFINE_int32_(
repeat, repeat, testing::internal::Int32FromGTestEnv("repeat", 1),
internal::Int32FromGTestEnv("repeat", 1),
"How many times to repeat each test. Specify a negative number " "How many times to repeat each test. Specify a negative number "
"for repeating forever. Useful for shaking out flaky tests."); "for repeating forever. Useful for shaking out flaky tests.");
GTEST_DEFINE_bool_(
recreate_environments_when_repeating,
testing::internal::BoolFromGTestEnv("recreate_environments_when_repeating",
false),
"Controls whether global test environments are recreated for each repeat "
"of the tests. If set to false the global test environments are only set "
"up once, for the first iteration, and only torn down once, for the last. "
"Useful for shaking out flaky tests with stable, expensive test "
"environments. If --gtest_repeat is set to a negative number, meaning "
"there is no last run, the environments will always be recreated to avoid "
"leaks.");
GTEST_DEFINE_bool_(show_internal_stack_frames, false, GTEST_DEFINE_bool_(show_internal_stack_frames, false,
"True if and only if " GTEST_NAME_ "True if and only if " GTEST_NAME_
" should include internal stack frames when " " should include internal stack frames when "
"printing test failure stack traces."); "printing test failure stack traces.");
GTEST_DEFINE_bool_(shuffle, internal::BoolFromGTestEnv("shuffle", false), GTEST_DEFINE_bool_(shuffle,
testing::internal::BoolFromGTestEnv("shuffle", false),
"True if and only if " GTEST_NAME_ "True if and only if " GTEST_NAME_
" should randomize tests' order on every run."); " should randomize tests' order on every run.");
GTEST_DEFINE_int32_( GTEST_DEFINE_int32_(
stack_trace_depth, stack_trace_depth,
internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth), testing::internal::Int32FromGTestEnv("stack_trace_depth",
testing::kMaxStackTraceDepth),
"The maximum number of stack frames to print when an " "The maximum number of stack frames to print when an "
"assertion fails. The valid range is 0 through 100, inclusive."); "assertion fails. The valid range is 0 through 100, inclusive.");
GTEST_DEFINE_string_( GTEST_DEFINE_string_(
stream_result_to, stream_result_to,
internal::StringFromGTestEnv("stream_result_to", ""), testing::internal::StringFromGTestEnv("stream_result_to", ""),
"This flag specifies the host name and the port number on which to stream " "This flag specifies the host name and the port number on which to stream "
"test results. Example: \"localhost:555\". The flag is effective only on " "test results. Example: \"localhost:555\". The flag is effective only on "
"Linux."); "Linux.");
GTEST_DEFINE_bool_( GTEST_DEFINE_bool_(
throw_on_failure, throw_on_failure,
internal::BoolFromGTestEnv("throw_on_failure", false), testing::internal::BoolFromGTestEnv("throw_on_failure", false),
"When this flag is specified, a failed assertion will throw an exception " "When this flag is specified, a failed assertion will throw an exception "
"if exceptions are enabled or exit the program with a non-zero code " "if exceptions are enabled or exit the program with a non-zero code "
"otherwise. For use with an external test framework."); "otherwise. For use with an external test framework.");
#if GTEST_USE_OWN_FLAGFILE_FLAG_ #if GTEST_USE_OWN_FLAGFILE_FLAG_
GTEST_DEFINE_string_( GTEST_DEFINE_string_(
flagfile, flagfile, testing::internal::StringFromGTestEnv("flagfile", ""),
internal::StringFromGTestEnv("flagfile", ""),
"This flag specifies the flagfile to read command-line flags from."); "This flag specifies the flagfile to read command-line flags from.");
#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ #endif // GTEST_USE_OWN_FLAGFILE_FLAG_
namespace testing {
namespace internal { namespace internal {
// Generates a random number from [0, range), using a Linear // Generates a random number from [0, range), using a Linear
// Congruential Generator (LCG). Crashes if 'range' is 0 or greater // Congruential Generator (LCG). Crashes if 'range' is 0 or greater
// than kMaxRange. // than kMaxRange.
uint32_t Random::Generate(uint32_t range) { uint32_t Random::Generate(uint32_t range) {
// These constants are the same as are used in glibc's rand(3). // These constants are the same as are used in glibc's rand(3).
// Use wider types than necessary to prevent unsigned overflow diagnostics. // Use wider types than necessary to prevent unsigned overflow diagnostics.
state_ = static_cast<uint32_t>(1103515245ULL*state_ + 12345U) % kMaxRange; state_ = static_cast<uint32_t>(1103515245ULL * state_ + 12345U) % kMaxRange;
GTEST_CHECK_(range > 0) GTEST_CHECK_(range > 0) << "Cannot generate a number in the range [0, 0).";
<< "Cannot generate a number in the range [0, 0).";
GTEST_CHECK_(range <= kMaxRange) GTEST_CHECK_(range <= kMaxRange)
<< "Generation of a number in [0, " << range << ") was requested, " << "Generation of a number in [0, " << range << ") was requested, "
<< "but this can only generate numbers in [0, " << kMaxRange << ")."; << "but this can only generate numbers in [0, " << kMaxRange << ").";
// Converting via modulus introduces a bit of downward bias, but // Converting via modulus introduces a bit of downward bias, but
// it's simple, and a linear congruential generator isn't too good // it's simple, and a linear congruential generator isn't too good
// to begin with. // to begin with.
return state_ % range; return state_ % range;
} }
skipping to change at line 399 skipping to change at line 428
return test_suite->should_run() && test_suite->Failed(); return test_suite->should_run() && test_suite->Failed();
} }
// Returns true if and only if test_suite contains at least one test that // Returns true if and only if test_suite contains at least one test that
// should run. // should run.
static bool ShouldRunTestSuite(const TestSuite* test_suite) { static bool ShouldRunTestSuite(const TestSuite* test_suite) {
return test_suite->should_run(); return test_suite->should_run();
} }
// AssertHelper constructor. // AssertHelper constructor.
AssertHelper::AssertHelper(TestPartResult::Type type, AssertHelper::AssertHelper(TestPartResult::Type type, const char* file,
const char* file, int line, const char* message)
int line, : data_(new AssertHelperData(type, file, line, message)) {}
const char* message)
: data_(new AssertHelperData(type, file, line, message)) {
}
AssertHelper::~AssertHelper() { AssertHelper::~AssertHelper() { delete data_; }
delete data_;
}
// Message assignment, for assertion streaming support. // Message assignment, for assertion streaming support.
void AssertHelper::operator=(const Message& message) const { void AssertHelper::operator=(const Message& message) const {
UnitTest::GetInstance()-> UnitTest::GetInstance()->AddTestPartResult(
AddTestPartResult(data_->type, data_->file, data_->line, data_->type, data_->file, data_->line,
AppendUserMessage(data_->message, message), AppendUserMessage(data_->message, message),
UnitTest::GetInstance()->impl() UnitTest::GetInstance()->impl()->CurrentOsStackTraceExceptTop(1)
->CurrentOsStackTraceExceptTop(1) // Skips the stack frame for this function itself.
// Skips the stack frame for this function itself. ); // NOLINT
); // NOLINT
} }
namespace { namespace {
// When TEST_P is found without a matching INSTANTIATE_TEST_SUITE_P // When TEST_P is found without a matching INSTANTIATE_TEST_SUITE_P
// to creates test cases for it, a syntetic test case is // to creates test cases for it, a synthetic test case is
// inserted to report ether an error or a log message. // inserted to report ether an error or a log message.
// //
// This configuration bit will likely be removed at some point. // This configuration bit will likely be removed at some point.
constexpr bool kErrorOnUninstantiatedParameterizedTest = true; constexpr bool kErrorOnUninstantiatedParameterizedTest = true;
constexpr bool kErrorOnUninstantiatedTypeParameterizedTest = true; constexpr bool kErrorOnUninstantiatedTypeParameterizedTest = true;
// A test that fails at a given file/line location with a given message. // A test that fails at a given file/line location with a given message.
class FailureTest : public Test { class FailureTest : public Test {
public: public:
explicit FailureTest(const CodeLocation& loc, std::string error_message, explicit FailureTest(const CodeLocation& loc, std::string error_message,
skipping to change at line 498 skipping to change at line 521
"so is often an indication of dead code, e.g. the last TEST_P was " "so is often an indication of dead code, e.g. the last TEST_P was "
"removed but the rest got left behind."; "removed but the rest got left behind.";
std::string message = std::string message =
"Parameterized test suite " + name + "Parameterized test suite " + name +
(has_test_p ? kMissingInstantiation : kMissingTestCase) + (has_test_p ? kMissingInstantiation : kMissingTestCase) +
"\n\n" "\n\n"
"To suppress this error for this test suite, insert the following line " "To suppress this error for this test suite, insert the following line "
"(in a non-header) in the namespace it is defined in:" "(in a non-header) in the namespace it is defined in:"
"\n\n" "\n\n"
"GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" + name + ");"; "GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" +
name + ");";
std::string full_name = "UninstantiatedParameterizedTestSuite<" + name + ">"; std::string full_name = "UninstantiatedParameterizedTestSuite<" + name + ">";
RegisterTest( // RegisterTest( //
"GoogleTestVerification", full_name.c_str(), "GoogleTestVerification", full_name.c_str(),
nullptr, // No type parameter. nullptr, // No type parameter.
nullptr, // No value parameter. nullptr, // No value parameter.
location.file.c_str(), location.line, [message, location] { location.file.c_str(), location.line, [message, location] {
return new FailureTest(location, message, return new FailureTest(location, message,
kErrorOnUninstantiatedParameterizedTest); kErrorOnUninstantiatedParameterizedTest);
}); });
} }
void RegisterTypeParameterizedTestSuite(const char* test_suite_name, void RegisterTypeParameterizedTestSuite(const char* test_suite_name,
CodeLocation code_location) { CodeLocation code_location) {
GetUnitTestImpl()->type_parameterized_test_registry().RegisterTestSuite( GetUnitTestImpl()->type_parameterized_test_registry().RegisterTestSuite(
test_suite_name, code_location); test_suite_name, code_location);
} }
void RegisterTypeParameterizedTestSuiteInstantiation(const char* case_name) { void RegisterTypeParameterizedTestSuiteInstantiation(const char* case_name) {
GetUnitTestImpl() GetUnitTestImpl()->type_parameterized_test_registry().RegisterInstantiation(
->type_parameterized_test_registry() case_name);
.RegisterInstantiation(case_name);
} }
void TypeParameterizedTestSuiteRegistry::RegisterTestSuite( void TypeParameterizedTestSuiteRegistry::RegisterTestSuite(
const char* test_suite_name, CodeLocation code_location) { const char* test_suite_name, CodeLocation code_location) {
suites_.emplace(std::string(test_suite_name), suites_.emplace(std::string(test_suite_name),
TypeParameterizedTestSuiteInfo(code_location)); TypeParameterizedTestSuiteInfo(code_location));
} }
void TypeParameterizedTestSuiteRegistry::RegisterInstantiation( void TypeParameterizedTestSuiteRegistry::RegisterInstantiation(
const char* test_suite_name) { const char* test_suite_name) {
auto it = suites_.find(std::string(test_suite_name)); auto it = suites_.find(std::string(test_suite_name));
if (it != suites_.end()) { if (it != suites_.end()) {
it->second.instantiated = true; it->second.instantiated = true;
} else { } else {
GTEST_LOG_(ERROR) << "Unknown type parameterized test suit '" GTEST_LOG_(ERROR) << "Unknown type parameterized test suit '"
<< test_suite_name << "'"; << test_suite_name << "'";
} }
} }
void TypeParameterizedTestSuiteRegistry::CheckForInstantiations() { void TypeParameterizedTestSuiteRegistry::CheckForInstantiations() {
skipping to change at line 608 skipping to change at line 631
result.Set(FilePath(GetArgvs()[0])); result.Set(FilePath(GetArgvs()[0]));
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
return result.RemoveDirectoryName(); return result.RemoveDirectoryName();
} }
// Functions for processing the gtest_output flag. // Functions for processing the gtest_output flag.
// Returns the output format, or "" for normal printed output. // Returns the output format, or "" for normal printed output.
std::string UnitTestOptions::GetOutputFormat() { std::string UnitTestOptions::GetOutputFormat() {
const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); std::string s = GTEST_FLAG_GET(output);
const char* const gtest_output_flag = s.c_str();
const char* const colon = strchr(gtest_output_flag, ':'); const char* const colon = strchr(gtest_output_flag, ':');
return (colon == nullptr) return (colon == nullptr)
? std::string(gtest_output_flag) ? std::string(gtest_output_flag)
: std::string(gtest_output_flag, : std::string(gtest_output_flag,
static_cast<size_t>(colon - gtest_output_flag)); static_cast<size_t>(colon - gtest_output_flag));
} }
// Returns the name of the requested output file, or the default if none // Returns the name of the requested output file, or the default if none
// was explicitly specified. // was explicitly specified.
std::string UnitTestOptions::GetAbsolutePathToOutputFile() { std::string UnitTestOptions::GetAbsolutePathToOutputFile() {
const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); std::string s = GTEST_FLAG_GET(output);
const char* const gtest_output_flag = s.c_str();
std::string format = GetOutputFormat(); std::string format = GetOutputFormat();
if (format.empty()) if (format.empty()) format = std::string(kDefaultOutputFormat);
format = std::string(kDefaultOutputFormat);
const char* const colon = strchr(gtest_output_flag, ':'); const char* const colon = strchr(gtest_output_flag, ':');
if (colon == nullptr) if (colon == nullptr)
return internal::FilePath::MakeFileName( return internal::FilePath::MakeFileName(
internal::FilePath( internal::FilePath(
UnitTest::GetInstance()->original_working_dir()), UnitTest::GetInstance()->original_working_dir()),
internal::FilePath(kDefaultOutputFile), 0, internal::FilePath(kDefaultOutputFile), 0, format.c_str())
format.c_str()).string(); .string();
internal::FilePath output_name(colon + 1); internal::FilePath output_name(colon + 1);
if (!output_name.IsAbsolutePath()) if (!output_name.IsAbsolutePath())
output_name = internal::FilePath::ConcatPaths( output_name = internal::FilePath::ConcatPaths(
internal::FilePath(UnitTest::GetInstance()->original_working_dir()), internal::FilePath(UnitTest::GetInstance()->original_working_dir()),
internal::FilePath(colon + 1)); internal::FilePath(colon + 1));
if (!output_name.IsDirectory()) if (!output_name.IsDirectory()) return output_name.string();
return output_name.string();
internal::FilePath result(internal::FilePath::GenerateUniqueFileName( internal::FilePath result(internal::FilePath::GenerateUniqueFileName(
output_name, internal::GetCurrentExecutableName(), output_name, internal::GetCurrentExecutableName(),
GetOutputFormat().c_str())); GetOutputFormat().c_str()));
return result.string(); return result.string();
} }
// Returns true if and only if the wildcard pattern matches the string. Each // Returns true if and only if the wildcard pattern matches the string. Each
// pattern consists of regular characters, single-character wildcards (?), and // pattern consists of regular characters, single-character wildcards (?), and
// multi-character wildcards (*). // multi-character wildcards (*).
skipping to change at line 701 skipping to change at line 724
if (name_begin < name_next && name_next <= name_end) { if (name_begin < name_next && name_next <= name_end) {
pattern = pattern_next; pattern = pattern_next;
name = name_next; name = name_next;
continue; continue;
} }
return false; return false;
} }
return true; return true;
} }
bool UnitTestOptions::MatchesFilter(const std::string& name_str, namespace {
const char* filter) {
// The filter is a list of patterns separated by colons (:). bool IsGlobPattern(const std::string& pattern) {
const char* pattern = filter; return std::any_of(pattern.begin(), pattern.end(),
while (true) { [](const char c) { return c == '?' || c == '*'; });
// Find the bounds of this pattern. }
const char* const next_sep = strchr(pattern, ':');
const char* const pattern_end = class UnitTestFilter {
next_sep != nullptr ? next_sep : pattern + strlen(pattern); public:
UnitTestFilter() = default;
// Check if this pattern matches name_str.
if (PatternMatchesString(name_str, pattern, pattern_end)) { // Constructs a filter from a string of patterns separated by `:`.
return true; explicit UnitTestFilter(const std::string& filter) {
} // By design "" filter matches "" string.
std::vector<std::string> all_patterns;
// Give up on this pattern. However, if we found a pattern separator (:), SplitString(filter, ':', &all_patterns);
// advance to the next pattern (skipping over the separator) and restart. const auto exact_match_patterns_begin = std::partition(
if (next_sep == nullptr) { all_patterns.begin(), all_patterns.end(), &IsGlobPattern);
return false;
glob_patterns_.reserve(static_cast<size_t>(
std::distance(all_patterns.begin(), exact_match_patterns_begin)));
std::move(all_patterns.begin(), exact_match_patterns_begin,
std::inserter(glob_patterns_, glob_patterns_.begin()));
std::move(
exact_match_patterns_begin, all_patterns.end(),
std::inserter(exact_match_patterns_, exact_match_patterns_.begin()));
}
// Returns true if and only if name matches at least one of the patterns in
// the filter.
bool MatchesName(const std::string& name) const {
return exact_match_patterns_.count(name) > 0 ||
std::any_of(glob_patterns_.begin(), glob_patterns_.end(),
[&name](const std::string& pattern) {
return PatternMatchesString(
name, pattern.c_str(),
pattern.c_str() + pattern.size());
});
}
private:
std::vector<std::string> glob_patterns_;
std::unordered_set<std::string> exact_match_patterns_;
};
class PositiveAndNegativeUnitTestFilter {
public:
// Constructs a positive and a negative filter from a string. The string
// contains a positive filter optionally followed by a '-' character and a
// negative filter. In case only a negative filter is provided the positive
// filter will be assumed "*".
// A filter is a list of patterns separated by ':'.
explicit PositiveAndNegativeUnitTestFilter(const std::string& filter) {
std::vector<std::string> positive_and_negative_filters;
// NOTE: `SplitString` always returns a non-empty container.
SplitString(filter, '-', &positive_and_negative_filters);
const auto& positive_filter = positive_and_negative_filters.front();
if (positive_and_negative_filters.size() > 1) {
positive_filter_ = UnitTestFilter(
positive_filter.empty() ? kUniversalFilter : positive_filter);
// TODO(b/214626361): Fail on multiple '-' characters
// For the moment to preserve old behavior we concatenate the rest of the
// string parts with `-` as separator to generate the negative filter.
auto negative_filter_string = positive_and_negative_filters[1];
for (std::size_t i = 2; i < positive_and_negative_filters.size(); i++)
negative_filter_string =
negative_filter_string + '-' + positive_and_negative_filters[i];
negative_filter_ = UnitTestFilter(negative_filter_string);
} else {
// In case we don't have a negative filter and positive filter is ""
// we do not use kUniversalFilter by design as opposed to when we have a
// negative filter.
positive_filter_ = UnitTestFilter(positive_filter);
} }
pattern = next_sep + 1;
} }
return true;
// Returns true if and only if test name (this is generated by appending test
// suit name and test name via a '.' character) matches the positive filter
// and does not match the negative filter.
bool MatchesTest(const std::string& test_suite_name,
const std::string& test_name) const {
return MatchesName(test_suite_name + "." + test_name);
}
// Returns true if and only if name matches the positive filter and does not
// match the negative filter.
bool MatchesName(const std::string& name) const {
return positive_filter_.MatchesName(name) &&
!negative_filter_.MatchesName(name);
}
private:
UnitTestFilter positive_filter_;
UnitTestFilter negative_filter_;
};
} // namespace
bool UnitTestOptions::MatchesFilter(const std::string& name_str,
const char* filter) {
return UnitTestFilter(filter).MatchesName(name_str);
} }
// Returns true if and only if the user-specified filter matches the test // Returns true if and only if the user-specified filter matches the test
// suite name and the test name. // suite name and the test name.
bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name, bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name,
const std::string& test_name) { const std::string& test_name) {
const std::string& full_name = test_suite_name + "." + test_name.c_str();
// Split --gtest_filter at '-', if there is one, to separate into // Split --gtest_filter at '-', if there is one, to separate into
// positive filter and negative filter portions // positive filter and negative filter portions
const char* const p = GTEST_FLAG(filter).c_str(); return PositiveAndNegativeUnitTestFilter(GTEST_FLAG_GET(filter))
const char* const dash = strchr(p, '-'); .MatchesTest(test_suite_name, test_name);
std::string positive;
std::string negative;
if (dash == nullptr) {
positive = GTEST_FLAG(filter).c_str(); // Whole string is a positive filter
negative = "";
} else {
positive = std::string(p, dash); // Everything up to the dash
negative = std::string(dash + 1); // Everything after the dash
if (positive.empty()) {
// Treat '-test1' as the same as '*-test1'
positive = kUniversalFilter;
}
}
// A filter is a colon-separated list of patterns. It matches a
// test if any pattern in it matches the test.
return (MatchesFilter(full_name, positive.c_str()) &&
!MatchesFilter(full_name, negative.c_str()));
} }
#if GTEST_HAS_SEH #if GTEST_HAS_SEH
// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
// This function is useful as an __except condition. // This function is useful as an __except condition.
int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) { int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {
// Google Test should handle a SEH exception if: // Google Test should handle a SEH exception if:
// 1. the user wants it to, AND // 1. the user wants it to, AND
// 2. this is not a breakpoint exception, AND // 2. this is not a breakpoint exception, AND
// 3. this is not a C++ exception (VC++ implements them via SEH, // 3. this is not a C++ exception (VC++ implements them via SEH,
// apparently). // apparently).
// //
// SEH exception code for C++ exceptions. // SEH exception code for C++ exceptions.
// (see http://support.microsoft.com/kb/185294 for more information). // (see http://support.microsoft.com/kb/185294 for more information).
const DWORD kCxxExceptionCode = 0xe06d7363; const DWORD kCxxExceptionCode = 0xe06d7363;
bool should_handle = true; bool should_handle = true;
if (!GTEST_FLAG(catch_exceptions)) if (!GTEST_FLAG_GET(catch_exceptions))
should_handle = false; should_handle = false;
else if (exception_code == EXCEPTION_BREAKPOINT) else if (exception_code == EXCEPTION_BREAKPOINT)
should_handle = false; should_handle = false;
else if (exception_code == kCxxExceptionCode) else if (exception_code == kCxxExceptionCode)
should_handle = false; should_handle = false;
return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
} }
#endif // GTEST_HAS_SEH #endif // GTEST_HAS_SEH
} // namespace internal } // namespace internal
// The c'tor sets this object as the test part result reporter used by // The c'tor sets this object as the test part result reporter used by
// Google Test. The 'result' parameter specifies where to report the // Google Test. The 'result' parameter specifies where to report the
// results. Intercepts only failures from the current thread. // results. Intercepts only failures from the current thread.
ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
TestPartResultArray* result) TestPartResultArray* result)
: intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), result_(result) {
result_(result) {
Init(); Init();
} }
// The c'tor sets this object as the test part result reporter used by // The c'tor sets this object as the test part result reporter used by
// Google Test. The 'result' parameter specifies where to report the // Google Test. The 'result' parameter specifies where to report the
// results. // results.
ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
InterceptMode intercept_mode, TestPartResultArray* result) InterceptMode intercept_mode, TestPartResultArray* result)
: intercept_mode_(intercept_mode), : intercept_mode_(intercept_mode), result_(result) {
result_(result) {
Init(); Init();
} }
void ScopedFakeTestPartResultReporter::Init() { void ScopedFakeTestPartResultReporter::Init() {
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
if (intercept_mode_ == INTERCEPT_ALL_THREADS) { if (intercept_mode_ == INTERCEPT_ALL_THREADS) {
old_reporter_ = impl->GetGlobalTestPartResultReporter(); old_reporter_ = impl->GetGlobalTestPartResultReporter();
impl->SetGlobalTestPartResultReporter(this); impl->SetGlobalTestPartResultReporter(this);
} else { } else {
old_reporter_ = impl->GetTestPartResultReporterForCurrentThread(); old_reporter_ = impl->GetTestPartResultReporterForCurrentThread();
skipping to change at line 846 skipping to change at line 927
// Returns the type ID of ::testing::Test. We should always call this // Returns the type ID of ::testing::Test. We should always call this
// instead of GetTypeId< ::testing::Test>() to get the type ID of // instead of GetTypeId< ::testing::Test>() to get the type ID of
// testing::Test. This is to work around a suspected linker bug when // testing::Test. This is to work around a suspected linker bug when
// using Google Test as a framework on Mac OS X. The bug causes // using Google Test as a framework on Mac OS X. The bug causes
// GetTypeId< ::testing::Test>() to return different values depending // GetTypeId< ::testing::Test>() to return different values depending
// on whether the call is from the Google Test framework itself or // on whether the call is from the Google Test framework itself or
// from user test code. GetTestTypeId() is guaranteed to always // from user test code. GetTestTypeId() is guaranteed to always
// return the same value, as it always calls GetTypeId<>() from the // return the same value, as it always calls GetTypeId<>() from the
// gtest.cc, which is within the Google Test framework. // gtest.cc, which is within the Google Test framework.
TypeId GetTestTypeId() { TypeId GetTestTypeId() { return GetTypeId<Test>(); }
return GetTypeId<Test>();
}
// The value of GetTestTypeId() as seen from within the Google Test // The value of GetTestTypeId() as seen from within the Google Test
// library. This is solely for testing GetTestTypeId(). // library. This is solely for testing GetTestTypeId().
extern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId(); extern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId();
// This predicate-formatter checks that 'results' contains a test part // This predicate-formatter checks that 'results' contains a test part
// failure of the given type and that the failure message contains the // failure of the given type and that the failure message contains the
// given substring. // given substring.
static AssertionResult HasOneFailure(const char* /* results_expr */, static AssertionResult HasOneFailure(const char* /* results_expr */,
const char* /* type_expr */, const char* /* type_expr */,
const char* /* substr_expr */, const char* /* substr_expr */,
const TestPartResultArray& results, const TestPartResultArray& results,
TestPartResult::Type type, TestPartResult::Type type,
const std::string& substr) { const std::string& substr) {
const std::string expected(type == TestPartResult::kFatalFailure ? const std::string expected(type == TestPartResult::kFatalFailure
"1 fatal failure" : ? "1 fatal failure"
"1 non-fatal failure"); : "1 non-fatal failure");
Message msg; Message msg;
if (results.size() != 1) { if (results.size() != 1) {
msg << "Expected: " << expected << "\n" msg << "Expected: " << expected << "\n"
<< " Actual: " << results.size() << " failures"; << " Actual: " << results.size() << " failures";
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
msg << "\n" << results.GetTestPartResult(i); msg << "\n" << results.GetTestPartResult(i);
} }
return AssertionFailure() << msg; return AssertionFailure() << msg;
} }
const TestPartResult& r = results.GetTestPartResult(0); const TestPartResult& r = results.GetTestPartResult(0);
if (r.type() != type) { if (r.type() != type) {
return AssertionFailure() << "Expected: " << expected << "\n" return AssertionFailure() << "Expected: " << expected << "\n"
<< " Actual:\n" << " Actual:\n"
<< r; << r;
} }
if (strstr(r.message(), substr.c_str()) == nullptr) { if (strstr(r.message(), substr.c_str()) == nullptr) {
return AssertionFailure() << "Expected: " << expected << " containing \"" return AssertionFailure()
<< substr << "\"\n" << "Expected: " << expected << " containing \"" << substr << "\"\n"
<< " Actual:\n" << " Actual:\n"
<< r; << r;
} }
return AssertionSuccess(); return AssertionSuccess();
} }
// The constructor of SingleFailureChecker remembers where to look up // The constructor of SingleFailureChecker remembers where to look up
// test part results, what type of failure we expect, and what // test part results, what type of failure we expect, and what
// substring the failure message should contain. // substring the failure message should contain.
SingleFailureChecker::SingleFailureChecker(const TestPartResultArray* results, SingleFailureChecker::SingleFailureChecker(const TestPartResultArray* results,
TestPartResult::Type type, TestPartResult::Type type,
skipping to change at line 910 skipping to change at line 989
// The destructor of SingleFailureChecker verifies that the given // The destructor of SingleFailureChecker verifies that the given
// TestPartResultArray contains exactly one failure that has the given // TestPartResultArray contains exactly one failure that has the given
// type and contains the given substring. If that's not the case, a // type and contains the given substring. If that's not the case, a
// non-fatal failure will be generated. // non-fatal failure will be generated.
SingleFailureChecker::~SingleFailureChecker() { SingleFailureChecker::~SingleFailureChecker() {
EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_); EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_);
} }
DefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter( DefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter(
UnitTestImpl* unit_test) : unit_test_(unit_test) {} UnitTestImpl* unit_test)
: unit_test_(unit_test) {}
void DefaultGlobalTestPartResultReporter::ReportTestPartResult( void DefaultGlobalTestPartResultReporter::ReportTestPartResult(
const TestPartResult& result) { const TestPartResult& result) {
unit_test_->current_test_result()->AddTestPartResult(result); unit_test_->current_test_result()->AddTestPartResult(result);
unit_test_->listeners()->repeater()->OnTestPartResult(result); unit_test_->listeners()->repeater()->OnTestPartResult(result);
} }
DefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter( DefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter(
UnitTestImpl* unit_test) : unit_test_(unit_test) {} UnitTestImpl* unit_test)
: unit_test_(unit_test) {}
void DefaultPerThreadTestPartResultReporter::ReportTestPartResult( void DefaultPerThreadTestPartResultReporter::ReportTestPartResult(
const TestPartResult& result) { const TestPartResult& result) {
unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result); unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result);
} }
// Returns the global test part result reporter. // Returns the global test part result reporter.
TestPartResultReporterInterface* TestPartResultReporterInterface*
UnitTestImpl::GetGlobalTestPartResultReporter() { UnitTestImpl::GetGlobalTestPartResultReporter() {
internal::MutexLock lock(&global_test_part_result_reporter_mutex_); internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
skipping to change at line 1026 skipping to change at line 1107
// The maximum number of stack frames to be included is specified by // The maximum number of stack frames to be included is specified by
// the gtest_stack_trace_depth flag. The skip_count parameter // the gtest_stack_trace_depth flag. The skip_count parameter
// specifies the number of top frames to be skipped, which doesn't // specifies the number of top frames to be skipped, which doesn't
// count against the number of frames to be included. // count against the number of frames to be included.
// //
// For example, if Foo() calls Bar(), which in turn calls // For example, if Foo() calls Bar(), which in turn calls
// CurrentOsStackTraceExceptTop(1), Foo() will be included in the // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
// trace but Bar() and CurrentOsStackTraceExceptTop() won't. // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
std::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) { std::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) {
return os_stack_trace_getter()->CurrentStackTrace( return os_stack_trace_getter()->CurrentStackTrace(
static_cast<int>(GTEST_FLAG(stack_trace_depth)), static_cast<int>(GTEST_FLAG_GET(stack_trace_depth)), skip_count + 1
skip_count + 1
// Skips the user-specified number of frames plus this function // Skips the user-specified number of frames plus this function
// itself. // itself.
); // NOLINT ); // NOLINT
} }
// A helper class for measuring elapsed times. // A helper class for measuring elapsed times.
class Timer { class Timer {
public: public:
Timer() : start_(std::chrono::steady_clock::now()) {} Timer() : start_(std::chrono::steady_clock::now()) {}
// Return time elapsed in milliseconds since the timer was created. // Return time elapsed in milliseconds since the timer was created.
TimeInMillis Elapsed() { TimeInMillis Elapsed() {
return std::chrono::duration_cast<std::chrono::milliseconds>( return std::chrono::duration_cast<std::chrono::milliseconds>(
skipping to change at line 1074 skipping to change at line 1154
// Creates a UTF-16 wide string from the given ANSI string, allocating // Creates a UTF-16 wide string from the given ANSI string, allocating
// memory using new. The caller is responsible for deleting the return // memory using new. The caller is responsible for deleting the return
// value using delete[]. Returns the wide string, or NULL if the // value using delete[]. Returns the wide string, or NULL if the
// input is NULL. // input is NULL.
LPCWSTR String::AnsiToUtf16(const char* ansi) { LPCWSTR String::AnsiToUtf16(const char* ansi) {
if (!ansi) return nullptr; if (!ansi) return nullptr;
const int length = strlen(ansi); const int length = strlen(ansi);
const int unicode_length = const int unicode_length =
MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0);
WCHAR* unicode = new WCHAR[unicode_length + 1]; WCHAR* unicode = new WCHAR[unicode_length + 1];
MultiByteToWideChar(CP_ACP, 0, ansi, length, MultiByteToWideChar(CP_ACP, 0, ansi, length, unicode, unicode_length);
unicode, unicode_length);
unicode[unicode_length] = 0; unicode[unicode_length] = 0;
return unicode; return unicode;
} }
// Creates an ANSI string from the given wide string, allocating // Creates an ANSI string from the given wide string, allocating
// memory using new. The caller is responsible for deleting the return // memory using new. The caller is responsible for deleting the return
// value using delete[]. Returns the ANSI string, or NULL if the // value using delete[]. Returns the ANSI string, or NULL if the
// input is NULL. // input is NULL.
const char* String::Utf16ToAnsi(LPCWSTR utf16_str) { const char* String::Utf16ToAnsi(LPCWSTR utf16_str) {
if (!utf16_str) return nullptr; if (!utf16_str) return nullptr;
const int ansi_length = WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, nullptr, const int ansi_length = WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, nullptr,
0, nullptr, nullptr); 0, nullptr, nullptr);
char* ansi = new char[ansi_length + 1]; char* ansi = new char[ansi_length + 1];
WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, ansi, ansi_length, nullptr, WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, ansi, ansi_length, nullptr,
nullptr); nullptr);
ansi[ansi_length] = 0; ansi[ansi_length] = 0;
return ansi; return ansi;
} }
#endif // GTEST_OS_WINDOWS_MOBILE #endif // GTEST_OS_WINDOWS_MOBILE
// Compares two C strings. Returns true if and only if they have the same // Compares two C strings. Returns true if and only if they have the same
// content. // content.
// //
// Unlike strcmp(), this function can handle NULL argument(s). A NULL // Unlike strcmp(), this function can handle NULL argument(s). A NULL
// C string is considered different to any non-NULL C string, // C string is considered different to any non-NULL C string,
// including the empty string. // including the empty string.
bool String::CStringEquals(const char * lhs, const char * rhs) { bool String::CStringEquals(const char* lhs, const char* rhs) {
if (lhs == nullptr) return rhs == nullptr; if (lhs == nullptr) return rhs == nullptr;
if (rhs == nullptr) return false; if (rhs == nullptr) return false;
return strcmp(lhs, rhs) == 0; return strcmp(lhs, rhs) == 0;
} }
#if GTEST_HAS_STD_WSTRING #if GTEST_HAS_STD_WSTRING
// Converts an array of wide chars to a narrow string using the UTF-8 // Converts an array of wide chars to a narrow string using the UTF-8
// encoding, and streams the result to the given Message object. // encoding, and streams the result to the given Message object.
static void StreamWideCharsToMessage(const wchar_t* wstr, size_t length, static void StreamWideCharsToMessage(const wchar_t* wstr, size_t length,
Message* msg) { Message* msg) {
for (size_t i = 0; i != length; ) { // NOLINT for (size_t i = 0; i != length;) { // NOLINT
if (wstr[i] != L'\0') { if (wstr[i] != L'\0') {
*msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i)); *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i));
while (i != length && wstr[i] != L'\0') while (i != length && wstr[i] != L'\0') i++;
i++;
} else { } else {
*msg << '\0'; *msg << '\0';
i++; i++;
} }
} }
} }
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
void SplitString(const ::std::string& str, char delimiter, void SplitString(const ::std::string& str, char delimiter,
skipping to change at line 1163 skipping to change at line 1241
// stack frame leading to huge stack frames in some cases; gcc does not reuse // stack frame leading to huge stack frames in some cases; gcc does not reuse
// the stack space. // the stack space.
Message::Message() : ss_(new ::std::stringstream) { Message::Message() : ss_(new ::std::stringstream) {
// By default, we want there to be enough precision when printing // By default, we want there to be enough precision when printing
// a double to a Message. // a double to a Message.
*ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2); *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
} }
// These two overloads allow streaming a wide C string to a Message // These two overloads allow streaming a wide C string to a Message
// using the UTF-8 encoding. // using the UTF-8 encoding.
Message& Message::operator <<(const wchar_t* wide_c_str) { Message& Message::operator<<(const wchar_t* wide_c_str) {
return *this << internal::String::ShowWideCString(wide_c_str); return *this << internal::String::ShowWideCString(wide_c_str);
} }
Message& Message::operator <<(wchar_t* wide_c_str) { Message& Message::operator<<(wchar_t* wide_c_str) {
return *this << internal::String::ShowWideCString(wide_c_str); return *this << internal::String::ShowWideCString(wide_c_str);
} }
#if GTEST_HAS_STD_WSTRING #if GTEST_HAS_STD_WSTRING
// Converts the given wide string to a narrow string using the UTF-8 // Converts the given wide string to a narrow string using the UTF-8
// encoding, and streams the result to this Message object. // encoding, and streams the result to this Message object.
Message& Message::operator <<(const ::std::wstring& wstr) { Message& Message::operator<<(const ::std::wstring& wstr) {
internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);
return *this; return *this;
} }
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
// Gets the text streamed to this object so far as an std::string. // Gets the text streamed to this object so far as an std::string.
// Each '\0' character in the buffer is replaced with "\\0". // Each '\0' character in the buffer is replaced with "\\0".
std::string Message::GetString() const { std::string Message::GetString() const {
return internal::StringStreamToString(ss_.get()); return internal::StringStreamToString(ss_.get());
} }
// AssertionResult constructors.
// Used in EXPECT_TRUE/FALSE(assertion_result).
AssertionResult::AssertionResult(const AssertionResult& other)
: success_(other.success_),
message_(other.message_.get() != nullptr
? new ::std::string(*other.message_)
: static_cast< ::std::string*>(nullptr)) {}
// Swaps two AssertionResults.
void AssertionResult::swap(AssertionResult& other) {
using std::swap;
swap(success_, other.success_);
swap(message_, other.message_);
}
// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
AssertionResult AssertionResult::operator!() const {
AssertionResult negation(!success_);
if (message_.get() != nullptr) negation << *message_;
return negation;
}
// Makes a successful assertion result.
AssertionResult AssertionSuccess() {
return AssertionResult(true);
}
// Makes a failed assertion result.
AssertionResult AssertionFailure() {
return AssertionResult(false);
}
// Makes a failed assertion result with the given failure message.
// Deprecated; use AssertionFailure() << message.
AssertionResult AssertionFailure(const Message& message) {
return AssertionFailure() << message;
}
namespace internal { namespace internal {
namespace edit_distance { namespace edit_distance {
std::vector<EditType> CalculateOptimalEdits(const std::vector<size_t>& left, std::vector<EditType> CalculateOptimalEdits(const std::vector<size_t>& left,
const std::vector<size_t>& right) { const std::vector<size_t>& right) {
std::vector<std::vector<double> > costs( std::vector<std::vector<double> > costs(
left.size() + 1, std::vector<double>(right.size() + 1)); left.size() + 1, std::vector<double>(right.size() + 1));
std::vector<std::vector<EditType> > best_move( std::vector<std::vector<EditType> > best_move(
left.size() + 1, std::vector<EditType>(right.size() + 1)); left.size() + 1, std::vector<EditType>(right.size() + 1));
skipping to change at line 1514 skipping to change at line 1554
// rhs_expression: "bar" // rhs_expression: "bar"
// lhs_value: "5" // lhs_value: "5"
// rhs_value: "6" // rhs_value: "6"
// //
// The ignoring_case parameter is true if and only if the assertion is a // The ignoring_case parameter is true if and only if the assertion is a
// *_STRCASEEQ*. When it's true, the string "Ignoring case" will // *_STRCASEEQ*. When it's true, the string "Ignoring case" will
// be inserted into the message. // be inserted into the message.
AssertionResult EqFailure(const char* lhs_expression, AssertionResult EqFailure(const char* lhs_expression,
const char* rhs_expression, const char* rhs_expression,
const std::string& lhs_value, const std::string& lhs_value,
const std::string& rhs_value, const std::string& rhs_value, bool ignoring_case) {
bool ignoring_case) {
Message msg; Message msg;
msg << "Expected equality of these values:"; msg << "Expected equality of these values:";
msg << "\n " << lhs_expression; msg << "\n " << lhs_expression;
if (lhs_value != lhs_expression) { if (lhs_value != lhs_expression) {
msg << "\n Which is: " << lhs_value; msg << "\n Which is: " << lhs_value;
} }
msg << "\n " << rhs_expression; msg << "\n " << rhs_expression;
if (rhs_value != rhs_expression) { if (rhs_value != rhs_expression) {
msg << "\n Which is: " << rhs_value; msg << "\n Which is: " << rhs_value;
} }
if (ignoring_case) { if (ignoring_case) {
msg << "\nIgnoring case"; msg << "\nIgnoring case";
} }
if (!lhs_value.empty() && !rhs_value.empty()) { if (!lhs_value.empty() && !rhs_value.empty()) {
const std::vector<std::string> lhs_lines = const std::vector<std::string> lhs_lines = SplitEscapedString(lhs_value);
SplitEscapedString(lhs_value); const std::vector<std::string> rhs_lines = SplitEscapedString(rhs_value);
const std::vector<std::string> rhs_lines =
SplitEscapedString(rhs_value);
if (lhs_lines.size() > 1 || rhs_lines.size() > 1) { if (lhs_lines.size() > 1 || rhs_lines.size() > 1) {
msg << "\nWith diff:\n" msg << "\nWith diff:\n"
<< edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines); << edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines);
} }
} }
return AssertionFailure() << msg; return AssertionFailure() << msg;
} }
// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. // Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
std::string GetBoolAssertionFailureMessage( std::string GetBoolAssertionFailureMessage(
const AssertionResult& assertion_result, const AssertionResult& assertion_result, const char* expression_text,
const char* expression_text, const char* actual_predicate_value, const char* expected_predicate_value) {
const char* actual_predicate_value,
const char* expected_predicate_value) {
const char* actual_message = assertion_result.message(); const char* actual_message = assertion_result.message();
Message msg; Message msg;
msg << "Value of: " << expression_text msg << "Value of: " << expression_text
<< "\n Actual: " << actual_predicate_value; << "\n Actual: " << actual_predicate_value;
if (actual_message[0] != '\0') if (actual_message[0] != '\0') msg << " (" << actual_message << ")";
msg << " (" << actual_message << ")";
msg << "\nExpected: " << expected_predicate_value; msg << "\nExpected: " << expected_predicate_value;
return msg.GetString(); return msg.GetString();
} }
// Helper function for implementing ASSERT_NEAR. // Helper function for implementing ASSERT_NEAR.
AssertionResult DoubleNearPredFormat(const char* expr1, AssertionResult DoubleNearPredFormat(const char* expr1, const char* expr2,
const char* expr2, const char* abs_error_expr, double val1,
const char* abs_error_expr, double val2, double abs_error) {
double val1,
double val2,
double abs_error) {
const double diff = fabs(val1 - val2); const double diff = fabs(val1 - val2);
if (diff <= abs_error) return AssertionSuccess(); if (diff <= abs_error) return AssertionSuccess();
// Find the value which is closest to zero. // Find the value which is closest to zero.
const double min_abs = std::min(fabs(val1), fabs(val2)); const double min_abs = std::min(fabs(val1), fabs(val2));
// Find the distance to the next double from that value. // Find the distance to the next double from that value.
const double epsilon = const double epsilon =
nextafter(min_abs, std::numeric_limits<double>::infinity()) - min_abs; nextafter(min_abs, std::numeric_limits<double>::infinity()) - min_abs;
// Detect the case where abs_error is so small that EXPECT_NEAR is // Detect the case where abs_error is so small that EXPECT_NEAR is
// effectively the same as EXPECT_EQUAL, and give an informative error // effectively the same as EXPECT_EQUAL, and give an informative error
skipping to change at line 1597 skipping to change at line 1628
<< expr1 << " evaluates to " << val1 << ",\n" << expr1 << " evaluates to " << val1 << ",\n"
<< expr2 << " evaluates to " << val2 << ".\nThe abs_error parameter " << expr2 << " evaluates to " << val2 << ".\nThe abs_error parameter "
<< abs_error_expr << " evaluates to " << abs_error << abs_error_expr << " evaluates to " << abs_error
<< " which is smaller than the minimum distance between doubles for " << " which is smaller than the minimum distance between doubles for "
"numbers of this magnitude which is " "numbers of this magnitude which is "
<< epsilon << epsilon
<< ", thus making this EXPECT_NEAR check equivalent to " << ", thus making this EXPECT_NEAR check equivalent to "
"EXPECT_EQUAL. Consider using EXPECT_DOUBLE_EQ instead."; "EXPECT_EQUAL. Consider using EXPECT_DOUBLE_EQ instead.";
} }
return AssertionFailure() return AssertionFailure()
<< "The difference between " << expr1 << " and " << expr2 << "The difference between " << expr1 << " and " << expr2 << " is "
<< " is " << diff << ", which exceeds " << abs_error_expr << ", where\n" << diff << ", which exceeds " << abs_error_expr << ", where\n"
<< expr1 << " evaluates to " << val1 << ",\n" << expr1 << " evaluates to " << val1 << ",\n"
<< expr2 << " evaluates to " << val2 << ", and\n" << expr2 << " evaluates to " << val2 << ", and\n"
<< abs_error_expr << " evaluates to " << abs_error << "."; << abs_error_expr << " evaluates to " << abs_error << ".";
} }
// Helper template for implementing FloatLE() and DoubleLE(). // Helper template for implementing FloatLE() and DoubleLE().
template <typename RawType> template <typename RawType>
AssertionResult FloatingPointLE(const char* expr1, AssertionResult FloatingPointLE(const char* expr1, const char* expr2,
const char* expr2, RawType val1, RawType val2) {
RawType val1,
RawType val2) {
// Returns success if val1 is less than val2, // Returns success if val1 is less than val2,
if (val1 < val2) { if (val1 < val2) {
return AssertionSuccess(); return AssertionSuccess();
} }
// or if val1 is almost equal to val2. // or if val1 is almost equal to val2.
const FloatingPoint<RawType> lhs(val1), rhs(val2); const FloatingPoint<RawType> lhs(val1), rhs(val2);
if (lhs.AlmostEquals(rhs)) { if (lhs.AlmostEquals(rhs)) {
return AssertionSuccess(); return AssertionSuccess();
} }
skipping to change at line 1634 skipping to change at line 1663
::std::stringstream val1_ss; ::std::stringstream val1_ss;
val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
<< val1; << val1;
::std::stringstream val2_ss; ::std::stringstream val2_ss;
val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
<< val2; << val2;
return AssertionFailure() return AssertionFailure()
<< "Expected: (" << expr1 << ") <= (" << expr2 << ")\n" << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n"
<< " Actual: " << StringStreamToString(&val1_ss) << " vs " << " Actual: " << StringStreamToString(&val1_ss) << " vs "
<< StringStreamToString(&val2_ss); << StringStreamToString(&val2_ss);
} }
} // namespace internal } // namespace internal
// Asserts that val1 is less than, or almost equal to, val2. Fails // Asserts that val1 is less than, or almost equal to, val2. Fails
// otherwise. In particular, it fails if either val1 or val2 is NaN. // otherwise. In particular, it fails if either val1 or val2 is NaN.
AssertionResult FloatLE(const char* expr1, const char* expr2, AssertionResult FloatLE(const char* expr1, const char* expr2, float val1,
float val1, float val2) { float val2) {
return internal::FloatingPointLE<float>(expr1, expr2, val1, val2); return internal::FloatingPointLE<float>(expr1, expr2, val1, val2);
} }
// Asserts that val1 is less than, or almost equal to, val2. Fails // Asserts that val1 is less than, or almost equal to, val2. Fails
// otherwise. In particular, it fails if either val1 or val2 is NaN. // otherwise. In particular, it fails if either val1 or val2 is NaN.
AssertionResult DoubleLE(const char* expr1, const char* expr2, AssertionResult DoubleLE(const char* expr1, const char* expr2, double val1,
double val1, double val2) { double val2) {
return internal::FloatingPointLE<double>(expr1, expr2, val1, val2); return internal::FloatingPointLE<double>(expr1, expr2, val1, val2);
} }
namespace internal { namespace internal {
// The helper function for {ASSERT|EXPECT}_STREQ. // The helper function for {ASSERT|EXPECT}_STREQ.
AssertionResult CmpHelperSTREQ(const char* lhs_expression, AssertionResult CmpHelperSTREQ(const char* lhs_expression,
const char* rhs_expression, const char* rhs_expression, const char* lhs,
const char* lhs,
const char* rhs) { const char* rhs) {
if (String::CStringEquals(lhs, rhs)) { if (String::CStringEquals(lhs, rhs)) {
return AssertionSuccess(); return AssertionSuccess();
} }
return EqFailure(lhs_expression, return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs),
rhs_expression, PrintToString(rhs), false);
PrintToString(lhs),
PrintToString(rhs),
false);
} }
// The helper function for {ASSERT|EXPECT}_STRCASEEQ. // The helper function for {ASSERT|EXPECT}_STRCASEEQ.
AssertionResult CmpHelperSTRCASEEQ(const char* lhs_expression, AssertionResult CmpHelperSTRCASEEQ(const char* lhs_expression,
const char* rhs_expression, const char* rhs_expression, const char* lhs,
const char* lhs,
const char* rhs) { const char* rhs) {
if (String::CaseInsensitiveCStringEquals(lhs, rhs)) { if (String::CaseInsensitiveCStringEquals(lhs, rhs)) {
return AssertionSuccess(); return AssertionSuccess();
} }
return EqFailure(lhs_expression, return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs),
rhs_expression, PrintToString(rhs), true);
PrintToString(lhs),
PrintToString(rhs),
true);
} }
// The helper function for {ASSERT|EXPECT}_STRNE. // The helper function for {ASSERT|EXPECT}_STRNE.
AssertionResult CmpHelperSTRNE(const char* s1_expression, AssertionResult CmpHelperSTRNE(const char* s1_expression,
const char* s2_expression, const char* s2_expression, const char* s1,
const char* s1,
const char* s2) { const char* s2) {
if (!String::CStringEquals(s1, s2)) { if (!String::CStringEquals(s1, s2)) {
return AssertionSuccess(); return AssertionSuccess();
} else { } else {
return AssertionFailure() << "Expected: (" << s1_expression << ") != (" return AssertionFailure()
<< s2_expression << "), actual: \"" << "Expected: (" << s1_expression << ") != (" << s2_expression
<< s1 << "\" vs \"" << s2 << "\""; << "), actual: \"" << s1 << "\" vs \"" << s2 << "\"";
} }
} }
// The helper function for {ASSERT|EXPECT}_STRCASENE. // The helper function for {ASSERT|EXPECT}_STRCASENE.
AssertionResult CmpHelperSTRCASENE(const char* s1_expression, AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
const char* s2_expression, const char* s2_expression, const char* s1,
const char* s1,
const char* s2) { const char* s2) {
if (!String::CaseInsensitiveCStringEquals(s1, s2)) { if (!String::CaseInsensitiveCStringEquals(s1, s2)) {
return AssertionSuccess(); return AssertionSuccess();
} else { } else {
return AssertionFailure() return AssertionFailure()
<< "Expected: (" << s1_expression << ") != (" << "Expected: (" << s1_expression << ") != (" << s2_expression
<< s2_expression << ") (ignoring case), actual: \"" << ") (ignoring case), actual: \"" << s1 << "\" vs \"" << s2 << "\"";
<< s1 << "\" vs \"" << s2 << "\"";
} }
} }
} // namespace internal } // namespace internal
namespace { namespace {
// Helper functions for implementing IsSubString() and IsNotSubstring(). // Helper functions for implementing IsSubString() and IsNotSubstring().
// This group of overloaded functions return true if and only if needle // This group of overloaded functions return true if and only if needle
skipping to change at line 1742 skipping to change at line 1760
} }
bool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) { bool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) {
if (needle == nullptr || haystack == nullptr) return needle == haystack; if (needle == nullptr || haystack == nullptr) return needle == haystack;
return wcsstr(haystack, needle) != nullptr; return wcsstr(haystack, needle) != nullptr;
} }
// StringType here can be either ::std::string or ::std::wstring. // StringType here can be either ::std::string or ::std::wstring.
template <typename StringType> template <typename StringType>
bool IsSubstringPred(const StringType& needle, bool IsSubstringPred(const StringType& needle, const StringType& haystack) {
const StringType& haystack) {
return haystack.find(needle) != StringType::npos; return haystack.find(needle) != StringType::npos;
} }
// This function implements either IsSubstring() or IsNotSubstring(), // This function implements either IsSubstring() or IsNotSubstring(),
// depending on the value of the expected_to_be_substring parameter. // depending on the value of the expected_to_be_substring parameter.
// StringType here can be const char*, const wchar_t*, ::std::string, // StringType here can be const char*, const wchar_t*, ::std::string,
// or ::std::wstring. // or ::std::wstring.
template <typename StringType> template <typename StringType>
AssertionResult IsSubstringImpl( AssertionResult IsSubstringImpl(bool expected_to_be_substring,
bool expected_to_be_substring, const char* needle_expr,
const char* needle_expr, const char* haystack_expr, const char* haystack_expr,
const StringType& needle, const StringType& haystack) { const StringType& needle,
const StringType& haystack) {
if (IsSubstringPred(needle, haystack) == expected_to_be_substring) if (IsSubstringPred(needle, haystack) == expected_to_be_substring)
return AssertionSuccess(); return AssertionSuccess();
const bool is_wide_string = sizeof(needle[0]) > 1; const bool is_wide_string = sizeof(needle[0]) > 1;
const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
return AssertionFailure() return AssertionFailure()
<< "Value of: " << needle_expr << "\n" << "Value of: " << needle_expr << "\n"
<< " Actual: " << begin_string_quote << needle << "\"\n" << " Actual: " << begin_string_quote << needle << "\"\n"
<< "Expected: " << (expected_to_be_substring ? "" : "not ") << "Expected: " << (expected_to_be_substring ? "" : "not ")
<< "a substring of " << haystack_expr << "\n" << "a substring of " << haystack_expr << "\n"
<< "Which is: " << begin_string_quote << haystack << "\""; << "Which is: " << begin_string_quote << haystack << "\"";
} }
} // namespace } // namespace
// IsSubstring() and IsNotSubstring() check whether needle is a // IsSubstring() and IsNotSubstring() check whether needle is a
// substring of haystack (NULL is considered a substring of itself // substring of haystack (NULL is considered a substring of itself
// only), and return an appropriate error message when they fail. // only), and return an appropriate error message when they fail.
AssertionResult IsSubstring( AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr,
const char* needle_expr, const char* haystack_expr, const char* needle, const char* haystack) {
const char* needle, const char* haystack) {
return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsSubstring( AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr,
const char* needle_expr, const char* haystack_expr, const wchar_t* needle, const wchar_t* haystack) {
const wchar_t* needle, const wchar_t* haystack) {
return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsNotSubstring( AssertionResult IsNotSubstring(const char* needle_expr,
const char* needle_expr, const char* haystack_expr, const char* haystack_expr, const char* needle,
const char* needle, const char* haystack) { const char* haystack) {
return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsNotSubstring( AssertionResult IsNotSubstring(const char* needle_expr,
const char* needle_expr, const char* haystack_expr, const char* haystack_expr, const wchar_t* needle,
const wchar_t* needle, const wchar_t* haystack) { const wchar_t* haystack) {
return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsSubstring( AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr,
const char* needle_expr, const char* haystack_expr, const ::std::string& needle,
const ::std::string& needle, const ::std::string& haystack) { const ::std::string& haystack) {
return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsNotSubstring( AssertionResult IsNotSubstring(const char* needle_expr,
const char* needle_expr, const char* haystack_expr, const char* haystack_expr,
const ::std::string& needle, const ::std::string& haystack) { const ::std::string& needle,
const ::std::string& haystack) {
return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
} }
#if GTEST_HAS_STD_WSTRING #if GTEST_HAS_STD_WSTRING
AssertionResult IsSubstring( AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr,
const char* needle_expr, const char* haystack_expr, const ::std::wstring& needle,
const ::std::wstring& needle, const ::std::wstring& haystack) { const ::std::wstring& haystack) {
return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
} }
AssertionResult IsNotSubstring( AssertionResult IsNotSubstring(const char* needle_expr,
const char* needle_expr, const char* haystack_expr, const char* haystack_expr,
const ::std::wstring& needle, const ::std::wstring& haystack) { const ::std::wstring& needle,
const ::std::wstring& haystack) {
return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
} }
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
namespace internal { namespace internal {
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
namespace { namespace {
// Helper function for IsHRESULT{SuccessFailure} predicates // Helper function for IsHRESULT{SuccessFailure} predicates
AssertionResult HRESULTFailureHelper(const char* expr, AssertionResult HRESULTFailureHelper(const char* expr, const char* expected,
const char* expected,
long hr) { // NOLINT long hr) { // NOLINT
# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_TV_TITLE #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_TV_TITLE
// Windows CE doesn't support FormatMessage. // Windows CE doesn't support FormatMessage.
const char error_text[] = ""; const char error_text[] = "";
# else #else
// Looks up the human-readable system message for the HRESULT code // Looks up the human-readable system message for the HRESULT code
// and since we're not passing any params to FormatMessage, we don't // and since we're not passing any params to FormatMessage, we don't
// want inserts expanded. // want inserts expanded.
const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM | const DWORD kFlags =
FORMAT_MESSAGE_IGNORE_INSERTS; FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
const DWORD kBufSize = 4096; const DWORD kBufSize = 4096;
// Gets the system's human readable message string for this HRESULT. // Gets the system's human readable message string for this HRESULT.
char error_text[kBufSize] = { '\0' }; char error_text[kBufSize] = {'\0'};
DWORD message_length = ::FormatMessageA(kFlags, DWORD message_length = ::FormatMessageA(kFlags,
0, // no source, we're asking system 0, // no source, we're asking system
static_cast<DWORD>(hr), // the error static_cast<DWORD>(hr), // the error
0, // no line width restrictions 0, // no line width restrictions
error_text, // output buffer error_text, // output buffer
kBufSize, // buf size kBufSize, // buf size
nullptr); // no arguments for inserts nullptr); // no arguments for inserts
// Trims tailing white space (FormatMessage leaves a trailing CR-LF) // Trims tailing white space (FormatMessage leaves a trailing CR-LF)
for (; message_length && IsSpace(error_text[message_length - 1]); for (; message_length && IsSpace(error_text[message_length - 1]);
--message_length) { --message_length) {
error_text[message_length - 1] = '\0'; error_text[message_length - 1] = '\0';
} }
# endif // GTEST_OS_WINDOWS_MOBILE #endif // GTEST_OS_WINDOWS_MOBILE
const std::string error_hex("0x" + String::FormatHexInt(hr)); const std::string error_hex("0x" + String::FormatHexInt(hr));
return ::testing::AssertionFailure() return ::testing::AssertionFailure()
<< "Expected: " << expr << " " << expected << ".\n" << "Expected: " << expr << " " << expected << ".\n"
<< " Actual: " << error_hex << " " << error_text << "\n"; << " Actual: " << error_hex << " " << error_text << "\n";
} }
} // namespace } // namespace
AssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT AssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
return AssertionSuccess(); return AssertionSuccess();
} }
return HRESULTFailureHelper(expr, "succeeds", hr); return HRESULTFailureHelper(expr, "succeeds", hr);
} }
skipping to change at line 1902 skipping to change at line 1919
// A Unicode code-point can have up to 21 bits, and is encoded in UTF-8 // A Unicode code-point can have up to 21 bits, and is encoded in UTF-8
// like this: // like this:
// //
// Code-point length Encoding // Code-point length Encoding
// 0 - 7 bits 0xxxxxxx // 0 - 7 bits 0xxxxxxx
// 8 - 11 bits 110xxxxx 10xxxxxx // 8 - 11 bits 110xxxxx 10xxxxxx
// 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx // 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx
// 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx // 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// The maximum code-point a one-byte UTF-8 sequence can represent. // The maximum code-point a one-byte UTF-8 sequence can represent.
constexpr uint32_t kMaxCodePoint1 = (static_cast<uint32_t>(1) << 7) - 1; constexpr uint32_t kMaxCodePoint1 = (static_cast<uint32_t>(1) << 7) - 1;
// The maximum code-point a two-byte UTF-8 sequence can represent. // The maximum code-point a two-byte UTF-8 sequence can represent.
constexpr uint32_t kMaxCodePoint2 = (static_cast<uint32_t>(1) << (5 + 6)) - 1; constexpr uint32_t kMaxCodePoint2 = (static_cast<uint32_t>(1) << (5 + 6)) - 1;
// The maximum code-point a three-byte UTF-8 sequence can represent. // The maximum code-point a three-byte UTF-8 sequence can represent.
constexpr uint32_t kMaxCodePoint3 = (static_cast<uint32_t>(1) << (4 + 2*6)) - 1; constexpr uint32_t kMaxCodePoint3 =
(static_cast<uint32_t>(1) << (4 + 2 * 6)) - 1;
// The maximum code-point a four-byte UTF-8 sequence can represent. // The maximum code-point a four-byte UTF-8 sequence can represent.
constexpr uint32_t kMaxCodePoint4 = (static_cast<uint32_t>(1) << (3 + 3*6)) - 1; constexpr uint32_t kMaxCodePoint4 =
(static_cast<uint32_t>(1) << (3 + 3 * 6)) - 1;
// Chops off the n lowest bits from a bit pattern. Returns the n // Chops off the n lowest bits from a bit pattern. Returns the n
// lowest bits. As a side effect, the original bit pattern will be // lowest bits. As a side effect, the original bit pattern will be
// shifted to the right by n bits. // shifted to the right by n bits.
inline uint32_t ChopLowBits(uint32_t* bits, int n) { inline uint32_t ChopLowBits(uint32_t* bits, int n) {
const uint32_t low_bits = *bits & ((static_cast<uint32_t>(1) << n) - 1); const uint32_t low_bits = *bits & ((static_cast<uint32_t>(1) << n) - 1);
*bits >>= n; *bits >>= n;
return low_bits; return low_bits;
} }
skipping to change at line 1936 skipping to change at line 1955
// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted // (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted
// to "(Invalid Unicode 0xXXXXXXXX)". // to "(Invalid Unicode 0xXXXXXXXX)".
std::string CodePointToUtf8(uint32_t code_point) { std::string CodePointToUtf8(uint32_t code_point) {
if (code_point > kMaxCodePoint4) { if (code_point > kMaxCodePoint4) {
return "(Invalid Unicode 0x" + String::FormatHexUInt32(code_point) + ")"; return "(Invalid Unicode 0x" + String::FormatHexUInt32(code_point) + ")";
} }
char str[5]; // Big enough for the largest valid code point. char str[5]; // Big enough for the largest valid code point.
if (code_point <= kMaxCodePoint1) { if (code_point <= kMaxCodePoint1) {
str[1] = '\0'; str[1] = '\0';
str[0] = static_cast<char>(code_point); // 0xxxxxxx str[0] = static_cast<char>(code_point); // 0xxxxxxx
} else if (code_point <= kMaxCodePoint2) { } else if (code_point <= kMaxCodePoint2) {
str[2] = '\0'; str[2] = '\0';
str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx
str[0] = static_cast<char>(0xC0 | code_point); // 110xxxxx str[0] = static_cast<char>(0xC0 | code_point); // 110xxxxx
} else if (code_point <= kMaxCodePoint3) { } else if (code_point <= kMaxCodePoint3) {
str[3] = '\0'; str[3] = '\0';
str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx
str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx
str[0] = static_cast<char>(0xE0 | code_point); // 1110xxxx str[0] = static_cast<char>(0xE0 | code_point); // 1110xxxx
} else { // code_point <= kMaxCodePoint4 } else { // code_point <= kMaxCodePoint4
skipping to change at line 1964 skipping to change at line 1983
} }
// The following two functions only make sense if the system // The following two functions only make sense if the system
// uses UTF-16 for wide string encoding. All supported systems // uses UTF-16 for wide string encoding. All supported systems
// with 16 bit wchar_t (Windows, Cygwin) do use UTF-16. // with 16 bit wchar_t (Windows, Cygwin) do use UTF-16.
// Determines if the arguments constitute UTF-16 surrogate pair // Determines if the arguments constitute UTF-16 surrogate pair
// and thus should be combined into a single Unicode code point // and thus should be combined into a single Unicode code point
// using CreateCodePointFromUtf16SurrogatePair. // using CreateCodePointFromUtf16SurrogatePair.
inline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { inline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) {
return sizeof(wchar_t) == 2 && return sizeof(wchar_t) == 2 && (first & 0xFC00) == 0xD800 &&
(first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; (second & 0xFC00) == 0xDC00;
} }
// Creates a Unicode code point from UTF16 surrogate pair. // Creates a Unicode code point from UTF16 surrogate pair.
inline uint32_t CreateCodePointFromUtf16SurrogatePair(wchar_t first, inline uint32_t CreateCodePointFromUtf16SurrogatePair(wchar_t first,
wchar_t second) { wchar_t second) {
const auto first_u = static_cast<uint32_t>(first); const auto first_u = static_cast<uint32_t>(first);
const auto second_u = static_cast<uint32_t>(second); const auto second_u = static_cast<uint32_t>(second);
const uint32_t mask = (1 << 10) - 1; const uint32_t mask = (1 << 10) - 1;
return (sizeof(wchar_t) == 2) return (sizeof(wchar_t) == 2)
? (((first_u & mask) << 10) | (second_u & mask)) + 0x10000 ? (((first_u & mask) << 10) | (second_u & mask)) + 0x10000
skipping to change at line 1996 skipping to change at line 2015
// Parameter str points to a null-terminated wide string. // Parameter str points to a null-terminated wide string.
// Parameter num_chars may additionally limit the number // Parameter num_chars may additionally limit the number
// of wchar_t characters processed. -1 is used when the entire string // of wchar_t characters processed. -1 is used when the entire string
// should be processed. // should be processed.
// If the string contains code points that are not valid Unicode code points // If the string contains code points that are not valid Unicode code points
// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output // (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding // as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
// and contains invalid UTF-16 surrogate pairs, values in those pairs // and contains invalid UTF-16 surrogate pairs, values in those pairs
// will be encoded as individual Unicode characters from Basic Normal Plane. // will be encoded as individual Unicode characters from Basic Normal Plane.
std::string WideStringToUtf8(const wchar_t* str, int num_chars) { std::string WideStringToUtf8(const wchar_t* str, int num_chars) {
if (num_chars == -1) if (num_chars == -1) num_chars = static_cast<int>(wcslen(str));
num_chars = static_cast<int>(wcslen(str));
::std::stringstream stream; ::std::stringstream stream;
for (int i = 0; i < num_chars; ++i) { for (int i = 0; i < num_chars; ++i) {
uint32_t unicode_code_point; uint32_t unicode_code_point;
if (str[i] == L'\0') { if (str[i] == L'\0') {
break; break;
} else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i], unicode_code_point =
str[i + 1]); CreateCodePointFromUtf16SurrogatePair(str[i], str[i + 1]);
i++; i++;
} else { } else {
unicode_code_point = static_cast<uint32_t>(str[i]); unicode_code_point = static_cast<uint32_t>(str[i]);
} }
stream << CodePointToUtf8(unicode_code_point); stream << CodePointToUtf8(unicode_code_point);
} }
return StringStreamToString(&stream); return StringStreamToString(&stream);
} }
// Converts a wide C string to an std::string using the UTF-8 encoding. // Converts a wide C string to an std::string using the UTF-8 encoding.
// NULL will be converted to "(null)". // NULL will be converted to "(null)".
std::string String::ShowWideCString(const wchar_t * wide_c_str) { std::string String::ShowWideCString(const wchar_t* wide_c_str) {
if (wide_c_str == nullptr) return "(null)"; if (wide_c_str == nullptr) return "(null)";
return internal::WideStringToUtf8(wide_c_str, -1); return internal::WideStringToUtf8(wide_c_str, -1);
} }
// Compares two wide C strings. Returns true if and only if they have the // Compares two wide C strings. Returns true if and only if they have the
// same content. // same content.
// //
// Unlike wcscmp(), this function can handle NULL argument(s). A NULL // Unlike wcscmp(), this function can handle NULL argument(s). A NULL
// C string is considered different to any non-NULL C string, // C string is considered different to any non-NULL C string,
// including the empty string. // including the empty string.
bool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) { bool String::WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs) {
if (lhs == nullptr) return rhs == nullptr; if (lhs == nullptr) return rhs == nullptr;
if (rhs == nullptr) return false; if (rhs == nullptr) return false;
return wcscmp(lhs, rhs) == 0; return wcscmp(lhs, rhs) == 0;
} }
// Helper function for *_STREQ on wide strings. // Helper function for *_STREQ on wide strings.
AssertionResult CmpHelperSTREQ(const char* lhs_expression, AssertionResult CmpHelperSTREQ(const char* lhs_expression,
const char* rhs_expression, const char* rhs_expression, const wchar_t* lhs,
const wchar_t* lhs,
const wchar_t* rhs) { const wchar_t* rhs) {
if (String::WideCStringEquals(lhs, rhs)) { if (String::WideCStringEquals(lhs, rhs)) {
return AssertionSuccess(); return AssertionSuccess();
} }
return EqFailure(lhs_expression, return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs),
rhs_expression, PrintToString(rhs), false);
PrintToString(lhs),
PrintToString(rhs),
false);
} }
// Helper function for *_STRNE on wide strings. // Helper function for *_STRNE on wide strings.
AssertionResult CmpHelperSTRNE(const char* s1_expression, AssertionResult CmpHelperSTRNE(const char* s1_expression,
const char* s2_expression, const char* s2_expression, const wchar_t* s1,
const wchar_t* s1,
const wchar_t* s2) { const wchar_t* s2) {
if (!String::WideCStringEquals(s1, s2)) { if (!String::WideCStringEquals(s1, s2)) {
return AssertionSuccess(); return AssertionSuccess();
} }
return AssertionFailure() << "Expected: (" << s1_expression << ") != (" return AssertionFailure()
<< s2_expression << "), actual: " << "Expected: (" << s1_expression << ") != (" << s2_expression
<< PrintToString(s1) << "), actual: " << PrintToString(s1) << " vs " << PrintToString(s2);
<< " vs " << PrintToString(s2);
} }
// Compares two C strings, ignoring case. Returns true if and only if they have // Compares two C strings, ignoring case. Returns true if and only if they have
// the same content. // the same content.
// //
// Unlike strcasecmp(), this function can handle NULL argument(s). A // Unlike strcasecmp(), this function can handle NULL argument(s). A
// NULL C string is considered different to any non-NULL C string, // NULL C string is considered different to any non-NULL C string,
// including the empty string. // including the empty string.
bool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) { bool String::CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
if (lhs == nullptr) return rhs == nullptr; if (lhs == nullptr) return rhs == nullptr;
if (rhs == nullptr) return false; if (rhs == nullptr) return false;
return posix::StrCaseCmp(lhs, rhs) == 0; return posix::StrCaseCmp(lhs, rhs) == 0;
} }
// Compares two wide C strings, ignoring case. Returns true if and only if they // Compares two wide C strings, ignoring case. Returns true if and only if they
// have the same content. // have the same content.
// //
// Unlike wcscasecmp(), this function can handle NULL argument(s). // Unlike wcscasecmp(), this function can handle NULL argument(s).
// A NULL C string is considered different to any non-NULL wide C string, // A NULL C string is considered different to any non-NULL wide C string,
skipping to change at line 2119 skipping to change at line 2131
do { do {
left = towlower(static_cast<wint_t>(*lhs++)); left = towlower(static_cast<wint_t>(*lhs++));
right = towlower(static_cast<wint_t>(*rhs++)); right = towlower(static_cast<wint_t>(*rhs++));
} while (left && left == right); } while (left && left == right);
return left == right; return left == right;
#endif // OS selector #endif // OS selector
} }
// Returns true if and only if str ends with the given suffix, ignoring case. // Returns true if and only if str ends with the given suffix, ignoring case.
// Any string is considered to end with an empty suffix. // Any string is considered to end with an empty suffix.
bool String::EndsWithCaseInsensitive( bool String::EndsWithCaseInsensitive(const std::string& str,
const std::string& str, const std::string& suffix) { const std::string& suffix) {
const size_t str_len = str.length(); const size_t str_len = str.length();
const size_t suffix_len = suffix.length(); const size_t suffix_len = suffix.length();
return (str_len >= suffix_len) && return (str_len >= suffix_len) &&
CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len, CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len,
suffix.c_str()); suffix.c_str());
} }
// Formats an int value as "%02d". // Formats an int value as "%02d".
std::string String::FormatIntWidth2(int value) { std::string String::FormatIntWidth2(int value) {
return FormatIntWidthN(value, 2); return FormatIntWidthN(value, 2);
skipping to change at line 2203 skipping to change at line 2215
} // namespace internal } // namespace internal
// class TestResult // class TestResult
// Creates an empty TestResult. // Creates an empty TestResult.
TestResult::TestResult() TestResult::TestResult()
: death_test_count_(0), start_timestamp_(0), elapsed_time_(0) {} : death_test_count_(0), start_timestamp_(0), elapsed_time_(0) {}
// D'tor. // D'tor.
TestResult::~TestResult() { TestResult::~TestResult() {}
}
// Returns the i-th test part result among all the results. i can // Returns the i-th test part result among all the results. i can
// range from 0 to total_part_count() - 1. If i is not in that range, // range from 0 to total_part_count() - 1. If i is not in that range,
// aborts the program. // aborts the program.
const TestPartResult& TestResult::GetTestPartResult(int i) const { const TestPartResult& TestResult::GetTestPartResult(int i) const {
if (i < 0 || i >= total_part_count()) if (i < 0 || i >= total_part_count()) internal::posix::Abort();
internal::posix::Abort();
return test_part_results_.at(static_cast<size_t>(i)); return test_part_results_.at(static_cast<size_t>(i));
} }
// Returns the i-th test property. i can range from 0 to // Returns the i-th test property. i can range from 0 to
// test_property_count() - 1. If i is not in that range, aborts the // test_property_count() - 1. If i is not in that range, aborts the
// program. // program.
const TestProperty& TestResult::GetTestProperty(int i) const { const TestProperty& TestResult::GetTestProperty(int i) const {
if (i < 0 || i >= test_property_count()) if (i < 0 || i >= test_property_count()) internal::posix::Abort();
internal::posix::Abort();
return test_properties_.at(static_cast<size_t>(i)); return test_properties_.at(static_cast<size_t>(i));
} }
// Clears the test part results. // Clears the test part results.
void TestResult::ClearTestPartResults() { void TestResult::ClearTestPartResults() { test_part_results_.clear(); }
test_part_results_.clear();
}
// Adds a test part result to the list. // Adds a test part result to the list.
void TestResult::AddTestPartResult(const TestPartResult& test_part_result) { void TestResult::AddTestPartResult(const TestPartResult& test_part_result) {
test_part_results_.push_back(test_part_result); test_part_results_.push_back(test_part_result);
} }
// Adds a test property to the list. If a property with the same key as the // Adds a test property to the list. If a property with the same key as the
// supplied property is already represented, the value of this test_property // supplied property is already represented, the value of this test_property
// replaces the old value for that key. // replaces the old value for that key.
void TestResult::RecordProperty(const std::string& xml_element, void TestResult::RecordProperty(const std::string& xml_element,
skipping to change at line 2256 skipping to change at line 2263
if (property_with_matching_key == test_properties_.end()) { if (property_with_matching_key == test_properties_.end()) {
test_properties_.push_back(test_property); test_properties_.push_back(test_property);
return; return;
} }
property_with_matching_key->SetValue(test_property.value()); property_with_matching_key->SetValue(test_property.value());
} }
// The list of reserved attributes used in the <testsuites> element of XML // The list of reserved attributes used in the <testsuites> element of XML
// output. // output.
static const char* const kReservedTestSuitesAttributes[] = { static const char* const kReservedTestSuitesAttributes[] = {
"disabled", "disabled", "errors", "failures", "name",
"errors", "random_seed", "tests", "time", "timestamp"};
"failures",
"name",
"random_seed",
"tests",
"time",
"timestamp"
};
// The list of reserved attributes used in the <testsuite> element of XML // The list of reserved attributes used in the <testsuite> element of XML
// output. // output.
static const char* const kReservedTestSuiteAttributes[] = { static const char* const kReservedTestSuiteAttributes[] = {
"disabled", "errors", "failures", "name", "disabled", "errors", "failures", "name",
"tests", "time", "timestamp", "skipped"}; "tests", "time", "timestamp", "skipped"};
// The list of reserved attributes used in the <testcase> element of XML output. // The list of reserved attributes used in the <testcase> element of XML output.
static const char* const kReservedTestCaseAttributes[] = { static const char* const kReservedTestCaseAttributes[] = {
"classname", "name", "status", "time", "type_param", "classname", "name", "status", "time",
"value_param", "file", "line"}; "type_param", "value_param", "file", "line"};
// Use a slightly different set for allowed output to ensure existing tests can // Use a slightly different set for allowed output to ensure existing tests can
// still RecordProperty("result") or "RecordProperty(timestamp") // still RecordProperty("result") or "RecordProperty(timestamp")
static const char* const kReservedOutputTestCaseAttributes[] = { static const char* const kReservedOutputTestCaseAttributes[] = {
"classname", "name", "status", "time", "type_param", "classname", "name", "status", "time", "type_param",
"value_param", "file", "line", "result", "timestamp"}; "value_param", "file", "line", "result", "timestamp"};
template <size_t kSize> template <size_t kSize>
std::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) { std::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) {
return std::vector<std::string>(array, array + kSize); return std::vector<std::string>(array, array + kSize);
skipping to change at line 2337 skipping to change at line 2337
} }
word_list << "'" << words[i] << "'"; word_list << "'" << words[i] << "'";
} }
return word_list.GetString(); return word_list.GetString();
} }
static bool ValidateTestPropertyName( static bool ValidateTestPropertyName(
const std::string& property_name, const std::string& property_name,
const std::vector<std::string>& reserved_names) { const std::vector<std::string>& reserved_names) {
if (std::find(reserved_names.begin(), reserved_names.end(), property_name) != if (std::find(reserved_names.begin(), reserved_names.end(), property_name) !=
reserved_names.end()) { reserved_names.end()) {
ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name
<< " (" << FormatWordList(reserved_names) << " (" << FormatWordList(reserved_names)
<< " are reserved by " << GTEST_NAME_ << ")"; << " are reserved by " << GTEST_NAME_ << ")";
return false; return false;
} }
return true; return true;
} }
// Adds a failure if the key is a reserved attribute of the element named // Adds a failure if the key is a reserved attribute of the element named
// xml_element. Returns true if the property is valid. // xml_element. Returns true if the property is valid.
skipping to change at line 2375 skipping to change at line 2375
} }
// Returns true if and only if the test was skipped. // Returns true if and only if the test was skipped.
bool TestResult::Skipped() const { bool TestResult::Skipped() const {
return !Failed() && CountIf(test_part_results_, TestPartSkipped) > 0; return !Failed() && CountIf(test_part_results_, TestPartSkipped) > 0;
} }
// Returns true if and only if the test failed. // Returns true if and only if the test failed.
bool TestResult::Failed() const { bool TestResult::Failed() const {
for (int i = 0; i < total_part_count(); ++i) { for (int i = 0; i < total_part_count(); ++i) {
if (GetTestPartResult(i).failed()) if (GetTestPartResult(i).failed()) return true;
return true;
} }
return false; return false;
} }
// Returns true if and only if the test part fatally failed. // Returns true if and only if the test part fatally failed.
static bool TestPartFatallyFailed(const TestPartResult& result) { static bool TestPartFatallyFailed(const TestPartResult& result) {
return result.fatally_failed(); return result.fatally_failed();
} }
// Returns true if and only if the test fatally failed. // Returns true if and only if the test fatally failed.
skipping to change at line 2417 skipping to change at line 2416
// Returns the number of the test properties. // Returns the number of the test properties.
int TestResult::test_property_count() const { int TestResult::test_property_count() const {
return static_cast<int>(test_properties_.size()); return static_cast<int>(test_properties_.size());
} }
// class Test // class Test
// Creates a Test object. // Creates a Test object.
// The c'tor saves the states of all flags. // The c'tor saves the states of all flags.
Test::Test() Test::Test() : gtest_flag_saver_(new GTEST_FLAG_SAVER_) {}
: gtest_flag_saver_(new GTEST_FLAG_SAVER_) {
}
// The d'tor restores the states of all flags. The actual work is // The d'tor restores the states of all flags. The actual work is
// done by the d'tor of the gtest_flag_saver_ field, and thus not // done by the d'tor of the gtest_flag_saver_ field, and thus not
// visible here. // visible here.
Test::~Test() { Test::~Test() {}
}
// Sets up the test fixture. // Sets up the test fixture.
// //
// A sub-class may override this. // A sub-class may override this.
void Test::SetUp() { void Test::SetUp() {}
}
// Tears down the test fixture. // Tears down the test fixture.
// //
// A sub-class may override this. // A sub-class may override this.
void Test::TearDown() { void Test::TearDown() {}
}
// Allows user supplied key value pairs to be recorded for later output. // Allows user supplied key value pairs to be recorded for later output.
void Test::RecordProperty(const std::string& key, const std::string& value) { void Test::RecordProperty(const std::string& key, const std::string& value) {
UnitTest::GetInstance()->RecordProperty(key, value); UnitTest::GetInstance()->RecordProperty(key, value);
} }
// Allows user supplied key value pairs to be recorded for later output. // Allows user supplied key value pairs to be recorded for later output.
void Test::RecordProperty(const std::string& key, int value) { void Test::RecordProperty(const std::string& key, int value) {
Message value_message; Message value_message;
value_message << value; value_message << value;
skipping to change at line 2542 skipping to change at line 2536
#if GTEST_HAS_SEH #if GTEST_HAS_SEH
// Adds an "exception thrown" fatal failure to the current test. This // Adds an "exception thrown" fatal failure to the current test. This
// function returns its result via an output parameter pointer because VC++ // function returns its result via an output parameter pointer because VC++
// prohibits creation of objects with destructors on stack in functions // prohibits creation of objects with destructors on stack in functions
// using __try (see error C2712). // using __try (see error C2712).
static std::string* FormatSehExceptionMessage(DWORD exception_code, static std::string* FormatSehExceptionMessage(DWORD exception_code,
const char* location) { const char* location) {
Message message; Message message;
message << "SEH exception with code 0x" << std::setbase(16) << message << "SEH exception with code 0x" << std::setbase(16) << exception_code
exception_code << std::setbase(10) << " thrown in " << location << "."; << std::setbase(10) << " thrown in " << location << ".";
return new std::string(message.GetString()); return new std::string(message.GetString());
} }
#endif // GTEST_HAS_SEH #endif // GTEST_HAS_SEH
namespace internal { namespace internal {
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
skipping to change at line 2586 skipping to change at line 2580
// We put these helper functions in the internal namespace as IBM's xlC // We put these helper functions in the internal namespace as IBM's xlC
// compiler rejects the code if they were declared static. // compiler rejects the code if they were declared static.
// Runs the given method and handles SEH exceptions it throws, when // Runs the given method and handles SEH exceptions it throws, when
// SEH is supported; returns the 0-value for type Result in case of an // SEH is supported; returns the 0-value for type Result in case of an
// SEH exception. (Microsoft compilers cannot handle SEH and C++ // SEH exception. (Microsoft compilers cannot handle SEH and C++
// exceptions in the same function. Therefore, we provide a separate // exceptions in the same function. Therefore, we provide a separate
// wrapper function for handling SEH exceptions.) // wrapper function for handling SEH exceptions.)
template <class T, typename Result> template <class T, typename Result>
Result HandleSehExceptionsInMethodIfSupported( Result HandleSehExceptionsInMethodIfSupported(T* object, Result (T::*method)(),
T* object, Result (T::*method)(), const char* location) { const char* location) {
#if GTEST_HAS_SEH #if GTEST_HAS_SEH
__try { __try {
return (object->*method)(); return (object->*method)();
} __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT } __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT
GetExceptionCode())) { GetExceptionCode())) {
// We create the exception message on the heap because VC++ prohibits // We create the exception message on the heap because VC++ prohibits
// creation of objects with destructors on stack in functions using __try // creation of objects with destructors on stack in functions using __try
// (see error C2712). // (see error C2712).
std::string* exception_message = FormatSehExceptionMessage( std::string* exception_message =
GetExceptionCode(), location); FormatSehExceptionMessage(GetExceptionCode(), location);
internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure, internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure,
*exception_message); *exception_message);
delete exception_message; delete exception_message;
return static_cast<Result>(0); return static_cast<Result>(0);
} }
#else #else
(void)location; (void)location;
return (object->*method)(); return (object->*method)();
#endif // GTEST_HAS_SEH #endif // GTEST_HAS_SEH
} }
// Runs the given method and catches and reports C++ and/or SEH-style // Runs the given method and catches and reports C++ and/or SEH-style
// exceptions, if they are supported; returns the 0-value for type // exceptions, if they are supported; returns the 0-value for type
// Result in case of an SEH exception. // Result in case of an SEH exception.
template <class T, typename Result> template <class T, typename Result>
Result HandleExceptionsInMethodIfSupported( Result HandleExceptionsInMethodIfSupported(T* object, Result (T::*method)(),
T* object, Result (T::*method)(), const char* location) { const char* location) {
// NOTE: The user code can affect the way in which Google Test handles // NOTE: The user code can affect the way in which Google Test handles
// exceptions by setting GTEST_FLAG(catch_exceptions), but only before // exceptions by setting GTEST_FLAG(catch_exceptions), but only before
// RUN_ALL_TESTS() starts. It is technically possible to check the flag // RUN_ALL_TESTS() starts. It is technically possible to check the flag
// after the exception is caught and either report or re-throw the // after the exception is caught and either report or re-throw the
// exception based on the flag's value: // exception based on the flag's value:
// //
// try { // try {
// // Perform the test method. // // Perform the test method.
// } catch (...) { // } catch (...) {
// if (GTEST_FLAG(catch_exceptions)) // if (GTEST_FLAG_GET(catch_exceptions))
// // Report the exception as failure. // // Report the exception as failure.
// else // else
// throw; // Re-throws the original exception. // throw; // Re-throws the original exception.
// } // }
// //
// However, the purpose of this flag is to allow the program to drop into // However, the purpose of this flag is to allow the program to drop into
// the debugger when the exception is thrown. On most platforms, once the // the debugger when the exception is thrown. On most platforms, once the
// control enters the catch block, the exception origin information is // control enters the catch block, the exception origin information is
// lost and the debugger will stop the program at the point of the // lost and the debugger will stop the program at the point of the
// re-throw in this function -- instead of at the point of the original // re-throw in this function -- instead of at the point of the original
skipping to change at line 2680 skipping to change at line 2674
void Test::Run() { void Test::Run() {
if (!HasSameFixtureClass()) return; if (!HasSameFixtureClass()) return;
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()"); internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()");
// We will run the test only if SetUp() was successful and didn't call // We will run the test only if SetUp() was successful and didn't call
// GTEST_SKIP(). // GTEST_SKIP().
if (!HasFatalFailure() && !IsSkipped()) { if (!HasFatalFailure() && !IsSkipped()) {
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(this, &Test::TestBody,
this, &Test::TestBody, "the test body"); "the test body");
} }
// However, we want to clean up as much as possible. Hence we will // However, we want to clean up as much as possible. Hence we will
// always call TearDown(), even if SetUp() or the test body has // always call TearDown(), even if SetUp() or the test body has
// failed. // failed.
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(this, &Test::TearDown,
this, &Test::TearDown, "TearDown()"); "TearDown()");
} }
// Returns true if and only if the current test has a fatal failure. // Returns true if and only if the current test has a fatal failure.
bool Test::HasFatalFailure() { bool Test::HasFatalFailure() {
return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure(); return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure();
} }
// Returns true if and only if the current test has a non-fatal failure. // Returns true if and only if the current test has a non-fatal failure.
bool Test::HasNonfatalFailure() { bool Test::HasNonfatalFailure() {
return internal::GetUnitTestImpl()->current_test_result()-> return internal::GetUnitTestImpl()
HasNonfatalFailure(); ->current_test_result()
->HasNonfatalFailure();
} }
// Returns true if and only if the current test was skipped. // Returns true if and only if the current test was skipped.
bool Test::IsSkipped() { bool Test::IsSkipped() {
return internal::GetUnitTestImpl()->current_test_result()->Skipped(); return internal::GetUnitTestImpl()->current_test_result()->Skipped();
} }
// class TestInfo // class TestInfo
// Constructs a TestInfo object. It assumes ownership of the test factory // Constructs a TestInfo object. It assumes ownership of the test factory
skipping to change at line 2800 skipping to change at line 2795
// This is used for implementation of the TestSuite class only. We put // This is used for implementation of the TestSuite class only. We put
// it in the anonymous namespace to prevent polluting the outer // it in the anonymous namespace to prevent polluting the outer
// namespace. // namespace.
// //
// TestNameIs is copyable. // TestNameIs is copyable.
class TestNameIs { class TestNameIs {
public: public:
// Constructor. // Constructor.
// //
// TestNameIs has NO default constructor. // TestNameIs has NO default constructor.
explicit TestNameIs(const char* name) explicit TestNameIs(const char* name) : name_(name) {}
: name_(name) {}
// Returns true if and only if the test name of test_info matches name_. // Returns true if and only if the test name of test_info matches name_.
bool operator()(const TestInfo * test_info) const { bool operator()(const TestInfo* test_info) const {
return test_info && test_info->name() == name_; return test_info && test_info->name() == name_;
} }
private: private:
std::string name_; std::string name_;
}; };
} // namespace } // namespace
namespace internal { namespace internal {
skipping to change at line 2832 skipping to change at line 2826
type_parameterized_test_registry_.CheckForInstantiations(); type_parameterized_test_registry_.CheckForInstantiations();
parameterized_tests_registered_ = true; parameterized_tests_registered_ = true;
} }
} }
} // namespace internal } // namespace internal
// Creates the test object, runs it, records its result, and then // Creates the test object, runs it, records its result, and then
// deletes it. // deletes it.
void TestInfo::Run() { void TestInfo::Run() {
if (!should_run_) return; TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
if (!should_run_) {
if (is_disabled_ && matches_filter_) repeater->OnTestDisabled(*this);
return;
}
// Tells UnitTest where to store test result. // Tells UnitTest where to store test result.
internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
impl->set_current_test_info(this); impl->set_current_test_info(this);
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
// Notifies the unit test event listeners that a test is about to start. // Notifies the unit test event listeners that a test is about to start.
repeater->OnTestStart(*this); repeater->OnTestStart(*this);
result_.set_start_timestamp(internal::GetTimeInMillis()); result_.set_start_timestamp(internal::GetTimeInMillis());
internal::Timer timer; internal::Timer timer;
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
// Creates the test object. // Creates the test object.
Test* const test = internal::HandleExceptionsInMethodIfSupported( Test* const test = internal::HandleExceptionsInMethodIfSupported(
factory_, &internal::TestFactoryBase::CreateTest, factory_, &internal::TestFactoryBase::CreateTest,
"the test fixture's constructor"); "the test fixture's constructor");
// Runs the test if the constructor didn't generate a fatal failure or invoke // Runs the test if the constructor didn't generate a fatal failure or invoke
// GTEST_SKIP(). // GTEST_SKIP().
// Note that the object will not be null // Note that the object will not be null
skipping to change at line 3010 skipping to change at line 3004
repeater->OnTestSuiteStart(*this); repeater->OnTestSuiteStart(*this);
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
repeater->OnTestCaseStart(*this); repeater->OnTestCaseStart(*this);
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(
this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()"); this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()");
const bool skip_all = ad_hoc_test_result().Failed();
start_timestamp_ = internal::GetTimeInMillis(); start_timestamp_ = internal::GetTimeInMillis();
internal::Timer timer; internal::Timer timer;
for (int i = 0; i < total_test_count(); i++) { for (int i = 0; i < total_test_count(); i++) {
GetMutableTestInfo(i)->Run(); if (skip_all) {
if (GTEST_FLAG(fail_fast) && GetMutableTestInfo(i)->result()->Failed()) { GetMutableTestInfo(i)->Skip();
} else {
GetMutableTestInfo(i)->Run();
}
if (GTEST_FLAG_GET(fail_fast) &&
GetMutableTestInfo(i)->result()->Failed()) {
for (int j = i + 1; j < total_test_count(); j++) { for (int j = i + 1; j < total_test_count(); j++) {
GetMutableTestInfo(j)->Skip(); GetMutableTestInfo(j)->Skip();
} }
break; break;
} }
} }
elapsed_time_ = timer.Elapsed(); elapsed_time_ = timer.Elapsed();
impl->os_stack_trace_getter()->UponLeavingGTest(); impl->os_stack_trace_getter()->UponLeavingGTest();
internal::HandleExceptionsInMethodIfSupported( internal::HandleExceptionsInMethodIfSupported(
skipping to change at line 3090 skipping to change at line 3091
for (size_t i = 0; i < test_indices_.size(); i++) { for (size_t i = 0; i < test_indices_.size(); i++) {
test_indices_[i] = static_cast<int>(i); test_indices_[i] = static_cast<int>(i);
} }
} }
// Formats a countable noun. Depending on its quantity, either the // Formats a countable noun. Depending on its quantity, either the
// singular form or the plural form is used. e.g. // singular form or the plural form is used. e.g.
// //
// FormatCountableNoun(1, "formula", "formuli") returns "1 formula". // FormatCountableNoun(1, "formula", "formuli") returns "1 formula".
// FormatCountableNoun(5, "book", "books") returns "5 books". // FormatCountableNoun(5, "book", "books") returns "5 books".
static std::string FormatCountableNoun(int count, static std::string FormatCountableNoun(int count, const char* singular_form,
const char * singular_form, const char* plural_form) {
const char * plural_form) {
return internal::StreamableToString(count) + " " + return internal::StreamableToString(count) + " " +
(count == 1 ? singular_form : plural_form); (count == 1 ? singular_form : plural_form);
} }
// Formats the count of tests. // Formats the count of tests.
static std::string FormatTestCount(int test_count) { static std::string FormatTestCount(int test_count) {
return FormatCountableNoun(test_count, "test", "tests"); return FormatCountableNoun(test_count, "test", "tests");
} }
// Formats the count of test suites. // Formats the count of test suites.
static std::string FormatTestSuiteCount(int test_suite_count) { static std::string FormatTestSuiteCount(int test_suite_count) {
return FormatCountableNoun(test_suite_count, "test suite", "test suites"); return FormatCountableNoun(test_suite_count, "test suite", "test suites");
} }
// Converts a TestPartResult::Type enum to human-friendly string // Converts a TestPartResult::Type enum to human-friendly string
// representation. Both kNonFatalFailure and kFatalFailure are translated // representation. Both kNonFatalFailure and kFatalFailure are translated
// to "Failure", as the user usually doesn't care about the difference // to "Failure", as the user usually doesn't care about the difference
// between the two when viewing the test result. // between the two when viewing the test result.
static const char * TestPartResultTypeToString(TestPartResult::Type type) { static const char* TestPartResultTypeToString(TestPartResult::Type type) {
switch (type) { switch (type) {
case TestPartResult::kSkip: case TestPartResult::kSkip:
return "Skipped\n"; return "Skipped\n";
case TestPartResult::kSuccess: case TestPartResult::kSuccess:
return "Success"; return "Success";
case TestPartResult::kNonFatalFailure: case TestPartResult::kNonFatalFailure:
case TestPartResult::kFatalFailure: case TestPartResult::kFatalFailure:
#ifdef _MSC_VER #ifdef _MSC_VER
return "error: "; return "error: ";
skipping to change at line 3138 skipping to change at line 3138
} }
namespace internal { namespace internal {
namespace { namespace {
enum class GTestColor { kDefault, kRed, kGreen, kYellow }; enum class GTestColor { kDefault, kRed, kGreen, kYellow };
} // namespace } // namespace
// Prints a TestPartResult to an std::string. // Prints a TestPartResult to an std::string.
static std::string PrintTestPartResultToString( static std::string PrintTestPartResultToString(
const TestPartResult& test_part_result) { const TestPartResult& test_part_result) {
return (Message() return (Message() << internal::FormatFileLocation(
<< internal::FormatFileLocation(test_part_result.file_name(), test_part_result.file_name(),
test_part_result.line_number()) test_part_result.line_number())
<< " " << TestPartResultTypeToString(test_part_result.type()) << " "
<< test_part_result.message()).GetString(); << TestPartResultTypeToString(test_part_result.type())
<< test_part_result.message())
.GetString();
} }
// Prints a TestPartResult. // Prints a TestPartResult.
static void PrintTestPartResult(const TestPartResult& test_part_result) { static void PrintTestPartResult(const TestPartResult& test_part_result) {
const std::string& result = const std::string& result = PrintTestPartResultToString(test_part_result);
PrintTestPartResultToString(test_part_result);
printf("%s\n", result.c_str()); printf("%s\n", result.c_str());
fflush(stdout); fflush(stdout);
// If the test program runs in Visual Studio or a debugger, the // If the test program runs in Visual Studio or a debugger, the
// following statements add the test part result message to the Output // following statements add the test part result message to the Output
// window such that the user can double-click on it to jump to the // window such that the user can double-click on it to jump to the
// corresponding source code location; otherwise they do nothing. // corresponding source code location; otherwise they do nothing.
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
// We don't call OutputDebugString*() on Windows Mobile, as printing // We don't call OutputDebugString*() on Windows Mobile, as printing
// to stdout is done by OutputDebugString() there already - we don't // to stdout is done by OutputDebugString() there already - we don't
// want the same message printed twice. // want the same message printed twice.
::OutputDebugStringA(result.c_str()); ::OutputDebugStringA(result.c_str());
::OutputDebugStringA("\n"); ::OutputDebugStringA("\n");
#endif #endif
} }
// class PrettyUnitTestResultPrinter // class PrettyUnitTestResultPrinter
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \ #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \
!GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW
// Returns the character attribute for the given color. // Returns the character attribute for the given color.
static WORD GetColorAttribute(GTestColor color) { static WORD GetColorAttribute(GTestColor color) {
switch (color) { switch (color) {
case GTestColor::kRed: case GTestColor::kRed:
return FOREGROUND_RED; return FOREGROUND_RED;
case GTestColor::kGreen: case GTestColor::kGreen:
return FOREGROUND_GREEN; return FOREGROUND_GREEN;
case GTestColor::kYellow: case GTestColor::kYellow:
return FOREGROUND_RED | FOREGROUND_GREEN; return FOREGROUND_RED | FOREGROUND_GREEN;
default: return 0; default:
return 0;
} }
} }
static int GetBitOffset(WORD color_mask) { static int GetBitOffset(WORD color_mask) {
if (color_mask == 0) return 0; if (color_mask == 0) return 0;
int bitOffset = 0; int bitOffset = 0;
while ((color_mask & 1) == 0) { while ((color_mask & 1) == 0) {
color_mask >>= 1; color_mask >>= 1;
++bitOffset; ++bitOffset;
skipping to change at line 3233 skipping to change at line 3235
return "3"; return "3";
default: default:
return nullptr; return nullptr;
} }
} }
#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE #endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
// Returns true if and only if Google Test should use colors in the output. // Returns true if and only if Google Test should use colors in the output.
bool ShouldUseColor(bool stdout_is_tty) { bool ShouldUseColor(bool stdout_is_tty) {
const char* const gtest_color = GTEST_FLAG(color).c_str(); std::string c = GTEST_FLAG_GET(color);
const char* const gtest_color = c.c_str();
if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) { if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) {
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
// On Windows the TERM variable is usually not set, but the // On Windows the TERM variable is usually not set, but the
// console there does support colors. // console there does support colors.
return stdout_is_tty; return stdout_is_tty;
#else #else
// On non-Windows platforms, we rely on the TERM variable. // On non-Windows platforms, we rely on the TERM variable.
const char* const term = posix::GetEnv("TERM"); const char* const term = posix::GetEnv("TERM");
const bool term_supports_color = const bool term_supports_color =
skipping to change at line 3260 skipping to change at line 3263
String::CStringEquals(term, "tmux-256color") || String::CStringEquals(term, "tmux-256color") ||
String::CStringEquals(term, "rxvt-unicode") || String::CStringEquals(term, "rxvt-unicode") ||
String::CStringEquals(term, "rxvt-unicode-256color") || String::CStringEquals(term, "rxvt-unicode-256color") ||
String::CStringEquals(term, "linux") || String::CStringEquals(term, "linux") ||
String::CStringEquals(term, "cygwin"); String::CStringEquals(term, "cygwin");
return stdout_is_tty && term_supports_color; return stdout_is_tty && term_supports_color;
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
} }
return String::CaseInsensitiveCStringEquals(gtest_color, "yes") || return String::CaseInsensitiveCStringEquals(gtest_color, "yes") ||
String::CaseInsensitiveCStringEquals(gtest_color, "true") || String::CaseInsensitiveCStringEquals(gtest_color, "true") ||
String::CaseInsensitiveCStringEquals(gtest_color, "t") || String::CaseInsensitiveCStringEquals(gtest_color, "t") ||
String::CStringEquals(gtest_color, "1"); String::CStringEquals(gtest_color, "1");
// We take "yes", "true", "t", and "1" as meaning "yes". If the // We take "yes", "true", "t", and "1" as meaning "yes". If the
// value is neither one of these nor "auto", we treat it as "no" to // value is neither one of these nor "auto", we treat it as "no" to
// be conservative. // be conservative.
} }
// Helpers for printing colored strings to stdout. Note that on Windows, we // Helpers for printing colored strings to stdout. Note that on Windows, we
// cannot simply emit special characters and have the terminal change colors. // cannot simply emit special characters and have the terminal change colors.
// This routine must actually emit the characters rather than return a string // This routine must actually emit the characters rather than return a string
// that would be colored when printed, as can be done on Linux. // that would be colored when printed, as can be done on Linux.
GTEST_ATTRIBUTE_PRINTF_(2, 3) GTEST_ATTRIBUTE_PRINTF_(2, 3)
static void ColoredPrintf(GTestColor color, const char *fmt, ...) { static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \
GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM)
const bool use_color = AlwaysFalse();
#else
static const bool in_color_mode = static const bool in_color_mode =
ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);
const bool use_color = in_color_mode && (color != GTestColor::kDefault); const bool use_color = in_color_mode && (color != GTestColor::kDefault);
#endif // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS
if (!use_color) { if (!use_color) {
vprintf(fmt, args); vprintf(fmt, args);
va_end(args); va_end(args);
return; return;
} }
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \ #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \
!GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW
const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
// Gets the current text color. // Gets the current text color.
CONSOLE_SCREEN_BUFFER_INFO buffer_info; CONSOLE_SCREEN_BUFFER_INFO buffer_info;
GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
const WORD old_color_attrs = buffer_info.wAttributes; const WORD old_color_attrs = buffer_info.wAttributes;
const WORD new_color = GetNewColor(color, old_color_attrs); const WORD new_color = GetNewColor(color, old_color_attrs);
// We need to flush the stream buffers into the console before each // We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already // SetConsoleTextAttribute call lest it affect the text that is already
skipping to change at line 3365 skipping to change at line 3363
void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; void OnTestIterationStart(const UnitTest& unit_test, int iteration) override;
void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override;
void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {}
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseStart(const TestCase& test_case) override; void OnTestCaseStart(const TestCase& test_case) override;
#else #else
void OnTestSuiteStart(const TestSuite& test_suite) override; void OnTestSuiteStart(const TestSuite& test_suite) override;
#endif // OnTestCaseStart #endif // OnTestCaseStart
void OnTestStart(const TestInfo& test_info) override; void OnTestStart(const TestInfo& test_info) override;
void OnTestDisabled(const TestInfo& test_info) override;
void OnTestPartResult(const TestPartResult& result) override; void OnTestPartResult(const TestPartResult& result) override;
void OnTestEnd(const TestInfo& test_info) override; void OnTestEnd(const TestInfo& test_info) override;
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseEnd(const TestCase& test_case) override; void OnTestCaseEnd(const TestCase& test_case) override;
#else #else
void OnTestSuiteEnd(const TestSuite& test_suite) override; void OnTestSuiteEnd(const TestSuite& test_suite) override;
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override;
void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {}
void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override;
void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {}
private: private:
static void PrintFailedTests(const UnitTest& unit_test); static void PrintFailedTests(const UnitTest& unit_test);
static void PrintFailedTestSuites(const UnitTest& unit_test); static void PrintFailedTestSuites(const UnitTest& unit_test);
static void PrintSkippedTests(const UnitTest& unit_test); static void PrintSkippedTests(const UnitTest& unit_test);
}; };
// Fired before each iteration of tests starts. // Fired before each iteration of tests starts.
void PrettyUnitTestResultPrinter::OnTestIterationStart( void PrettyUnitTestResultPrinter::OnTestIterationStart(
const UnitTest& unit_test, int iteration) { const UnitTest& unit_test, int iteration) {
if (GTEST_FLAG(repeat) != 1) if (GTEST_FLAG_GET(repeat) != 1)
printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1); printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1);
const char* const filter = GTEST_FLAG(filter).c_str(); std::string f = GTEST_FLAG_GET(filter);
const char* const filter = f.c_str();
// Prints the filter if it's not *. This reminds the user that some // Prints the filter if it's not *. This reminds the user that some
// tests may be skipped. // tests may be skipped.
if (!String::CStringEquals(filter, kUniversalFilter)) { if (!String::CStringEquals(filter, kUniversalFilter)) {
ColoredPrintf(GTestColor::kYellow, "Note: %s filter = %s\n", GTEST_NAME_, ColoredPrintf(GTestColor::kYellow, "Note: %s filter = %s\n", GTEST_NAME_,
filter); filter);
} }
if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) {
const int32_t shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); const int32_t shard_index = Int32FromEnvOrDie(kTestShardIndex, -1);
ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %s.\n", ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %s.\n",
static_cast<int>(shard_index) + 1, static_cast<int>(shard_index) + 1,
internal::posix::GetEnv(kTestTotalShards)); internal::posix::GetEnv(kTestTotalShards));
} }
if (GTEST_FLAG(shuffle)) { if (GTEST_FLAG_GET(shuffle)) {
ColoredPrintf(GTestColor::kYellow, ColoredPrintf(GTestColor::kYellow,
"Note: Randomizing tests' orders with a seed of %d .\n", "Note: Randomizing tests' orders with a seed of %d .\n",
unit_test.random_seed()); unit_test.random_seed());
} }
ColoredPrintf(GTestColor::kGreen, "[==========] "); ColoredPrintf(GTestColor::kGreen, "[==========] ");
printf("Running %s from %s.\n", printf("Running %s from %s.\n",
FormatTestCount(unit_test.test_to_run_count()).c_str(), FormatTestCount(unit_test.test_to_run_count()).c_str(),
FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str());
fflush(stdout); fflush(stdout);
skipping to change at line 3463 skipping to change at line 3463
} }
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
ColoredPrintf(GTestColor::kGreen, "[ RUN ] "); ColoredPrintf(GTestColor::kGreen, "[ RUN ] ");
PrintTestName(test_info.test_suite_name(), test_info.name()); PrintTestName(test_info.test_suite_name(), test_info.name());
printf("\n"); printf("\n");
fflush(stdout); fflush(stdout);
} }
void PrettyUnitTestResultPrinter::OnTestDisabled(const TestInfo& test_info) {
ColoredPrintf(GTestColor::kYellow, "[ DISABLED ] ");
PrintTestName(test_info.test_suite_name(), test_info.name());
printf("\n");
fflush(stdout);
}
// Called after an assertion failure. // Called after an assertion failure.
void PrettyUnitTestResultPrinter::OnTestPartResult( void PrettyUnitTestResultPrinter::OnTestPartResult(
const TestPartResult& result) { const TestPartResult& result) {
switch (result.type()) { switch (result.type()) {
// If the test part succeeded, we don't need to do anything. // If the test part succeeded, we don't need to do anything.
case TestPartResult::kSuccess: case TestPartResult::kSuccess:
return; return;
default: default:
// Print failure message from the assertion // Print failure message from the assertion
// (e.g. expected this and got that). // (e.g. expected this and got that).
skipping to change at line 3487 skipping to change at line 3494
void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
if (test_info.result()->Passed()) { if (test_info.result()->Passed()) {
ColoredPrintf(GTestColor::kGreen, "[ OK ] "); ColoredPrintf(GTestColor::kGreen, "[ OK ] ");
} else if (test_info.result()->Skipped()) { } else if (test_info.result()->Skipped()) {
ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] ");
} else { } else {
ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); ColoredPrintf(GTestColor::kRed, "[ FAILED ] ");
} }
PrintTestName(test_info.test_suite_name(), test_info.name()); PrintTestName(test_info.test_suite_name(), test_info.name());
if (test_info.result()->Failed()) if (test_info.result()->Failed()) PrintFullTestCommentIfPresent(test_info);
PrintFullTestCommentIfPresent(test_info);
if (GTEST_FLAG(print_time)) { if (GTEST_FLAG_GET(print_time)) {
printf(" (%s ms)\n", internal::StreamableToString( printf(" (%s ms)\n",
test_info.result()->elapsed_time()).c_str()); internal::StreamableToString(test_info.result()->elapsed_time())
.c_str());
} else { } else {
printf("\n"); printf("\n");
} }
fflush(stdout); fflush(stdout);
} }
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) { void PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) {
if (!GTEST_FLAG(print_time)) return; if (!GTEST_FLAG_GET(print_time)) return;
const std::string counts = const std::string counts =
FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
ColoredPrintf(GTestColor::kGreen, "[----------] "); ColoredPrintf(GTestColor::kGreen, "[----------] ");
printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_case.name(), printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_case.name(),
internal::StreamableToString(test_case.elapsed_time()).c_str()); internal::StreamableToString(test_case.elapsed_time()).c_str());
fflush(stdout); fflush(stdout);
} }
#else #else
void PrettyUnitTestResultPrinter::OnTestSuiteEnd(const TestSuite& test_suite) { void PrettyUnitTestResultPrinter::OnTestSuiteEnd(const TestSuite& test_suite) {
if (!GTEST_FLAG(print_time)) return; if (!GTEST_FLAG_GET(print_time)) return;
const std::string counts = const std::string counts =
FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests"); FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests");
ColoredPrintf(GTestColor::kGreen, "[----------] "); ColoredPrintf(GTestColor::kGreen, "[----------] ");
printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_suite.name(), printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_suite.name(),
internal::StreamableToString(test_suite.elapsed_time()).c_str()); internal::StreamableToString(test_suite.elapsed_time()).c_str());
fflush(stdout); fflush(stdout);
} }
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
skipping to change at line 3608 skipping to change at line 3615
} }
} }
} }
void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) { int /*iteration*/) {
ColoredPrintf(GTestColor::kGreen, "[==========] "); ColoredPrintf(GTestColor::kGreen, "[==========] ");
printf("%s from %s ran.", printf("%s from %s ran.",
FormatTestCount(unit_test.test_to_run_count()).c_str(), FormatTestCount(unit_test.test_to_run_count()).c_str(),
FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str());
if (GTEST_FLAG(print_time)) { if (GTEST_FLAG_GET(print_time)) {
printf(" (%s ms total)", printf(" (%s ms total)",
internal::StreamableToString(unit_test.elapsed_time()).c_str()); internal::StreamableToString(unit_test.elapsed_time()).c_str());
} }
printf("\n"); printf("\n");
ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); ColoredPrintf(GTestColor::kGreen, "[ PASSED ] ");
printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str());
const int skipped_test_count = unit_test.skipped_test_count(); const int skipped_test_count = unit_test.skipped_test_count();
if (skipped_test_count > 0) { if (skipped_test_count > 0) {
ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] ");
printf("%s, listed below:\n", FormatTestCount(skipped_test_count).c_str()); printf("%s, listed below:\n", FormatTestCount(skipped_test_count).c_str());
PrintSkippedTests(unit_test); PrintSkippedTests(unit_test);
} }
if (!unit_test.Passed()) { if (!unit_test.Passed()) {
PrintFailedTests(unit_test); PrintFailedTests(unit_test);
PrintFailedTestSuites(unit_test); PrintFailedTestSuites(unit_test);
} }
int num_disabled = unit_test.reportable_disabled_test_count(); int num_disabled = unit_test.reportable_disabled_test_count();
if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { if (num_disabled && !GTEST_FLAG_GET(also_run_disabled_tests)) {
if (unit_test.Passed()) { if (unit_test.Passed()) {
printf("\n"); // Add a spacer if no FAILURE banner is displayed. printf("\n"); // Add a spacer if no FAILURE banner is displayed.
} }
ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n",
num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); num_disabled, num_disabled == 1 ? "TEST" : "TESTS");
} }
// Ensure that Google Test output is printed before, e.g., heapchecker output. // Ensure that Google Test output is printed before, e.g., heapchecker output.
fflush(stdout); fflush(stdout);
} }
skipping to change at line 3665 skipping to change at line 3672
int /*iteration*/) override {} int /*iteration*/) override {}
void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {}
void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {}
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseStart(const TestCase& /*test_case*/) override {} void OnTestCaseStart(const TestCase& /*test_case*/) override {}
#else #else
void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {}
#endif // OnTestCaseStart #endif // OnTestCaseStart
void OnTestStart(const TestInfo& /*test_info*/) override {} void OnTestStart(const TestInfo& /*test_info*/) override {}
void OnTestDisabled(const TestInfo& /*test_info*/) override {}
void OnTestPartResult(const TestPartResult& result) override; void OnTestPartResult(const TestPartResult& result) override;
void OnTestEnd(const TestInfo& test_info) override; void OnTestEnd(const TestInfo& test_info) override;
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseEnd(const TestCase& /*test_case*/) override {} void OnTestCaseEnd(const TestCase& /*test_case*/) override {}
#else #else
void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {}
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {}
skipping to change at line 3701 skipping to change at line 3709
fflush(stdout); fflush(stdout);
} }
} }
void BriefUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { void BriefUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
if (test_info.result()->Failed()) { if (test_info.result()->Failed()) {
ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); ColoredPrintf(GTestColor::kRed, "[ FAILED ] ");
PrintTestName(test_info.test_suite_name(), test_info.name()); PrintTestName(test_info.test_suite_name(), test_info.name());
PrintFullTestCommentIfPresent(test_info); PrintFullTestCommentIfPresent(test_info);
if (GTEST_FLAG(print_time)) { if (GTEST_FLAG_GET(print_time)) {
printf(" (%s ms)\n", printf(" (%s ms)\n",
internal::StreamableToString(test_info.result()->elapsed_time()) internal::StreamableToString(test_info.result()->elapsed_time())
.c_str()); .c_str());
} else { } else {
printf("\n"); printf("\n");
} }
fflush(stdout); fflush(stdout);
} }
} }
void BriefUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, void BriefUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) { int /*iteration*/) {
ColoredPrintf(GTestColor::kGreen, "[==========] "); ColoredPrintf(GTestColor::kGreen, "[==========] ");
printf("%s from %s ran.", printf("%s from %s ran.",
FormatTestCount(unit_test.test_to_run_count()).c_str(), FormatTestCount(unit_test.test_to_run_count()).c_str(),
FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str());
if (GTEST_FLAG(print_time)) { if (GTEST_FLAG_GET(print_time)) {
printf(" (%s ms total)", printf(" (%s ms total)",
internal::StreamableToString(unit_test.elapsed_time()).c_str()); internal::StreamableToString(unit_test.elapsed_time()).c_str());
} }
printf("\n"); printf("\n");
ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); ColoredPrintf(GTestColor::kGreen, "[ PASSED ] ");
printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str());
const int skipped_test_count = unit_test.skipped_test_count(); const int skipped_test_count = unit_test.skipped_test_count();
if (skipped_test_count > 0) { if (skipped_test_count > 0) {
ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] ");
printf("%s.\n", FormatTestCount(skipped_test_count).c_str()); printf("%s.\n", FormatTestCount(skipped_test_count).c_str());
} }
int num_disabled = unit_test.reportable_disabled_test_count(); int num_disabled = unit_test.reportable_disabled_test_count();
if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { if (num_disabled && !GTEST_FLAG_GET(also_run_disabled_tests)) {
if (unit_test.Passed()) { if (unit_test.Passed()) {
printf("\n"); // Add a spacer if no FAILURE banner is displayed. printf("\n"); // Add a spacer if no FAILURE banner is displayed.
} }
ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n",
num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); num_disabled, num_disabled == 1 ? "TEST" : "TESTS");
} }
// Ensure that Google Test output is printed before, e.g., heapchecker output. // Ensure that Google Test output is printed before, e.g., heapchecker output.
fflush(stdout); fflush(stdout);
} }
// End BriefUnitTestResultPrinter // End BriefUnitTestResultPrinter
// class TestEventRepeater // class TestEventRepeater
// //
// This class forwards events to other event listeners. // This class forwards events to other event listeners.
class TestEventRepeater : public TestEventListener { class TestEventRepeater : public TestEventListener {
public: public:
TestEventRepeater() : forwarding_enabled_(true) {} TestEventRepeater() : forwarding_enabled_(true) {}
~TestEventRepeater() override; ~TestEventRepeater() override;
void Append(TestEventListener *listener); void Append(TestEventListener* listener);
TestEventListener* Release(TestEventListener* listener); TestEventListener* Release(TestEventListener* listener);
// Controls whether events will be forwarded to listeners_. Set to false // Controls whether events will be forwarded to listeners_. Set to false
// in death test child processes. // in death test child processes.
bool forwarding_enabled() const { return forwarding_enabled_; } bool forwarding_enabled() const { return forwarding_enabled_; }
void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; } void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; }
void OnTestProgramStart(const UnitTest& unit_test) override; void OnTestProgramStart(const UnitTest& unit_test) override;
void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; void OnTestIterationStart(const UnitTest& unit_test, int iteration) override;
void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override;
void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) override; void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) override;
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseStart(const TestSuite& parameter) override; void OnTestCaseStart(const TestSuite& parameter) override;
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestSuiteStart(const TestSuite& parameter) override; void OnTestSuiteStart(const TestSuite& parameter) override;
void OnTestStart(const TestInfo& test_info) override; void OnTestStart(const TestInfo& test_info) override;
void OnTestDisabled(const TestInfo& test_info) override;
void OnTestPartResult(const TestPartResult& result) override; void OnTestPartResult(const TestPartResult& result) override;
void OnTestEnd(const TestInfo& test_info) override; void OnTestEnd(const TestInfo& test_info) override;
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestCaseEnd(const TestCase& parameter) override; void OnTestCaseEnd(const TestCase& parameter) override;
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
void OnTestSuiteEnd(const TestSuite& parameter) override; void OnTestSuiteEnd(const TestSuite& parameter) override;
void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override;
void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) override; void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) override;
void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override;
void OnTestProgramEnd(const UnitTest& unit_test) override; void OnTestProgramEnd(const UnitTest& unit_test) override;
private: private:
// Controls whether events will be forwarded to listeners_. Set to false // Controls whether events will be forwarded to listeners_. Set to false
// in death test child processes. // in death test child processes.
bool forwarding_enabled_; bool forwarding_enabled_;
// The list of listeners that receive events. // The list of listeners that receive events.
std::vector<TestEventListener*> listeners_; std::vector<TestEventListener*> listeners_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater); TestEventRepeater(const TestEventRepeater&) = delete;
TestEventRepeater& operator=(const TestEventRepeater&) = delete;
}; };
TestEventRepeater::~TestEventRepeater() { TestEventRepeater::~TestEventRepeater() {
ForEach(listeners_, Delete<TestEventListener>); ForEach(listeners_, Delete<TestEventListener>);
} }
void TestEventRepeater::Append(TestEventListener *listener) { void TestEventRepeater::Append(TestEventListener* listener) {
listeners_.push_back(listener); listeners_.push_back(listener);
} }
TestEventListener* TestEventRepeater::Release(TestEventListener *listener) { TestEventListener* TestEventRepeater::Release(TestEventListener* listener) {
for (size_t i = 0; i < listeners_.size(); ++i) { for (size_t i = 0; i < listeners_.size(); ++i) {
if (listeners_[i] == listener) { if (listeners_[i] == listener) {
listeners_.erase(listeners_.begin() + static_cast<int>(i)); listeners_.erase(listeners_.begin() + static_cast<int>(i));
return listener; return listener;
} }
} }
return nullptr; return nullptr;
} }
// Since most methods are very similar, use macros to reduce boilerplate. // Since most methods are very similar, use macros to reduce boilerplate.
// This defines a member that forwards the call to all listeners. // This defines a member that forwards the call to all listeners.
#define GTEST_REPEATER_METHOD_(Name, Type) \ #define GTEST_REPEATER_METHOD_(Name, Type) \
void TestEventRepeater::Name(const Type& parameter) { \ void TestEventRepeater::Name(const Type& parameter) { \
if (forwarding_enabled_) { \ if (forwarding_enabled_) { \
for (size_t i = 0; i < listeners_.size(); i++) { \ for (size_t i = 0; i < listeners_.size(); i++) { \
listeners_[i]->Name(parameter); \ listeners_[i]->Name(parameter); \
} \ } \
} \ } \
} }
// This defines a member that forwards the call to all listeners in reverse // This defines a member that forwards the call to all listeners in reverse
// order. // order.
#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \ #define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \
void TestEventRepeater::Name(const Type& parameter) { \ void TestEventRepeater::Name(const Type& parameter) { \
if (forwarding_enabled_) { \ if (forwarding_enabled_) { \
for (size_t i = listeners_.size(); i != 0; i--) { \ for (size_t i = listeners_.size(); i != 0; i--) { \
listeners_[i - 1]->Name(parameter); \ listeners_[i - 1]->Name(parameter); \
} \ } \
} \ } \
} }
GTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest) GTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest)
GTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest) GTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest)
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
GTEST_REPEATER_METHOD_(OnTestCaseStart, TestSuite) GTEST_REPEATER_METHOD_(OnTestCaseStart, TestSuite)
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
GTEST_REPEATER_METHOD_(OnTestSuiteStart, TestSuite) GTEST_REPEATER_METHOD_(OnTestSuiteStart, TestSuite)
GTEST_REPEATER_METHOD_(OnTestStart, TestInfo) GTEST_REPEATER_METHOD_(OnTestStart, TestInfo)
GTEST_REPEATER_METHOD_(OnTestDisabled, TestInfo)
GTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult) GTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult)
GTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest) GTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest) GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest) GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest)
GTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo) GTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo)
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
GTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestSuite) GTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestSuite)
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
GTEST_REVERSE_REPEATER_METHOD_(OnTestSuiteEnd, TestSuite) GTEST_REVERSE_REPEATER_METHOD_(OnTestSuiteEnd, TestSuite)
skipping to change at line 3891 skipping to change at line 3902
void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override;
void ListTestsMatchingFilter(const std::vector<TestSuite*>& test_suites); void ListTestsMatchingFilter(const std::vector<TestSuite*>& test_suites);
// Prints an XML summary of all unit tests. // Prints an XML summary of all unit tests.
static void PrintXmlTestsList(std::ostream* stream, static void PrintXmlTestsList(std::ostream* stream,
const std::vector<TestSuite*>& test_suites); const std::vector<TestSuite*>& test_suites);
private: private:
// Is c a whitespace character that is normalized to a space character // Is c a whitespace character that is normalized to a space character
// when it appears in an XML attribute value? // when it appears in an XML attribute value?
static bool IsNormalizableWhitespace(char c) { static bool IsNormalizableWhitespace(unsigned char c) {
return c == 0x9 || c == 0xA || c == 0xD; return c == '\t' || c == '\n' || c == '\r';
} }
// May c appear in a well-formed XML document? // May c appear in a well-formed XML document?
static bool IsValidXmlCharacter(char c) { // https://www.w3.org/TR/REC-xml/#charsets
static bool IsValidXmlCharacter(unsigned char c) {
return IsNormalizableWhitespace(c) || c >= 0x20; return IsNormalizableWhitespace(c) || c >= 0x20;
} }
// Returns an XML-escaped copy of the input string str. If // Returns an XML-escaped copy of the input string str. If
// is_attribute is true, the text is meant to appear as an attribute // is_attribute is true, the text is meant to appear as an attribute
// value, and normalizable whitespace is preserved by replacing it // value, and normalizable whitespace is preserved by replacing it
// with character references. // with character references.
static std::string EscapeXml(const std::string& str, bool is_attribute); static std::string EscapeXml(const std::string& str, bool is_attribute);
// Returns the given string with all characters invalid in XML removed. // Returns the given string with all characters invalid in XML removed.
skipping to change at line 3966 skipping to change at line 3978
static std::string TestPropertiesAsXmlAttributes(const TestResult& result); static std::string TestPropertiesAsXmlAttributes(const TestResult& result);
// Streams an XML representation of the test properties of a TestResult // Streams an XML representation of the test properties of a TestResult
// object. // object.
static void OutputXmlTestProperties(std::ostream* stream, static void OutputXmlTestProperties(std::ostream* stream,
const TestResult& result); const TestResult& result);
// The output file. // The output file.
const std::string output_file_; const std::string output_file_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter); XmlUnitTestResultPrinter(const XmlUnitTestResultPrinter&) = delete;
XmlUnitTestResultPrinter& operator=(const XmlUnitTestResultPrinter&) = delete;
}; };
// Creates a new XmlUnitTestResultPrinter. // Creates a new XmlUnitTestResultPrinter.
XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file) XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)
: output_file_(output_file) { : output_file_(output_file) {
if (output_file_.empty()) { if (output_file_.empty()) {
GTEST_LOG_(FATAL) << "XML output file may not be null"; GTEST_LOG_(FATAL) << "XML output file may not be null";
} }
} }
skipping to change at line 4006 skipping to change at line 4019
// Returns an XML-escaped copy of the input string str. If is_attribute // Returns an XML-escaped copy of the input string str. If is_attribute
// is true, the text is meant to appear as an attribute value, and // is true, the text is meant to appear as an attribute value, and
// normalizable whitespace is preserved by replacing it with character // normalizable whitespace is preserved by replacing it with character
// references. // references.
// //
// Invalid XML characters in str, if any, are stripped from the output. // Invalid XML characters in str, if any, are stripped from the output.
// It is expected that most, if not all, of the text processed by this // It is expected that most, if not all, of the text processed by this
// module will consist of ordinary English text. // module will consist of ordinary English text.
// If this module is ever modified to produce version 1.1 XML output, // If this module is ever modified to produce version 1.1 XML output,
// most invalid characters can be retained using character references. // most invalid characters can be retained using character references.
std::string XmlUnitTestResultPrinter::EscapeXml( std::string XmlUnitTestResultPrinter::EscapeXml(const std::string& str,
const std::string& str, bool is_attribute) { bool is_attribute) {
Message m; Message m;
for (size_t i = 0; i < str.size(); ++i) { for (size_t i = 0; i < str.size(); ++i) {
const char ch = str[i]; const char ch = str[i];
switch (ch) { switch (ch) {
case '<': case '<':
m << "&lt;"; m << "&lt;";
break; break;
case '>': case '>':
m << "&gt;"; m << "&gt;";
skipping to change at line 4035 skipping to change at line 4048
else else
m << '\''; m << '\'';
break; break;
case '"': case '"':
if (is_attribute) if (is_attribute)
m << "&quot;"; m << "&quot;";
else else
m << '"'; m << '"';
break; break;
default: default:
if (IsValidXmlCharacter(ch)) { if (IsValidXmlCharacter(static_cast<unsigned char>(ch))) {
if (is_attribute && IsNormalizableWhitespace(ch)) if (is_attribute &&
IsNormalizableWhitespace(static_cast<unsigned char>(ch)))
m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch)) m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
<< ";"; << ";";
else else
m << ch; m << ch;
} }
break; break;
} }
} }
return m.GetString(); return m.GetString();
} }
// Returns the given string with all characters invalid in XML removed. // Returns the given string with all characters invalid in XML removed.
// Currently invalid characters are dropped from the string. An // Currently invalid characters are dropped from the string. An
// alternative is to replace them with certain characters such as . or ?. // alternative is to replace them with certain characters such as . or ?.
std::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters( std::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(
const std::string& str) { const std::string& str) {
std::string output; std::string output;
output.reserve(str.size()); output.reserve(str.size());
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
if (IsValidXmlCharacter(*it)) if (IsValidXmlCharacter(static_cast<unsigned char>(*it)))
output.push_back(*it); output.push_back(*it);
return output; return output;
} }
// The following routines generate an XML representation of a UnitTest // The following routines generate an XML representation of a UnitTest
// object. // object.
// GOOGLETEST_CM0009 DO NOT DELETE
// //
// This is how Google Test concepts map to the DTD: // This is how Google Test concepts map to the DTD:
// //
// <testsuites name="AllTests"> <-- corresponds to a UnitTest object // <testsuites name="AllTests"> <-- corresponds to a UnitTest object
// <testsuite name="testcase-name"> <-- corresponds to a TestSuite object // <testsuite name="testcase-name"> <-- corresponds to a TestSuite object
// <testcase name="test-name"> <-- corresponds to a TestInfo object // <testcase name="test-name"> <-- corresponds to a TestInfo object
// <failure message="...">...</failure> // <failure message="...">...</failure>
// <failure message="...">...</failure> // <failure message="...">...</failure>
// <failure message="...">...</failure> // <failure message="...">...</failure>
// <-- individual assertion failures // <-- individual assertion failures
skipping to change at line 4114 skipping to change at line 4127
} }
// Converts the given epoch time in milliseconds to a date string in the ISO // Converts the given epoch time in milliseconds to a date string in the ISO
// 8601 format, without the timezone information. // 8601 format, without the timezone information.
std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) { std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {
struct tm time_struct; struct tm time_struct;
if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct)) if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct))
return ""; return "";
// YYYY-MM-DDThh:mm:ss.sss // YYYY-MM-DDThh:mm:ss.sss
return StreamableToString(time_struct.tm_year + 1900) + "-" + return StreamableToString(time_struct.tm_year + 1900) + "-" +
String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" +
String::FormatIntWidth2(time_struct.tm_mday) + "T" + String::FormatIntWidth2(time_struct.tm_mday) + "T" +
String::FormatIntWidth2(time_struct.tm_hour) + ":" + String::FormatIntWidth2(time_struct.tm_hour) + ":" +
String::FormatIntWidth2(time_struct.tm_min) + ":" + String::FormatIntWidth2(time_struct.tm_min) + ":" +
String::FormatIntWidth2(time_struct.tm_sec) + "." + String::FormatIntWidth2(time_struct.tm_sec) + "." +
String::FormatIntWidthN(static_cast<int>(ms % 1000), 3); String::FormatIntWidthN(static_cast<int>(ms % 1000), 3);
} }
// Streams an XML CDATA section, escaping invalid CDATA sequences as needed. // Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
void XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream, void XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream,
const char* data) { const char* data) {
const char* segment = data; const char* segment = data;
*stream << "<![CDATA["; *stream << "<![CDATA[";
for (;;) { for (;;) {
const char* const next_segment = strstr(segment, "]]>"); const char* const next_segment = strstr(segment, "]]>");
if (next_segment != nullptr) { if (next_segment != nullptr) {
stream->write( stream->write(segment,
segment, static_cast<std::streamsize>(next_segment - segment)); static_cast<std::streamsize>(next_segment - segment));
*stream << "]]>]]&gt;<![CDATA["; *stream << "]]>]]&gt;<![CDATA[";
segment = next_segment + strlen("]]>"); segment = next_segment + strlen("]]>");
} else { } else {
*stream << segment; *stream << segment;
break; break;
} }
} }
*stream << "]]>"; *stream << "]]>";
} }
void XmlUnitTestResultPrinter::OutputXmlAttribute( void XmlUnitTestResultPrinter::OutputXmlAttribute(
std::ostream* stream, std::ostream* stream, const std::string& element_name,
const std::string& element_name, const std::string& name, const std::string& value) {
const std::string& name,
const std::string& value) {
const std::vector<std::string>& allowed_names = const std::vector<std::string>& allowed_names =
GetReservedOutputAttributesForElement(element_name); GetReservedOutputAttributesForElement(element_name);
GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=
allowed_names.end()) allowed_names.end())
<< "Attribute " << name << " is not allowed for element <" << element_name << "Attribute " << name << " is not allowed for element <" << element_name
<< ">."; << ">.";
*stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\""; *stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\"";
} }
// Streams a test suite XML stanza containing the given test result. // Streams a test suite XML stanza containing the given test result.
void XmlUnitTestResultPrinter::OutputXmlTestSuiteForTestResult( void XmlUnitTestResultPrinter::OutputXmlTestSuiteForTestResult(
::std::ostream* stream, const TestResult& result) { ::std::ostream* stream, const TestResult& result) {
// Output the boilerplate for a minimal test suite with one test. // Output the boilerplate for a minimal test suite with one test.
skipping to change at line 4217 skipping to change at line 4228
OutputXmlAttribute(stream, kTestsuite, "name", test_info.name()); OutputXmlAttribute(stream, kTestsuite, "name", test_info.name());
if (test_info.value_param() != nullptr) { if (test_info.value_param() != nullptr) {
OutputXmlAttribute(stream, kTestsuite, "value_param", OutputXmlAttribute(stream, kTestsuite, "value_param",
test_info.value_param()); test_info.value_param());
} }
if (test_info.type_param() != nullptr) { if (test_info.type_param() != nullptr) {
OutputXmlAttribute(stream, kTestsuite, "type_param", OutputXmlAttribute(stream, kTestsuite, "type_param",
test_info.type_param()); test_info.type_param());
} }
if (GTEST_FLAG(list_tests)) {
OutputXmlAttribute(stream, kTestsuite, "file", test_info.file()); OutputXmlAttribute(stream, kTestsuite, "file", test_info.file());
OutputXmlAttribute(stream, kTestsuite, "line", OutputXmlAttribute(stream, kTestsuite, "line",
StreamableToString(test_info.line())); StreamableToString(test_info.line()));
if (GTEST_FLAG_GET(list_tests)) {
*stream << " />\n"; *stream << " />\n";
return; return;
} }
OutputXmlAttribute(stream, kTestsuite, "status", OutputXmlAttribute(stream, kTestsuite, "status",
test_info.should_run() ? "run" : "notrun"); test_info.should_run() ? "run" : "notrun");
OutputXmlAttribute(stream, kTestsuite, "result", OutputXmlAttribute(stream, kTestsuite, "result",
test_info.should_run() test_info.should_run()
? (result.Skipped() ? "skipped" : "completed") ? (result.Skipped() ? "skipped" : "completed")
: "suppressed"); : "suppressed");
skipping to change at line 4255 skipping to change at line 4267
for (int i = 0; i < result.total_part_count(); ++i) { for (int i = 0; i < result.total_part_count(); ++i) {
const TestPartResult& part = result.GetTestPartResult(i); const TestPartResult& part = result.GetTestPartResult(i);
if (part.failed()) { if (part.failed()) {
if (++failures == 1 && skips == 0) { if (++failures == 1 && skips == 0) {
*stream << ">\n"; *stream << ">\n";
} }
const std::string location = const std::string location =
internal::FormatCompilerIndependentFileLocation(part.file_name(), internal::FormatCompilerIndependentFileLocation(part.file_name(),
part.line_number()); part.line_number());
const std::string summary = location + "\n" + part.summary(); const std::string summary = location + "\n" + part.summary();
*stream << " <failure message=\"" *stream << " <failure message=\"" << EscapeXmlAttribute(summary)
<< EscapeXmlAttribute(summary)
<< "\" type=\"\">"; << "\" type=\"\">";
const std::string detail = location + "\n" + part.message(); const std::string detail = location + "\n" + part.message();
OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str()); OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str());
*stream << "</failure>\n"; *stream << "</failure>\n";
} else if (part.skipped()) { } else if (part.skipped()) {
if (++skips == 1 && failures == 0) { if (++skips == 1 && failures == 0) {
*stream << ">\n"; *stream << ">\n";
} }
const std::string location = const std::string location =
internal::FormatCompilerIndependentFileLocation(part.file_name(), internal::FormatCompilerIndependentFileLocation(part.file_name(),
skipping to change at line 4296 skipping to change at line 4307
} }
// Prints an XML representation of a TestSuite object // Prints an XML representation of a TestSuite object
void XmlUnitTestResultPrinter::PrintXmlTestSuite(std::ostream* stream, void XmlUnitTestResultPrinter::PrintXmlTestSuite(std::ostream* stream,
const TestSuite& test_suite) { const TestSuite& test_suite) {
const std::string kTestsuite = "testsuite"; const std::string kTestsuite = "testsuite";
*stream << " <" << kTestsuite; *stream << " <" << kTestsuite;
OutputXmlAttribute(stream, kTestsuite, "name", test_suite.name()); OutputXmlAttribute(stream, kTestsuite, "name", test_suite.name());
OutputXmlAttribute(stream, kTestsuite, "tests", OutputXmlAttribute(stream, kTestsuite, "tests",
StreamableToString(test_suite.reportable_test_count())); StreamableToString(test_suite.reportable_test_count()));
if (!GTEST_FLAG(list_tests)) { if (!GTEST_FLAG_GET(list_tests)) {
OutputXmlAttribute(stream, kTestsuite, "failures", OutputXmlAttribute(stream, kTestsuite, "failures",
StreamableToString(test_suite.failed_test_count())); StreamableToString(test_suite.failed_test_count()));
OutputXmlAttribute( OutputXmlAttribute(
stream, kTestsuite, "disabled", stream, kTestsuite, "disabled",
StreamableToString(test_suite.reportable_disabled_test_count())); StreamableToString(test_suite.reportable_disabled_test_count()));
OutputXmlAttribute(stream, kTestsuite, "skipped", OutputXmlAttribute(stream, kTestsuite, "skipped",
StreamableToString(test_suite.skipped_test_count())); StreamableToString(test_suite.skipped_test_count()));
OutputXmlAttribute(stream, kTestsuite, "errors", "0"); OutputXmlAttribute(stream, kTestsuite, "errors", "0");
skipping to change at line 4344 skipping to change at line 4355
OutputXmlAttribute( OutputXmlAttribute(
stream, kTestsuites, "disabled", stream, kTestsuites, "disabled",
StreamableToString(unit_test.reportable_disabled_test_count())); StreamableToString(unit_test.reportable_disabled_test_count()));
OutputXmlAttribute(stream, kTestsuites, "errors", "0"); OutputXmlAttribute(stream, kTestsuites, "errors", "0");
OutputXmlAttribute(stream, kTestsuites, "time", OutputXmlAttribute(stream, kTestsuites, "time",
FormatTimeInMillisAsSeconds(unit_test.elapsed_time())); FormatTimeInMillisAsSeconds(unit_test.elapsed_time()));
OutputXmlAttribute( OutputXmlAttribute(
stream, kTestsuites, "timestamp", stream, kTestsuites, "timestamp",
FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp())); FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp()));
if (GTEST_FLAG(shuffle)) { if (GTEST_FLAG_GET(shuffle)) {
OutputXmlAttribute(stream, kTestsuites, "random_seed", OutputXmlAttribute(stream, kTestsuites, "random_seed",
StreamableToString(unit_test.random_seed())); StreamableToString(unit_test.random_seed()));
} }
*stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result()); *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result());
OutputXmlAttribute(stream, kTestsuites, "name", "AllTests"); OutputXmlAttribute(stream, kTestsuites, "name", "AllTests");
*stream << ">\n"; *stream << ">\n";
for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
if (unit_test.GetTestSuite(i)->reportable_test_count() > 0) if (unit_test.GetTestSuite(i)->reportable_test_count() > 0)
skipping to change at line 4397 skipping to change at line 4408
} }
// Produces a string representing the test properties in a result as space // Produces a string representing the test properties in a result as space
// delimited XML attributes based on the property key="value" pairs. // delimited XML attributes based on the property key="value" pairs.
std::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( std::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
const TestResult& result) { const TestResult& result) {
Message attributes; Message attributes;
for (int i = 0; i < result.test_property_count(); ++i) { for (int i = 0; i < result.test_property_count(); ++i) {
const TestProperty& property = result.GetTestProperty(i); const TestProperty& property = result.GetTestProperty(i);
attributes << " " << property.key() << "=" attributes << " " << property.key() << "="
<< "\"" << EscapeXmlAttribute(property.value()) << "\""; << "\"" << EscapeXmlAttribute(property.value()) << "\"";
} }
return attributes.GetString(); return attributes.GetString();
} }
void XmlUnitTestResultPrinter::OutputXmlTestProperties( void XmlUnitTestResultPrinter::OutputXmlTestProperties(
std::ostream* stream, const TestResult& result) { std::ostream* stream, const TestResult& result) {
const std::string kProperties = "properties"; const std::string kProperties = "properties";
const std::string kProperty = "property"; const std::string kProperty = "property";
if (result.test_property_count() <= 0) { if (result.test_property_count() <= 0) {
return; return;
} }
*stream << "<" << kProperties << ">\n"; *stream << " <" << kProperties << ">\n";
for (int i = 0; i < result.test_property_count(); ++i) { for (int i = 0; i < result.test_property_count(); ++i) {
const TestProperty& property = result.GetTestProperty(i); const TestProperty& property = result.GetTestProperty(i);
*stream << "<" << kProperty; *stream << " <" << kProperty;
*stream << " name=\"" << EscapeXmlAttribute(property.key()) << "\""; *stream << " name=\"" << EscapeXmlAttribute(property.key()) << "\"";
*stream << " value=\"" << EscapeXmlAttribute(property.value()) << "\""; *stream << " value=\"" << EscapeXmlAttribute(property.value()) << "\"";
*stream << "/>\n"; *stream << "/>\n";
} }
*stream << "</" << kProperties << ">\n"; *stream << " </" << kProperties << ">\n";
} }
// End XmlUnitTestResultPrinter // End XmlUnitTestResultPrinter
// This class generates an JSON output file. // This class generates an JSON output file.
class JsonUnitTestResultPrinter : public EmptyTestEventListener { class JsonUnitTestResultPrinter : public EmptyTestEventListener {
public: public:
explicit JsonUnitTestResultPrinter(const char* output_file); explicit JsonUnitTestResultPrinter(const char* output_file);
void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override;
skipping to change at line 4443 skipping to change at line 4454
const std::vector<TestSuite*>& test_suites); const std::vector<TestSuite*>& test_suites);
private: private:
// Returns an JSON-escaped copy of the input string str. // Returns an JSON-escaped copy of the input string str.
static std::string EscapeJson(const std::string& str); static std::string EscapeJson(const std::string& str);
//// Verifies that the given attribute belongs to the given element and //// Verifies that the given attribute belongs to the given element and
//// streams the attribute as JSON. //// streams the attribute as JSON.
static void OutputJsonKey(std::ostream* stream, static void OutputJsonKey(std::ostream* stream,
const std::string& element_name, const std::string& element_name,
const std::string& name, const std::string& name, const std::string& value,
const std::string& value, const std::string& indent, bool comma = true);
const std::string& indent,
bool comma = true);
static void OutputJsonKey(std::ostream* stream, static void OutputJsonKey(std::ostream* stream,
const std::string& element_name, const std::string& element_name,
const std::string& name, const std::string& name, int value,
int value, const std::string& indent, bool comma = true);
const std::string& indent,
bool comma = true);
// Streams a test suite JSON stanza containing the given test result. // Streams a test suite JSON stanza containing the given test result.
// //
// Requires: result.Failed() // Requires: result.Failed()
static void OutputJsonTestSuiteForTestResult(::std::ostream* stream, static void OutputJsonTestSuiteForTestResult(::std::ostream* stream,
const TestResult& result); const TestResult& result);
// Streams a JSON representation of a TestResult object. // Streams a JSON representation of a TestResult object.
static void OutputJsonTestResult(::std::ostream* stream, static void OutputJsonTestResult(::std::ostream* stream,
const TestResult& result); const TestResult& result);
skipping to change at line 4485 skipping to change at line 4492
const UnitTest& unit_test); const UnitTest& unit_test);
// Produces a string representing the test properties in a result as // Produces a string representing the test properties in a result as
// a JSON dictionary. // a JSON dictionary.
static std::string TestPropertiesAsJson(const TestResult& result, static std::string TestPropertiesAsJson(const TestResult& result,
const std::string& indent); const std::string& indent);
// The output file. // The output file.
const std::string output_file_; const std::string output_file_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(JsonUnitTestResultPrinter); JsonUnitTestResultPrinter(const JsonUnitTestResultPrinter&) = delete;
JsonUnitTestResultPrinter& operator=(const JsonUnitTestResultPrinter&) =
delete;
}; };
// Creates a new JsonUnitTestResultPrinter. // Creates a new JsonUnitTestResultPrinter.
JsonUnitTestResultPrinter::JsonUnitTestResultPrinter(const char* output_file) JsonUnitTestResultPrinter::JsonUnitTestResultPrinter(const char* output_file)
: output_file_(output_file) { : output_file_(output_file) {
if (output_file_.empty()) { if (output_file_.empty()) {
GTEST_LOG_(FATAL) << "JSON output file may not be null"; GTEST_LOG_(FATAL) << "JSON output file may not be null";
} }
} }
void JsonUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, void JsonUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) { int /*iteration*/) {
FILE* jsonout = OpenFileForWriting(output_file_); FILE* jsonout = OpenFileForWriting(output_file_);
std::stringstream stream; std::stringstream stream;
PrintJsonUnitTest(&stream, unit_test); PrintJsonUnitTest(&stream, unit_test);
fprintf(jsonout, "%s", StringStreamToString(&stream).c_str()); fprintf(jsonout, "%s", StringStreamToString(&stream).c_str());
fclose(jsonout); fclose(jsonout);
} }
// Returns an JSON-escaped copy of the input string str. // Returns an JSON-escaped copy of the input string str.
std::string JsonUnitTestResultPrinter::EscapeJson(const std::string& str) { std::string JsonUnitTestResultPrinter::EscapeJson(const std::string& str) {
Message m; Message m;
skipping to change at line 4563 skipping to change at line 4572
} }
// Converts the given epoch time in milliseconds to a date string in the // Converts the given epoch time in milliseconds to a date string in the
// RFC3339 format, without the timezone information. // RFC3339 format, without the timezone information.
static std::string FormatEpochTimeInMillisAsRFC3339(TimeInMillis ms) { static std::string FormatEpochTimeInMillisAsRFC3339(TimeInMillis ms) {
struct tm time_struct; struct tm time_struct;
if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct)) if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct))
return ""; return "";
// YYYY-MM-DDThh:mm:ss // YYYY-MM-DDThh:mm:ss
return StreamableToString(time_struct.tm_year + 1900) + "-" + return StreamableToString(time_struct.tm_year + 1900) + "-" +
String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" +
String::FormatIntWidth2(time_struct.tm_mday) + "T" + String::FormatIntWidth2(time_struct.tm_mday) + "T" +
String::FormatIntWidth2(time_struct.tm_hour) + ":" + String::FormatIntWidth2(time_struct.tm_hour) + ":" +
String::FormatIntWidth2(time_struct.tm_min) + ":" + String::FormatIntWidth2(time_struct.tm_min) + ":" +
String::FormatIntWidth2(time_struct.tm_sec) + "Z"; String::FormatIntWidth2(time_struct.tm_sec) + "Z";
} }
static inline std::string Indent(size_t width) { static inline std::string Indent(size_t width) {
return std::string(width, ' '); return std::string(width, ' ');
} }
void JsonUnitTestResultPrinter::OutputJsonKey( void JsonUnitTestResultPrinter::OutputJsonKey(std::ostream* stream,
std::ostream* stream, const std::string& element_name,
const std::string& element_name, const std::string& name,
const std::string& name, const std::string& value,
const std::string& value, const std::string& indent,
const std::string& indent, bool comma) {
bool comma) {
const std::vector<std::string>& allowed_names = const std::vector<std::string>& allowed_names =
GetReservedOutputAttributesForElement(element_name); GetReservedOutputAttributesForElement(element_name);
GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=
allowed_names.end()) allowed_names.end())
<< "Key \"" << name << "\" is not allowed for value \"" << element_name << "Key \"" << name << "\" is not allowed for value \"" << element_name
<< "\"."; << "\".";
*stream << indent << "\"" << name << "\": \"" << EscapeJson(value) << "\""; *stream << indent << "\"" << name << "\": \"" << EscapeJson(value) << "\"";
if (comma) if (comma) *stream << ",\n";
*stream << ",\n";
} }
void JsonUnitTestResultPrinter::OutputJsonKey( void JsonUnitTestResultPrinter::OutputJsonKey(
std::ostream* stream, std::ostream* stream, const std::string& element_name,
const std::string& element_name, const std::string& name, int value, const std::string& indent, bool comma) {
const std::string& name,
int value,
const std::string& indent,
bool comma) {
const std::vector<std::string>& allowed_names = const std::vector<std::string>& allowed_names =
GetReservedOutputAttributesForElement(element_name); GetReservedOutputAttributesForElement(element_name);
GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=
allowed_names.end()) allowed_names.end())
<< "Key \"" << name << "\" is not allowed for value \"" << element_name << "Key \"" << name << "\" is not allowed for value \"" << element_name
<< "\"."; << "\".";
*stream << indent << "\"" << name << "\": " << StreamableToString(value); *stream << indent << "\"" << name << "\": " << StreamableToString(value);
if (comma) if (comma) *stream << ",\n";
*stream << ",\n";
} }
// Streams a test suite JSON stanza containing the given test result. // Streams a test suite JSON stanza containing the given test result.
void JsonUnitTestResultPrinter::OutputJsonTestSuiteForTestResult( void JsonUnitTestResultPrinter::OutputJsonTestSuiteForTestResult(
::std::ostream* stream, const TestResult& result) { ::std::ostream* stream, const TestResult& result) {
// Output the boilerplate for a new test suite. // Output the boilerplate for a new test suite.
*stream << Indent(4) << "{\n"; *stream << Indent(4) << "{\n";
OutputJsonKey(stream, "testsuite", "name", "NonTestSuiteFailure", Indent(6)); OutputJsonKey(stream, "testsuite", "name", "NonTestSuiteFailure", Indent(6));
OutputJsonKey(stream, "testsuite", "tests", 1, Indent(6)); OutputJsonKey(stream, "testsuite", "tests", 1, Indent(6));
if (!GTEST_FLAG(list_tests)) { if (!GTEST_FLAG_GET(list_tests)) {
OutputJsonKey(stream, "testsuite", "failures", 1, Indent(6)); OutputJsonKey(stream, "testsuite", "failures", 1, Indent(6));
OutputJsonKey(stream, "testsuite", "disabled", 0, Indent(6)); OutputJsonKey(stream, "testsuite", "disabled", 0, Indent(6));
OutputJsonKey(stream, "testsuite", "skipped", 0, Indent(6)); OutputJsonKey(stream, "testsuite", "skipped", 0, Indent(6));
OutputJsonKey(stream, "testsuite", "errors", 0, Indent(6)); OutputJsonKey(stream, "testsuite", "errors", 0, Indent(6));
OutputJsonKey(stream, "testsuite", "time", OutputJsonKey(stream, "testsuite", "time",
FormatTimeInMillisAsDuration(result.elapsed_time()), FormatTimeInMillisAsDuration(result.elapsed_time()),
Indent(6)); Indent(6));
OutputJsonKey(stream, "testsuite", "timestamp", OutputJsonKey(stream, "testsuite", "timestamp",
FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()),
Indent(6)); Indent(6));
skipping to change at line 4675 skipping to change at line 4677
OutputJsonKey(stream, kTestsuite, "name", test_info.name(), kIndent); OutputJsonKey(stream, kTestsuite, "name", test_info.name(), kIndent);
if (test_info.value_param() != nullptr) { if (test_info.value_param() != nullptr) {
OutputJsonKey(stream, kTestsuite, "value_param", test_info.value_param(), OutputJsonKey(stream, kTestsuite, "value_param", test_info.value_param(),
kIndent); kIndent);
} }
if (test_info.type_param() != nullptr) { if (test_info.type_param() != nullptr) {
OutputJsonKey(stream, kTestsuite, "type_param", test_info.type_param(), OutputJsonKey(stream, kTestsuite, "type_param", test_info.type_param(),
kIndent); kIndent);
} }
if (GTEST_FLAG(list_tests)) {
OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent); OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent);
OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false); OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false);
if (GTEST_FLAG_GET(list_tests)) {
*stream << "\n" << Indent(8) << "}"; *stream << "\n" << Indent(8) << "}";
return; return;
} else {
*stream << ",\n";
} }
OutputJsonKey(stream, kTestsuite, "status", OutputJsonKey(stream, kTestsuite, "status",
test_info.should_run() ? "RUN" : "NOTRUN", kIndent); test_info.should_run() ? "RUN" : "NOTRUN", kIndent);
OutputJsonKey(stream, kTestsuite, "result", OutputJsonKey(stream, kTestsuite, "result",
test_info.should_run() test_info.should_run()
? (result.Skipped() ? "SKIPPED" : "COMPLETED") ? (result.Skipped() ? "SKIPPED" : "COMPLETED")
: "SUPPRESSED", : "SUPPRESSED",
kIndent); kIndent);
OutputJsonKey(stream, kTestsuite, "timestamp", OutputJsonKey(stream, kTestsuite, "timestamp",
skipping to change at line 4711 skipping to change at line 4716
void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream, void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream,
const TestResult& result) { const TestResult& result) {
const std::string kIndent = Indent(10); const std::string kIndent = Indent(10);
int failures = 0; int failures = 0;
for (int i = 0; i < result.total_part_count(); ++i) { for (int i = 0; i < result.total_part_count(); ++i) {
const TestPartResult& part = result.GetTestPartResult(i); const TestPartResult& part = result.GetTestPartResult(i);
if (part.failed()) { if (part.failed()) {
*stream << ",\n"; *stream << ",\n";
if (++failures == 1) { if (++failures == 1) {
*stream << kIndent << "\"" << "failures" << "\": [\n"; *stream << kIndent << "\""
<< "failures"
<< "\": [\n";
} }
const std::string location = const std::string location =
internal::FormatCompilerIndependentFileLocation(part.file_name(), internal::FormatCompilerIndependentFileLocation(part.file_name(),
part.line_number()); part.line_number());
const std::string message = EscapeJson(location + "\n" + part.message()); const std::string message = EscapeJson(location + "\n" + part.message());
*stream << kIndent << " {\n" *stream << kIndent << " {\n"
<< kIndent << " \"failure\": \"" << message << "\",\n" << kIndent << " \"failure\": \"" << message << "\",\n"
<< kIndent << " \"type\": \"\"\n" << kIndent << " \"type\": \"\"\n"
<< kIndent << " }"; << kIndent << " }";
} }
} }
if (failures > 0) if (failures > 0) *stream << "\n" << kIndent << "]";
*stream << "\n" << kIndent << "]";
*stream << "\n" << Indent(8) << "}"; *stream << "\n" << Indent(8) << "}";
} }
// Prints an JSON representation of a TestSuite object // Prints an JSON representation of a TestSuite object
void JsonUnitTestResultPrinter::PrintJsonTestSuite( void JsonUnitTestResultPrinter::PrintJsonTestSuite(
std::ostream* stream, const TestSuite& test_suite) { std::ostream* stream, const TestSuite& test_suite) {
const std::string kTestsuite = "testsuite"; const std::string kTestsuite = "testsuite";
const std::string kIndent = Indent(6); const std::string kIndent = Indent(6);
*stream << Indent(4) << "{\n"; *stream << Indent(4) << "{\n";
OutputJsonKey(stream, kTestsuite, "name", test_suite.name(), kIndent); OutputJsonKey(stream, kTestsuite, "name", test_suite.name(), kIndent);
OutputJsonKey(stream, kTestsuite, "tests", test_suite.reportable_test_count(), OutputJsonKey(stream, kTestsuite, "tests", test_suite.reportable_test_count(),
kIndent); kIndent);
if (!GTEST_FLAG(list_tests)) { if (!GTEST_FLAG_GET(list_tests)) {
OutputJsonKey(stream, kTestsuite, "failures", OutputJsonKey(stream, kTestsuite, "failures",
test_suite.failed_test_count(), kIndent); test_suite.failed_test_count(), kIndent);
OutputJsonKey(stream, kTestsuite, "disabled", OutputJsonKey(stream, kTestsuite, "disabled",
test_suite.reportable_disabled_test_count(), kIndent); test_suite.reportable_disabled_test_count(), kIndent);
OutputJsonKey(stream, kTestsuite, "errors", 0, kIndent); OutputJsonKey(stream, kTestsuite, "errors", 0, kIndent);
OutputJsonKey( OutputJsonKey(
stream, kTestsuite, "timestamp", stream, kTestsuite, "timestamp",
FormatEpochTimeInMillisAsRFC3339(test_suite.start_timestamp()), FormatEpochTimeInMillisAsRFC3339(test_suite.start_timestamp()),
kIndent); kIndent);
OutputJsonKey(stream, kTestsuite, "time", OutputJsonKey(stream, kTestsuite, "time",
skipping to change at line 4786 skipping to change at line 4792
const std::string kIndent = Indent(2); const std::string kIndent = Indent(2);
*stream << "{\n"; *stream << "{\n";
OutputJsonKey(stream, kTestsuites, "tests", unit_test.reportable_test_count(), OutputJsonKey(stream, kTestsuites, "tests", unit_test.reportable_test_count(),
kIndent); kIndent);
OutputJsonKey(stream, kTestsuites, "failures", unit_test.failed_test_count(), OutputJsonKey(stream, kTestsuites, "failures", unit_test.failed_test_count(),
kIndent); kIndent);
OutputJsonKey(stream, kTestsuites, "disabled", OutputJsonKey(stream, kTestsuites, "disabled",
unit_test.reportable_disabled_test_count(), kIndent); unit_test.reportable_disabled_test_count(), kIndent);
OutputJsonKey(stream, kTestsuites, "errors", 0, kIndent); OutputJsonKey(stream, kTestsuites, "errors", 0, kIndent);
if (GTEST_FLAG(shuffle)) { if (GTEST_FLAG_GET(shuffle)) {
OutputJsonKey(stream, kTestsuites, "random_seed", unit_test.random_seed(), OutputJsonKey(stream, kTestsuites, "random_seed", unit_test.random_seed(),
kIndent); kIndent);
} }
OutputJsonKey(stream, kTestsuites, "timestamp", OutputJsonKey(stream, kTestsuites, "timestamp",
FormatEpochTimeInMillisAsRFC3339(unit_test.start_timestamp()), FormatEpochTimeInMillisAsRFC3339(unit_test.start_timestamp()),
kIndent); kIndent);
OutputJsonKey(stream, kTestsuites, "time", OutputJsonKey(stream, kTestsuites, "time",
FormatTimeInMillisAsDuration(unit_test.elapsed_time()), kIndent, FormatTimeInMillisAsDuration(unit_test.elapsed_time()), kIndent,
false); false);
skipping to change at line 4821 skipping to change at line 4827
PrintJsonTestSuite(stream, *unit_test.GetTestSuite(i)); PrintJsonTestSuite(stream, *unit_test.GetTestSuite(i));
} }
} }
// If there was a test failure outside of one of the test suites (like in a // If there was a test failure outside of one of the test suites (like in a
// test environment) include that in the output. // test environment) include that in the output.
if (unit_test.ad_hoc_test_result().Failed()) { if (unit_test.ad_hoc_test_result().Failed()) {
OutputJsonTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result()); OutputJsonTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result());
} }
*stream << "\n" << kIndent << "]\n" << "}\n"; *stream << "\n"
<< kIndent << "]\n"
<< "}\n";
} }
void JsonUnitTestResultPrinter::PrintJsonTestList( void JsonUnitTestResultPrinter::PrintJsonTestList(
std::ostream* stream, const std::vector<TestSuite*>& test_suites) { std::ostream* stream, const std::vector<TestSuite*>& test_suites) {
const std::string kTestsuites = "testsuites"; const std::string kTestsuites = "testsuites";
const std::string kIndent = Indent(2); const std::string kIndent = Indent(2);
*stream << "{\n"; *stream << "{\n";
int total_tests = 0; int total_tests = 0;
for (auto test_suite : test_suites) { for (auto test_suite : test_suites) {
total_tests += test_suite->total_test_count(); total_tests += test_suite->total_test_count();
skipping to change at line 4856 skipping to change at line 4864
<< kIndent << "]\n" << kIndent << "]\n"
<< "}\n"; << "}\n";
} }
// Produces a string representing the test properties in a result as // Produces a string representing the test properties in a result as
// a JSON dictionary. // a JSON dictionary.
std::string JsonUnitTestResultPrinter::TestPropertiesAsJson( std::string JsonUnitTestResultPrinter::TestPropertiesAsJson(
const TestResult& result, const std::string& indent) { const TestResult& result, const std::string& indent) {
Message attributes; Message attributes;
for (int i = 0; i < result.test_property_count(); ++i) { for (int i = 0; i < result.test_property_count(); ++i) {
const TestProperty& property = result.GetTestProperty(i); const TestProperty& property = result.GetTestProperty(i);
attributes << ",\n" << indent << "\"" << property.key() << "\": " attributes << ",\n"
<< indent << "\"" << property.key() << "\": "
<< "\"" << EscapeJson(property.value()) << "\""; << "\"" << EscapeJson(property.value()) << "\"";
} }
return attributes.GetString(); return attributes.GetString();
} }
// End JsonUnitTestResultPrinter // End JsonUnitTestResultPrinter
#if GTEST_CAN_STREAM_RESULTS_ #if GTEST_CAN_STREAM_RESULTS_
// Checks if str contains '=', '&', '%' or '\n' characters. If yes, // Checks if str contains '=', '&', '%' or '\n' characters. If yes,
skipping to change at line 4896 skipping to change at line 4905
} }
return result; return result;
} }
void StreamingListener::SocketWriter::MakeConnection() { void StreamingListener::SocketWriter::MakeConnection() {
GTEST_CHECK_(sockfd_ == -1) GTEST_CHECK_(sockfd_ == -1)
<< "MakeConnection() can't be called when there is already a connection."; << "MakeConnection() can't be called when there is already a connection.";
addrinfo hints; addrinfo hints;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses.
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
addrinfo* servinfo = nullptr; addrinfo* servinfo = nullptr;
// Use the getaddrinfo() to get a linked list of IP addresses for // Use the getaddrinfo() to get a linked list of IP addresses for
// the given host name. // the given host name.
const int error_num = getaddrinfo( const int error_num =
host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); getaddrinfo(host_name_.c_str(), port_num_.c_str(), &hints, &servinfo);
if (error_num != 0) { if (error_num != 0) {
GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: "
<< gai_strerror(error_num); << gai_strerror(error_num);
} }
// Loop through all the results and connect to the first we can. // Loop through all the results and connect to the first we can.
for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != nullptr; for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != nullptr;
cur_addr = cur_addr->ai_next) { cur_addr = cur_addr->ai_next) {
sockfd_ = socket( sockfd_ = socket(cur_addr->ai_family, cur_addr->ai_socktype,
cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol); cur_addr->ai_protocol);
if (sockfd_ != -1) { if (sockfd_ != -1) {
// Connect the client socket to the server socket. // Connect the client socket to the server socket.
if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) {
close(sockfd_); close(sockfd_);
sockfd_ = -1; sockfd_ = -1;
} }
} }
} }
freeaddrinfo(servinfo); // all done with this structure freeaddrinfo(servinfo); // all done with this structure
skipping to change at line 4963 skipping to change at line 4972
absl::GetStackTrace(&raw_stack[0], max_depth, skip_count + 1); absl::GetStackTrace(&raw_stack[0], max_depth, skip_count + 1);
void* caller_frame = nullptr; void* caller_frame = nullptr;
{ {
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
caller_frame = caller_frame_; caller_frame = caller_frame_;
} }
for (int i = 0; i < raw_stack_size; ++i) { for (int i = 0; i < raw_stack_size; ++i) {
if (raw_stack[i] == caller_frame && if (raw_stack[i] == caller_frame &&
!GTEST_FLAG(show_internal_stack_frames)) { !GTEST_FLAG_GET(show_internal_stack_frames)) {
// Add a marker to the trace and stop adding frames. // Add a marker to the trace and stop adding frames.
absl::StrAppend(&result, kElidedFramesMarker, "\n"); absl::StrAppend(&result, kElidedFramesMarker, "\n");
break; break;
} }
char tmp[1024]; char tmp[1024];
const char* symbol = "(unknown)"; const char* symbol = "(unknown)";
if (absl::Symbolize(raw_stack[i], tmp, sizeof(tmp))) { if (absl::Symbolize(raw_stack[i], tmp, sizeof(tmp))) {
symbol = tmp; symbol = tmp;
} }
char line[1024]; char line[1024];
snprintf(line, sizeof(line), " %p: %s\n", raw_stack[i], symbol); snprintf(line, sizeof(line), " %p: %s\n", raw_stack[i], symbol);
result += line; result += line;
} }
return result; return result;
#else // !GTEST_HAS_ABSL #else // !GTEST_HAS_ABSL
static_cast<void>(max_depth); static_cast<void>(max_depth);
static_cast<void>(skip_count); static_cast<void>(skip_count);
return ""; return "";
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
} }
void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) { void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) {
#if GTEST_HAS_ABSL #if GTEST_HAS_ABSL
void* caller_frame = nullptr; void* caller_frame = nullptr;
if (absl::GetStackTrace(&caller_frame, 1, 3) <= 0) { if (absl::GetStackTrace(&caller_frame, 1, 3) <= 0) {
skipping to change at line 5006 skipping to change at line 5015
MutexLock lock(&mutex_); MutexLock lock(&mutex_);
caller_frame_ = caller_frame; caller_frame_ = caller_frame;
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
} }
// A helper class that creates the premature-exit file in its // A helper class that creates the premature-exit file in its
// constructor and deletes the file in its destructor. // constructor and deletes the file in its destructor.
class ScopedPrematureExitFile { class ScopedPrematureExitFile {
public: public:
explicit ScopedPrematureExitFile(const char* premature_exit_filepath) explicit ScopedPrematureExitFile(const char* premature_exit_filepath)
: premature_exit_filepath_(premature_exit_filepath ? : premature_exit_filepath_(
premature_exit_filepath : "") { premature_exit_filepath ? premature_exit_filepath : "") {
// If a path to the premature-exit file is specified... // If a path to the premature-exit file is specified...
if (!premature_exit_filepath_.empty()) { if (!premature_exit_filepath_.empty()) {
// create the file with a single "0" character in it. I/O // create the file with a single "0" character in it. I/O
// errors are ignored as there's nothing better we can do and we // errors are ignored as there's nothing better we can do and we
// don't want to fail the test because of this. // don't want to fail the test because of this.
FILE* pfile = posix::FOpen(premature_exit_filepath, "w"); FILE* pfile = posix::FOpen(premature_exit_filepath_.c_str(), "w");
fwrite("0", 1, 1, pfile); fwrite("0", 1, 1, pfile);
fclose(pfile); fclose(pfile);
} }
} }
~ScopedPrematureExitFile() { ~ScopedPrematureExitFile() {
#if !defined GTEST_OS_ESP8266 #if !defined GTEST_OS_ESP8266
if (!premature_exit_filepath_.empty()) { if (!premature_exit_filepath_.empty()) {
int retval = remove(premature_exit_filepath_.c_str()); int retval = remove(premature_exit_filepath_.c_str());
if (retval) { if (retval) {
skipping to change at line 5035 skipping to change at line 5044
<< premature_exit_filepath_ << "\" with error " << premature_exit_filepath_ << "\" with error "
<< retval; << retval;
} }
} }
#endif #endif
} }
private: private:
const std::string premature_exit_filepath_; const std::string premature_exit_filepath_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedPrematureExitFile); ScopedPrematureExitFile(const ScopedPrematureExitFile&) = delete;
ScopedPrematureExitFile& operator=(const ScopedPrematureExitFile&) = delete;
}; };
} // namespace internal } // namespace internal
// class TestEventListeners // class TestEventListeners
TestEventListeners::TestEventListeners() TestEventListeners::TestEventListeners()
: repeater_(new internal::TestEventRepeater()), : repeater_(new internal::TestEventRepeater()),
default_result_printer_(nullptr), default_result_printer_(nullptr),
default_xml_generator_(nullptr) {} default_xml_generator_(nullptr) {}
skipping to change at line 5209 skipping to change at line 5219
// Gets the number of all tests. // Gets the number of all tests.
int UnitTest::total_test_count() const { return impl()->total_test_count(); } int UnitTest::total_test_count() const { return impl()->total_test_count(); }
// Gets the number of tests that should run. // Gets the number of tests that should run.
int UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); } int UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); }
// Gets the time of the test program start, in ms from the start of the // Gets the time of the test program start, in ms from the start of the
// UNIX epoch. // UNIX epoch.
internal::TimeInMillis UnitTest::start_timestamp() const { internal::TimeInMillis UnitTest::start_timestamp() const {
return impl()->start_timestamp(); return impl()->start_timestamp();
} }
// Gets the elapsed time, in milliseconds. // Gets the elapsed time, in milliseconds.
internal::TimeInMillis UnitTest::elapsed_time() const { internal::TimeInMillis UnitTest::elapsed_time() const {
return impl()->elapsed_time(); return impl()->elapsed_time();
} }
// Returns true if and only if the unit test passed (i.e. all test suites // Returns true if and only if the unit test passed (i.e. all test suites
// passed). // passed).
bool UnitTest::Passed() const { return impl()->Passed(); } bool UnitTest::Passed() const { return impl()->Passed(); }
skipping to change at line 5252 skipping to change at line 5262
} }
// Gets the i-th test suite among all the test suites. i can range from 0 to // Gets the i-th test suite among all the test suites. i can range from 0 to
// total_test_suite_count() - 1. If i is not in that range, returns NULL. // total_test_suite_count() - 1. If i is not in that range, returns NULL.
TestSuite* UnitTest::GetMutableTestSuite(int i) { TestSuite* UnitTest::GetMutableTestSuite(int i) {
return impl()->GetMutableSuiteCase(i); return impl()->GetMutableSuiteCase(i);
} }
// Returns the list of event listeners that can be used to track events // Returns the list of event listeners that can be used to track events
// inside Google Test. // inside Google Test.
TestEventListeners& UnitTest::listeners() { TestEventListeners& UnitTest::listeners() { return *impl()->listeners(); }
return *impl()->listeners();
}
// Registers and returns a global test environment. When a test // Registers and returns a global test environment. When a test
// program is run, all global test environments will be set-up in the // program is run, all global test environments will be set-up in the
// order they were registered. After all tests in the program have // order they were registered. After all tests in the program have
// finished, all global test environments will be torn-down in the // finished, all global test environments will be torn-down in the
// *reverse* order they were registered. // *reverse* order they were registered.
// //
// The UnitTest object takes ownership of the given environment. // The UnitTest object takes ownership of the given environment.
// //
// We don't protect this under mutex_, as we only support calling it // We don't protect this under mutex_, as we only support calling it
skipping to change at line 5279 skipping to change at line 5287
} }
impl_->environments().push_back(env); impl_->environments().push_back(env);
return env; return env;
} }
// Adds a TestPartResult to the current TestResult object. All Google Test // Adds a TestPartResult to the current TestResult object. All Google Test
// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call // assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call
// this to report their results. The user code should use the // this to report their results. The user code should use the
// assertion macros instead of calling this directly. // assertion macros instead of calling this directly.
void UnitTest::AddTestPartResult( void UnitTest::AddTestPartResult(TestPartResult::Type result_type,
TestPartResult::Type result_type, const char* file_name, int line_number,
const char* file_name, const std::string& message,
int line_number, const std::string& os_stack_trace)
const std::string& message, GTEST_LOCK_EXCLUDED_(mutex_) {
const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_) {
Message msg; Message msg;
msg << message; msg << message;
internal::MutexLock lock(&mutex_); internal::MutexLock lock(&mutex_);
if (impl_->gtest_trace_stack().size() > 0) { if (impl_->gtest_trace_stack().size() > 0) {
msg << "\n" << GTEST_NAME_ << " trace:"; msg << "\n" << GTEST_NAME_ << " trace:";
for (size_t i = impl_->gtest_trace_stack().size(); i > 0; --i) { for (size_t i = impl_->gtest_trace_stack().size(); i > 0; --i) {
const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1]; const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1];
msg << "\n" << internal::FormatFileLocation(trace.file, trace.line) msg << "\n"
<< " " << trace.message; << internal::FormatFileLocation(trace.file, trace.line) << " "
<< trace.message;
} }
} }
if (os_stack_trace.c_str() != nullptr && !os_stack_trace.empty()) { if (os_stack_trace.c_str() != nullptr && !os_stack_trace.empty()) {
msg << internal::kStackTraceMarker << os_stack_trace; msg << internal::kStackTraceMarker << os_stack_trace;
} }
const TestPartResult result = TestPartResult( const TestPartResult result = TestPartResult(
result_type, file_name, line_number, msg.GetString().c_str()); result_type, file_name, line_number, msg.GetString().c_str());
impl_->GetTestPartResultReporterForCurrentThread()-> impl_->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult(
ReportTestPartResult(result); result);
if (result_type != TestPartResult::kSuccess && if (result_type != TestPartResult::kSuccess &&
result_type != TestPartResult::kSkip) { result_type != TestPartResult::kSkip) {
// gtest_break_on_failure takes precedence over // gtest_break_on_failure takes precedence over
// gtest_throw_on_failure. This allows a user to set the latter // gtest_throw_on_failure. This allows a user to set the latter
// in the code (perhaps in order to use Google Test assertions // in the code (perhaps in order to use Google Test assertions
// with another testing framework) and specify the former on the // with another testing framework) and specify the former on the
// command line for debugging. // command line for debugging.
if (GTEST_FLAG(break_on_failure)) { if (GTEST_FLAG_GET(break_on_failure)) {
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
// Using DebugBreak on Windows allows gtest to still break into a debugger // Using DebugBreak on Windows allows gtest to still break into a debugger
// when a failure happens and both the --gtest_break_on_failure and // when a failure happens and both the --gtest_break_on_failure and
// the --gtest_catch_exceptions flags are specified. // the --gtest_catch_exceptions flags are specified.
DebugBreak(); DebugBreak();
#elif (!defined(__native_client__)) && \ #elif (!defined(__native_client__)) && \
((defined(__clang__) || defined(__GNUC__)) && \ ((defined(__clang__) || defined(__GNUC__)) && \
(defined(__x86_64__) || defined(__i386__))) (defined(__x86_64__) || defined(__i386__)))
// with clang/gcc we can achieve the same effect on x86 by invoking int3 // with clang/gcc we can achieve the same effect on x86 by invoking int3
asm("int3"); asm("int3");
#else #else
// Dereference nullptr through a volatile pointer to prevent the compiler // Dereference nullptr through a volatile pointer to prevent the compiler
// from removing. We use this rather than abort() or __builtin_trap() for // from removing. We use this rather than abort() or __builtin_trap() for
// portability: some debuggers don't correctly trap abort(). // portability: some debuggers don't correctly trap abort().
*static_cast<volatile int*>(nullptr) = 1; *static_cast<volatile int*>(nullptr) = 1;
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
} else if (GTEST_FLAG(throw_on_failure)) { } else if (GTEST_FLAG_GET(throw_on_failure)) {
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
throw internal::GoogleTestFailureException(result); throw internal::GoogleTestFailureException(result);
#else #else
// We cannot call abort() as it generates a pop-up in debug mode // We cannot call abort() as it generates a pop-up in debug mode
// that cannot be suppressed in VC 7.1 or below. // that cannot be suppressed in VC 7.1 or below.
exit(1); exit(1);
#endif #endif
} }
} }
} }
skipping to change at line 5361 skipping to change at line 5369
impl_->RecordProperty(TestProperty(key, value)); impl_->RecordProperty(TestProperty(key, value));
} }
// Runs all tests in this UnitTest object and prints the result. // Runs all tests in this UnitTest object and prints the result.
// Returns 0 if successful, or 1 otherwise. // Returns 0 if successful, or 1 otherwise.
// //
// We don't protect this under mutex_, as we only support calling it // We don't protect this under mutex_, as we only support calling it
// from the main thread. // from the main thread.
int UnitTest::Run() { int UnitTest::Run() {
const bool in_death_test_child_process = const bool in_death_test_child_process =
internal::GTEST_FLAG(internal_run_death_test).length() > 0; GTEST_FLAG_GET(internal_run_death_test).length() > 0;
// Google Test implements this protocol for catching that a test // Google Test implements this protocol for catching that a test
// program exits before returning control to Google Test: // program exits before returning control to Google Test:
// //
// 1. Upon start, Google Test creates a file whose absolute path // 1. Upon start, Google Test creates a file whose absolute path
// is specified by the environment variable // is specified by the environment variable
// TEST_PREMATURE_EXIT_FILE. // TEST_PREMATURE_EXIT_FILE.
// 2. When Google Test has finished its work, it deletes the file. // 2. When Google Test has finished its work, it deletes the file.
// //
// This allows a test runner to set TEST_PREMATURE_EXIT_FILE before // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before
skipping to change at line 5391 skipping to change at line 5399
// premature-exit file will be left undeleted, causing a test runner // premature-exit file will be left undeleted, causing a test runner
// that understands the premature-exit-file protocol to report the // that understands the premature-exit-file protocol to report the
// test as having failed. // test as having failed.
const internal::ScopedPrematureExitFile premature_exit_file( const internal::ScopedPrematureExitFile premature_exit_file(
in_death_test_child_process in_death_test_child_process
? nullptr ? nullptr
: internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE")); : internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE"));
// Captures the value of GTEST_FLAG(catch_exceptions). This value will be // Captures the value of GTEST_FLAG(catch_exceptions). This value will be
// used for the duration of the program. // used for the duration of the program.
impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions)); impl()->set_catch_exceptions(GTEST_FLAG_GET(catch_exceptions));
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
// Either the user wants Google Test to catch exceptions thrown by the // Either the user wants Google Test to catch exceptions thrown by the
// tests or this is executing in the context of death test child // tests or this is executing in the context of death test child
// process. In either case the user does not want to see pop-up dialogs // process. In either case the user does not want to see pop-up dialogs
// about crashes - they are expected. // about crashes - they are expected.
if (impl()->catch_exceptions() || in_death_test_child_process) { if (impl()->catch_exceptions() || in_death_test_child_process) {
# if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
// SetErrorMode doesn't exist on CE. // SetErrorMode doesn't exist on CE.
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
# endif // !GTEST_OS_WINDOWS_MOBILE #endif // !GTEST_OS_WINDOWS_MOBILE
# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE #if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE
// Death test children can be terminated with _abort(). On Windows, // Death test children can be terminated with _abort(). On Windows,
// _abort() can show a dialog with a warning message. This forces the // _abort() can show a dialog with a warning message. This forces the
// abort message to go to stderr instead. // abort message to go to stderr instead.
_set_error_mode(_OUT_TO_STDERR); _set_error_mode(_OUT_TO_STDERR);
# endif #endif
# if defined(_MSC_VER) && !GTEST_OS_WINDOWS_MOBILE #if defined(_MSC_VER) && !GTEST_OS_WINDOWS_MOBILE
// In the debug version, Visual Studio pops up a separate dialog // In the debug version, Visual Studio pops up a separate dialog
// offering a choice to debug the aborted program. We need to suppress // offering a choice to debug the aborted program. We need to suppress
// this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement
// executed. Google Test will notify the user of any unexpected // executed. Google Test will notify the user of any unexpected
// failure via stderr. // failure via stderr.
if (!GTEST_FLAG(break_on_failure)) if (!GTEST_FLAG_GET(break_on_failure))
_set_abort_behavior( _set_abort_behavior(
0x0, // Clear the following flags: 0x0, // Clear the following flags:
_WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump.
// In debug mode, the Windows CRT can crash with an assertion over invalid // In debug mode, the Windows CRT can crash with an assertion over invalid
// input (e.g. passing an invalid file descriptor). The default handling // input (e.g. passing an invalid file descriptor). The default handling
// for these assertions is to pop up a dialog and wait for user input. // for these assertions is to pop up a dialog and wait for user input.
// Instead ask the CRT to dump such assertions to stderr non-interactively. // Instead ask the CRT to dump such assertions to stderr non-interactively.
if (!IsDebuggerPresent()) { if (!IsDebuggerPresent()) {
(void)_CrtSetReportMode(_CRT_ASSERT, (void)_CrtSetReportMode(_CRT_ASSERT,
_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
(void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); (void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
} }
# endif #endif
} }
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
return internal::HandleExceptionsInMethodIfSupported( return internal::HandleExceptionsInMethodIfSupported(
impl(), impl(), &internal::UnitTestImpl::RunAllTests,
&internal::UnitTestImpl::RunAllTests, "auxiliary test code (environments or event listeners)")
"auxiliary test code (environments or event listeners)") ? 0 : 1; ? 0
: 1;
} }
// Returns the working directory when the first TEST() or TEST_F() was // Returns the working directory when the first TEST() or TEST_F() was
// executed. // executed.
const char* UnitTest::original_working_dir() const { const char* UnitTest::original_working_dir() const {
return impl_->original_working_dir_.c_str(); return impl_->original_working_dir_.c_str();
} }
// Returns the TestSuite object for the test that's currently running, // Returns the TestSuite object for the test that's currently running,
// or NULL if no test is running. // or NULL if no test is running.
skipping to change at line 5484 skipping to change at line 5493
int UnitTest::random_seed() const { return impl_->random_seed(); } int UnitTest::random_seed() const { return impl_->random_seed(); }
// Returns ParameterizedTestSuiteRegistry object used to keep track of // Returns ParameterizedTestSuiteRegistry object used to keep track of
// value-parameterized tests and instantiate and register them. // value-parameterized tests and instantiate and register them.
internal::ParameterizedTestSuiteRegistry& internal::ParameterizedTestSuiteRegistry&
UnitTest::parameterized_test_registry() GTEST_LOCK_EXCLUDED_(mutex_) { UnitTest::parameterized_test_registry() GTEST_LOCK_EXCLUDED_(mutex_) {
return impl_->parameterized_test_registry(); return impl_->parameterized_test_registry();
} }
// Creates an empty UnitTest. // Creates an empty UnitTest.
UnitTest::UnitTest() { UnitTest::UnitTest() { impl_ = new internal::UnitTestImpl(this); }
impl_ = new internal::UnitTestImpl(this);
}
// Destructor of UnitTest. // Destructor of UnitTest.
UnitTest::~UnitTest() { UnitTest::~UnitTest() { delete impl_; }
delete impl_;
}
// Pushes a trace defined by SCOPED_TRACE() on to the per-thread // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
// Google Test trace stack. // Google Test trace stack.
void UnitTest::PushGTestTrace(const internal::TraceInfo& trace) void UnitTest::PushGTestTrace(const internal::TraceInfo& trace)
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(&mutex_);
impl_->gtest_trace_stack().push_back(trace); impl_->gtest_trace_stack().push_back(trace);
} }
// Pops a trace from the per-thread Google Test trace stack. // Pops a trace from the per-thread Google Test trace stack.
void UnitTest::PopGTestTrace() void UnitTest::PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_) {
GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(&mutex_);
impl_->gtest_trace_stack().pop_back(); impl_->gtest_trace_stack().pop_back();
} }
namespace internal { namespace internal {
UnitTestImpl::UnitTestImpl(UnitTest* parent) UnitTestImpl::UnitTestImpl(UnitTest* parent)
: parent_(parent), : parent_(parent),
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355 /* using this in initializer */) GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355 /* using this in initializer */)
default_global_test_part_result_reporter_(this), default_global_test_part_result_reporter_(this),
skipping to change at line 5600 skipping to change at line 5604
} else if (output_format != "") { } else if (output_format != "") {
GTEST_LOG_(WARNING) << "WARNING: unrecognized output format \"" GTEST_LOG_(WARNING) << "WARNING: unrecognized output format \""
<< output_format << "\" ignored."; << output_format << "\" ignored.";
} }
} }
#if GTEST_CAN_STREAM_RESULTS_ #if GTEST_CAN_STREAM_RESULTS_
// Initializes event listeners for streaming test results in string form. // Initializes event listeners for streaming test results in string form.
// Must not be called before InitGoogleTest. // Must not be called before InitGoogleTest.
void UnitTestImpl::ConfigureStreamingOutput() { void UnitTestImpl::ConfigureStreamingOutput() {
const std::string& target = GTEST_FLAG(stream_result_to); const std::string& target = GTEST_FLAG_GET(stream_result_to);
if (!target.empty()) { if (!target.empty()) {
const size_t pos = target.find(':'); const size_t pos = target.find(':');
if (pos != std::string::npos) { if (pos != std::string::npos) {
listeners()->Append(new StreamingListener(target.substr(0, pos), listeners()->Append(
target.substr(pos+1))); new StreamingListener(target.substr(0, pos), target.substr(pos + 1)));
} else { } else {
GTEST_LOG_(WARNING) << "unrecognized streaming target \"" << target GTEST_LOG_(WARNING) << "unrecognized streaming target \"" << target
<< "\" ignored."; << "\" ignored.";
} }
} }
} }
#endif // GTEST_CAN_STREAM_RESULTS_ #endif // GTEST_CAN_STREAM_RESULTS_
// Performs initialization dependent upon flag values obtained in // Performs initialization dependent upon flag values obtained in
// ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to
skipping to change at line 5643 skipping to change at line 5647
// Registers parameterized tests. This makes parameterized tests // Registers parameterized tests. This makes parameterized tests
// available to the UnitTest reflection API without running // available to the UnitTest reflection API without running
// RUN_ALL_TESTS. // RUN_ALL_TESTS.
RegisterParameterizedTests(); RegisterParameterizedTests();
// Configures listeners for XML output. This makes it possible for users // Configures listeners for XML output. This makes it possible for users
// to shut down the default XML output before invoking RUN_ALL_TESTS. // to shut down the default XML output before invoking RUN_ALL_TESTS.
ConfigureXmlOutput(); ConfigureXmlOutput();
if (GTEST_FLAG(brief)) { if (GTEST_FLAG_GET(brief)) {
listeners()->SetDefaultResultPrinter(new BriefUnitTestResultPrinter); listeners()->SetDefaultResultPrinter(new BriefUnitTestResultPrinter);
} }
#if GTEST_CAN_STREAM_RESULTS_ #if GTEST_CAN_STREAM_RESULTS_
// Configures listeners for streaming test results to the specified server. // Configures listeners for streaming test results to the specified server.
ConfigureStreamingOutput(); ConfigureStreamingOutput();
#endif // GTEST_CAN_STREAM_RESULTS_ #endif // GTEST_CAN_STREAM_RESULTS_
#if GTEST_HAS_ABSL #if GTEST_HAS_ABSL
if (GTEST_FLAG(install_failure_signal_handler)) { if (GTEST_FLAG_GET(install_failure_signal_handler)) {
absl::FailureSignalHandlerOptions options; absl::FailureSignalHandlerOptions options;
absl::InstallFailureSignalHandler(options); absl::InstallFailureSignalHandler(options);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
} }
} }
// A predicate that checks the name of a TestSuite against a known // A predicate that checks the name of a TestSuite against a known
// value. // value.
// //
skipping to change at line 5711 skipping to change at line 5715
const auto test_suite = const auto test_suite =
std::find_if(test_suites_.rbegin(), test_suites_.rend(), std::find_if(test_suites_.rbegin(), test_suites_.rend(),
TestSuiteNameIs(test_suite_name)); TestSuiteNameIs(test_suite_name));
if (test_suite != test_suites_.rend()) return *test_suite; if (test_suite != test_suites_.rend()) return *test_suite;
// No. Let's create one. // No. Let's create one.
auto* const new_test_suite = auto* const new_test_suite =
new TestSuite(test_suite_name, type_param, set_up_tc, tear_down_tc); new TestSuite(test_suite_name, type_param, set_up_tc, tear_down_tc);
const UnitTestFilter death_test_suite_filter(kDeathTestSuiteFilter);
// Is this a death test suite? // Is this a death test suite?
if (internal::UnitTestOptions::MatchesFilter(test_suite_name, if (death_test_suite_filter.MatchesName(test_suite_name)) {
kDeathTestSuiteFilter)) {
// Yes. Inserts the test suite after the last death test suite // Yes. Inserts the test suite after the last death test suite
// defined so far. This only works when the test suites haven't // defined so far. This only works when the test suites haven't
// been shuffled. Otherwise we may end up running a death test // been shuffled. Otherwise we may end up running a death test
// after a non-death test. // after a non-death test.
++last_death_test_suite_; ++last_death_test_suite_;
test_suites_.insert(test_suites_.begin() + last_death_test_suite_, test_suites_.insert(test_suites_.begin() + last_death_test_suite_,
new_test_suite); new_test_suite);
} else { } else {
// No. Appends to the end of the list. // No. Appends to the end of the list.
test_suites_.push_back(new_test_suite); test_suites_.push_back(new_test_suite);
skipping to change at line 5750 skipping to change at line 5754
// When parameterized tests are enabled, it expands and registers // When parameterized tests are enabled, it expands and registers
// parameterized tests first in RegisterParameterizedTests(). // parameterized tests first in RegisterParameterizedTests().
// All other functions called from RunAllTests() may safely assume that // All other functions called from RunAllTests() may safely assume that
// parameterized tests are ready to be counted and run. // parameterized tests are ready to be counted and run.
bool UnitTestImpl::RunAllTests() { bool UnitTestImpl::RunAllTests() {
// True if and only if Google Test is initialized before RUN_ALL_TESTS() is // True if and only if Google Test is initialized before RUN_ALL_TESTS() is
// called. // called.
const bool gtest_is_initialized_before_run_all_tests = GTestIsInitialized(); const bool gtest_is_initialized_before_run_all_tests = GTestIsInitialized();
// Do not run any test if the --help flag was specified. // Do not run any test if the --help flag was specified.
if (g_help_flag) if (g_help_flag) return true;
return true;
// Repeats the call to the post-flag parsing initialization in case the // Repeats the call to the post-flag parsing initialization in case the
// user didn't call InitGoogleTest. // user didn't call InitGoogleTest.
PostFlagParsingInit(); PostFlagParsingInit();
// Even if sharding is not on, test runners may want to use the // Even if sharding is not on, test runners may want to use the
// GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding
// protocol. // protocol.
internal::WriteToShardStatusFileIfNeeded(); internal::WriteToShardStatusFileIfNeeded();
// True if and only if we are in a subprocess for running a thread-safe-style // True if and only if we are in a subprocess for running a thread-safe-style
// death test. // death test.
bool in_subprocess_for_death_test = false; bool in_subprocess_for_death_test = false;
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_DEATH_TEST
in_subprocess_for_death_test = in_subprocess_for_death_test =
(internal_run_death_test_flag_.get() != nullptr); (internal_run_death_test_flag_.get() != nullptr);
# if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) #if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)
if (in_subprocess_for_death_test) { if (in_subprocess_for_death_test) {
GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_(); GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_();
} }
# endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) #endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)
#endif // GTEST_HAS_DEATH_TEST #endif // GTEST_HAS_DEATH_TEST
const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex,
in_subprocess_for_death_test); in_subprocess_for_death_test);
// Compares the full test names with the filter to decide which // Compares the full test names with the filter to decide which
// tests to run. // tests to run.
const bool has_tests_to_run = FilterTests(should_shard const bool has_tests_to_run =
? HONOR_SHARDING_PROTOCOL FilterTests(should_shard ? HONOR_SHARDING_PROTOCOL
: IGNORE_SHARDING_PROTOCOL) > 0; : IGNORE_SHARDING_PROTOCOL) > 0;
// Lists the tests and exits if the --gtest_list_tests flag was specified. // Lists the tests and exits if the --gtest_list_tests flag was specified.
if (GTEST_FLAG(list_tests)) { if (GTEST_FLAG_GET(list_tests)) {
// This must be called *after* FilterTests() has been called. // This must be called *after* FilterTests() has been called.
ListTestsMatchingFilter(); ListTestsMatchingFilter();
return true; return true;
} }
random_seed_ = GTEST_FLAG(shuffle) ? random_seed_ = GetRandomSeedFromFlag(GTEST_FLAG_GET(random_seed));
GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;
// True if and only if at least one test has failed. // True if and only if at least one test has failed.
bool failed = false; bool failed = false;
TestEventListener* repeater = listeners()->repeater(); TestEventListener* repeater = listeners()->repeater();
start_timestamp_ = GetTimeInMillis(); start_timestamp_ = GetTimeInMillis();
repeater->OnTestProgramStart(*parent_); repeater->OnTestProgramStart(*parent_);
// How many times to repeat the tests? We don't want to repeat them // How many times to repeat the tests? We don't want to repeat them
// when we are inside the subprocess of a death test. // when we are inside the subprocess of a death test.
const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat); const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG_GET(repeat);
// Repeats forever if the repeat count is negative. // Repeats forever if the repeat count is negative.
const bool gtest_repeat_forever = repeat < 0; const bool gtest_repeat_forever = repeat < 0;
// Should test environments be set up and torn down for each repeat, or only
// set up on the first and torn down on the last iteration? If there is no
// "last" iteration because the tests will repeat forever, always recreate the
// environments to avoid leaks in case one of the environments is using
// resources that are external to this process. Without this check there would
// be no way to clean up those external resources automatically.
const bool recreate_environments_when_repeating =
GTEST_FLAG_GET(recreate_environments_when_repeating) ||
gtest_repeat_forever;
for (int i = 0; gtest_repeat_forever || i != repeat; i++) { for (int i = 0; gtest_repeat_forever || i != repeat; i++) {
// We want to preserve failures generated by ad-hoc test // We want to preserve failures generated by ad-hoc test
// assertions executed before RUN_ALL_TESTS(). // assertions executed before RUN_ALL_TESTS().
ClearNonAdHocTestResult(); ClearNonAdHocTestResult();
Timer timer; Timer timer;
// Shuffles test suites and tests if requested. // Shuffles test suites and tests if requested.
if (has_tests_to_run && GTEST_FLAG(shuffle)) { if (has_tests_to_run && GTEST_FLAG_GET(shuffle)) {
random()->Reseed(static_cast<uint32_t>(random_seed_)); random()->Reseed(static_cast<uint32_t>(random_seed_));
// This should be done before calling OnTestIterationStart(), // This should be done before calling OnTestIterationStart(),
// such that a test event listener can see the actual test order // such that a test event listener can see the actual test order
// in the event. // in the event.
ShuffleTests(); ShuffleTests();
} }
// Tells the unit test event listeners that the tests are about to start. // Tells the unit test event listeners that the tests are about to start.
repeater->OnTestIterationStart(*parent_, i); repeater->OnTestIterationStart(*parent_, i);
// Runs each test suite if there is at least one test to run. // Runs each test suite if there is at least one test to run.
if (has_tests_to_run) { if (has_tests_to_run) {
// Sets up all environments beforehand. // Sets up all environments beforehand. If test environments aren't
repeater->OnEnvironmentsSetUpStart(*parent_); // recreated for each iteration, only do so on the first iteration.
ForEach(environments_, SetUpEnvironment); if (i == 0 || recreate_environments_when_repeating) {
repeater->OnEnvironmentsSetUpEnd(*parent_); repeater->OnEnvironmentsSetUpStart(*parent_);
ForEach(environments_, SetUpEnvironment);
repeater->OnEnvironmentsSetUpEnd(*parent_);
}
// Runs the tests only if there was no fatal failure or skip triggered // Runs the tests only if there was no fatal failure or skip triggered
// during global set-up. // during global set-up.
if (Test::IsSkipped()) { if (Test::IsSkipped()) {
// Emit diagnostics when global set-up calls skip, as it will not be // Emit diagnostics when global set-up calls skip, as it will not be
// emitted by default. // emitted by default.
TestResult& test_result = TestResult& test_result =
*internal::GetUnitTestImpl()->current_test_result(); *internal::GetUnitTestImpl()->current_test_result();
for (int j = 0; j < test_result.total_part_count(); ++j) { for (int j = 0; j < test_result.total_part_count(); ++j) {
const TestPartResult& test_part_result = const TestPartResult& test_part_result =
skipping to change at line 5854 skipping to change at line 5871
if (test_part_result.type() == TestPartResult::kSkip) { if (test_part_result.type() == TestPartResult::kSkip) {
const std::string& result = test_part_result.message(); const std::string& result = test_part_result.message();
printf("%s\n", result.c_str()); printf("%s\n", result.c_str());
} }
} }
fflush(stdout); fflush(stdout);
} else if (!Test::HasFatalFailure()) { } else if (!Test::HasFatalFailure()) {
for (int test_index = 0; test_index < total_test_suite_count(); for (int test_index = 0; test_index < total_test_suite_count();
test_index++) { test_index++) {
GetMutableSuiteCase(test_index)->Run(); GetMutableSuiteCase(test_index)->Run();
if (GTEST_FLAG(fail_fast) && if (GTEST_FLAG_GET(fail_fast) &&
GetMutableSuiteCase(test_index)->Failed()) { GetMutableSuiteCase(test_index)->Failed()) {
for (int j = test_index + 1; j < total_test_suite_count(); j++) { for (int j = test_index + 1; j < total_test_suite_count(); j++) {
GetMutableSuiteCase(j)->Skip(); GetMutableSuiteCase(j)->Skip();
} }
break; break;
} }
} }
} else if (Test::HasFatalFailure()) { } else if (Test::HasFatalFailure()) {
// If there was a fatal failure during the global setup then we know we // If there was a fatal failure during the global setup then we know we
// aren't going to run any tests. Explicitly mark all of the tests as // aren't going to run any tests. Explicitly mark all of the tests as
// skipped to make this obvious in the output. // skipped to make this obvious in the output.
for (int test_index = 0; test_index < total_test_suite_count(); for (int test_index = 0; test_index < total_test_suite_count();
test_index++) { test_index++) {
GetMutableSuiteCase(test_index)->Skip(); GetMutableSuiteCase(test_index)->Skip();
} }
} }
// Tears down all environments in reverse order afterwards. // Tears down all environments in reverse order afterwards. If test
repeater->OnEnvironmentsTearDownStart(*parent_); // environments aren't recreated for each iteration, only do so on the
std::for_each(environments_.rbegin(), environments_.rend(), // last iteration.
TearDownEnvironment); if (i == repeat - 1 || recreate_environments_when_repeating) {
repeater->OnEnvironmentsTearDownEnd(*parent_); repeater->OnEnvironmentsTearDownStart(*parent_);
std::for_each(environments_.rbegin(), environments_.rend(),
TearDownEnvironment);
repeater->OnEnvironmentsTearDownEnd(*parent_);
}
} }
elapsed_time_ = timer.Elapsed(); elapsed_time_ = timer.Elapsed();
// Tells the unit test event listener that the tests have just finished. // Tells the unit test event listener that the tests have just finished.
repeater->OnTestIterationEnd(*parent_, i); repeater->OnTestIterationEnd(*parent_, i);
// Gets the result and clears it. // Gets the result and clears it.
if (!Passed()) { if (!Passed()) {
failed = true; failed = true;
} }
// Restores the original test order after the iteration. This // Restores the original test order after the iteration. This
// allows the user to quickly repro a failure that happens in the // allows the user to quickly repro a failure that happens in the
// N-th iteration without repeating the first (N - 1) iterations. // N-th iteration without repeating the first (N - 1) iterations.
// This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in
// case the user somehow changes the value of the flag somewhere // case the user somehow changes the value of the flag somewhere
// (it's always safe to unshuffle the tests). // (it's always safe to unshuffle the tests).
UnshuffleTests(); UnshuffleTests();
if (GTEST_FLAG(shuffle)) { if (GTEST_FLAG_GET(shuffle)) {
// Picks a new random seed for each iteration. // Picks a new random seed for each iteration.
random_seed_ = GetNextRandomSeed(random_seed_); random_seed_ = GetNextRandomSeed(random_seed_);
} }
} }
repeater->OnTestProgramEnd(*parent_); repeater->OnTestProgramEnd(*parent_);
if (!gtest_is_initialized_before_run_all_tests) { if (!gtest_is_initialized_before_run_all_tests) {
ColoredPrintf( ColoredPrintf(
GTestColor::kRed, GTestColor::kRed,
skipping to change at line 5948 skipping to change at line 5969
fclose(file); fclose(file);
} }
} }
// Checks whether sharding is enabled by examining the relevant // Checks whether sharding is enabled by examining the relevant
// environment variable values. If the variables are present, // environment variable values. If the variables are present,
// but inconsistent (i.e., shard_index >= total_shards), prints // but inconsistent (i.e., shard_index >= total_shards), prints
// an error and exits. If in_subprocess_for_death_test, sharding is // an error and exits. If in_subprocess_for_death_test, sharding is
// disabled because it must only be applied to the original test // disabled because it must only be applied to the original test
// process. Otherwise, we could filter out death tests we intended to execute. // process. Otherwise, we could filter out death tests we intended to execute.
bool ShouldShard(const char* total_shards_env, bool ShouldShard(const char* total_shards_env, const char* shard_index_env,
const char* shard_index_env,
bool in_subprocess_for_death_test) { bool in_subprocess_for_death_test) {
if (in_subprocess_for_death_test) { if (in_subprocess_for_death_test) {
return false; return false;
} }
const int32_t total_shards = Int32FromEnvOrDie(total_shards_env, -1); const int32_t total_shards = Int32FromEnvOrDie(total_shards_env, -1);
const int32_t shard_index = Int32FromEnvOrDie(shard_index_env, -1); const int32_t shard_index = Int32FromEnvOrDie(shard_index_env, -1);
if (total_shards == -1 && shard_index == -1) { if (total_shards == -1 && shard_index == -1) {
return false; return false;
} else if (total_shards == -1 && shard_index != -1) { } else if (total_shards == -1 && shard_index != -1) {
const Message msg = Message() const Message msg = Message() << "Invalid environment variables: you have "
<< "Invalid environment variables: you have " << kTestShardIndex << " = " << shard_index
<< kTestShardIndex << " = " << shard_index << ", but have left " << kTestTotalShards
<< ", but have left " << kTestTotalShards << " unset.\n"; << " unset.\n";
ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str());
fflush(stdout); fflush(stdout);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (total_shards != -1 && shard_index == -1) { } else if (total_shards != -1 && shard_index == -1) {
const Message msg = Message() const Message msg = Message()
<< "Invalid environment variables: you have " << "Invalid environment variables: you have "
<< kTestTotalShards << " = " << total_shards << kTestTotalShards << " = " << total_shards
<< ", but have left " << kTestShardIndex << " unset.\n"; << ", but have left " << kTestShardIndex << " unset.\n";
ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str());
fflush(stdout); fflush(stdout);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (shard_index < 0 || shard_index >= total_shards) { } else if (shard_index < 0 || shard_index >= total_shards) {
const Message msg = Message() const Message msg =
<< "Invalid environment variables: we require 0 <= " Message() << "Invalid environment variables: we require 0 <= "
<< kTestShardIndex << " < " << kTestTotalShards << kTestShardIndex << " < " << kTestTotalShards
<< ", but you have " << kTestShardIndex << "=" << shard_index << ", but you have " << kTestShardIndex << "=" << shard_index
<< ", " << kTestTotalShards << "=" << total_shards << ".\n"; << ", " << kTestTotalShards << "=" << total_shards << ".\n";
ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str());
fflush(stdout); fflush(stdout);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return total_shards > 1; return total_shards > 1;
} }
// Parses the environment variable var as an Int32. If it is unset, // Parses the environment variable var as an Int32. If it is unset,
// returns default_val. If it is not an Int32, prints an error // returns default_val. If it is not an Int32, prints an error
skipping to change at line 6023 skipping to change at line 6043
} }
// Compares the name of each test with the user-specified filter to // Compares the name of each test with the user-specified filter to
// decide whether the test should be run, then records the result in // decide whether the test should be run, then records the result in
// each TestSuite and TestInfo object. // each TestSuite and TestInfo object.
// If shard_tests == true, further filters tests based on sharding // If shard_tests == true, further filters tests based on sharding
// variables in the environment - see // variables in the environment - see
// https://github.com/google/googletest/blob/master/googletest/docs/advanced.md // https://github.com/google/googletest/blob/master/googletest/docs/advanced.md
// . Returns the number of tests that should run. // . Returns the number of tests that should run.
int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ? const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL
Int32FromEnvOrDie(kTestTotalShards, -1) : -1; ? Int32FromEnvOrDie(kTestTotalShards, -1)
const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ? : -1;
Int32FromEnvOrDie(kTestShardIndex, -1) : -1; const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL
? Int32FromEnvOrDie(kTestShardIndex, -1)
: -1;
const PositiveAndNegativeUnitTestFilter gtest_flag_filter(
GTEST_FLAG_GET(filter));
const UnitTestFilter disable_test_filter(kDisableTestFilter);
// num_runnable_tests are the number of tests that will // num_runnable_tests are the number of tests that will
// run across all shards (i.e., match filter and are not disabled). // run across all shards (i.e., match filter and are not disabled).
// num_selected_tests are the number of tests to be run on // num_selected_tests are the number of tests to be run on
// this shard. // this shard.
int num_runnable_tests = 0; int num_runnable_tests = 0;
int num_selected_tests = 0; int num_selected_tests = 0;
for (auto* test_suite : test_suites_) { for (auto* test_suite : test_suites_) {
const std::string& test_suite_name = test_suite->name(); const std::string& test_suite_name = test_suite->name();
test_suite->set_should_run(false); test_suite->set_should_run(false);
for (size_t j = 0; j < test_suite->test_info_list().size(); j++) { for (size_t j = 0; j < test_suite->test_info_list().size(); j++) {
TestInfo* const test_info = test_suite->test_info_list()[j]; TestInfo* const test_info = test_suite->test_info_list()[j];
const std::string test_name(test_info->name()); const std::string test_name(test_info->name());
// A test is disabled if test suite name or test name matches // A test is disabled if test suite name or test name matches
// kDisableTestFilter. // kDisableTestFilter.
const bool is_disabled = internal::UnitTestOptions::MatchesFilter( const bool is_disabled =
test_suite_name, kDisableTestFilter) || disable_test_filter.MatchesName(test_suite_name) ||
internal::UnitTestOptions::MatchesFilter( disable_test_filter.MatchesName(test_name);
test_name, kDisableTestFilter);
test_info->is_disabled_ = is_disabled; test_info->is_disabled_ = is_disabled;
const bool matches_filter = internal::UnitTestOptions::FilterMatchesTest( const bool matches_filter =
test_suite_name, test_name); gtest_flag_filter.MatchesTest(test_suite_name, test_name);
test_info->matches_filter_ = matches_filter; test_info->matches_filter_ = matches_filter;
const bool is_runnable = const bool is_runnable =
(GTEST_FLAG(also_run_disabled_tests) || !is_disabled) && (GTEST_FLAG_GET(also_run_disabled_tests) || !is_disabled) &&
matches_filter; matches_filter;
const bool is_in_another_shard = const bool is_in_another_shard =
shard_tests != IGNORE_SHARDING_PROTOCOL && shard_tests != IGNORE_SHARDING_PROTOCOL &&
!ShouldRunTestOnShard(total_shards, shard_index, num_runnable_tests); !ShouldRunTestOnShard(total_shards, shard_index, num_runnable_tests);
test_info->is_in_another_shard_ = is_in_another_shard; test_info->is_in_another_shard_ = is_in_another_shard;
const bool is_selected = is_runnable && !is_in_another_shard; const bool is_selected = is_runnable && !is_in_another_shard;
num_runnable_tests += is_runnable; num_runnable_tests += is_runnable;
num_selected_tests += is_selected; num_selected_tests += is_selected;
skipping to change at line 6223 skipping to change at line 6247
// Returns the current OS stack trace as an std::string. // Returns the current OS stack trace as an std::string.
// //
// The maximum number of stack frames to be included is specified by // The maximum number of stack frames to be included is specified by
// the gtest_stack_trace_depth flag. The skip_count parameter // the gtest_stack_trace_depth flag. The skip_count parameter
// specifies the number of top frames to be skipped, which doesn't // specifies the number of top frames to be skipped, which doesn't
// count against the number of frames to be included. // count against the number of frames to be included.
// //
// For example, if Foo() calls Bar(), which in turn calls // For example, if Foo() calls Bar(), which in turn calls
// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in // GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. // the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
std::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_ std::string
int skip_count) { GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, int skip_count) {
// We pass skip_count + 1 to skip this wrapper function in addition // We pass skip_count + 1 to skip this wrapper function in addition
// to what the user really wants to skip. // to what the user really wants to skip.
return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1);
} }
// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to // Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to
// suppress unreachable code warnings. // suppress unreachable code warnings.
namespace { namespace {
class ClassUniqueToAlwaysTrue {}; class ClassUniqueToAlwaysTrue {};
} } // namespace
bool IsTrue(bool condition) { return condition; } bool IsTrue(bool condition) { return condition; }
bool AlwaysTrue() { bool AlwaysTrue() {
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
// This condition is always false so AlwaysTrue() never actually throws, // This condition is always false so AlwaysTrue() never actually throws,
// but it makes the compiler think that it may throw. // but it makes the compiler think that it may throw.
if (IsTrue(false)) if (IsTrue(false)) throw ClassUniqueToAlwaysTrue();
throw ClassUniqueToAlwaysTrue();
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
return true; return true;
} }
// If *pstr starts with the given prefix, modifies *pstr to be right // If *pstr starts with the given prefix, modifies *pstr to be right
// past the prefix and returns true; otherwise leaves *pstr unchanged // past the prefix and returns true; otherwise leaves *pstr unchanged
// and returns false. None of pstr, *pstr, and prefix can be NULL. // and returns false. None of pstr, *pstr, and prefix can be NULL.
bool SkipPrefix(const char* prefix, const char** pstr) { bool SkipPrefix(const char* prefix, const char** pstr) {
const size_t prefix_len = strlen(prefix); const size_t prefix_len = strlen(prefix);
if (strncmp(*pstr, prefix, prefix_len) == 0) { if (strncmp(*pstr, prefix, prefix_len) == 0) {
skipping to change at line 6265 skipping to change at line 6288
return true; return true;
} }
return false; return false;
} }
// Parses a string as a command line flag. The string should have // Parses a string as a command line flag. The string should have
// the format "--flag=value". When def_optional is true, the "=value" // the format "--flag=value". When def_optional is true, the "=value"
// part can be omitted. // part can be omitted.
// //
// Returns the value of the flag, or NULL if the parsing failed. // Returns the value of the flag, or NULL if the parsing failed.
static const char* ParseFlagValue(const char* str, const char* flag, static const char* ParseFlagValue(const char* str, const char* flag_name,
bool def_optional) { bool def_optional) {
// str and flag must not be NULL. // str and flag must not be NULL.
if (str == nullptr || flag == nullptr) return nullptr; if (str == nullptr || flag_name == nullptr) return nullptr;
// The flag must start with "--" followed by GTEST_FLAG_PREFIX_. // The flag must start with "--" followed by GTEST_FLAG_PREFIX_.
const std::string flag_str = std::string("--") + GTEST_FLAG_PREFIX_ + flag; const std::string flag_str =
std::string("--") + GTEST_FLAG_PREFIX_ + flag_name;
const size_t flag_len = flag_str.length(); const size_t flag_len = flag_str.length();
if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr; if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr;
// Skips the flag name. // Skips the flag name.
const char* flag_end = str + flag_len; const char* flag_end = str + flag_len;
// When def_optional is true, it's OK to not have a "=value" part. // When def_optional is true, it's OK to not have a "=value" part.
if (def_optional && (flag_end[0] == '\0')) { if (def_optional && (flag_end[0] == '\0')) {
return flag_end; return flag_end;
} }
skipping to change at line 6302 skipping to change at line 6326
// Parses a string for a bool flag, in the form of either // Parses a string for a bool flag, in the form of either
// "--flag=value" or "--flag". // "--flag=value" or "--flag".
// //
// In the former case, the value is taken as true as long as it does // In the former case, the value is taken as true as long as it does
// not start with '0', 'f', or 'F'. // not start with '0', 'f', or 'F'.
// //
// In the latter case, the value is taken as true. // In the latter case, the value is taken as true.
// //
// On success, stores the value of the flag in *value, and returns // On success, stores the value of the flag in *value, and returns
// true. On failure, returns false without changing *value. // true. On failure, returns false without changing *value.
static bool ParseBoolFlag(const char* str, const char* flag, bool* value) { static bool ParseFlag(const char* str, const char* flag_name, bool* value) {
// Gets the value of the flag as a string. // Gets the value of the flag as a string.
const char* const value_str = ParseFlagValue(str, flag, true); const char* const value_str = ParseFlagValue(str, flag_name, true);
// Aborts if the parsing failed. // Aborts if the parsing failed.
if (value_str == nullptr) return false; if (value_str == nullptr) return false;
// Converts the string value to a bool. // Converts the string value to a bool.
*value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
return true; return true;
} }
// Parses a string for an int32_t flag, in the form of "--flag=value". // Parses a string for an int32_t flag, in the form of "--flag=value".
// //
// On success, stores the value of the flag in *value, and returns // On success, stores the value of the flag in *value, and returns
// true. On failure, returns false without changing *value. // true. On failure, returns false without changing *value.
bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) { bool ParseFlag(const char* str, const char* flag_name, int32_t* value) {
// Gets the value of the flag as a string. // Gets the value of the flag as a string.
const char* const value_str = ParseFlagValue(str, flag, false); const char* const value_str = ParseFlagValue(str, flag_name, false);
// Aborts if the parsing failed. // Aborts if the parsing failed.
if (value_str == nullptr) return false; if (value_str == nullptr) return false;
// Sets *value to the value of the flag. // Sets *value to the value of the flag.
return ParseInt32(Message() << "The value of flag --" << flag, return ParseInt32(Message() << "The value of flag --" << flag_name, value_str,
value_str, value); value);
} }
// Parses a string for a string flag, in the form of "--flag=value". // Parses a string for a string flag, in the form of "--flag=value".
// //
// On success, stores the value of the flag in *value, and returns // On success, stores the value of the flag in *value, and returns
// true. On failure, returns false without changing *value. // true. On failure, returns false without changing *value.
template <typename String> template <typename String>
static bool ParseStringFlag(const char* str, const char* flag, String* value) { static bool ParseFlag(const char* str, const char* flag_name, String* value) {
// Gets the value of the flag as a string. // Gets the value of the flag as a string.
const char* const value_str = ParseFlagValue(str, flag, false); const char* const value_str = ParseFlagValue(str, flag_name, false);
// Aborts if the parsing failed. // Aborts if the parsing failed.
if (value_str == nullptr) return false; if (value_str == nullptr) return false;
// Sets *value to the value of the flag. // Sets *value to the value of the flag.
*value = value_str; *value = value_str;
return true; return true;
} }
// Determines whether a string has a prefix that Google Test uses for its // Determines whether a string has a prefix that Google Test uses for its
// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. // flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_.
// If Google Test detects that a command line flag has its prefix but is not // If Google Test detects that a command line flag has its prefix but is not
// recognized, it will print its help message. Flags starting with // recognized, it will print its help message. Flags starting with
// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test // GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test
// internal flags and do not trigger the help message. // internal flags and do not trigger the help message.
static bool HasGoogleTestFlagPrefix(const char* str) { static bool HasGoogleTestFlagPrefix(const char* str) {
return (SkipPrefix("--", &str) || return (SkipPrefix("--", &str) || SkipPrefix("-", &str) ||
SkipPrefix("-", &str) ||
SkipPrefix("/", &str)) && SkipPrefix("/", &str)) &&
!SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) &&
(SkipPrefix(GTEST_FLAG_PREFIX_, &str) || (SkipPrefix(GTEST_FLAG_PREFIX_, &str) ||
SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str));
} }
// Prints a string containing code-encoded text. The following escape // Prints a string containing code-encoded text. The following escape
// sequences can be used in the string to control the text color: // sequences can be used in the string to control the text color:
// //
// @@ prints a single '@' character. // @@ prints a single '@' character.
skipping to change at line 6438 skipping to change at line 6461
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"repeat=@Y[COUNT]@D\n" "repeat=@Y[COUNT]@D\n"
" Run the tests repeatedly; use a negative count to repeat forever.\n" " Run the tests repeatedly; use a negative count to repeat forever.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"shuffle@D\n" "shuffle@D\n"
" Randomize tests' orders on every iteration.\n" " Randomize tests' orders on every iteration.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"random_seed=@Y[NUMBER]@D\n" "random_seed=@Y[NUMBER]@D\n"
" Random number seed to use for shuffling test orders (between 1 and\n" " Random number seed to use for shuffling test orders (between 1 and\n"
" 99999, or 0 to use a seed based on the current time).\n" " 99999, or 0 to use a seed based on the current time).\n"
" @G--" GTEST_FLAG_PREFIX_
"recreate_environments_when_repeating@D\n"
" Sets up and tears down the global test environment on each repeat\n"
" of the test.\n"
"\n" "\n"
"Test Output:\n" "Test Output:\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n" "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n"
" Enable/disable colored output. The default is @Gauto@D.\n" " Enable/disable colored output. The default is @Gauto@D.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"brief=1@D\n" "brief=1@D\n"
" Only print test failures.\n" " Only print test failures.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"print_time=0@D\n" "print_time=0@D\n"
" Don't print the elapsed time of each test.\n" " Don't print the elapsed time of each test.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"output=@Y(@Gjson@Y|@Gxml@Y)[@G:@YDIRECTORY_PATH@G" GTEST_PATH_SEP_ "output=@Y(@Gjson@Y|@Gxml@Y)[@G:@YDIRECTORY_PATH@G" GTEST_PATH_SEP_
"@Y|@G:@YFILE_PATH]@D\n" "@Y|@G:@YFILE_PATH]@D\n"
" Generate a JSON or XML report in the given directory or with the " " Generate a JSON or XML report in the given directory or with the "
"given\n" "given\n"
" file name. @YFILE_PATH@D defaults to @Gtest_detail.xml@D.\n" " file name. @YFILE_PATH@D defaults to @Gtest_detail.xml@D.\n"
# if GTEST_CAN_STREAM_RESULTS_ #if GTEST_CAN_STREAM_RESULTS_
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"stream_result_to=@YHOST@G:@YPORT@D\n" "stream_result_to=@YHOST@G:@YPORT@D\n"
" Stream test results to the given server.\n" " Stream test results to the given server.\n"
# endif // GTEST_CAN_STREAM_RESULTS_ #endif // GTEST_CAN_STREAM_RESULTS_
"\n" "\n"
"Assertion Behavior:\n" "Assertion Behavior:\n"
# if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS #if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n" "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n"
" Set the default death test style.\n" " Set the default death test style.\n"
# endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS #endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"break_on_failure@D\n" "break_on_failure@D\n"
" Turn assertion failures into debugger break-points.\n" " Turn assertion failures into debugger break-points.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"throw_on_failure@D\n" "throw_on_failure@D\n"
" Turn assertion failures into C++ exceptions for use by an external\n" " Turn assertion failures into C++ exceptions for use by an external\n"
" test framework.\n" " test framework.\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"catch_exceptions=0@D\n" "catch_exceptions=0@D\n"
" Do not report exceptions as test failures. Instead, allow them\n" " Do not report exceptions as test failures. Instead, allow them\n"
skipping to change at line 6498 skipping to change at line 6525
"COLOR@D environment variable to @Gno@D.\n" "COLOR@D environment variable to @Gno@D.\n"
"\n" "\n"
"For more information, please read the " GTEST_NAME_ "For more information, please read the " GTEST_NAME_
" documentation at\n" " documentation at\n"
"@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ "@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_
"\n" "\n"
"(not one in your own code or tests), please report it to\n" "(not one in your own code or tests), please report it to\n"
"@G<" GTEST_DEV_EMAIL_ ">@D.\n"; "@G<" GTEST_DEV_EMAIL_ ">@D.\n";
static bool ParseGoogleTestFlag(const char* const arg) { static bool ParseGoogleTestFlag(const char* const arg) {
return ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag, #define GTEST_INTERNAL_PARSE_FLAG(flag_name) \
&GTEST_FLAG(also_run_disabled_tests)) || do { \
ParseBoolFlag(arg, kBreakOnFailureFlag, auto value = GTEST_FLAG_GET(flag_name); \
&GTEST_FLAG(break_on_failure)) || if (ParseFlag(arg, #flag_name, &value)) { \
ParseBoolFlag(arg, kCatchExceptionsFlag, GTEST_FLAG_SET(flag_name, value); \
&GTEST_FLAG(catch_exceptions)) || return true; \
ParseStringFlag(arg, kColorFlag, &GTEST_FLAG(color)) || } \
ParseStringFlag(arg, kDeathTestStyleFlag, } while (false)
&GTEST_FLAG(death_test_style)) ||
ParseBoolFlag(arg, kDeathTestUseFork, GTEST_INTERNAL_PARSE_FLAG(also_run_disabled_tests);
&GTEST_FLAG(death_test_use_fork)) || GTEST_INTERNAL_PARSE_FLAG(break_on_failure);
ParseBoolFlag(arg, kFailFast, &GTEST_FLAG(fail_fast)) || GTEST_INTERNAL_PARSE_FLAG(catch_exceptions);
ParseStringFlag(arg, kFilterFlag, &GTEST_FLAG(filter)) || GTEST_INTERNAL_PARSE_FLAG(color);
ParseStringFlag(arg, kInternalRunDeathTestFlag, GTEST_INTERNAL_PARSE_FLAG(death_test_style);
&GTEST_FLAG(internal_run_death_test)) || GTEST_INTERNAL_PARSE_FLAG(death_test_use_fork);
ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) || GTEST_INTERNAL_PARSE_FLAG(fail_fast);
ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) || GTEST_INTERNAL_PARSE_FLAG(filter);
ParseBoolFlag(arg, kBriefFlag, &GTEST_FLAG(brief)) || GTEST_INTERNAL_PARSE_FLAG(internal_run_death_test);
ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) || GTEST_INTERNAL_PARSE_FLAG(list_tests);
ParseBoolFlag(arg, kPrintUTF8Flag, &GTEST_FLAG(print_utf8)) || GTEST_INTERNAL_PARSE_FLAG(output);
ParseInt32Flag(arg, kRandomSeedFlag, &GTEST_FLAG(random_seed)) || GTEST_INTERNAL_PARSE_FLAG(brief);
ParseInt32Flag(arg, kRepeatFlag, &GTEST_FLAG(repeat)) || GTEST_INTERNAL_PARSE_FLAG(print_time);
ParseBoolFlag(arg, kShuffleFlag, &GTEST_FLAG(shuffle)) || GTEST_INTERNAL_PARSE_FLAG(print_utf8);
ParseInt32Flag(arg, kStackTraceDepthFlag, GTEST_INTERNAL_PARSE_FLAG(random_seed);
&GTEST_FLAG(stack_trace_depth)) || GTEST_INTERNAL_PARSE_FLAG(repeat);
ParseStringFlag(arg, kStreamResultToFlag, GTEST_INTERNAL_PARSE_FLAG(recreate_environments_when_repeating);
&GTEST_FLAG(stream_result_to)) || GTEST_INTERNAL_PARSE_FLAG(shuffle);
ParseBoolFlag(arg, kThrowOnFailureFlag, &GTEST_FLAG(throw_on_failure)); GTEST_INTERNAL_PARSE_FLAG(stack_trace_depth);
GTEST_INTERNAL_PARSE_FLAG(stream_result_to);
GTEST_INTERNAL_PARSE_FLAG(throw_on_failure);
return false;
} }
#if GTEST_USE_OWN_FLAGFILE_FLAG_ #if GTEST_USE_OWN_FLAGFILE_FLAG_
static void LoadFlagsFromFile(const std::string& path) { static void LoadFlagsFromFile(const std::string& path) {
FILE* flagfile = posix::FOpen(path.c_str(), "r"); FILE* flagfile = posix::FOpen(path.c_str(), "r");
if (!flagfile) { if (!flagfile) {
GTEST_LOG_(FATAL) << "Unable to open file \"" << GTEST_FLAG(flagfile) GTEST_LOG_(FATAL) << "Unable to open file \"" << GTEST_FLAG_GET(flagfile)
<< "\""; << "\"";
} }
std::string contents(ReadEntireFile(flagfile)); std::string contents(ReadEntireFile(flagfile));
posix::FClose(flagfile); posix::FClose(flagfile);
std::vector<std::string> lines; std::vector<std::string> lines;
SplitString(contents, '\n', &lines); SplitString(contents, '\n', &lines);
for (size_t i = 0; i < lines.size(); ++i) { for (size_t i = 0; i < lines.size(); ++i) {
if (lines[i].empty()) if (lines[i].empty()) continue;
continue; if (!ParseGoogleTestFlag(lines[i].c_str())) g_help_flag = true;
if (!ParseGoogleTestFlag(lines[i].c_str()))
g_help_flag = true;
} }
} }
#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ #endif // GTEST_USE_OWN_FLAGFILE_FLAG_
// Parses the command line for Google Test flags, without initializing // Parses the command line for Google Test flags, without initializing
// other parts of Google Test. The type parameter CharType can be // other parts of Google Test. The type parameter CharType can be
// instantiated to either char or wchar_t. // instantiated to either char or wchar_t.
template <typename CharType> template <typename CharType>
void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
std::string flagfile_value;
for (int i = 1; i < *argc; i++) { for (int i = 1; i < *argc; i++) {
const std::string arg_string = StreamableToString(argv[i]); const std::string arg_string = StreamableToString(argv[i]);
const char* const arg = arg_string.c_str(); const char* const arg = arg_string.c_str();
using internal::ParseBoolFlag; using internal::ParseFlag;
using internal::ParseInt32Flag;
using internal::ParseStringFlag;
bool remove_flag = false; bool remove_flag = false;
if (ParseGoogleTestFlag(arg)) { if (ParseGoogleTestFlag(arg)) {
remove_flag = true; remove_flag = true;
#if GTEST_USE_OWN_FLAGFILE_FLAG_ #if GTEST_USE_OWN_FLAGFILE_FLAG_
} else if (ParseStringFlag(arg, kFlagfileFlag, &GTEST_FLAG(flagfile))) { } else if (ParseFlag(arg, "flagfile", &flagfile_value)) {
LoadFlagsFromFile(GTEST_FLAG(flagfile)); GTEST_FLAG_SET(flagfile, flagfile_value);
LoadFlagsFromFile(flagfile_value);
remove_flag = true; remove_flag = true;
#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ #endif // GTEST_USE_OWN_FLAGFILE_FLAG_
} else if (arg_string == "--help" || arg_string == "-h" || } else if (arg_string == "--help" || HasGoogleTestFlagPrefix(arg)) {
arg_string == "-?" || arg_string == "/?" ||
HasGoogleTestFlagPrefix(arg)) {
// Both help flag and unrecognized Google Test flags (excluding // Both help flag and unrecognized Google Test flags (excluding
// internal ones) trigger help display. // internal ones) trigger help display.
g_help_flag = true; g_help_flag = true;
} }
if (remove_flag) { if (remove_flag) {
// Shift the remainder of the argv list left by one. Note // Shift the remainder of the argv list left by one. Note
// that argv has (*argc + 1) elements, the last one always being // that argv has (*argc + 1) elements, the last one always being
// NULL. The following loop moves the trailing NULL element as // NULL. The following loop moves the trailing NULL element as
// well. // well.
skipping to change at line 6606 skipping to change at line 6632
// We print the help here instead of in RUN_ALL_TESTS(), as the // We print the help here instead of in RUN_ALL_TESTS(), as the
// latter may not be called at all if the user is using Google // latter may not be called at all if the user is using Google
// Test with another testing framework. // Test with another testing framework.
PrintColorEncoded(kColorEncodedHelpMessage); PrintColorEncoded(kColorEncodedHelpMessage);
} }
} }
// Parses the command line for Google Test flags, without initializing // Parses the command line for Google Test flags, without initializing
// other parts of Google Test. // other parts of Google Test.
void ParseGoogleTestFlagsOnly(int* argc, char** argv) { void ParseGoogleTestFlagsOnly(int* argc, char** argv) {
#if GTEST_HAS_ABSL
if (*argc > 0) {
// absl::ParseCommandLine() requires *argc > 0.
auto positional_args = absl::flags_internal::ParseCommandLineImpl(
*argc, argv, absl::flags_internal::ArgvListAction::kRemoveParsedArgs,
absl::flags_internal::UsageFlagsAction::kHandleUsage,
absl::flags_internal::OnUndefinedFlag::kReportUndefined);
// Any command-line positional arguments not part of any command-line flag
// (or arguments to a flag) are copied back out to argv, with the program
// invocation name at position 0, and argc is resized. This includes
// positional arguments after the flag-terminating delimiter '--'.
// See https://abseil.io/docs/cpp/guides/flags.
std::copy(positional_args.begin(), positional_args.end(), argv);
if (static_cast<int>(positional_args.size()) < *argc) {
argv[positional_args.size()] = nullptr;
*argc = static_cast<int>(positional_args.size());
}
}
#else
ParseGoogleTestFlagsOnlyImpl(argc, argv); ParseGoogleTestFlagsOnlyImpl(argc, argv);
#endif
// Fix the value of *_NSGetArgc() on macOS, but if and only if // Fix the value of *_NSGetArgc() on macOS, but if and only if
// *_NSGetArgv() == argv // *_NSGetArgv() == argv
// Only applicable to char** version of argv // Only applicable to char** version of argv
#if GTEST_OS_MAC #if GTEST_OS_MAC
#ifndef GTEST_OS_IOS #ifndef GTEST_OS_IOS
if (*_NSGetArgv() == argv) { if (*_NSGetArgv() == argv) {
*_NSGetArgc() = *argc; *_NSGetArgc() = *argc;
} }
#endif #endif
skipping to change at line 6641 skipping to change at line 6687
if (*argc <= 0) return; if (*argc <= 0) return;
g_argvs.clear(); g_argvs.clear();
for (int i = 0; i != *argc; i++) { for (int i = 0; i != *argc; i++) {
g_argvs.push_back(StreamableToString(argv[i])); g_argvs.push_back(StreamableToString(argv[i]));
} }
#if GTEST_HAS_ABSL #if GTEST_HAS_ABSL
absl::InitializeSymbolizer(g_argvs[0].c_str()); absl::InitializeSymbolizer(g_argvs[0].c_str());
// When using the Abseil Flags library, set the program usage message to the
// help message, but remove the color-encoding from the message first.
absl::SetProgramUsageMessage(absl::StrReplaceAll(
kColorEncodedHelpMessage,
{{"@D", ""}, {"@R", ""}, {"@G", ""}, {"@Y", ""}, {"@@", "@"}}));
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
ParseGoogleTestFlagsOnly(argc, argv); ParseGoogleTestFlagsOnly(argc, argv);
GetUnitTestImpl()->PostFlagParsingInit(); GetUnitTestImpl()->PostFlagParsingInit();
} }
} // namespace internal } // namespace internal
// Initializes Google Test. This must be called before calling // Initializes Google Test. This must be called before calling
// RUN_ALL_TESTS(). In particular, it parses a command line for the // RUN_ALL_TESTS(). In particular, it parses a command line for the
// flags that Google Test recognizes. Whenever a Google Test flag is // flags that Google Test recognizes. Whenever a Google Test flag is
// seen, it is removed from argv, and *argc is decremented. // seen, it is removed from argv, and *argc is decremented.
// //
// No value is returned. Instead, the Google Test flag variables are // No value is returned. Instead, the Google Test flag variables are
// updated. // updated.
// //
// Calling the function for the second time has no user-visible effect. // Calling the function for the second time has no user-visible effect.
void InitGoogleTest(int* argc, char** argv) { void InitGoogleTest(int* argc, char** argv) {
#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv);
#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
internal::InitGoogleTestImpl(argc, argv); internal::InitGoogleTestImpl(argc, argv);
#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
} }
// This overloaded version can be used in Windows programs compiled in // This overloaded version can be used in Windows programs compiled in
// UNICODE mode. // UNICODE mode.
void InitGoogleTest(int* argc, wchar_t** argv) { void InitGoogleTest(int* argc, wchar_t** argv) {
#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv);
#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
internal::InitGoogleTestImpl(argc, argv); internal::InitGoogleTestImpl(argc, argv);
#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
} }
// This overloaded version can be used on Arduino/embedded platforms where // This overloaded version can be used on Arduino/embedded platforms where
// there is no argc/argv. // there is no argc/argv.
void InitGoogleTest() { void InitGoogleTest() {
// Since Arduino doesn't have a command line, fake out the argc/argv arguments // Since Arduino doesn't have a command line, fake out the argc/argv arguments
int argc = 1; int argc = 1;
const auto arg0 = "dummy"; const auto arg0 = "dummy";
char* argv0 = const_cast<char*>(arg0); char* argv0 = const_cast<char*>(arg0);
char** argv = &argv0; char** argv = &argv0;
#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(&argc, argv); GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(&argc, argv);
#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
internal::InitGoogleTestImpl(&argc, argv); internal::InitGoogleTestImpl(&argc, argv);
#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
} }
#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
// Return value of first environment variable that is set and contains
// a non-empty string. If there are none, return the "fallback" string.
// Since we like the temporary directory to have a directory separator suffix,
// add it if not provided in the environment variable value.
static std::string GetTempDirFromEnv(
std::initializer_list<const char*> environment_variables,
const char* fallback, char separator) {
for (const char* variable_name : environment_variables) {
const char* value = internal::posix::GetEnv(variable_name);
if (value != nullptr && value[0] != '\0') {
if (value[strlen(value) - 1] != separator) {
return std::string(value).append(1, separator);
}
return value;
}
}
return fallback;
}
#endif
std::string TempDir() { std::string TempDir() {
#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) #if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)
return GTEST_CUSTOM_TEMPDIR_FUNCTION_(); return GTEST_CUSTOM_TEMPDIR_FUNCTION_();
#elif GTEST_OS_WINDOWS_MOBILE #elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE
return "\\temp\\"; return GetTempDirFromEnv({"TEST_TMPDIR", "TEMP"}, "\\temp\\", '\\');
#elif GTEST_OS_WINDOWS
const char* temp_dir = internal::posix::GetEnv("TEMP");
if (temp_dir == nullptr || temp_dir[0] == '\0') {
return "\\temp\\";
} else if (temp_dir[strlen(temp_dir) - 1] == '\\') {
return temp_dir;
} else {
return std::string(temp_dir) + "\\";
}
#elif GTEST_OS_LINUX_ANDROID #elif GTEST_OS_LINUX_ANDROID
const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR"); return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/');
if (temp_dir == nullptr || temp_dir[0] == '\0') {
return "/data/local/tmp/";
} else {
return temp_dir;
}
#elif GTEST_OS_LINUX
const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR");
if (temp_dir == nullptr || temp_dir[0] == '\0') {
return "/tmp/";
} else {
return temp_dir;
}
#else #else
return "/tmp/"; return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/tmp/", '/');
#endif // GTEST_OS_WINDOWS_MOBILE #endif
} }
// Class ScopedTrace // Class ScopedTrace
// Pushes the given source file location and message onto a per-thread // Pushes the given source file location and message onto a per-thread
// trace stack maintained by Google Test. // trace stack maintained by Google Test.
void ScopedTrace::PushTrace(const char* file, int line, std::string message) { void ScopedTrace::PushTrace(const char* file, int line, std::string message) {
internal::TraceInfo trace; internal::TraceInfo trace;
trace.file = file; trace.file = file;
trace.line = line; trace.line = line;
trace.message.swap(message); trace.message.swap(message);
UnitTest::GetInstance()->PushGTestTrace(trace); UnitTest::GetInstance()->PushGTestTrace(trace);
} }
// Pops the info pushed by the c'tor. // Pops the info pushed by the c'tor.
ScopedTrace::~ScopedTrace() ScopedTrace::~ScopedTrace() GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
UnitTest::GetInstance()->PopGTestTrace(); UnitTest::GetInstance()->PopGTestTrace();
} }
} // namespace testing } // namespace testing
 End of changes. 330 change blocks. 
730 lines changed or deleted 781 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)