diff options
Diffstat (limited to 'googletest/test/gtest_unittest.cc')
-rw-r--r-- | googletest/test/gtest_unittest.cc | 146 |
1 files changed, 101 insertions, 45 deletions
diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index 9ddb37d..5e0e43a 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -511,8 +511,6 @@ TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) { EXPECT_EQ("1970-01-01T00:00:00", FormatEpochTimeInMillisAsIso8601(0)); } -#if GTEST_CAN_COMPARE_NULL - # ifdef __BORLANDC__ // Silences warnings: "Condition is always true", "Unreachable code" # pragma option push -w-ccc -w-rch @@ -541,7 +539,6 @@ TEST(NullLiteralTest, IsFalseForNonNullLiterals) { # pragma option pop # endif -#endif // GTEST_CAN_COMPARE_NULL // // Tests CodePointToUtf8(). @@ -586,7 +583,7 @@ TEST(CodePointToUtf8Test, CanEncode12To16Bits) { #if !GTEST_WIDE_STRING_USES_UTF16_ // Tests in this group require a wchar_t to hold > 16 bits, and thus -// are skipped on Windows, Cygwin, and Symbian, where a wchar_t is +// are skipped on Windows, and Cygwin, where a wchar_t is // 16-bit wide. This code may not compile on those systems. // Tests that Unicode code-points that have 17 to 21 bits are encoded @@ -2360,6 +2357,16 @@ TEST(PredTest, SingleEvaluationOnFailure) { EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; } +// Test predicate assertions for sets +TEST(PredTest, ExpectPredEvalFailure) { + std::set<int> set_a = {2, 1, 3, 4, 5}; + std::set<int> set_b = {0, 4, 8}; + const auto compare_sets = [] (std::set<int>, std::set<int>) { return false; }; + EXPECT_NONFATAL_FAILURE( + EXPECT_PRED2(compare_sets, set_a, set_b), + "compare_sets(set_a, set_b) evaluates to false, where\nset_a evaluates " + "to { 1, 2, 3, 4, 5 }\nset_b evaluates to { 0, 4, 8 }"); +} // Some helper functions for testing using overloaded/template // functions with ASSERT_PREDn and EXPECT_PREDn. @@ -2822,8 +2829,6 @@ TEST_F(FloatTest, LargeDiff) { TEST_F(FloatTest, Infinity) { EXPECT_FLOAT_EQ(values_.infinity, values_.close_to_infinity); EXPECT_FLOAT_EQ(-values_.infinity, -values_.close_to_infinity); -#if !GTEST_OS_SYMBIAN - // Nokia's STLport crashes if we try to output infinity or NaN. EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, -values_.infinity), "-values_.infinity"); @@ -2831,14 +2836,10 @@ TEST_F(FloatTest, Infinity) { // are only 1 DLP apart. EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, values_.nan1), "values_.nan1"); -#endif // !GTEST_OS_SYMBIAN } // Tests that comparing with NAN always returns false. TEST_F(FloatTest, NaN) { -#if !GTEST_OS_SYMBIAN -// Nokia's STLport crashes if we try to output infinity or NaN. - // In C++Builder, names within local classes (such as used by // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the // scoping class. Use a static local alias as a workaround. @@ -2856,7 +2857,6 @@ TEST_F(FloatTest, NaN) { EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(v.nan1, v.infinity), "v.infinity"); -#endif // !GTEST_OS_SYMBIAN } // Tests that *_FLOAT_EQ are reflexive. @@ -2918,10 +2918,6 @@ TEST_F(FloatTest, FloatLEFails) { EXPECT_PRED_FORMAT2(FloatLE, values_.further_from_one, 1.0f); }, "(values_.further_from_one) <= (1.0f)"); -#if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) - // Nokia's STLport crashes if we try to output infinity or NaN. - // C++Builder gives bad results for ordered comparisons involving NaNs - // due to compiler bugs. EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_PRED_FORMAT2(FloatLE, values_.nan1, values_.infinity); }, "(values_.nan1) <= (values_.infinity)"); @@ -2931,7 +2927,6 @@ TEST_F(FloatTest, FloatLEFails) { EXPECT_FATAL_FAILURE({ // NOLINT ASSERT_PRED_FORMAT2(FloatLE, values_.nan1, values_.nan1); }, "(values_.nan1) <= (values_.nan1)"); -#endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) } // Instantiates FloatingPointTest for testing *_DOUBLE_EQ. @@ -2995,8 +2990,6 @@ TEST_F(DoubleTest, LargeDiff) { TEST_F(DoubleTest, Infinity) { EXPECT_DOUBLE_EQ(values_.infinity, values_.close_to_infinity); EXPECT_DOUBLE_EQ(-values_.infinity, -values_.close_to_infinity); -#if !GTEST_OS_SYMBIAN - // Nokia's STLport crashes if we try to output infinity or NaN. EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, -values_.infinity), "-values_.infinity"); @@ -3004,18 +2997,10 @@ TEST_F(DoubleTest, Infinity) { // are only 1 DLP apart. EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, values_.nan1), "values_.nan1"); -#endif // !GTEST_OS_SYMBIAN } // Tests that comparing with NAN always returns false. TEST_F(DoubleTest, NaN) { -#if !GTEST_OS_SYMBIAN - // In C++Builder, names within local classes (such as used by - // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the - // scoping class. Use a static local alias as a workaround. - // We use the assignment syntax since some compilers, like Sun Studio, - // don't allow initializing references using construction syntax - // (parentheses). static const DoubleTest::TestValues& v = this->values_; // Nokia's STLport crashes if we try to output infinity or NaN. @@ -3025,17 +3010,13 @@ TEST_F(DoubleTest, NaN) { EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, v.nan1), "v.nan1"); EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(v.nan1, v.infinity), "v.infinity"); -#endif // !GTEST_OS_SYMBIAN } // Tests that *_DOUBLE_EQ are reflexive. TEST_F(DoubleTest, Reflexive) { EXPECT_DOUBLE_EQ(0.0, 0.0); EXPECT_DOUBLE_EQ(1.0, 1.0); -#if !GTEST_OS_SYMBIAN - // Nokia's STLport crashes if we try to output infinity or NaN. ASSERT_DOUBLE_EQ(values_.infinity, values_.infinity); -#endif // !GTEST_OS_SYMBIAN } // Tests that *_DOUBLE_EQ are commutative. @@ -3090,10 +3071,6 @@ TEST_F(DoubleTest, DoubleLEFails) { EXPECT_PRED_FORMAT2(DoubleLE, values_.further_from_one, 1.0); }, "(values_.further_from_one) <= (1.0)"); -#if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) - // Nokia's STLport crashes if we try to output infinity or NaN. - // C++Builder gives bad results for ordered comparisons involving NaNs - // due to compiler bugs. EXPECT_NONFATAL_FAILURE({ // NOLINT EXPECT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.infinity); }, "(values_.nan1) <= (values_.infinity)"); @@ -3103,7 +3080,6 @@ TEST_F(DoubleTest, DoubleLEFails) { EXPECT_FATAL_FAILURE({ // NOLINT ASSERT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.nan1); }, "(values_.nan1) <= (values_.nan1)"); -#endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) } @@ -3711,7 +3687,6 @@ TEST(AssertionTest, ASSERT_EQ) { } // Tests ASSERT_EQ(NULL, pointer). -#if GTEST_CAN_COMPARE_NULL TEST(AssertionTest, ASSERT_EQ_NULL) { // A success. const char* p = nullptr; @@ -3725,7 +3700,6 @@ TEST(AssertionTest, ASSERT_EQ_NULL) { static int n = 0; EXPECT_FATAL_FAILURE(ASSERT_EQ(nullptr, &n), " &n\n Which is:"); } -#endif // GTEST_CAN_COMPARE_NULL // 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 @@ -3916,11 +3890,8 @@ TEST(AssertionTest, NamedEnum) { EXPECT_NONFATAL_FAILURE(EXPECT_EQ(kE1, kE2), "Which is: 1"); } -// The version of gcc used in XCode 2.2 has a bug and doesn't allow -// anonymous enums in assertions. Therefore the following test is not -// done on Mac. -// Sun Studio and HP aCC also reject this code. -#if !GTEST_OS_MAC && !defined(__SUNPRO_CC) && !defined(__HP_aCC) +// Sun Studio and HP aCC2reject this code. +#if !defined(__SUNPRO_CC) && !defined(__HP_aCC) // Tests using assertions with anonymous enums. enum { @@ -4439,7 +4410,6 @@ TEST(ExpectTest, EXPECT_EQ_Double) { "5.1"); } -#if GTEST_CAN_COMPARE_NULL // Tests EXPECT_EQ(NULL, pointer). TEST(ExpectTest, EXPECT_EQ_NULL) { // A success. @@ -4454,7 +4424,6 @@ TEST(ExpectTest, EXPECT_EQ_NULL) { int n = 0; EXPECT_NONFATAL_FAILURE(EXPECT_EQ(nullptr, &n), " &n\n Which is:"); } -#endif // GTEST_CAN_COMPARE_NULL // 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 @@ -5472,6 +5441,67 @@ TEST_F(SetUpTestCaseTest, Test2) { EXPECT_STREQ("123", shared_resource_); } +// Tests SetupTestSuite/TearDown TestSuite API +class SetUpTestSuiteTest : public Test { + protected: + // This will be called once before the first test in this test case + // is run. + static void SetUpTestSuite() { + printf("Setting up the test suite . . .\n"); + + // Initializes some shared resource. In this simple example, we + // just create a C string. More complex stuff can be done if + // desired. + shared_resource_ = "123"; + + // Increments the number of test cases that have been set up. + counter_++; + + // SetUpTestSuite() should be called only once. + EXPECT_EQ(1, counter_); + } + + // This will be called once after the last test in this test case is + // run. + static void TearDownTestSuite() { + printf("Tearing down the test suite . . .\n"); + + // Decrements the number of test suites that have been set up. + counter_--; + + // TearDownTestSuite() should be called only once. + EXPECT_EQ(0, counter_); + + // Cleans up the shared resource. + shared_resource_ = nullptr; + } + + // This will be called before each test in this test case. + void SetUp() override { + // SetUpTestSuite() should be called only once, so counter_ should + // always be 1. + EXPECT_EQ(1, counter_); + } + + // Number of test suites that have been set up. + static int counter_; + + // Some resource to be shared by all tests in this test case. + static const char* shared_resource_; +}; + +int SetUpTestSuiteTest::counter_ = 0; +const char* SetUpTestSuiteTest::shared_resource_ = nullptr; + +// A test that uses the shared resource. +TEST_F(SetUpTestSuiteTest, TestSetupTestSuite1) { + EXPECT_STRNE(nullptr, shared_resource_); +} + +// Another test that uses the shared resource. +TEST_F(SetUpTestSuiteTest, TestSetupTestSuite2) { + EXPECT_STREQ("123", shared_resource_); +} // The ParseFlagsTest test case tests ParseGoogleTestFlagsOnly. @@ -7054,7 +7084,6 @@ GTEST_TEST(AlternativeNameTest, Works) { // GTEST_TEST is the same as TEST. // Tests for internal utilities necessary for implementation of the universal // printing. -// FIXME: Find a better home for them. class ConversionHelperBase {}; class ConversionHelperDerived : public ConversionHelperBase {}; @@ -7547,3 +7576,30 @@ TEST_F(AdHocTestResultTest, AdHocTestResultTestForUnitTestDoesNotShowFailure) { testing::UnitTest::GetInstance()->ad_hoc_test_result(); EXPECT_FALSE(test_result.Failed()); } + +class DynamicUnitTestFixture : public testing::Test {}; + +class DynamicTest : public DynamicUnitTestFixture { + void TestBody() override { EXPECT_TRUE(true); } +}; + +auto* dynamic_test = testing::RegisterTest( + "DynamicUnitTestFixture", "DynamicTest", "TYPE", "VALUE", __FILE__, + __LINE__, []() -> DynamicUnitTestFixture* { return new DynamicTest; }); + +TEST(RegisterTest, WasRegistered) { + auto* unittest = testing::UnitTest::GetInstance(); + for (int i = 0; i < unittest->total_test_case_count(); ++i) { + auto* tests = unittest->GetTestCase(i); + if (tests->name() != std::string("DynamicUnitTestFixture")) continue; + for (int j = 0; j < tests->total_test_count(); ++j) { + if (tests->GetTestInfo(j)->name() != std::string("DynamicTest")) continue; + // Found it. + EXPECT_STREQ(tests->GetTestInfo(j)->value_param(), "VALUE"); + EXPECT_STREQ(tests->GetTestInfo(j)->type_param(), "TYPE"); + return; + } + } + + FAIL() << "Didn't find the test!"; +} |