gtest-typed-test_test.cc (googletest-release-1.11.0) | : | gtest-typed-test_test.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 52 | skipping to change at line 52 | |||
using testing::Test; | using testing::Test; | |||
// Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture | // Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture | |||
// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and | // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and | |||
// type-parameterized test. | // type-parameterized test. | |||
template <typename T> | template <typename T> | |||
class CommonTest : public Test { | class CommonTest : public Test { | |||
// For some technical reason, SetUpTestSuite() and TearDownTestSuite() | // For some technical reason, SetUpTestSuite() and TearDownTestSuite() | |||
// must be public. | // must be public. | |||
public: | public: | |||
static void SetUpTestSuite() { | static void SetUpTestSuite() { shared_ = new T(5); } | |||
shared_ = new T(5); | ||||
} | ||||
static void TearDownTestSuite() { | static void TearDownTestSuite() { | |||
delete shared_; | delete shared_; | |||
shared_ = nullptr; | shared_ = nullptr; | |||
} | } | |||
// This 'protected:' is optional. There's no harm in making all | // This 'protected:' is optional. There's no harm in making all | |||
// members of this fixture class template public. | // members of this fixture class template public. | |||
protected: | protected: | |||
// We used to use std::list here, but switched to std::vector since | // We used to use std::list here, but switched to std::vector since | |||
skipping to change at line 132 | skipping to change at line 130 | |||
EXPECT_EQ(5, *this->shared_); | EXPECT_EQ(5, *this->shared_); | |||
// TypeParam can be used to refer to the type parameter. | // TypeParam can be used to refer to the type parameter. | |||
EXPECT_EQ(static_cast<TypeParam>(2), this->value_); | EXPECT_EQ(static_cast<TypeParam>(2), this->value_); | |||
} | } | |||
// Tests that multiple TYPED_TEST_SUITE's can be defined in the same | // Tests that multiple TYPED_TEST_SUITE's can be defined in the same | |||
// translation unit. | // translation unit. | |||
template <typename T> | template <typename T> | |||
class TypedTest1 : public Test { | class TypedTest1 : public Test {}; | |||
}; | ||||
// Verifies that the second argument of TYPED_TEST_SUITE can be a | // Verifies that the second argument of TYPED_TEST_SUITE can be a | |||
// single type. | // single type. | |||
TYPED_TEST_SUITE(TypedTest1, int); | TYPED_TEST_SUITE(TypedTest1, int); | |||
TYPED_TEST(TypedTest1, A) {} | TYPED_TEST(TypedTest1, A) {} | |||
template <typename T> | template <typename T> | |||
class TypedTest2 : public Test { | class TypedTest2 : public Test {}; | |||
}; | ||||
// Verifies that the second argument of TYPED_TEST_SUITE can be a | // Verifies that the second argument of TYPED_TEST_SUITE can be a | |||
// Types<...> type list. | // Types<...> type list. | |||
TYPED_TEST_SUITE(TypedTest2, Types<int>); | TYPED_TEST_SUITE(TypedTest2, Types<int>); | |||
// This also verifies that tests from different typed test cases can | // This also verifies that tests from different typed test cases can | |||
// share the same name. | // share the same name. | |||
TYPED_TEST(TypedTest2, A) {} | TYPED_TEST(TypedTest2, A) {} | |||
// Tests that a typed test case can be defined in a namespace. | // Tests that a typed test case can be defined in a namespace. | |||
namespace library1 { | namespace library1 { | |||
template <typename T> | template <typename T> | |||
class NumericTest : public Test { | class NumericTest : public Test {}; | |||
}; | ||||
typedef Types<int, long> NumericTypes; | typedef Types<int, long> NumericTypes; | |||
TYPED_TEST_SUITE(NumericTest, NumericTypes); | TYPED_TEST_SUITE(NumericTest, NumericTypes); | |||
TYPED_TEST(NumericTest, DefaultIsZero) { | TYPED_TEST(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); } | |||
EXPECT_EQ(0, TypeParam()); | ||||
} | ||||
} // namespace library1 | } // namespace library1 | |||
// Tests that custom names work. | // Tests that custom names work. | |||
template <typename T> | template <typename T> | |||
class TypedTestWithNames : public Test {}; | class TypedTestWithNames : public Test {}; | |||
class TypedTestNames { | class TypedTestNames { | |||
public: | public: | |||
template <typename T> | template <typename T> | |||
skipping to change at line 267 | skipping to change at line 260 | |||
EXPECT_DEATH_IF_SUPPORTED( | EXPECT_DEATH_IF_SUPPORTED( | |||
state_.AddTestName("foo.cc", 2, "FooTest", "D"), | state_.AddTestName("foo.cc", 2, "FooTest", "D"), | |||
"foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P" | "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P" | |||
"\\(FooTest, \\.\\.\\.\\)\\."); | "\\(FooTest, \\.\\.\\.\\)\\."); | |||
} | } | |||
// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor, | // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor, | |||
// and SetUp()/TearDown() work correctly in type-parameterized tests. | // and SetUp()/TearDown() work correctly in type-parameterized tests. | |||
template <typename T> | template <typename T> | |||
class DerivedTest : public CommonTest<T> { | class DerivedTest : public CommonTest<T> {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(DerivedTest); | TYPED_TEST_SUITE_P(DerivedTest); | |||
TYPED_TEST_P(DerivedTest, ValuesAreCorrect) { | TYPED_TEST_P(DerivedTest, ValuesAreCorrect) { | |||
// Static members of the fixture class template can be visited via | // Static members of the fixture class template can be visited via | |||
// the TestFixture:: prefix. | // the TestFixture:: prefix. | |||
EXPECT_EQ(5, *TestFixture::shared_); | EXPECT_EQ(5, *TestFixture::shared_); | |||
// Non-static members of the fixture class must be visited via | // Non-static members of the fixture class must be visited via | |||
// 'this', as required by C++ for class templates. | // 'this', as required by C++ for class templates. | |||
skipping to change at line 292 | skipping to change at line 284 | |||
// The second test makes sure shared_ is not deleted after the first | // The second test makes sure shared_ is not deleted after the first | |||
// test. | // test. | |||
TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) { | TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) { | |||
// Static members of the fixture class template can also be visited | // Static members of the fixture class template can also be visited | |||
// via 'this'. | // via 'this'. | |||
ASSERT_TRUE(this->shared_ != nullptr); | ASSERT_TRUE(this->shared_ != nullptr); | |||
EXPECT_EQ(5, *this->shared_); | EXPECT_EQ(5, *this->shared_); | |||
EXPECT_EQ(2, this->value_); | EXPECT_EQ(2, this->value_); | |||
} | } | |||
REGISTER_TYPED_TEST_SUITE_P(DerivedTest, | REGISTER_TYPED_TEST_SUITE_P(DerivedTest, ValuesAreCorrect, | |||
ValuesAreCorrect, ValuesAreStillCorrect); | ValuesAreStillCorrect); | |||
typedef Types<short, long> MyTwoTypes; | typedef Types<short, long> MyTwoTypes; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes); | INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes); | |||
// Tests that custom names work with type parametrized tests. We reuse the | // Tests that custom names work with type parametrized tests. We reuse the | |||
// TwoTypes from above here. | // TwoTypes from above here. | |||
template <typename T> | template <typename T> | |||
class TypeParametrizedTestWithNames : public Test {}; | class TypeParametrizedTestWithNames : public Test {}; | |||
TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames); | TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames); | |||
skipping to change at line 336 | skipping to change at line 328 | |||
if (std::is_same<T, char>::value) { | if (std::is_same<T, char>::value) { | |||
return std::string("parChar") + ::testing::PrintToString(i); | return std::string("parChar") + ::testing::PrintToString(i); | |||
} | } | |||
if (std::is_same<T, int>::value) { | if (std::is_same<T, int>::value) { | |||
return std::string("parInt") + ::testing::PrintToString(i); | return std::string("parInt") + ::testing::PrintToString(i); | |||
} | } | |||
} | } | |||
}; | }; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames, | INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames, | |||
TwoTypes, TypeParametrizedTestNames); | TwoTypes, TypeParametrizedTestNames); | |||
// Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same | // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same | |||
// translation unit. | // translation unit. | |||
template <typename T> | template <typename T> | |||
class TypedTestP1 : public Test { | class TypedTestP1 : public Test {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(TypedTestP1); | TYPED_TEST_SUITE_P(TypedTestP1); | |||
// For testing that the code between TYPED_TEST_SUITE_P() and | // For testing that the code between TYPED_TEST_SUITE_P() and | |||
// TYPED_TEST_P() is not enclosed in a namespace. | // TYPED_TEST_P() is not enclosed in a namespace. | |||
using IntAfterTypedTestSuiteP = int; | using IntAfterTypedTestSuiteP = int; | |||
TYPED_TEST_P(TypedTestP1, A) {} | TYPED_TEST_P(TypedTestP1, A) {} | |||
TYPED_TEST_P(TypedTestP1, B) {} | TYPED_TEST_P(TypedTestP1, B) {} | |||
// For testing that the code between TYPED_TEST_P() and | // For testing that the code between TYPED_TEST_P() and | |||
// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace. | // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace. | |||
using IntBeforeRegisterTypedTestSuiteP = int; | using IntBeforeRegisterTypedTestSuiteP = int; | |||
REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B); | REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B); | |||
template <typename T> | template <typename T> | |||
class TypedTestP2 : public Test { | class TypedTestP2 : public Test {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(TypedTestP2); | TYPED_TEST_SUITE_P(TypedTestP2); | |||
// This also verifies that tests from different type-parameterized | // This also verifies that tests from different type-parameterized | |||
// test cases can share the same name. | // test cases can share the same name. | |||
TYPED_TEST_P(TypedTestP2, A) {} | TYPED_TEST_P(TypedTestP2, A) {} | |||
REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A); | REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A); | |||
// Verifies that the code between TYPED_TEST_SUITE_P() and | // Verifies that the code between TYPED_TEST_SUITE_P() and | |||
skipping to change at line 398 | skipping to change at line 388 | |||
// (ContainerTest is also instantiated in gtest-typed-test_test.cc.) | // (ContainerTest is also instantiated in gtest-typed-test_test.cc.) | |||
typedef Types<std::vector<double>, std::set<char> > MyContainers; | typedef Types<std::vector<double>, std::set<char> > MyContainers; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers); | INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers); | |||
// Tests that a type-parameterized test case can be defined and | // Tests that a type-parameterized test case can be defined and | |||
// instantiated in a namespace. | // instantiated in a namespace. | |||
namespace library2 { | namespace library2 { | |||
template <typename T> | template <typename T> | |||
class NumericTest : public Test { | class NumericTest : public Test {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(NumericTest); | TYPED_TEST_SUITE_P(NumericTest); | |||
TYPED_TEST_P(NumericTest, DefaultIsZero) { | TYPED_TEST_P(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); } | |||
EXPECT_EQ(0, TypeParam()); | ||||
} | ||||
TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) { | TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) { | |||
EXPECT_LT(TypeParam(0), TypeParam(1)); | EXPECT_LT(TypeParam(0), TypeParam(1)); | |||
} | } | |||
REGISTER_TYPED_TEST_SUITE_P(NumericTest, | REGISTER_TYPED_TEST_SUITE_P(NumericTest, DefaultIsZero, ZeroIsLessThanOne); | |||
DefaultIsZero, ZeroIsLessThanOne); | ||||
typedef Types<int, double> NumericTypes; | typedef Types<int, double> NumericTypes; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes); | INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes); | |||
static const char* GetTestName() { | static const char* GetTestName() { | |||
return testing::UnitTest::GetInstance()->current_test_info()->name(); | return testing::UnitTest::GetInstance()->current_test_info()->name(); | |||
} | } | |||
// Test the stripping of space from test names | // Test the stripping of space from test names | |||
template <typename T> class TrimmedTest : public Test { }; | template <typename T> | |||
class TrimmedTest : public Test {}; | ||||
TYPED_TEST_SUITE_P(TrimmedTest); | TYPED_TEST_SUITE_P(TrimmedTest); | |||
TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); } | TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); } | |||
TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); } | TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); } | |||
TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); } | TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); } | |||
TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); } | TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); } | |||
TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); } | TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); } | |||
REGISTER_TYPED_TEST_SUITE_P( | REGISTER_TYPED_TEST_SUITE_P(TrimmedTest, Test1, Test2, Test3, Test4, | |||
TrimmedTest, | Test5); // NOLINT | |||
Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT | template <typename T1, typename T2> | |||
template <typename T1, typename T2> struct MyPair {}; | struct MyPair {}; | |||
// Be sure to try a type with a comma in its name just in case it matters. | // Be sure to try a type with a comma in its name just in case it matters. | |||
typedef Types<int, double, MyPair<int, int> > TrimTypes; | typedef Types<int, double, MyPair<int, int> > TrimTypes; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes); | INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes); | |||
} // namespace library2 | } // namespace library2 | |||
End of changes. 15 change blocks. | ||||
33 lines changed or deleted | 20 lines changed or added |