Googletest export

Add the possibility of specifying the name in type parameterized tests.

Similar to how the last parameter of INSTANTIATE_TEST_CASE_P allows to override the name for (non-type) parametrized tests, this adds the possibility of adding a parameter to INSTANTIATE_TYPED_TEST_CASE_P. The argument has to be a class, which contains a static templated function GetName<T>(int), returning the name for type T.

PiperOrigin-RevId: 210532231
This commit is contained in:
Abseil Team
2018-08-28 09:40:18 -04:00
committed by Gennadiy Civil
parent 668c1f4440
commit 6e639d6719
4 changed files with 246 additions and 52 deletions

View File

@@ -801,6 +801,28 @@ TYPED_TEST(TypedTest, Failure) {
EXPECT_EQ(1, TypeParam()) << "Expected failure";
}
typedef testing::Types<char, int> TypesForTestWithNames;
template <typename T>
class TypedTestWithNames : public testing::Test {};
class TypedTestNames {
public:
template <typename T>
static std::string GetName(int i) {
if (testing::internal::IsSame<T, char>::value)
return std::string("char_") + ::testing::PrintToString(i);
if (testing::internal::IsSame<T, int>::value)
return std::string("int_") + ::testing::PrintToString(i);
}
};
TYPED_TEST_CASE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
TYPED_TEST(TypedTestWithNames, Success) {}
TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); }
#endif // GTEST_HAS_TYPED_TEST
// This #ifdef block tests the output of type-parameterized tests.
@@ -825,6 +847,22 @@ REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
class TypedTestPNames {
public:
template <typename T>
static std::string GetName(int i) {
if (testing::internal::IsSame<T, unsigned char>::value) {
return std::string("unsigned_char_") + ::testing::PrintToString(i);
}
if (testing::internal::IsSame<T, unsigned int>::value) {
return std::string("unsigned_int_") + ::testing::PrintToString(i);
}
}
};
INSTANTIATE_TYPED_TEST_CASE_P(UnsignedCustomName, TypedTestP, UnsignedTypes,
TypedTestPNames);
#endif // GTEST_HAS_TYPED_TEST_P
#if GTEST_HAS_DEATH_TEST

View File

@@ -165,6 +165,40 @@ TYPED_TEST(NumericTest, DefaultIsZero) {
} // namespace library1
// Tests that custom names work.
template <typename T>
class TypedTestWithNames : public Test {};
class TypedTestNames {
public:
template <typename T>
static std::string GetName(int i) {
if (testing::internal::IsSame<T, char>::value) {
return std::string("char_") + ::testing::PrintToString(i);
}
if (testing::internal::IsSame<T, int>::value) {
return std::string("int_") + ::testing::PrintToString(i);
}
}
};
TYPED_TEST_CASE(TypedTestWithNames, TwoTypes, TypedTestNames);
TYPED_TEST(TypedTestWithNames, TestCaseName) {
if (testing::internal::IsSame<TypeParam, char>::value) {
EXPECT_STREQ(::testing::UnitTest::GetInstance()
->current_test_info()
->test_case_name(),
"TypedTestWithNames/char_0");
}
if (testing::internal::IsSame<TypeParam, int>::value) {
EXPECT_STREQ(::testing::UnitTest::GetInstance()
->current_test_info()
->test_case_name(),
"TypedTestWithNames/int_1");
}
}
#endif // GTEST_HAS_TYPED_TEST
// This #ifdef block tests type-parameterized tests.
@@ -265,6 +299,46 @@ REGISTER_TYPED_TEST_CASE_P(DerivedTest,
typedef Types<short, long> MyTwoTypes;
INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
// Tests that custom names work with type parametrized tests. We reuse the
// TwoTypes from above here.
template <typename T>
class TypeParametrizedTestWithNames : public Test {};
TYPED_TEST_CASE_P(TypeParametrizedTestWithNames);
TYPED_TEST_P(TypeParametrizedTestWithNames, TestCaseName) {
if (testing::internal::IsSame<TypeParam, char>::value) {
EXPECT_STREQ(::testing::UnitTest::GetInstance()
->current_test_info()
->test_case_name(),
"CustomName/TypeParametrizedTestWithNames/p_char_0");
}
if (testing::internal::IsSame<TypeParam, int>::value) {
EXPECT_STREQ(::testing::UnitTest::GetInstance()
->current_test_info()
->test_case_name(),
"CustomName/TypeParametrizedTestWithNames/p_int_1");
}
}
REGISTER_TYPED_TEST_CASE_P(TypeParametrizedTestWithNames, TestCaseName);
class TypeParametrizedTestNames {
public:
template <typename T>
static std::string GetName(int i) {
if (testing::internal::IsSame<T, char>::value) {
return std::string("p_char_") + ::testing::PrintToString(i);
}
if (testing::internal::IsSame<T, int>::value) {
return std::string("p_int_") + ::testing::PrintToString(i);
}
}
};
INSTANTIATE_TYPED_TEST_CASE_P(CustomName, TypeParametrizedTestWithNames,
TwoTypes, TypeParametrizedTestNames);
// Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
// translation unit.