Googletest export

Add a compile time check to ensure that the parameters to TEST_P and INSTANTIATE_TEST_SUITE_P are not empty. Some compilers already fail in that case and, even where it works, it's likely to result in technically invalid code by virtue of creating reserved identifiers:
https://en.cppreference.com/w/cpp/language/identifiers

PiperOrigin-RevId: 273832263
This commit is contained in:
Abseil Team 2019-10-09 18:14:17 -04:00 committed by Gennadiy Civil
parent 5395345ca4
commit 20b5b8ecc7
4 changed files with 20 additions and 10 deletions

View File

@ -58,7 +58,9 @@ class FooTest : public ::testing::TestWithParam<const char*> {
// Then, use the TEST_P macro to define as many parameterized tests // Then, use the TEST_P macro to define as many parameterized tests
// for this fixture as you want. The _P suffix is for "parameterized" // for this fixture as you want. The _P suffix is for "parameterized"
// or "pattern", whichever you prefer to think. // or "pattern", whichever you prefer to think. The arguments to the
// TEST_P macro are the test_suite_name and test_case (both which must be
// non-empty) that will form the test name.
TEST_P(FooTest, DoesBlah) { TEST_P(FooTest, DoesBlah) {
// Inside a test, access the test parameter with the GetParam() method // Inside a test, access the test parameter with the GetParam() method
@ -101,10 +103,10 @@ INSTANTIATE_TEST_SUITE_P(InstantiationName,
// To distinguish different instances of the pattern, (yes, you // To distinguish different instances of the pattern, (yes, you
// can instantiate it more than once) the first argument to the // can instantiate it more than once) the first argument to the
// INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the // INSTANTIATE_TEST_SUITE_P macro is a prefix (which must be non-empty) that
// actual test suite name. Remember to pick unique prefixes for different // will be added to the actual test suite name. Remember to pick unique prefixes
// instantiations. The tests from the instantiation above will have // for different instantiations. The tests from the instantiation above will
// these names: // have these names:
// //
// * InstantiationName/FooTest.DoesBlah/0 for "meeny" // * InstantiationName/FooTest.DoesBlah/0 for "meeny"
// * InstantiationName/FooTest.DoesBlah/1 for "miny" // * InstantiationName/FooTest.DoesBlah/1 for "miny"
@ -412,6 +414,10 @@ internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
} }
#define TEST_P(test_suite_name, test_name) \ #define TEST_P(test_suite_name, test_name) \
static_assert(sizeof(GTEST_STRINGIFY_(test_suite_name)) > 1, \
"test_suite_name must not be empty"); \
static_assert(sizeof(GTEST_STRINGIFY_(test_name)) > 1, \
"test_name must not be empty"); \
class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
: public test_suite_name { \ : public test_suite_name { \
public: \ public: \
@ -458,6 +464,10 @@ internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
#define GTEST_GET_SECOND_(first, second, ...) second #define GTEST_GET_SECOND_(first, second, ...) second
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \ #define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \
static_assert(sizeof(GTEST_STRINGIFY_(test_suite_name)) > 1, \
"test_suite_name must not be empty"); \
static_assert(sizeof(GTEST_STRINGIFY_(prefix)) > 1, \
"prefix must not be empty"); \
static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \ static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \
gtest_##prefix##test_suite_name##_EvalGenerator_() { \ gtest_##prefix##test_suite_name##_EvalGenerator_() { \
return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \ return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \

View File

@ -966,9 +966,9 @@ Expected equality of these values:
Stack trace: (omitted) Stack trace: (omitted)
[ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2 [ FAILED ] PrintingFailingParams/FailingParamTest.Fails/0, where GetParam() = 2
[----------] 1 test from EmptyBasenameParamInst [----------] 1 test from All/EmptyBasenameParamInst
[ RUN ] EmptyBasenameParamInst.Passes/0 [ RUN ] All/EmptyBasenameParamInst.Passes/0
[ OK ] EmptyBasenameParamInst.Passes/0 [ OK ] All/EmptyBasenameParamInst.Passes/0
[----------] 2 tests from PrintingStrings/ParamTest [----------] 2 tests from PrintingStrings/ParamTest
[ RUN ] PrintingStrings/ParamTest.Success/a [ RUN ] PrintingStrings/ParamTest.Success/a
[ OK ] PrintingStrings/ParamTest.Success/a [ OK ] PrintingStrings/ParamTest.Success/a

View File

@ -102,7 +102,7 @@ class EmptyBasenameParamInst : public testing::TestWithParam<int> {};
TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); } TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); }
INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1)); INSTANTIATE_TEST_SUITE_P(All, EmptyBasenameParamInst, testing::Values(1));
static const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";

View File

@ -5343,7 +5343,7 @@ TEST_P(CodeLocationForTESTP, Verify) {
VERIFY_CODE_LOCATION; VERIFY_CODE_LOCATION;
} }
INSTANTIATE_TEST_SUITE_P(, CodeLocationForTESTP, Values(0)); INSTANTIATE_TEST_SUITE_P(All, CodeLocationForTESTP, Values(0));
template <typename T> template <typename T>
class CodeLocationForTYPEDTEST : public Test { class CodeLocationForTYPEDTEST : public Test {