Skip to content

Replace GTEST_AMBIGUOUS_ELSE_BLOCKER_ #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

ecatmur
Copy link

@ecatmur ecatmur commented Aug 27, 2019

with for loops and (where necessary) lambdas.

Fixes #1119. Requires C++11.

For example, replace:

if (::testing::internal::IsTrue(condition))
  ;
else
  GTEST_LOG_(FATAL) << "Condition " #condition " failed. "

with:

for (bool gtest_check_ = ::testing::internal::IsTrue(condition); \
    !gtest_check_; gtest_check_ = true) \
  GTEST_LOG_(FATAL) << "Condition " #condition " failed. "

This allows the user to continue to stream more information into the violation message, and avoids the "dangling else" warning-error since we aren't using any if/else statements.

If the predicate or any branches are more complicated than a single statement-expression, use a lambda; this requires C++11. This is necessary for death, throw, no-throw, and no-fatal-failure checks. As a result:

  • Behavior of death checks remains the same: return (including GTEST_FAIL and GTEST_SKIP) from within the test statement is a runtime error.
  • Behavior of throw and no-throw checks is altered: return from within the test statement is typically a compile error, but might not be if the user accidentally returns a compatible type. It would be possible to prevent this, or make it a runtime error. GTEST_FAIL and GTEST_SKIP are always a compile error, but could be changed to a runtime error.
  • Behavior of no-fatal-failure checks is altered such that fatal failures immediately within the test statement (e.g. EXPECT_NO_FATAL_FAILURE(FAIL())) no longer abort the containing test. Test NoFatalFailureTest.MessageIsStreamable is modified accordingly; previously, it was broken as none of the checks within the test function (after EXPECT_NO_FATAL_FAILURE(FAIL())) ran. Other return statements (including GTEST_SKIP) are ignored; this could be changed to a runtime error.

Finally: remove the -Werror=dangling-else suppression, since it isn't required any more.

with for loops and (where necessary) lambdas.

Fixes google#1119. Requires C++11.

For example, replace:

    if (::testing::internal::IsTrue(condition))
      ;
    else
      GTEST_LOG_(FATAL) << "Condition " #condition " failed. "

with:

    for (bool gtest_check_ = ::testing::internal::IsTrue(condition); \
        !gtest_check_; gtest_check_ = true) \
      GTEST_LOG_(FATAL) << "Condition " #condition " failed. "

This allows the user to continue to stream more information into the
violation message, and avoids the "dangling else" warning-error since we
aren't using any if/else statements.

If the predicate or any branches are more complicated than a single
statement-expression, use a lambda; this requires C++11. This is
necessary for death, throw, no-throw, and no-fatal-failure tests.

Remove the -Werror=dangling-else suppression, since it isn't required
any more.
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@gennadiycivil
Copy link
Contributor

No CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GTEST_AMBIGUOUS_ELSE_BLOCKER_ does not block the warning for gcc-7.1.
3 participants