GoogleTest Framework doesn't seem to work with lambda functions (continued)

This is my last question:

Google Test macros don't seem to work with Lambda functions

The solution mentioned in this case worked in this particular case, namely, the constructor of the template class inside the lambda can be wrapped in parentheses and the assembly will succeed. And I accepted this answer. But the question still remains that the GoogleTest Framework doesn't seem to work with Lambda functions. I don't see anything in the documentation.

I did the following test:

TEST(errorhandlingInterpolator, NOTtoolargeInput) {
    ASSERT_NO_THROW(throw);
}

      

which will cause the test to fail. Good.

Then I did this,

TEST(errorhandlingInterpolator, NOTtoolargeInput) {
        ASSERT_NO_THROW([](){throw;});
}

      

which will cause the NOT test to fail. It's strange.

So finally, to be careful (something so trivial), I tested the following bit.

void dummy() { throw; }

TEST(errorhandlingInterpolator, NOTtoolargeInput) {
        ASSERT_NO_THROW(throw);
}

      

and the exception caused the test to fail. Things are good.

Which raised the flag in my head, make exceptions even when working with lambda functions. I thought they were just like regular functions, apart from anonymous ones. Apparently they do it. This is indicated by the following two questions.

Can C ++ lambda-expression throw?

throw an exception from a lambda expression, a bad habit?

So it looks like it boils down to the fact that macros in the Google Analytics framework DO NOT work with lambda functions.

+3


source to share


1 answer


The expression in ASSERT_NO_THROW([](){throw;})

does not throw an exception, it just declares a lambda, which is then discarded because it is not attached to anything.



You want ASSERT_NO_THROW([](){throw;}())

one that immediately tries to execute the lambda.

+3


source







All Articles