"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "googletest/test/gtest_unittest.cc" between
googletest-release-1.10.0.tar.gz and googletest-release-1.11.0.tar.gz

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

gtest_unittest.cc  (googletest-release-1.10.0):gtest_unittest.cc  (googletest-release-1.11.0)
skipping to change at line 40 skipping to change at line 40
// //
// Tests for Google Test itself. This verifies that the basic constructs of // Tests for Google Test itself. This verifies that the basic constructs of
// Google Test work. // Google Test work.
#include "gtest/gtest.h" #include "gtest/gtest.h"
// Verifies that the command line flag variables can be accessed in // Verifies that the command line flag variables can be accessed in
// code once "gtest.h" has been #included. // code once "gtest.h" has been #included.
// Do not move it after other gtest #includes. // Do not move it after other gtest #includes.
TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) { TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
bool dummy = testing::GTEST_FLAG(also_run_disabled_tests) bool dummy = testing::GTEST_FLAG(also_run_disabled_tests) ||
|| testing::GTEST_FLAG(break_on_failure) testing::GTEST_FLAG(break_on_failure) ||
|| testing::GTEST_FLAG(catch_exceptions) testing::GTEST_FLAG(catch_exceptions) ||
|| testing::GTEST_FLAG(color) != "unknown" testing::GTEST_FLAG(color) != "unknown" ||
|| testing::GTEST_FLAG(filter) != "unknown" testing::GTEST_FLAG(fail_fast) ||
|| testing::GTEST_FLAG(list_tests) testing::GTEST_FLAG(filter) != "unknown" ||
|| testing::GTEST_FLAG(output) != "unknown" testing::GTEST_FLAG(list_tests) ||
|| testing::GTEST_FLAG(print_time) testing::GTEST_FLAG(output) != "unknown" ||
|| testing::GTEST_FLAG(random_seed) testing::GTEST_FLAG(brief) || testing::GTEST_FLAG(print_time) ||
|| testing::GTEST_FLAG(repeat) > 0 testing::GTEST_FLAG(random_seed) ||
|| testing::GTEST_FLAG(show_internal_stack_frames) testing::GTEST_FLAG(repeat) > 0 ||
|| testing::GTEST_FLAG(shuffle) testing::GTEST_FLAG(show_internal_stack_frames) ||
|| testing::GTEST_FLAG(stack_trace_depth) > 0 testing::GTEST_FLAG(shuffle) ||
|| testing::GTEST_FLAG(stream_result_to) != "unknown" testing::GTEST_FLAG(stack_trace_depth) > 0 ||
|| testing::GTEST_FLAG(throw_on_failure); testing::GTEST_FLAG(stream_result_to) != "unknown" ||
testing::GTEST_FLAG(throw_on_failure);
EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused. EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused.
} }
#include <limits.h> // For INT_MAX. #include <limits.h> // For INT_MAX.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <cstdint>
#include <map> #include <map>
#include <ostream> #include <ostream>
#include <string>
#include <type_traits> #include <type_traits>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
#include "src/gtest-internal-inl.h" #include "src/gtest-internal-inl.h"
namespace testing { namespace testing {
namespace internal { namespace internal {
skipping to change at line 204 skipping to change at line 207
using testing::AssertionSuccess; using testing::AssertionSuccess;
using testing::DoubleLE; using testing::DoubleLE;
using testing::EmptyTestEventListener; using testing::EmptyTestEventListener;
using testing::Environment; using testing::Environment;
using testing::FloatLE; using testing::FloatLE;
using testing::GTEST_FLAG(also_run_disabled_tests); using testing::GTEST_FLAG(also_run_disabled_tests);
using testing::GTEST_FLAG(break_on_failure); using testing::GTEST_FLAG(break_on_failure);
using testing::GTEST_FLAG(catch_exceptions); using testing::GTEST_FLAG(catch_exceptions);
using testing::GTEST_FLAG(color); using testing::GTEST_FLAG(color);
using testing::GTEST_FLAG(death_test_use_fork); using testing::GTEST_FLAG(death_test_use_fork);
using testing::GTEST_FLAG(fail_fast);
using testing::GTEST_FLAG(filter); using testing::GTEST_FLAG(filter);
using testing::GTEST_FLAG(list_tests); using testing::GTEST_FLAG(list_tests);
using testing::GTEST_FLAG(output); using testing::GTEST_FLAG(output);
using testing::GTEST_FLAG(brief);
using testing::GTEST_FLAG(print_time); using testing::GTEST_FLAG(print_time);
using testing::GTEST_FLAG(random_seed); using testing::GTEST_FLAG(random_seed);
using testing::GTEST_FLAG(repeat); using testing::GTEST_FLAG(repeat);
using testing::GTEST_FLAG(show_internal_stack_frames); using testing::GTEST_FLAG(show_internal_stack_frames);
using testing::GTEST_FLAG(shuffle); using testing::GTEST_FLAG(shuffle);
using testing::GTEST_FLAG(stack_trace_depth); using testing::GTEST_FLAG(stack_trace_depth);
using testing::GTEST_FLAG(stream_result_to); using testing::GTEST_FLAG(stream_result_to);
using testing::GTEST_FLAG(throw_on_failure); using testing::GTEST_FLAG(throw_on_failure);
using testing::IsNotSubstring; using testing::IsNotSubstring;
using testing::IsSubstring; using testing::IsSubstring;
using testing::kMaxStackTraceDepth;
using testing::Message; using testing::Message;
using testing::ScopedFakeTestPartResultReporter; using testing::ScopedFakeTestPartResultReporter;
using testing::StaticAssertTypeEq; using testing::StaticAssertTypeEq;
using testing::Test; using testing::Test;
using testing::TestCase;
using testing::TestEventListeners; using testing::TestEventListeners;
using testing::TestInfo; using testing::TestInfo;
using testing::TestPartResult; using testing::TestPartResult;
using testing::TestPartResultArray; using testing::TestPartResultArray;
using testing::TestProperty; using testing::TestProperty;
using testing::TestResult; using testing::TestResult;
using testing::TestSuite;
using testing::TimeInMillis; using testing::TimeInMillis;
using testing::UnitTest; using testing::UnitTest;
using testing::internal::AlwaysFalse; using testing::internal::AlwaysFalse;
using testing::internal::AlwaysTrue; using testing::internal::AlwaysTrue;
using testing::internal::AppendUserMessage; using testing::internal::AppendUserMessage;
using testing::internal::ArrayAwareFind; using testing::internal::ArrayAwareFind;
using testing::internal::ArrayEq; using testing::internal::ArrayEq;
using testing::internal::CodePointToUtf8; using testing::internal::CodePointToUtf8;
using testing::internal::CopyArray; using testing::internal::CopyArray;
using testing::internal::CountIf; using testing::internal::CountIf;
using testing::internal::EqFailure; using testing::internal::EqFailure;
using testing::internal::FloatingPoint; using testing::internal::FloatingPoint;
using testing::internal::ForEach; using testing::internal::ForEach;
using testing::internal::FormatEpochTimeInMillisAsIso8601; using testing::internal::FormatEpochTimeInMillisAsIso8601;
using testing::internal::FormatTimeInMillisAsSeconds; using testing::internal::FormatTimeInMillisAsSeconds;
using testing::internal::GTestFlagSaver;
using testing::internal::GetCurrentOsStackTraceExceptTop; using testing::internal::GetCurrentOsStackTraceExceptTop;
using testing::internal::GetElementOr; using testing::internal::GetElementOr;
using testing::internal::GetNextRandomSeed; using testing::internal::GetNextRandomSeed;
using testing::internal::GetRandomSeedFromFlag; using testing::internal::GetRandomSeedFromFlag;
using testing::internal::GetTestTypeId; using testing::internal::GetTestTypeId;
using testing::internal::GetTimeInMillis; using testing::internal::GetTimeInMillis;
using testing::internal::GetTypeId; using testing::internal::GetTypeId;
using testing::internal::GetUnitTestImpl; using testing::internal::GetUnitTestImpl;
using testing::internal::Int32; using testing::internal::GTestFlagSaver;
using testing::internal::HasDebugStringAndShortDebugString;
using testing::internal::Int32FromEnvOrDie; using testing::internal::Int32FromEnvOrDie;
using testing::internal::IsAProtocolMessage;
using testing::internal::IsContainer; using testing::internal::IsContainer;
using testing::internal::IsContainerTest; using testing::internal::IsContainerTest;
using testing::internal::IsNotContainer; using testing::internal::IsNotContainer;
using testing::internal::kMaxRandomSeed;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::internal::NativeArray; using testing::internal::NativeArray;
using testing::internal::OsStackTraceGetter; using testing::internal::OsStackTraceGetter;
using testing::internal::OsStackTraceGetterInterface; using testing::internal::OsStackTraceGetterInterface;
using testing::internal::ParseInt32Flag; using testing::internal::ParseInt32Flag;
using testing::internal::RelationToSourceCopy; using testing::internal::RelationToSourceCopy;
using testing::internal::RelationToSourceReference; using testing::internal::RelationToSourceReference;
using testing::internal::ShouldRunTestOnShard; using testing::internal::ShouldRunTestOnShard;
using testing::internal::ShouldShard; using testing::internal::ShouldShard;
using testing::internal::ShouldUseColor; using testing::internal::ShouldUseColor;
using testing::internal::Shuffle; using testing::internal::Shuffle;
using testing::internal::ShuffleRange; using testing::internal::ShuffleRange;
using testing::internal::SkipPrefix; using testing::internal::SkipPrefix;
using testing::internal::StreamableToString; using testing::internal::StreamableToString;
using testing::internal::String; using testing::internal::String;
using testing::internal::TestEventListenersAccessor; using testing::internal::TestEventListenersAccessor;
using testing::internal::TestResultAccessor; using testing::internal::TestResultAccessor;
using testing::internal::UInt32;
using testing::internal::UnitTestImpl; using testing::internal::UnitTestImpl;
using testing::internal::WideStringToUtf8; using testing::internal::WideStringToUtf8;
using testing::internal::edit_distance::CalculateOptimalEdits; using testing::internal::edit_distance::CalculateOptimalEdits;
using testing::internal::edit_distance::CreateUnifiedDiff; using testing::internal::edit_distance::CreateUnifiedDiff;
using testing::internal::edit_distance::EditType; using testing::internal::edit_distance::EditType;
using testing::internal::kMaxRandomSeed;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::kMaxStackTraceDepth;
#if GTEST_HAS_STREAM_REDIRECTION #if GTEST_HAS_STREAM_REDIRECTION
using testing::internal::CaptureStdout; using testing::internal::CaptureStdout;
using testing::internal::GetCapturedStdout; using testing::internal::GetCapturedStdout;
#endif #endif
#if GTEST_IS_THREADSAFE #if GTEST_IS_THREADSAFE
using testing::internal::ThreadWithParam; using testing::internal::ThreadWithParam;
#endif #endif
skipping to change at line 486 skipping to change at line 489
tzset(); tzset();
#endif #endif
} }
const char* saved_tz_; const char* saved_tz_;
}; };
const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec; const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec;
TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) { TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) {
EXPECT_EQ("2011-10-31T18:52:42", EXPECT_EQ("2011-10-31T18:52:42.000",
FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec)); FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec));
} }
TEST_F(FormatEpochTimeInMillisAsIso8601Test, MillisecondsDoNotAffectResult) { TEST_F(FormatEpochTimeInMillisAsIso8601Test, IncludesMillisecondsAfterDot) {
EXPECT_EQ( EXPECT_EQ(
"2011-10-31T18:52:42", "2011-10-31T18:52:42.234",
FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec + 234)); FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec + 234));
} }
TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) { TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) {
EXPECT_EQ("2011-09-03T05:07:02", EXPECT_EQ("2011-09-03T05:07:02.000",
FormatEpochTimeInMillisAsIso8601(1315026422 * kMillisPerSec)); FormatEpochTimeInMillisAsIso8601(1315026422 * kMillisPerSec));
} }
TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) { TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) {
EXPECT_EQ("2011-09-28T17:08:22", EXPECT_EQ("2011-09-28T17:08:22.000",
FormatEpochTimeInMillisAsIso8601(1317229702 * kMillisPerSec)); FormatEpochTimeInMillisAsIso8601(1317229702 * kMillisPerSec));
} }
TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) { TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
EXPECT_EQ("1970-01-01T00:00:00", FormatEpochTimeInMillisAsIso8601(0)); EXPECT_EQ("1970-01-01T00:00:00.000", FormatEpochTimeInMillisAsIso8601(0));
} }
# ifdef __BORLANDC__ # ifdef __BORLANDC__
// Silences warnings: "Condition is always true", "Unreachable code" // Silences warnings: "Condition is always true", "Unreachable code"
# pragma option push -w-ccc -w-rch # pragma option push -w-ccc -w-rch
# endif # endif
// Tests that the LHS of EXPECT_EQ or ASSERT_EQ can be used as a null literal // Tests that the LHS of EXPECT_EQ or ASSERT_EQ can be used as a null literal
// when the RHS is a pointer type. // when the RHS is a pointer type.
TEST(NullLiteralTest, LHSAllowsNullLiterals) { TEST(NullLiteralTest, LHSAllowsNullLiterals) {
skipping to change at line 791 skipping to change at line 794
EXPECT_DEATH_IF_SUPPORTED( EXPECT_DEATH_IF_SUPPORTED(
random.Generate(0), random.Generate(0),
"Cannot generate a number in the range \\[0, 0\\)"); "Cannot generate a number in the range \\[0, 0\\)");
EXPECT_DEATH_IF_SUPPORTED( EXPECT_DEATH_IF_SUPPORTED(
random.Generate(testing::internal::Random::kMaxRange + 1), random.Generate(testing::internal::Random::kMaxRange + 1),
"Generation of a number in \\[0, 2147483649\\) was requested, " "Generation of a number in \\[0, 2147483649\\) was requested, "
"but this can only generate numbers in \\[0, 2147483648\\)"); "but this can only generate numbers in \\[0, 2147483648\\)");
} }
TEST(RandomTest, GeneratesNumbersWithinRange) { TEST(RandomTest, GeneratesNumbersWithinRange) {
const UInt32 kRange = 10000; constexpr uint32_t kRange = 10000;
testing::internal::Random random(12345); testing::internal::Random random(12345);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
EXPECT_LT(random.Generate(kRange), kRange) << " for iteration " << i; EXPECT_LT(random.Generate(kRange), kRange) << " for iteration " << i;
} }
testing::internal::Random random2(testing::internal::Random::kMaxRange); testing::internal::Random random2(testing::internal::Random::kMaxRange);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
EXPECT_LT(random2.Generate(kRange), kRange) << " for iteration " << i; EXPECT_LT(random2.Generate(kRange), kRange) << " for iteration " << i;
} }
} }
TEST(RandomTest, RepeatsWhenReseeded) { TEST(RandomTest, RepeatsWhenReseeded) {
const int kSeed = 123; constexpr int kSeed = 123;
const int kArraySize = 10; constexpr int kArraySize = 10;
const UInt32 kRange = 10000; constexpr uint32_t kRange = 10000;
UInt32 values[kArraySize]; uint32_t values[kArraySize];
testing::internal::Random random(kSeed); testing::internal::Random random(kSeed);
for (int i = 0; i < kArraySize; i++) { for (int i = 0; i < kArraySize; i++) {
values[i] = random.Generate(kRange); values[i] = random.Generate(kRange);
} }
random.Reseed(kSeed); random.Reseed(kSeed);
for (int i = 0; i < kArraySize; i++) { for (int i = 0; i < kArraySize; i++) {
EXPECT_EQ(values[i], random.Generate(kRange)) << " for iteration " << i; EXPECT_EQ(values[i], random.Generate(kRange)) << " for iteration " << i;
} }
skipping to change at line 1602 skipping to change at line 1605
// then sets them to their default values. This will be called // then sets them to their default values. This will be called
// before the first test in this test case is run. // before the first test in this test case is run.
static void SetUpTestSuite() { static void SetUpTestSuite() {
saver_ = new GTestFlagSaver; saver_ = new GTestFlagSaver;
GTEST_FLAG(also_run_disabled_tests) = false; GTEST_FLAG(also_run_disabled_tests) = false;
GTEST_FLAG(break_on_failure) = false; GTEST_FLAG(break_on_failure) = false;
GTEST_FLAG(catch_exceptions) = false; GTEST_FLAG(catch_exceptions) = false;
GTEST_FLAG(death_test_use_fork) = false; GTEST_FLAG(death_test_use_fork) = false;
GTEST_FLAG(color) = "auto"; GTEST_FLAG(color) = "auto";
GTEST_FLAG(fail_fast) = false;
GTEST_FLAG(filter) = ""; GTEST_FLAG(filter) = "";
GTEST_FLAG(list_tests) = false; GTEST_FLAG(list_tests) = false;
GTEST_FLAG(output) = ""; GTEST_FLAG(output) = "";
GTEST_FLAG(brief) = false;
GTEST_FLAG(print_time) = true; GTEST_FLAG(print_time) = true;
GTEST_FLAG(random_seed) = 0; GTEST_FLAG(random_seed) = 0;
GTEST_FLAG(repeat) = 1; GTEST_FLAG(repeat) = 1;
GTEST_FLAG(shuffle) = false; GTEST_FLAG(shuffle) = false;
GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth;
GTEST_FLAG(stream_result_to) = ""; GTEST_FLAG(stream_result_to) = "";
GTEST_FLAG(throw_on_failure) = false; GTEST_FLAG(throw_on_failure) = false;
} }
// Restores the Google Test flags that the tests have modified. This will // Restores the Google Test flags that the tests have modified. This will
skipping to change at line 1629 skipping to change at line 1634
} }
// Verifies that the Google Test flags have their default values, and then // Verifies that the Google Test flags have their default values, and then
// modifies each of them. // modifies each of them.
void VerifyAndModifyFlags() { void VerifyAndModifyFlags() {
EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests)); EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests));
EXPECT_FALSE(GTEST_FLAG(break_on_failure)); EXPECT_FALSE(GTEST_FLAG(break_on_failure));
EXPECT_FALSE(GTEST_FLAG(catch_exceptions)); EXPECT_FALSE(GTEST_FLAG(catch_exceptions));
EXPECT_STREQ("auto", GTEST_FLAG(color).c_str()); EXPECT_STREQ("auto", GTEST_FLAG(color).c_str());
EXPECT_FALSE(GTEST_FLAG(death_test_use_fork)); EXPECT_FALSE(GTEST_FLAG(death_test_use_fork));
EXPECT_FALSE(GTEST_FLAG(fail_fast));
EXPECT_STREQ("", GTEST_FLAG(filter).c_str()); EXPECT_STREQ("", GTEST_FLAG(filter).c_str());
EXPECT_FALSE(GTEST_FLAG(list_tests)); EXPECT_FALSE(GTEST_FLAG(list_tests));
EXPECT_STREQ("", GTEST_FLAG(output).c_str()); EXPECT_STREQ("", GTEST_FLAG(output).c_str());
EXPECT_FALSE(GTEST_FLAG(brief));
EXPECT_TRUE(GTEST_FLAG(print_time)); EXPECT_TRUE(GTEST_FLAG(print_time));
EXPECT_EQ(0, GTEST_FLAG(random_seed)); EXPECT_EQ(0, GTEST_FLAG(random_seed));
EXPECT_EQ(1, GTEST_FLAG(repeat)); EXPECT_EQ(1, GTEST_FLAG(repeat));
EXPECT_FALSE(GTEST_FLAG(shuffle)); EXPECT_FALSE(GTEST_FLAG(shuffle));
EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG(stack_trace_depth)); EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG(stack_trace_depth));
EXPECT_STREQ("", GTEST_FLAG(stream_result_to).c_str()); EXPECT_STREQ("", GTEST_FLAG(stream_result_to).c_str());
EXPECT_FALSE(GTEST_FLAG(throw_on_failure)); EXPECT_FALSE(GTEST_FLAG(throw_on_failure));
GTEST_FLAG(also_run_disabled_tests) = true; GTEST_FLAG(also_run_disabled_tests) = true;
GTEST_FLAG(break_on_failure) = true; GTEST_FLAG(break_on_failure) = true;
GTEST_FLAG(catch_exceptions) = true; GTEST_FLAG(catch_exceptions) = true;
GTEST_FLAG(color) = "no"; GTEST_FLAG(color) = "no";
GTEST_FLAG(death_test_use_fork) = true; GTEST_FLAG(death_test_use_fork) = true;
GTEST_FLAG(fail_fast) = true;
GTEST_FLAG(filter) = "abc"; GTEST_FLAG(filter) = "abc";
GTEST_FLAG(list_tests) = true; GTEST_FLAG(list_tests) = true;
GTEST_FLAG(output) = "xml:foo.xml"; GTEST_FLAG(output) = "xml:foo.xml";
GTEST_FLAG(brief) = true;
GTEST_FLAG(print_time) = false; GTEST_FLAG(print_time) = false;
GTEST_FLAG(random_seed) = 1; GTEST_FLAG(random_seed) = 1;
GTEST_FLAG(repeat) = 100; GTEST_FLAG(repeat) = 100;
GTEST_FLAG(shuffle) = true; GTEST_FLAG(shuffle) = true;
GTEST_FLAG(stack_trace_depth) = 1; GTEST_FLAG(stack_trace_depth) = 1;
GTEST_FLAG(stream_result_to) = "localhost:1234"; GTEST_FLAG(stream_result_to) = "localhost:1234";
GTEST_FLAG(throw_on_failure) = true; GTEST_FLAG(throw_on_failure) = true;
} }
private: private:
skipping to change at line 1775 skipping to change at line 1784
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321"); SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321");
EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0)); EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0));
} }
#endif // !GTEST_OS_WINDOWS_MOBILE #endif // !GTEST_OS_WINDOWS_MOBILE
// Tests ParseInt32Flag(). // Tests ParseInt32Flag().
// Tests that ParseInt32Flag() returns false and doesn't change the // Tests that ParseInt32Flag() returns false and doesn't change the
// output value when the flag has wrong format // output value when the flag has wrong format
TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) { TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) {
Int32 value = 123; int32_t value = 123;
EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value)); EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value)); EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
} }
// Tests that ParseInt32Flag() returns false and doesn't change the // Tests that ParseInt32Flag() returns false and doesn't change the
// output value when the flag overflows as an Int32. // output value when the flag overflows as an Int32.
TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) { TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) {
printf("(expecting 2 warnings)\n"); printf("(expecting 2 warnings)\n");
Int32 value = 123; int32_t value = 123;
EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value)); EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value)); EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
} }
// Tests that ParseInt32Flag() returns false and doesn't change the // Tests that ParseInt32Flag() returns false and doesn't change the
// output value when the flag does not represent a valid decimal // output value when the flag does not represent a valid decimal
// integer. // integer.
TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) { TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) {
printf("(expecting 2 warnings)\n"); printf("(expecting 2 warnings)\n");
Int32 value = 123; int32_t value = 123;
EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value)); EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value)); EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value));
EXPECT_EQ(123, value); EXPECT_EQ(123, value);
} }
// Tests that ParseInt32Flag() parses the value of the flag and // Tests that ParseInt32Flag() parses the value of the flag and
// returns true when the flag represents a valid decimal integer in // returns true when the flag represents a valid decimal integer in
// the range of an Int32. // the range of an Int32.
TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) { TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) {
Int32 value = 123; int32_t value = 123;
EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=456", "abc", &value)); EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=456", "abc", &value));
EXPECT_EQ(456, value); EXPECT_EQ(456, value);
EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=-789", EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=-789",
"abc", &value)); "abc", &value));
EXPECT_EQ(-789, value); EXPECT_EQ(-789, value);
} }
// Tests that Int32FromEnvOrDie() parses the value of the var or // Tests that Int32FromEnvOrDie() parses the value of the var or
// returns the correct default. // returns the correct default.
skipping to change at line 1837 skipping to change at line 1846
TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) { TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123"); SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123");
EXPECT_EQ(123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); EXPECT_EQ(123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123"); SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123");
EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
} }
#endif // !GTEST_OS_WINDOWS_MOBILE #endif // !GTEST_OS_WINDOWS_MOBILE
// Tests that Int32FromEnvOrDie() aborts with an error message // Tests that Int32FromEnvOrDie() aborts with an error message
// if the variable is not an Int32. // if the variable is not an int32_t.
TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) {
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx"); SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx");
EXPECT_DEATH_IF_SUPPORTED( EXPECT_DEATH_IF_SUPPORTED(
Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123),
".*"); ".*");
} }
// Tests that Int32FromEnvOrDie() aborts with an error message // Tests that Int32FromEnvOrDie() aborts with an error message
// if the variable cannot be represented by an Int32. // if the variable cannot be represented by an int32_t.
TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) { TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) {
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234"); SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234");
EXPECT_DEATH_IF_SUPPORTED( EXPECT_DEATH_IF_SUPPORTED(
Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123),
".*"); ".*");
} }
// Tests that ShouldRunTestOnShard() selects all tests // Tests that ShouldRunTestOnShard() selects all tests
// where there is 1 shard. // where there is 1 shard.
TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) { TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) {
skipping to change at line 2758 skipping to change at line 2767
RawType further_from_infinity; RawType further_from_infinity;
RawType nan1; RawType nan1;
RawType nan2; RawType nan2;
}; };
typedef typename testing::internal::FloatingPoint<RawType> Floating; typedef typename testing::internal::FloatingPoint<RawType> Floating;
typedef typename Floating::Bits Bits; typedef typename Floating::Bits Bits;
void SetUp() override { void SetUp() override {
const size_t max_ulps = Floating::kMaxUlps; const uint32_t max_ulps = Floating::kMaxUlps;
// The bits that represent 0.0. // The bits that represent 0.0.
const Bits zero_bits = Floating(0).bits(); const Bits zero_bits = Floating(0).bits();
// Makes some numbers close to 0.0. // Makes some numbers close to 0.0.
values_.close_to_positive_zero = Floating::ReinterpretBits( values_.close_to_positive_zero = Floating::ReinterpretBits(
zero_bits + max_ulps/2); zero_bits + max_ulps/2);
values_.close_to_negative_zero = -Floating::ReinterpretBits( values_.close_to_negative_zero = -Floating::ReinterpretBits(
zero_bits + max_ulps - max_ulps/2); zero_bits + max_ulps - max_ulps/2);
values_.further_from_negative_zero = -Floating::ReinterpretBits( values_.further_from_negative_zero = -Floating::ReinterpretBits(
skipping to change at line 2923 skipping to change at line 2932
// We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one). // We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one).
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0), EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0),
"1.0"); "1.0");
} }
// Tests EXPECT_NEAR. // Tests EXPECT_NEAR.
TEST_F(FloatTest, EXPECT_NEAR) { TEST_F(FloatTest, EXPECT_NEAR) {
EXPECT_NEAR(-1.0f, -1.1f, 0.2f); EXPECT_NEAR(-1.0f, -1.1f, 0.2f);
EXPECT_NEAR(2.0f, 3.0f, 1.0f); EXPECT_NEAR(2.0f, 3.0f, 1.0f);
EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.5f, 0.25f), // NOLINT EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f, 1.5f, 0.25f), // NOLINT
"The difference between 1.0f and 1.5f is 0.5, " "The difference between 1.0f and 1.5f is 0.5, "
"which exceeds 0.25f"); "which exceeds 0.25f");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous line.
} }
// Tests ASSERT_NEAR. // Tests ASSERT_NEAR.
TEST_F(FloatTest, ASSERT_NEAR) { TEST_F(FloatTest, ASSERT_NEAR) {
ASSERT_NEAR(-1.0f, -1.1f, 0.2f); ASSERT_NEAR(-1.0f, -1.1f, 0.2f);
ASSERT_NEAR(2.0f, 3.0f, 1.0f); ASSERT_NEAR(2.0f, 3.0f, 1.0f);
EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.5f, 0.25f), // NOLINT EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f, 1.5f, 0.25f), // NOLINT
"The difference between 1.0f and 1.5f is 0.5, " "The difference between 1.0f and 1.5f is 0.5, "
"which exceeds 0.25f"); "which exceeds 0.25f");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous line.
} }
// Tests the cases where FloatLE() should succeed. // Tests the cases where FloatLE() should succeed.
TEST_F(FloatTest, FloatLESucceeds) { TEST_F(FloatTest, FloatLESucceeds) {
EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2, EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2,
ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2, ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2,
// or when val1 is greater than, but almost equals to, val2. // or when val1 is greater than, but almost equals to, val2.
EXPECT_PRED_FORMAT2(FloatLE, values_.close_to_positive_zero, 0.0f); EXPECT_PRED_FORMAT2(FloatLE, values_.close_to_positive_zero, 0.0f);
} }
skipping to change at line 3079 skipping to change at line 3084
"1.0"); "1.0");
} }
// Tests EXPECT_NEAR. // Tests EXPECT_NEAR.
TEST_F(DoubleTest, EXPECT_NEAR) { TEST_F(DoubleTest, EXPECT_NEAR) {
EXPECT_NEAR(-1.0, -1.1, 0.2); EXPECT_NEAR(-1.0, -1.1, 0.2);
EXPECT_NEAR(2.0, 3.0, 1.0); EXPECT_NEAR(2.0, 3.0, 1.0);
EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT
"The difference between 1.0 and 1.5 is 0.5, " "The difference between 1.0 and 1.5 is 0.5, "
"which exceeds 0.25"); "which exceeds 0.25");
// To work around a bug in gcc 2.95.0, there is intentionally no // At this magnitude adjacent doubles are 512.0 apart, so this triggers a
// space after the first comma in the previous statement. // slightly different failure reporting path.
EXPECT_NONFATAL_FAILURE(
EXPECT_NEAR(4.2934311416234112e+18, 4.2934311416234107e+18, 1.0),
"The abs_error parameter 1.0 evaluates to 1 which is smaller than the "
"minimum distance between doubles for numbers of this magnitude which is "
"512");
} }
// Tests ASSERT_NEAR. // Tests ASSERT_NEAR.
TEST_F(DoubleTest, ASSERT_NEAR) { TEST_F(DoubleTest, ASSERT_NEAR) {
ASSERT_NEAR(-1.0, -1.1, 0.2); ASSERT_NEAR(-1.0, -1.1, 0.2);
ASSERT_NEAR(2.0, 3.0, 1.0); ASSERT_NEAR(2.0, 3.0, 1.0);
EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT
"The difference between 1.0 and 1.5 is 0.5, " "The difference between 1.0 and 1.5 is 0.5, "
"which exceeds 0.25"); "which exceeds 0.25");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous statement.
} }
// Tests the cases where DoubleLE() should succeed. // Tests the cases where DoubleLE() should succeed.
TEST_F(DoubleTest, DoubleLESucceeds) { TEST_F(DoubleTest, DoubleLESucceeds) {
EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2, EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2,
ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2, ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2,
// or when val1 is greater than, but almost equals to, val2. // or when val1 is greater than, but almost equals to, val2.
EXPECT_PRED_FORMAT2(DoubleLE, values_.close_to_positive_zero, 0.0); EXPECT_PRED_FORMAT2(DoubleLE, values_.close_to_positive_zero, 0.0);
} }
skipping to change at line 3177 skipping to change at line 3185
TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) { TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) {
FAIL() << "Unexpected failure: Disabled test should not be run."; FAIL() << "Unexpected failure: Disabled test should not be run.";
} }
TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) { TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) {
FAIL() << "Unexpected failure: Disabled test should not be run."; FAIL() << "Unexpected failure: Disabled test should not be run.";
} }
// Tests that disabled typed tests aren't run. // Tests that disabled typed tests aren't run.
#if GTEST_HAS_TYPED_TEST
template <typename T> template <typename T>
class TypedTest : public Test { class TypedTest : public Test {
}; };
typedef testing::Types<int, double> NumericTypes; typedef testing::Types<int, double> NumericTypes;
TYPED_TEST_SUITE(TypedTest, NumericTypes); TYPED_TEST_SUITE(TypedTest, NumericTypes);
TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) { TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) {
FAIL() << "Unexpected failure: Disabled typed test should not run."; FAIL() << "Unexpected failure: Disabled typed test should not run.";
} }
skipping to change at line 3200 skipping to change at line 3206
template <typename T> template <typename T>
class DISABLED_TypedTest : public Test { class DISABLED_TypedTest : public Test {
}; };
TYPED_TEST_SUITE(DISABLED_TypedTest, NumericTypes); TYPED_TEST_SUITE(DISABLED_TypedTest, NumericTypes);
TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) { TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) {
FAIL() << "Unexpected failure: Disabled typed test should not run."; FAIL() << "Unexpected failure: Disabled typed test should not run.";
} }
#endif // GTEST_HAS_TYPED_TEST
// Tests that disabled type-parameterized tests aren't run. // Tests that disabled type-parameterized tests aren't run.
#if GTEST_HAS_TYPED_TEST_P
template <typename T> template <typename T>
class TypedTestP : public Test { class TypedTestP : public Test {
}; };
TYPED_TEST_SUITE_P(TypedTestP); TYPED_TEST_SUITE_P(TypedTestP);
TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) { TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) {
FAIL() << "Unexpected failure: " FAIL() << "Unexpected failure: "
<< "Disabled type-parameterized test should not run."; << "Disabled type-parameterized test should not run.";
} }
skipping to change at line 3236 skipping to change at line 3238
TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) { TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) {
FAIL() << "Unexpected failure: " FAIL() << "Unexpected failure: "
<< "Disabled type-parameterized test should not run."; << "Disabled type-parameterized test should not run.";
} }
REGISTER_TYPED_TEST_SUITE_P(DISABLED_TypedTestP, ShouldNotRun); REGISTER_TYPED_TEST_SUITE_P(DISABLED_TypedTestP, ShouldNotRun);
INSTANTIATE_TYPED_TEST_SUITE_P(My, DISABLED_TypedTestP, NumericTypes); INSTANTIATE_TYPED_TEST_SUITE_P(My, DISABLED_TypedTestP, NumericTypes);
#endif // GTEST_HAS_TYPED_TEST_P
// Tests that assertion macros evaluate their arguments exactly once. // Tests that assertion macros evaluate their arguments exactly once.
class SingleEvaluationTest : public Test { class SingleEvaluationTest : public Test {
public: // Must be public and not protected due to a bug in g++ 3.4.2. public: // Must be public and not protected due to a bug in g++ 3.4.2.
// This helper function is needed by the FailedASSERT_STREQ test // This helper function is needed by the FailedASSERT_STREQ test
// below. It's public to work around C++Builder's bug with scoping local // below. It's public to work around C++Builder's bug with scoping local
// classes. // classes.
static void CompareAndIncrementCharPtrs() { static void CompareAndIncrementCharPtrs() {
ASSERT_STREQ(p1_++, p2_++); ASSERT_STREQ(p1_++, p2_++);
} }
skipping to change at line 3343 skipping to change at line 3343
EXPECT_EQ(5, a_); EXPECT_EQ(5, a_);
// successful ASSERT_GT // successful ASSERT_GT
ASSERT_GT(a_++, b_++); ASSERT_GT(a_++, b_++);
EXPECT_EQ(6, a_); EXPECT_EQ(6, a_);
EXPECT_EQ(3, b_); EXPECT_EQ(3, b_);
} }
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
#if GTEST_HAS_RTTI
#ifdef _MSC_VER
#define ERROR_DESC "class std::runtime_error"
#else
#define ERROR_DESC "std::runtime_error"
#endif
#else // GTEST_HAS_RTTI
#define ERROR_DESC "an std::exception-derived error"
#endif // GTEST_HAS_RTTI
void ThrowAnInteger() { void ThrowAnInteger() {
throw 1; throw 1;
} }
void ThrowRuntimeError(const char* what) {
throw std::runtime_error(what);
}
// Tests that assertion arguments are evaluated exactly once. // Tests that assertion arguments are evaluated exactly once.
TEST_F(SingleEvaluationTest, ExceptionTests) { TEST_F(SingleEvaluationTest, ExceptionTests) {
// successful EXPECT_THROW // successful EXPECT_THROW
EXPECT_THROW({ // NOLINT EXPECT_THROW({ // NOLINT
a_++; a_++;
ThrowAnInteger(); ThrowAnInteger();
}, int); }, int);
EXPECT_EQ(1, a_); EXPECT_EQ(1, a_);
// failed EXPECT_THROW, throws different // failed EXPECT_THROW, throws different
EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT
a_++; a_++;
ThrowAnInteger(); ThrowAnInteger();
}, bool), "throws a different type"); }, bool), "throws a different type");
EXPECT_EQ(2, a_); EXPECT_EQ(2, a_);
// failed EXPECT_THROW, throws runtime error
EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT
a_++;
ThrowRuntimeError("A description");
}, bool), "throws " ERROR_DESC " with description \"A description\"");
EXPECT_EQ(3, a_);
// failed EXPECT_THROW, throws nothing // failed EXPECT_THROW, throws nothing
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing"); EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing");
EXPECT_EQ(3, a_); EXPECT_EQ(4, a_);
// successful EXPECT_NO_THROW // successful EXPECT_NO_THROW
EXPECT_NO_THROW(a_++); EXPECT_NO_THROW(a_++);
EXPECT_EQ(4, a_); EXPECT_EQ(5, a_);
// failed EXPECT_NO_THROW // failed EXPECT_NO_THROW
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT
a_++; a_++;
ThrowAnInteger(); ThrowAnInteger();
}), "it throws"); }), "it throws");
EXPECT_EQ(5, a_); EXPECT_EQ(6, a_);
// successful EXPECT_ANY_THROW // successful EXPECT_ANY_THROW
EXPECT_ANY_THROW({ // NOLINT EXPECT_ANY_THROW({ // NOLINT
a_++; a_++;
ThrowAnInteger(); ThrowAnInteger();
}); });
EXPECT_EQ(6, a_); EXPECT_EQ(7, a_);
// failed EXPECT_ANY_THROW // failed EXPECT_ANY_THROW
EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't");
EXPECT_EQ(7, a_); EXPECT_EQ(8, a_);
} }
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
// Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE. // Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE.
class NoFatalFailureTest : public Test { class NoFatalFailureTest : public Test {
protected: protected:
void Succeeds() {} void Succeeds() {}
void FailsNonFatal() { void FailsNonFatal() {
ADD_FAILURE() << "some non-fatal failure"; ADD_FAILURE() << "some non-fatal failure";
skipping to change at line 3732 skipping to change at line 3756
"Expected equality of these values:\n" "Expected equality of these values:\n"
" 5\n" " 5\n"
" 2*3\n" " 2*3\n"
" Which is: 6"); " Which is: 6");
} }
// Tests ASSERT_EQ(NULL, pointer). // Tests ASSERT_EQ(NULL, pointer).
TEST(AssertionTest, ASSERT_EQ_NULL) { TEST(AssertionTest, ASSERT_EQ_NULL) {
// A success. // A success.
const char* p = nullptr; const char* p = nullptr;
// Some older GCC versions may issue a spurious warning in this or the next
// assertion statement. This warning should not be suppressed with
// static_cast since the test verifies the ability to use bare NULL as the
// expected parameter to the macro.
ASSERT_EQ(nullptr, p); ASSERT_EQ(nullptr, p);
// A failure. // A failure.
static int n = 0; static int n = 0;
EXPECT_FATAL_FAILURE(ASSERT_EQ(nullptr, &n), " &n\n Which is:"); EXPECT_FATAL_FAILURE(ASSERT_EQ(nullptr, &n), " &n\n Which is:");
} }
// Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be // Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be
// treated as a null pointer by the compiler, we need to make sure // treated as a null pointer by the compiler, we need to make sure
// that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as // that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as
skipping to change at line 3811 skipping to change at line 3831
TEST(AssertionTest, ASSERT_THROW) { TEST(AssertionTest, ASSERT_THROW) {
ASSERT_THROW(ThrowAnInteger(), int); ASSERT_THROW(ThrowAnInteger(), int);
# ifndef __BORLANDC__ # ifndef __BORLANDC__
// ICE's in C++Builder 2007 and 2009. // ICE's in C++Builder 2007 and 2009.
EXPECT_FATAL_FAILURE( EXPECT_FATAL_FAILURE(
ASSERT_THROW(ThrowAnInteger(), bool), ASSERT_THROW(ThrowAnInteger(), bool),
"Expected: ThrowAnInteger() throws an exception of type bool.\n" "Expected: ThrowAnInteger() throws an exception of type bool.\n"
" Actual: it throws a different type."); " Actual: it throws a different type.");
EXPECT_FATAL_FAILURE(
ASSERT_THROW(ThrowRuntimeError("A description"), std::logic_error),
"Expected: ThrowRuntimeError(\"A description\") "
"throws an exception of type std::logic_error.\n "
"Actual: it throws " ERROR_DESC " "
"with description \"A description\".");
# endif # endif
EXPECT_FATAL_FAILURE( EXPECT_FATAL_FAILURE(
ASSERT_THROW(ThrowNothing(), bool), ASSERT_THROW(ThrowNothing(), bool),
"Expected: ThrowNothing() throws an exception of type bool.\n" "Expected: ThrowNothing() throws an exception of type bool.\n"
" Actual: it throws nothing."); " Actual: it throws nothing.");
} }
// Tests ASSERT_NO_THROW. // Tests ASSERT_NO_THROW.
TEST(AssertionTest, ASSERT_NO_THROW) { TEST(AssertionTest, ASSERT_NO_THROW) {
ASSERT_NO_THROW(ThrowNothing()); ASSERT_NO_THROW(ThrowNothing());
EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()),
"Expected: ThrowAnInteger() doesn't throw an exception." "Expected: ThrowAnInteger() doesn't throw an exception."
"\n Actual: it throws."); "\n Actual: it throws.");
EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowRuntimeError("A description")),
"Expected: ThrowRuntimeError(\"A description\") "
"doesn't throw an exception.\n "
"Actual: it throws " ERROR_DESC " "
"with description \"A description\".");
} }
// Tests ASSERT_ANY_THROW. // Tests ASSERT_ANY_THROW.
TEST(AssertionTest, ASSERT_ANY_THROW) { TEST(AssertionTest, ASSERT_ANY_THROW) {
ASSERT_ANY_THROW(ThrowAnInteger()); ASSERT_ANY_THROW(ThrowAnInteger());
EXPECT_FATAL_FAILURE( EXPECT_FATAL_FAILURE(
ASSERT_ANY_THROW(ThrowNothing()), ASSERT_ANY_THROW(ThrowNothing()),
"Expected: ThrowNothing() throws an exception.\n" "Expected: ThrowNothing() throws an exception.\n"
" Actual: it doesn't."); " Actual: it doesn't.");
} }
skipping to change at line 4093 skipping to change at line 4124
EXPECT_HRESULT_FAILED(S_OK) << "expected failure", EXPECT_HRESULT_FAILED(S_OK) << "expected failure",
"expected failure"); "expected failure");
EXPECT_FATAL_FAILURE( EXPECT_FATAL_FAILURE(
ASSERT_HRESULT_FAILED(S_OK) << "expected failure", ASSERT_HRESULT_FAILED(S_OK) << "expected failure",
"expected failure"); "expected failure");
} }
#endif // GTEST_OS_WINDOWS #endif // GTEST_OS_WINDOWS
#ifdef __BORLANDC__ // The following code intentionally tests a suboptimal syntax.
// Silences warnings: "Condition is always true", "Unreachable code" #ifdef __GNUC__
# pragma option push -w-ccc -w-rch #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-else"
#pragma GCC diagnostic ignored "-Wempty-body"
#pragma GCC diagnostic ignored "-Wpragmas"
#endif #endif
// Tests that the assertion macros behave like single statements. // Tests that the assertion macros behave like single statements.
TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) { TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) {
if (AlwaysFalse()) if (AlwaysFalse())
ASSERT_TRUE(false) << "This should never be executed; " ASSERT_TRUE(false) << "This should never be executed; "
"It's a compilation test only."; "It's a compilation test only.";
if (AlwaysTrue()) if (AlwaysTrue())
EXPECT_FALSE(false); EXPECT_FALSE(false);
else else
; // NOLINT ; // NOLINT
if (AlwaysFalse()) if (AlwaysFalse())
ASSERT_LT(1, 3); ASSERT_LT(1, 3);
if (AlwaysFalse()) if (AlwaysFalse())
; // NOLINT ; // NOLINT
else else
EXPECT_GT(3, 2) << ""; EXPECT_GT(3, 2) << "";
} }
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
// Tests that the compiler will not complain about unreachable code in the // Tests that the compiler will not complain about unreachable code in the
// EXPECT_THROW/EXPECT_ANY_THROW/EXPECT_NO_THROW macros. // EXPECT_THROW/EXPECT_ANY_THROW/EXPECT_NO_THROW macros.
TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
int n = 0; int n = 0;
EXPECT_THROW(throw 1, int); EXPECT_THROW(throw 1, int);
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(n++, int), ""); EXPECT_NONFATAL_FAILURE(EXPECT_THROW(n++, int), "");
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(throw 1, const char*), ""); EXPECT_NONFATAL_FAILURE(EXPECT_THROW(throw 1, const char*), "");
EXPECT_NO_THROW(n++); EXPECT_NO_THROW(n++);
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(throw 1), ""); EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(throw 1), "");
EXPECT_ANY_THROW(throw 1); EXPECT_ANY_THROW(throw 1);
EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(n++), ""); EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(n++), "");
} }
TEST(ExpectThrowTest, DoesNotGenerateDuplicateCatchClauseWarning) {
EXPECT_THROW(throw std::exception(), std::exception);
}
// The following code intentionally tests a suboptimal syntax.
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-else"
#pragma GCC diagnostic ignored "-Wempty-body"
#pragma GCC diagnostic ignored "-Wpragmas"
#endif
TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) {
if (AlwaysFalse()) if (AlwaysFalse())
EXPECT_THROW(ThrowNothing(), bool); EXPECT_THROW(ThrowNothing(), bool);
if (AlwaysTrue()) if (AlwaysTrue())
EXPECT_THROW(ThrowAnInteger(), int); EXPECT_THROW(ThrowAnInteger(), int);
else else
; // NOLINT ; // NOLINT
if (AlwaysFalse()) if (AlwaysFalse())
skipping to change at line 4158 skipping to change at line 4205
; // NOLINT ; // NOLINT
if (AlwaysFalse()) if (AlwaysFalse())
EXPECT_ANY_THROW(ThrowNothing()); EXPECT_ANY_THROW(ThrowNothing());
if (AlwaysTrue()) if (AlwaysTrue())
EXPECT_ANY_THROW(ThrowAnInteger()); EXPECT_ANY_THROW(ThrowAnInteger());
else else
; // NOLINT ; // NOLINT
} }
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#endif // GTEST_HAS_EXCEPTIONS #endif // GTEST_HAS_EXCEPTIONS
// The following code intentionally tests a suboptimal syntax.
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-else"
#pragma GCC diagnostic ignored "-Wempty-body"
#pragma GCC diagnostic ignored "-Wpragmas"
#endif
TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) { TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) {
if (AlwaysFalse()) if (AlwaysFalse())
EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. " EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. "
<< "It's a compilation test only."; << "It's a compilation test only.";
else else
; // NOLINT ; // NOLINT
if (AlwaysFalse()) if (AlwaysFalse())
ASSERT_NO_FATAL_FAILURE(FAIL()) << ""; ASSERT_NO_FATAL_FAILURE(FAIL()) << "";
else else
skipping to change at line 4182 skipping to change at line 4240
if (AlwaysTrue()) if (AlwaysTrue())
EXPECT_NO_FATAL_FAILURE(SUCCEED()); EXPECT_NO_FATAL_FAILURE(SUCCEED());
else else
; // NOLINT ; // NOLINT
if (AlwaysFalse()) if (AlwaysFalse())
; // NOLINT ; // NOLINT
else else
ASSERT_NO_FATAL_FAILURE(SUCCEED()); ASSERT_NO_FATAL_FAILURE(SUCCEED());
} }
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// Tests that the assertion macros work well with switch statements. // Tests that the assertion macros work well with switch statements.
TEST(AssertionSyntaxTest, WorksWithSwitch) { TEST(AssertionSyntaxTest, WorksWithSwitch) {
switch (0) { switch (0) {
case 1: case 1:
break; break;
default: default:
ASSERT_TRUE(true); ASSERT_TRUE(true);
} }
skipping to change at line 4311 skipping to change at line 4372
ASSERT_STREQ("1", "1") << "This should succeed."; ASSERT_STREQ("1", "1") << "This should succeed.";
ASSERT_STRNE("1", "2") << "This should succeed."; ASSERT_STRNE("1", "2") << "This should succeed.";
ASSERT_STRCASEEQ("a", "A") << "This should succeed."; ASSERT_STRCASEEQ("a", "A") << "This should succeed.";
EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("a", "A") << "Expected failure.", EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("a", "A") << "Expected failure.",
"Expected failure."); "Expected failure.");
} }
TEST(AssertionWithMessageTest, ASSERT_FLOATING) { TEST(AssertionWithMessageTest, ASSERT_FLOATING) {
ASSERT_FLOAT_EQ(1, 1) << "This should succeed."; ASSERT_FLOAT_EQ(1, 1) << "This should succeed.";
ASSERT_DOUBLE_EQ(1, 1) << "This should succeed."; ASSERT_DOUBLE_EQ(1, 1) << "This should succeed.";
EXPECT_FATAL_FAILURE(ASSERT_NEAR(1,1.2, 0.1) << "Expect failure.", // NOLINT EXPECT_FATAL_FAILURE(ASSERT_NEAR(1, 1.2, 0.1) << "Expect failure.", // NOLINT
"Expect failure."); "Expect failure.");
// To work around a bug in gcc 2.95.0, there is intentionally no
// space after the first comma in the previous statement.
} }
// Tests using ASSERT_FALSE with a streamed message. // Tests using ASSERT_FALSE with a streamed message.
TEST(AssertionWithMessageTest, ASSERT_FALSE) { TEST(AssertionWithMessageTest, ASSERT_FALSE) {
ASSERT_FALSE(false) << "This shouldn't fail."; ASSERT_FALSE(false) << "This shouldn't fail.";
EXPECT_FATAL_FAILURE({ // NOLINT EXPECT_FATAL_FAILURE({ // NOLINT
ASSERT_FALSE(true) << "Expected failure: " << 2 << " > " << 1 ASSERT_FALSE(true) << "Expected failure: " << 2 << " > " << 1
<< " evaluates to " << true; << " evaluates to " << true;
}, "Expected failure"); }, "Expected failure");
} }
skipping to change at line 4455 skipping to change at line 4514
// A failure. // A failure.
EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2), EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2),
"5.1"); "5.1");
} }
// Tests EXPECT_EQ(NULL, pointer). // Tests EXPECT_EQ(NULL, pointer).
TEST(ExpectTest, EXPECT_EQ_NULL) { TEST(ExpectTest, EXPECT_EQ_NULL) {
// A success. // A success.
const char* p = nullptr; const char* p = nullptr;
// Some older GCC versions may issue a spurious warning in this or the next
// assertion statement. This warning should not be suppressed with
// static_cast since the test verifies the ability to use bare NULL as the
// expected parameter to the macro.
EXPECT_EQ(nullptr, p); EXPECT_EQ(nullptr, p);
// A failure. // A failure.
int n = 0; int n = 0;
EXPECT_NONFATAL_FAILURE(EXPECT_EQ(nullptr, &n), " &n\n Which is:"); EXPECT_NONFATAL_FAILURE(EXPECT_EQ(nullptr, &n), " &n\n Which is:");
} }
// Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be // Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be
// treated as a null pointer by the compiler, we need to make sure // treated as a null pointer by the compiler, we need to make sure
// that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as // that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as
skipping to change at line 4549 skipping to change at line 4604
} }
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
// Tests EXPECT_THROW. // Tests EXPECT_THROW.
TEST(ExpectTest, EXPECT_THROW) { TEST(ExpectTest, EXPECT_THROW) {
EXPECT_THROW(ThrowAnInteger(), int); EXPECT_THROW(ThrowAnInteger(), int);
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool),
"Expected: ThrowAnInteger() throws an exception of " "Expected: ThrowAnInteger() throws an exception of "
"type bool.\n Actual: it throws a different type."); "type bool.\n Actual: it throws a different type.");
EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowRuntimeError("A description"),
std::logic_error),
"Expected: ThrowRuntimeError(\"A description\") "
"throws an exception of type std::logic_error.\n "
"Actual: it throws " ERROR_DESC " "
"with description \"A description\".");
EXPECT_NONFATAL_FAILURE( EXPECT_NONFATAL_FAILURE(
EXPECT_THROW(ThrowNothing(), bool), EXPECT_THROW(ThrowNothing(), bool),
"Expected: ThrowNothing() throws an exception of type bool.\n" "Expected: ThrowNothing() throws an exception of type bool.\n"
" Actual: it throws nothing."); " Actual: it throws nothing.");
} }
// Tests EXPECT_NO_THROW. // Tests EXPECT_NO_THROW.
TEST(ExpectTest, EXPECT_NO_THROW) { TEST(ExpectTest, EXPECT_NO_THROW) {
EXPECT_NO_THROW(ThrowNothing()); EXPECT_NO_THROW(ThrowNothing());
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()),
"Expected: ThrowAnInteger() doesn't throw an " "Expected: ThrowAnInteger() doesn't throw an "
"exception.\n Actual: it throws."); "exception.\n Actual: it throws.");
EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowRuntimeError("A description")),
"Expected: ThrowRuntimeError(\"A description\") "
"doesn't throw an exception.\n "
"Actual: it throws " ERROR_DESC " "
"with description \"A description\".");
} }
// Tests EXPECT_ANY_THROW. // Tests EXPECT_ANY_THROW.
TEST(ExpectTest, EXPECT_ANY_THROW) { TEST(ExpectTest, EXPECT_ANY_THROW) {
EXPECT_ANY_THROW(ThrowAnInteger()); EXPECT_ANY_THROW(ThrowAnInteger());
EXPECT_NONFATAL_FAILURE( EXPECT_NONFATAL_FAILURE(
EXPECT_ANY_THROW(ThrowNothing()), EXPECT_ANY_THROW(ThrowNothing()),
"Expected: ThrowNothing() throws an exception.\n" "Expected: ThrowNothing() throws an exception.\n"
" Actual: it doesn't."); " Actual: it doesn't.");
} }
skipping to change at line 5298 skipping to change at line 5364
static const TestResult* GetTestResult( static const TestResult* GetTestResult(
const TestInfo* test_info) { const TestInfo* test_info) {
return test_info->result(); return test_info->result();
} }
}; };
// Tests TestInfo::test_case_name() and TestInfo::name(). // Tests TestInfo::test_case_name() and TestInfo::name().
TEST_F(TestInfoTest, Names) { TEST_F(TestInfoTest, Names) {
const TestInfo* const test_info = GetTestInfo("Names"); const TestInfo* const test_info = GetTestInfo("Names");
ASSERT_STREQ("TestInfoTest", test_info->test_case_name()); ASSERT_STREQ("TestInfoTest", test_info->test_suite_name());
ASSERT_STREQ("Names", test_info->name()); ASSERT_STREQ("Names", test_info->name());
} }
// Tests TestInfo::result(). // Tests TestInfo::result().
TEST_F(TestInfoTest, result) { TEST_F(TestInfoTest, result) {
const TestInfo* const test_info = GetTestInfo("result"); const TestInfo* const test_info = GetTestInfo("result");
// Initially, there is no TestPartResult for this test. // Initially, there is no TestPartResult for this test.
ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
skipping to change at line 5368 skipping to change at line 5434
} }
REGISTER_TYPED_TEST_SUITE_P(CodeLocationForTYPEDTESTP, Verify); REGISTER_TYPED_TEST_SUITE_P(CodeLocationForTYPEDTESTP, Verify);
INSTANTIATE_TYPED_TEST_SUITE_P(My, CodeLocationForTYPEDTESTP, int); INSTANTIATE_TYPED_TEST_SUITE_P(My, CodeLocationForTYPEDTESTP, int);
#undef VERIFY_CODE_LOCATION #undef VERIFY_CODE_LOCATION
// Tests setting up and tearing down a test case. // Tests setting up and tearing down a test case.
// Legacy API is deprecated but still available // Legacy API is deprecated but still available
#ifndef REMOVE_LEGACY_TEST_CASEAPI #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
class SetUpTestCaseTest : public Test { class SetUpTestCaseTest : public Test {
protected: protected:
// This will be called once before the first test in this test case // This will be called once before the first test in this test case
// is run. // is run.
static void SetUpTestCase() { static void SetUpTestCase() {
printf("Setting up the test case . . .\n"); printf("Setting up the test case . . .\n");
// Initializes some shared resource. In this simple example, we // Initializes some shared resource. In this simple example, we
// just create a C string. More complex stuff can be done if // just create a C string. More complex stuff can be done if
// desired. // desired.
skipping to change at line 5427 skipping to change at line 5493
int SetUpTestCaseTest::counter_ = 0; int SetUpTestCaseTest::counter_ = 0;
const char* SetUpTestCaseTest::shared_resource_ = nullptr; const char* SetUpTestCaseTest::shared_resource_ = nullptr;
// A test that uses the shared resource. // A test that uses the shared resource.
TEST_F(SetUpTestCaseTest, Test1) { EXPECT_STRNE(nullptr, shared_resource_); } TEST_F(SetUpTestCaseTest, Test1) { EXPECT_STRNE(nullptr, shared_resource_); }
// Another test that uses the shared resource. // Another test that uses the shared resource.
TEST_F(SetUpTestCaseTest, Test2) { TEST_F(SetUpTestCaseTest, Test2) {
EXPECT_STREQ("123", shared_resource_); EXPECT_STREQ("123", shared_resource_);
} }
#endif // REMOVE_LEGACY_TEST_CASEAPI #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
// Tests SetupTestSuite/TearDown TestSuite // Tests SetupTestSuite/TearDown TestSuite
class SetUpTestSuiteTest : public Test { class SetUpTestSuiteTest : public Test {
protected: protected:
// This will be called once before the first test in this test case // This will be called once before the first test in this test case
// is run. // is run.
static void SetUpTestSuite() { static void SetUpTestSuite() {
printf("Setting up the test suite . . .\n"); printf("Setting up the test suite . . .\n");
// Initializes some shared resource. In this simple example, we // Initializes some shared resource. In this simple example, we
skipping to change at line 5496 skipping to change at line 5562
// Another test that uses the shared resource. // Another test that uses the shared resource.
TEST_F(SetUpTestSuiteTest, TestSetupTestSuite2) { TEST_F(SetUpTestSuiteTest, TestSetupTestSuite2) {
EXPECT_STREQ("123", shared_resource_); EXPECT_STREQ("123", shared_resource_);
} }
// The ParseFlagsTest test case tests ParseGoogleTestFlagsOnly. // The ParseFlagsTest test case tests ParseGoogleTestFlagsOnly.
// The Flags struct stores a copy of all Google Test flags. // The Flags struct stores a copy of all Google Test flags.
struct Flags { struct Flags {
// Constructs a Flags struct where each flag has its default value. // Constructs a Flags struct where each flag has its default value.
Flags() : also_run_disabled_tests(false), Flags()
break_on_failure(false), : also_run_disabled_tests(false),
catch_exceptions(false), break_on_failure(false),
death_test_use_fork(false), catch_exceptions(false),
filter(""), death_test_use_fork(false),
list_tests(false), fail_fast(false),
output(""), filter(""),
print_time(true), list_tests(false),
random_seed(0), output(""),
repeat(1), brief(false),
shuffle(false), print_time(true),
stack_trace_depth(kMaxStackTraceDepth), random_seed(0),
stream_result_to(""), repeat(1),
throw_on_failure(false) {} shuffle(false),
stack_trace_depth(kMaxStackTraceDepth),
stream_result_to(""),
throw_on_failure(false) {}
// Factory methods. // Factory methods.
// Creates a Flags struct where the gtest_also_run_disabled_tests flag has // Creates a Flags struct where the gtest_also_run_disabled_tests flag has
// the given value. // the given value.
static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) { static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) {
Flags flags; Flags flags;
flags.also_run_disabled_tests = also_run_disabled_tests; flags.also_run_disabled_tests = also_run_disabled_tests;
return flags; return flags;
} }
skipping to change at line 5545 skipping to change at line 5614
} }
// Creates a Flags struct where the gtest_death_test_use_fork flag has // Creates a Flags struct where the gtest_death_test_use_fork flag has
// the given value. // the given value.
static Flags DeathTestUseFork(bool death_test_use_fork) { static Flags DeathTestUseFork(bool death_test_use_fork) {
Flags flags; Flags flags;
flags.death_test_use_fork = death_test_use_fork; flags.death_test_use_fork = death_test_use_fork;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_fail_fast flag has
// the given value.
static Flags FailFast(bool fail_fast) {
Flags flags;
flags.fail_fast = fail_fast;
return flags;
}
// Creates a Flags struct where the gtest_filter flag has the given // Creates a Flags struct where the gtest_filter flag has the given
// value. // value.
static Flags Filter(const char* filter) { static Flags Filter(const char* filter) {
Flags flags; Flags flags;
flags.filter = filter; flags.filter = filter;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_list_tests flag has the // Creates a Flags struct where the gtest_list_tests flag has the
// given value. // given value.
skipping to change at line 5569 skipping to change at line 5646
} }
// Creates a Flags struct where the gtest_output flag has the given // Creates a Flags struct where the gtest_output flag has the given
// value. // value.
static Flags Output(const char* output) { static Flags Output(const char* output) {
Flags flags; Flags flags;
flags.output = output; flags.output = output;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_brief flag has the given
// value.
static Flags Brief(bool brief) {
Flags flags;
flags.brief = brief;
return flags;
}
// Creates a Flags struct where the gtest_print_time flag has the given // Creates a Flags struct where the gtest_print_time flag has the given
// value. // value.
static Flags PrintTime(bool print_time) { static Flags PrintTime(bool print_time) {
Flags flags; Flags flags;
flags.print_time = print_time; flags.print_time = print_time;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_random_seed flag has the given // Creates a Flags struct where the gtest_random_seed flag has the given
// value. // value.
static Flags RandomSeed(Int32 random_seed) { static Flags RandomSeed(int32_t random_seed) {
Flags flags; Flags flags;
flags.random_seed = random_seed; flags.random_seed = random_seed;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_repeat flag has the given // Creates a Flags struct where the gtest_repeat flag has the given
// value. // value.
static Flags Repeat(Int32 repeat) { static Flags Repeat(int32_t repeat) {
Flags flags; Flags flags;
flags.repeat = repeat; flags.repeat = repeat;
return flags; return flags;
} }
// Creates a Flags struct where the gtest_shuffle flag has the given // Creates a Flags struct where the gtest_shuffle flag has the given
// value. // value.
static Flags Shuffle(bool shuffle) { static Flags Shuffle(bool shuffle) {
Flags flags; Flags flags;
flags.shuffle = shuffle; flags.shuffle = shuffle;
return flags; return flags;
} }
// Creates a Flags struct where the GTEST_FLAG(stack_trace_depth) flag has // Creates a Flags struct where the GTEST_FLAG(stack_trace_depth) flag has
// the given value. // the given value.
static Flags StackTraceDepth(Int32 stack_trace_depth) { static Flags StackTraceDepth(int32_t stack_trace_depth) {
Flags flags; Flags flags;
flags.stack_trace_depth = stack_trace_depth; flags.stack_trace_depth = stack_trace_depth;
return flags; return flags;
} }
// Creates a Flags struct where the GTEST_FLAG(stream_result_to) flag has // Creates a Flags struct where the GTEST_FLAG(stream_result_to) flag has
// the given value. // the given value.
static Flags StreamResultTo(const char* stream_result_to) { static Flags StreamResultTo(const char* stream_result_to) {
Flags flags; Flags flags;
flags.stream_result_to = stream_result_to; flags.stream_result_to = stream_result_to;
skipping to change at line 5630 skipping to change at line 5715
Flags flags; Flags flags;
flags.throw_on_failure = throw_on_failure; flags.throw_on_failure = throw_on_failure;
return flags; return flags;
} }
// These fields store the flag values. // These fields store the flag values.
bool also_run_disabled_tests; bool also_run_disabled_tests;
bool break_on_failure; bool break_on_failure;
bool catch_exceptions; bool catch_exceptions;
bool death_test_use_fork; bool death_test_use_fork;
bool fail_fast;
const char* filter; const char* filter;
bool list_tests; bool list_tests;
const char* output; const char* output;
bool brief;
bool print_time; bool print_time;
Int32 random_seed; int32_t random_seed;
Int32 repeat; int32_t repeat;
bool shuffle; bool shuffle;
Int32 stack_trace_depth; int32_t stack_trace_depth;
const char* stream_result_to; const char* stream_result_to;
bool throw_on_failure; bool throw_on_failure;
}; };
// Fixture for testing ParseGoogleTestFlagsOnly(). // Fixture for testing ParseGoogleTestFlagsOnly().
class ParseFlagsTest : public Test { class ParseFlagsTest : public Test {
protected: protected:
// Clears the flags before each test. // Clears the flags before each test.
void SetUp() override { void SetUp() override {
GTEST_FLAG(also_run_disabled_tests) = false; GTEST_FLAG(also_run_disabled_tests) = false;
GTEST_FLAG(break_on_failure) = false; GTEST_FLAG(break_on_failure) = false;
GTEST_FLAG(catch_exceptions) = false; GTEST_FLAG(catch_exceptions) = false;
GTEST_FLAG(death_test_use_fork) = false; GTEST_FLAG(death_test_use_fork) = false;
GTEST_FLAG(fail_fast) = false;
GTEST_FLAG(filter) = ""; GTEST_FLAG(filter) = "";
GTEST_FLAG(list_tests) = false; GTEST_FLAG(list_tests) = false;
GTEST_FLAG(output) = ""; GTEST_FLAG(output) = "";
GTEST_FLAG(brief) = false;
GTEST_FLAG(print_time) = true; GTEST_FLAG(print_time) = true;
GTEST_FLAG(random_seed) = 0; GTEST_FLAG(random_seed) = 0;
GTEST_FLAG(repeat) = 1; GTEST_FLAG(repeat) = 1;
GTEST_FLAG(shuffle) = false; GTEST_FLAG(shuffle) = false;
GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth;
GTEST_FLAG(stream_result_to) = ""; GTEST_FLAG(stream_result_to) = "";
GTEST_FLAG(throw_on_failure) = false; GTEST_FLAG(throw_on_failure) = false;
} }
// Asserts that two narrow or wide string arrays are equal. // Asserts that two narrow or wide string arrays are equal.
skipping to change at line 5681 skipping to change at line 5770
} }
} }
// Verifies that the flag values match the expected values. // Verifies that the flag values match the expected values.
static void CheckFlags(const Flags& expected) { static void CheckFlags(const Flags& expected) {
EXPECT_EQ(expected.also_run_disabled_tests, EXPECT_EQ(expected.also_run_disabled_tests,
GTEST_FLAG(also_run_disabled_tests)); GTEST_FLAG(also_run_disabled_tests));
EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure)); EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure));
EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions)); EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions));
EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork)); EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork));
EXPECT_EQ(expected.fail_fast, GTEST_FLAG(fail_fast));
EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str()); EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str());
EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests)); EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests));
EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str()); EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str());
EXPECT_EQ(expected.brief, GTEST_FLAG(brief));
EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time)); EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time));
EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed)); EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed));
EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat)); EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat));
EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle)); EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle));
EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG(stack_trace_depth)); EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG(stack_trace_depth));
EXPECT_STREQ(expected.stream_result_to, EXPECT_STREQ(expected.stream_result_to,
GTEST_FLAG(stream_result_to).c_str()); GTEST_FLAG(stream_result_to).c_str());
EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure)); EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure));
} }
skipping to change at line 5767 skipping to change at line 5858
// Tests parsing a command line that has no flag. // Tests parsing a command line that has no flag.
TEST_F(ParseFlagsTest, NoFlag) { TEST_F(ParseFlagsTest, NoFlag) {
const char* argv[] = {"foo.exe", nullptr}; const char* argv[] = {"foo.exe", nullptr};
const char* argv2[] = {"foo.exe", nullptr}; const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false);
} }
// Tests parsing --gtest_fail_fast.
TEST_F(ParseFlagsTest, FailFast) {
const char* argv[] = {"foo.exe", "--gtest_fail_fast", nullptr};
const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::FailFast(true), false);
}
// Tests parsing a bad --gtest_filter flag. // Tests parsing a bad --gtest_filter flag.
TEST_F(ParseFlagsTest, FilterBad) { TEST_F(ParseFlagsTest, FilterBad) {
const char* argv[] = {"foo.exe", "--gtest_filter", nullptr}; const char* argv[] = {"foo.exe", "--gtest_filter", nullptr};
const char* argv2[] = {"foo.exe", "--gtest_filter", nullptr}; const char* argv2[] = {"foo.exe", "--gtest_filter", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true); GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true);
} }
// Tests parsing an empty --gtest_filter flag. // Tests parsing an empty --gtest_filter flag.
skipping to change at line 5966 skipping to change at line 6066
TEST_F(ParseFlagsTest, OutputXmlDirectory) { TEST_F(ParseFlagsTest, OutputXmlDirectory) {
const char* argv[] = {"foo.exe", "--gtest_output=xml:directory/path/", const char* argv[] = {"foo.exe", "--gtest_output=xml:directory/path/",
nullptr}; nullptr};
const char* argv2[] = {"foo.exe", nullptr}; const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, GTEST_TEST_PARSING_FLAGS_(argv, argv2,
Flags::Output("xml:directory/path/"), false); Flags::Output("xml:directory/path/"), false);
} }
// Tests having a --gtest_brief flag
TEST_F(ParseFlagsTest, BriefFlag) {
const char* argv[] = {"foo.exe", "--gtest_brief", nullptr};
const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Brief(true), false);
}
// Tests having a --gtest_brief flag with a "true" value
TEST_F(ParseFlagsTest, BriefFlagTrue) {
const char* argv[] = {"foo.exe", "--gtest_brief=1", nullptr};
const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Brief(true), false);
}
// Tests having a --gtest_brief flag with a "false" value
TEST_F(ParseFlagsTest, BriefFlagFalse) {
const char* argv[] = {"foo.exe", "--gtest_brief=0", nullptr};
const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Brief(false), false);
}
// Tests having a --gtest_print_time flag // Tests having a --gtest_print_time flag
TEST_F(ParseFlagsTest, PrintTimeFlag) { TEST_F(ParseFlagsTest, PrintTimeFlag) {
const char* argv[] = {"foo.exe", "--gtest_print_time", nullptr}; const char* argv[] = {"foo.exe", "--gtest_print_time", nullptr};
const char* argv2[] = {"foo.exe", nullptr}; const char* argv2[] = {"foo.exe", nullptr};
GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false);
} }
// Tests having a --gtest_print_time flag with a "true" value // Tests having a --gtest_print_time flag with a "true" value
skipping to change at line 6165 skipping to change at line 6292
expected_flags.filter = "Foo*"; expected_flags.filter = "Foo*";
expected_flags.list_tests = true; expected_flags.list_tests = true;
GTEST_TEST_PARSING_FLAGS_(argv, argv2, expected_flags, false); GTEST_TEST_PARSING_FLAGS_(argv, argv2, expected_flags, false);
} }
# endif // GTEST_OS_WINDOWS # endif // GTEST_OS_WINDOWS
#if GTEST_USE_OWN_FLAGFILE_FLAG_ #if GTEST_USE_OWN_FLAGFILE_FLAG_
class FlagfileTest : public ParseFlagsTest { class FlagfileTest : public ParseFlagsTest {
public: public:
virtual void SetUp() { void SetUp() override {
ParseFlagsTest::SetUp(); ParseFlagsTest::SetUp();
testdata_path_.Set(internal::FilePath( testdata_path_.Set(internal::FilePath(
testing::TempDir() + internal::GetCurrentExecutableName().string() + testing::TempDir() + internal::GetCurrentExecutableName().string() +
"_flagfile_test")); "_flagfile_test"));
testing::internal::posix::RmDir(testdata_path_.c_str()); testing::internal::posix::RmDir(testdata_path_.c_str());
EXPECT_TRUE(testdata_path_.CreateFolder()); EXPECT_TRUE(testdata_path_.CreateFolder());
} }
virtual void TearDown() { void TearDown() override {
testing::internal::posix::RmDir(testdata_path_.c_str()); testing::internal::posix::RmDir(testdata_path_.c_str());
ParseFlagsTest::TearDown(); ParseFlagsTest::TearDown();
} }
internal::FilePath CreateFlagfile(const char* contents) { internal::FilePath CreateFlagfile(const char* contents) {
internal::FilePath file_path(internal::FilePath::GenerateUniqueFileName( internal::FilePath file_path(internal::FilePath::GenerateUniqueFileName(
testdata_path_, internal::FilePath("unique"), "txt")); testdata_path_, internal::FilePath("unique"), "txt"));
FILE* f = testing::internal::posix::FOpen(file_path.c_str(), "w"); FILE* f = testing::internal::posix::FOpen(file_path.c_str(), "w");
fprintf(f, "%s", contents); fprintf(f, "%s", contents);
fclose(f); fclose(f);
skipping to change at line 6272 skipping to change at line 6399
} }
}; };
// Tests that current_test_info() returns TestInfo for currently running // Tests that current_test_info() returns TestInfo for currently running
// test by checking the expected test name against the actual one. // test by checking the expected test name against the actual one.
TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestSuite) { TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestSuite) {
const TestInfo* test_info = const TestInfo* test_info =
UnitTest::GetInstance()->current_test_info(); UnitTest::GetInstance()->current_test_info();
ASSERT_TRUE(nullptr != test_info) ASSERT_TRUE(nullptr != test_info)
<< "There is a test running so we should have a valid TestInfo."; << "There is a test running so we should have a valid TestInfo.";
EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) EXPECT_STREQ("CurrentTestInfoTest", test_info->test_suite_name())
<< "Expected the name of the currently running test case."; << "Expected the name of the currently running test suite.";
EXPECT_STREQ("WorksForFirstTestInATestSuite", test_info->name()) EXPECT_STREQ("WorksForFirstTestInATestSuite", test_info->name())
<< "Expected the name of the currently running test."; << "Expected the name of the currently running test.";
} }
// Tests that current_test_info() returns TestInfo for currently running // Tests that current_test_info() returns TestInfo for currently running
// test by checking the expected test name against the actual one. We // test by checking the expected test name against the actual one. We
// use this test to see that the TestInfo object actually changed from // use this test to see that the TestInfo object actually changed from
// the previous invocation. // the previous invocation.
TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestSuite) { TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestSuite) {
const TestInfo* test_info = const TestInfo* test_info =
UnitTest::GetInstance()->current_test_info(); UnitTest::GetInstance()->current_test_info();
ASSERT_TRUE(nullptr != test_info) ASSERT_TRUE(nullptr != test_info)
<< "There is a test running so we should have a valid TestInfo."; << "There is a test running so we should have a valid TestInfo.";
EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) EXPECT_STREQ("CurrentTestInfoTest", test_info->test_suite_name())
<< "Expected the name of the currently running test case."; << "Expected the name of the currently running test suite.";
EXPECT_STREQ("WorksForSecondTestInATestSuite", test_info->name()) EXPECT_STREQ("WorksForSecondTestInATestSuite", test_info->name())
<< "Expected the name of the currently running test."; << "Expected the name of the currently running test.";
} }
} // namespace testing } // namespace testing
// These two lines test that we can define tests in a namespace that // These two lines test that we can define tests in a namespace that
// has the name "testing" and is nested in another namespace. // has the name "testing" and is nested in another namespace.
namespace my_namespace { namespace my_namespace {
namespace testing { namespace testing {
skipping to change at line 7075 skipping to change at line 7202
EXPECT_FATAL_FAILURE(GTEST_ASSERT_GT(1, 1) << "An expected failure", EXPECT_FATAL_FAILURE(GTEST_ASSERT_GT(1, 1) << "An expected failure",
"An expected failure"); "An expected failure");
} }
// Tests for internal utilities necessary for implementation of the universal // Tests for internal utilities necessary for implementation of the universal
// printing. // printing.
class ConversionHelperBase {}; class ConversionHelperBase {};
class ConversionHelperDerived : public ConversionHelperBase {}; class ConversionHelperDerived : public ConversionHelperBase {};
// Tests that IsAProtocolMessage<T>::value is a compile-time constant. struct HasDebugStringMethods {
TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { std::string DebugString() const { return ""; }
GTEST_COMPILE_ASSERT_(IsAProtocolMessage<::proto2::Message>::value, std::string ShortDebugString() const { return ""; }
const_true); };
GTEST_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false);
} struct InheritsDebugStringMethods : public HasDebugStringMethods {};
struct WrongTypeDebugStringMethod {
std::string DebugString() const { return ""; }
int ShortDebugString() const { return 1; }
};
struct NotConstDebugStringMethod {
std::string DebugString() { return ""; }
std::string ShortDebugString() const { return ""; }
};
struct MissingDebugStringMethod {
std::string DebugString() { return ""; }
};
struct IncompleteType;
// Tests that IsAProtocolMessage<T>::value is true when T is // Tests that HasDebugStringAndShortDebugString<T>::value is a compile-time
// proto2::Message or a sub-class of it. // constant.
TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { TEST(HasDebugStringAndShortDebugStringTest, ValueIsCompileTimeConstant) {
EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); GTEST_COMPILE_ASSERT_(
HasDebugStringAndShortDebugString<HasDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(
HasDebugStringAndShortDebugString<InheritsDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(HasDebugStringAndShortDebugString<
const InheritsDebugStringMethods>::value,
const_true);
GTEST_COMPILE_ASSERT_(
!HasDebugStringAndShortDebugString<WrongTypeDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
!HasDebugStringAndShortDebugString<NotConstDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
!HasDebugStringAndShortDebugString<MissingDebugStringMethod>::value,
const_false);
GTEST_COMPILE_ASSERT_(
!HasDebugStringAndShortDebugString<IncompleteType>::value, const_false);
GTEST_COMPILE_ASSERT_(!HasDebugStringAndShortDebugString<int>::value,
const_false);
}
// Tests that HasDebugStringAndShortDebugString<T>::value is true when T has
// needed methods.
TEST(HasDebugStringAndShortDebugStringTest,
ValueIsTrueWhenTypeHasDebugStringAndShortDebugString) {
EXPECT_TRUE(
HasDebugStringAndShortDebugString<InheritsDebugStringMethods>::value);
} }
// Tests that IsAProtocolMessage<T>::value is false when T is neither // Tests that HasDebugStringAndShortDebugString<T>::value is false when T
// ::proto2::Message nor a sub-class of it. // doesn't have needed methods.
TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { TEST(HasDebugStringAndShortDebugStringTest,
EXPECT_FALSE(IsAProtocolMessage<int>::value); ValueIsFalseWhenTypeIsNotAProtocolMessage) {
EXPECT_FALSE(IsAProtocolMessage<const ConversionHelperBase>::value); EXPECT_FALSE(HasDebugStringAndShortDebugString<int>::value);
EXPECT_FALSE(
HasDebugStringAndShortDebugString<const ConversionHelperBase>::value);
} }
// Tests GTEST_REMOVE_REFERENCE_AND_CONST_. // Tests GTEST_REMOVE_REFERENCE_AND_CONST_.
template <typename T1, typename T2> template <typename T1, typename T2>
void TestGTestRemoveReferenceAndConst() { void TestGTestRemoveReferenceAndConst() {
static_assert(std::is_same<T1, GTEST_REMOVE_REFERENCE_AND_CONST_(T2)>::value, static_assert(std::is_same<T1, GTEST_REMOVE_REFERENCE_AND_CONST_(T2)>::value,
"GTEST_REMOVE_REFERENCE_AND_CONST_ failed."); "GTEST_REMOVE_REFERENCE_AND_CONST_ failed.");
} }
skipping to change at line 7346 skipping to change at line 7520
(std::is_same<IndexSequence<0, 1>, MakeIndexSequence<2>::type>::value)); (std::is_same<IndexSequence<0, 1>, MakeIndexSequence<2>::type>::value));
EXPECT_TRUE(( EXPECT_TRUE((
std::is_same<IndexSequence<0, 1, 2>, MakeIndexSequence<3>::type>::value)); std::is_same<IndexSequence<0, 1, 2>, MakeIndexSequence<3>::type>::value));
EXPECT_TRUE( EXPECT_TRUE(
(std::is_base_of<IndexSequence<0, 1, 2>, MakeIndexSequence<3>>::value)); (std::is_base_of<IndexSequence<0, 1, 2>, MakeIndexSequence<3>>::value));
} }
// ElemFromList // ElemFromList
TEST(ElemFromList, Basic) { TEST(ElemFromList, Basic) {
using testing::internal::ElemFromList; using testing::internal::ElemFromList;
using Idx = testing::internal::MakeIndexSequence<3>::type;
EXPECT_TRUE((
std::is_same<int, ElemFromList<0, Idx, int, double, char>::type>::value));
EXPECT_TRUE( EXPECT_TRUE(
(std::is_same<double, (std::is_same<int, ElemFromList<0, int, double, char>::type>::value));
ElemFromList<1, Idx, int, double, char>::type>::value));
EXPECT_TRUE( EXPECT_TRUE(
(std::is_same<char, (std::is_same<double, ElemFromList<1, int, double, char>::type>::value));
ElemFromList<2, Idx, int, double, char>::type>::value));
EXPECT_TRUE( EXPECT_TRUE(
(std::is_same< (std::is_same<char, ElemFromList<2, int, double, char>::type>::value));
char, ElemFromList<7, testing::internal::MakeIndexSequence<12>::type, EXPECT_TRUE((
int, int, int, int, int, int, int, char, int, int, std::is_same<char, ElemFromList<7, int, int, int, int, int, int, int,
int, int>::type>::value)); char, int, int, int, int>::type>::value));
} }
// FlatTuple // FlatTuple
TEST(FlatTuple, Basic) { TEST(FlatTuple, Basic) {
using testing::internal::FlatTuple; using testing::internal::FlatTuple;
FlatTuple<int, double, const char*> tuple = {}; FlatTuple<int, double, const char*> tuple = {};
EXPECT_EQ(0, tuple.Get<0>()); EXPECT_EQ(0, tuple.Get<0>());
EXPECT_EQ(0.0, tuple.Get<1>()); EXPECT_EQ(0.0, tuple.Get<1>());
EXPECT_EQ(nullptr, tuple.Get<2>()); EXPECT_EQ(nullptr, tuple.Get<2>());
tuple = FlatTuple<int, double, const char*>(7, 3.2, "Foo"); tuple = FlatTuple<int, double, const char*>(
testing::internal::FlatTupleConstructTag{}, 7, 3.2, "Foo");
EXPECT_EQ(7, tuple.Get<0>()); EXPECT_EQ(7, tuple.Get<0>());
EXPECT_EQ(3.2, tuple.Get<1>()); EXPECT_EQ(3.2, tuple.Get<1>());
EXPECT_EQ(std::string("Foo"), tuple.Get<2>()); EXPECT_EQ(std::string("Foo"), tuple.Get<2>());
tuple.Get<1>() = 5.1; tuple.Get<1>() = 5.1;
EXPECT_EQ(5.1, tuple.Get<1>()); EXPECT_EQ(5.1, tuple.Get<1>());
} }
namespace {
std::string AddIntToString(int i, const std::string& s) {
return s + std::to_string(i);
}
} // namespace
TEST(FlatTuple, Apply) {
using testing::internal::FlatTuple;
FlatTuple<int, std::string> tuple{testing::internal::FlatTupleConstructTag{},
5, "Hello"};
// Lambda.
EXPECT_TRUE(tuple.Apply([](int i, const std::string& s) -> bool {
return i == static_cast<int>(s.size());
}));
// Function.
EXPECT_EQ(tuple.Apply(AddIntToString), "Hello5");
// Mutating operations.
tuple.Apply([](int& i, std::string& s) {
++i;
s += s;
});
EXPECT_EQ(tuple.Get<0>(), 6);
EXPECT_EQ(tuple.Get<1>(), "HelloHello");
}
struct ConstructionCounting {
ConstructionCounting() { ++default_ctor_calls; }
~ConstructionCounting() { ++dtor_calls; }
ConstructionCounting(const ConstructionCounting&) { ++copy_ctor_calls; }
ConstructionCounting(ConstructionCounting&&) noexcept { ++move_ctor_calls; }
ConstructionCounting& operator=(const ConstructionCounting&) {
++copy_assignment_calls;
return *this;
}
ConstructionCounting& operator=(ConstructionCounting&&) noexcept {
++move_assignment_calls;
return *this;
}
static void Reset() {
default_ctor_calls = 0;
dtor_calls = 0;
copy_ctor_calls = 0;
move_ctor_calls = 0;
copy_assignment_calls = 0;
move_assignment_calls = 0;
}
static int default_ctor_calls;
static int dtor_calls;
static int copy_ctor_calls;
static int move_ctor_calls;
static int copy_assignment_calls;
static int move_assignment_calls;
};
int ConstructionCounting::default_ctor_calls = 0;
int ConstructionCounting::dtor_calls = 0;
int ConstructionCounting::copy_ctor_calls = 0;
int ConstructionCounting::move_ctor_calls = 0;
int ConstructionCounting::copy_assignment_calls = 0;
int ConstructionCounting::move_assignment_calls = 0;
TEST(FlatTuple, ConstructorCalls) {
using testing::internal::FlatTuple;
// Default construction.
ConstructionCounting::Reset();
{ FlatTuple<ConstructionCounting> tuple; }
EXPECT_EQ(ConstructionCounting::default_ctor_calls, 1);
EXPECT_EQ(ConstructionCounting::dtor_calls, 1);
EXPECT_EQ(ConstructionCounting::copy_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::move_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::copy_assignment_calls, 0);
EXPECT_EQ(ConstructionCounting::move_assignment_calls, 0);
// Copy construction.
ConstructionCounting::Reset();
{
ConstructionCounting elem;
FlatTuple<ConstructionCounting> tuple{
testing::internal::FlatTupleConstructTag{}, elem};
}
EXPECT_EQ(ConstructionCounting::default_ctor_calls, 1);
EXPECT_EQ(ConstructionCounting::dtor_calls, 2);
EXPECT_EQ(ConstructionCounting::copy_ctor_calls, 1);
EXPECT_EQ(ConstructionCounting::move_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::copy_assignment_calls, 0);
EXPECT_EQ(ConstructionCounting::move_assignment_calls, 0);
// Move construction.
ConstructionCounting::Reset();
{
FlatTuple<ConstructionCounting> tuple{
testing::internal::FlatTupleConstructTag{}, ConstructionCounting{}};
}
EXPECT_EQ(ConstructionCounting::default_ctor_calls, 1);
EXPECT_EQ(ConstructionCounting::dtor_calls, 2);
EXPECT_EQ(ConstructionCounting::copy_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::move_ctor_calls, 1);
EXPECT_EQ(ConstructionCounting::copy_assignment_calls, 0);
EXPECT_EQ(ConstructionCounting::move_assignment_calls, 0);
// Copy assignment.
// TODO(ofats): it should be testing assignment operator of FlatTuple, not its
// elements
ConstructionCounting::Reset();
{
FlatTuple<ConstructionCounting> tuple;
ConstructionCounting elem;
tuple.Get<0>() = elem;
}
EXPECT_EQ(ConstructionCounting::default_ctor_calls, 2);
EXPECT_EQ(ConstructionCounting::dtor_calls, 2);
EXPECT_EQ(ConstructionCounting::copy_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::move_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::copy_assignment_calls, 1);
EXPECT_EQ(ConstructionCounting::move_assignment_calls, 0);
// Move assignment.
// TODO(ofats): it should be testing assignment operator of FlatTuple, not its
// elements
ConstructionCounting::Reset();
{
FlatTuple<ConstructionCounting> tuple;
tuple.Get<0>() = ConstructionCounting{};
}
EXPECT_EQ(ConstructionCounting::default_ctor_calls, 2);
EXPECT_EQ(ConstructionCounting::dtor_calls, 2);
EXPECT_EQ(ConstructionCounting::copy_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::move_ctor_calls, 0);
EXPECT_EQ(ConstructionCounting::copy_assignment_calls, 0);
EXPECT_EQ(ConstructionCounting::move_assignment_calls, 1);
ConstructionCounting::Reset();
}
TEST(FlatTuple, ManyTypes) { TEST(FlatTuple, ManyTypes) {
using testing::internal::FlatTuple; using testing::internal::FlatTuple;
// Instantiate FlatTuple with 257 ints. // Instantiate FlatTuple with 257 ints.
// Tests show that we can do it with thousands of elements, but very long // Tests show that we can do it with thousands of elements, but very long
// compile times makes it unusuitable for this test. // compile times makes it unusuitable for this test.
#define GTEST_FLAT_TUPLE_INT8 int, int, int, int, int, int, int, int, #define GTEST_FLAT_TUPLE_INT8 int, int, int, int, int, int, int, int,
#define GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT8 GTEST_FLAT_TUPLE_INT8 #define GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT8 GTEST_FLAT_TUPLE_INT8
#define GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT16 #define GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT16
#define GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT32 #define GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT32
skipping to change at line 7432 skipping to change at line 7743
const char* p = str; const char* p = str;
EXPECT_FALSE(SkipPrefix("W", &p)); EXPECT_FALSE(SkipPrefix("W", &p));
EXPECT_EQ(str, p); EXPECT_EQ(str, p);
p = str; p = str;
EXPECT_FALSE(SkipPrefix("world!", &p)); EXPECT_FALSE(SkipPrefix("world!", &p));
EXPECT_EQ(str, p); EXPECT_EQ(str, p);
} }
// Tests ad_hoc_test_result(). // Tests ad_hoc_test_result().
TEST(AdHocTestResultTest, AdHocTestResultForUnitTestDoesNotShowFailure) {
class AdHocTestResultTest : public testing::Test {
protected:
static void SetUpTestSuite() {
FAIL() << "A failure happened inside SetUpTestSuite().";
}
};
TEST_F(AdHocTestResultTest, AdHocTestResultForTestSuiteShowsFailure) {
const testing::TestResult& test_result = testing::UnitTest::GetInstance()
->current_test_suite()
->ad_hoc_test_result();
EXPECT_TRUE(test_result.Failed());
}
TEST_F(AdHocTestResultTest, AdHocTestResultTestForUnitTestDoesNotShowFailure) {
const testing::TestResult& test_result = const testing::TestResult& test_result =
testing::UnitTest::GetInstance()->ad_hoc_test_result(); testing::UnitTest::GetInstance()->ad_hoc_test_result();
EXPECT_FALSE(test_result.Failed()); EXPECT_FALSE(test_result.Failed());
} }
class DynamicUnitTestFixture : public testing::Test {}; class DynamicUnitTestFixture : public testing::Test {};
class DynamicTest : public DynamicUnitTestFixture { class DynamicTest : public DynamicUnitTestFixture {
void TestBody() override { EXPECT_TRUE(true); } void TestBody() override { EXPECT_TRUE(true); }
}; };
 End of changes. 101 change blocks. 
151 lines changed or deleted 447 lines changed or added

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