googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[FR]: Add more GTEST_DONT_DEFINE_XXX macros

Open steve-lorimer opened this issue 2 years ago • 0 comments

Does the feature exist in the most recent commit?

No

Why do we need this feature?

Our codebase defines ASSERT_THROW, as does gtest, which obviously clashes

Describe the proposal.

  1. Add macros to omit defining ASSERT|EXPECT_XXX
  2. Add macros to define GTEST_ASSERT|GTEST_EXPECT_XXX which is far more unlikely to clash

gtest already has several define checks to selectively skip defining helper macros

eg: GTEST_DONT_DEFINE_EXPECT_TRUE, as shown below:

// Define these macros to 1 to omit the definition of the corresponding
// EXPECT or ASSERT, which clashes with some users' own code.

#if !(defined(GTEST_DONT_DEFINE_EXPECT_TRUE) && GTEST_DONT_DEFINE_EXPECT_TRUE)
#define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition)
#endif

There are, however, some other macros which do not have this switch available, eg:

// Macros for testing exceptions.
//
//    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
//         Tests that the statement throws the expected exception.
//    * {ASSERT|EXPECT}_NO_THROW(statement):
//         Tests that the statement doesn't throw any exception.
//    * {ASSERT|EXPECT}_ANY_THROW(statement):
//         Tests that the statement throws an exception.

#define EXPECT_THROW(statement, expected_exception) \
  GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
#define EXPECT_NO_THROW(statement) \
  GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
#define EXPECT_ANY_THROW(statement) \
  GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
#define ASSERT_THROW(statement, expected_exception) \
  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
#define ASSERT_NO_THROW(statement) \
  GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
#define ASSERT_ANY_THROW(statement) \
  GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)

For codebases which already have a definition listed above defined elsewhere, this is problematic.

It would be helpful to omit defining the above macros in selective situations

It would also be helpful to define the above macros with a GTEST_ prefix which will be far less likely to clash

Is the feature specific to an operating system, compiler, or build system version?

No

steve-lorimer avatar Mar 22 '23 09:03 steve-lorimer