Google Test macros seem not to work with Lambda fu

2019-07-23 19:14发布

问题:

So, I have the following bit of code. Regardless of what the details of the Interpolator class are, it should in this case NOT throw an exception and that is what I wanted to test.

TEST(errorhandlingInterpolator, toolargeInput) {

    const size_t numSamples = 100000;

    std::array<double, numSamples> bf{{0.0, 0.5, 1.0, 0.0, 0.5, 0.0}};
    std::array<double, numSamples> ts{{0.0, 0.0, 0.0, 0.5, 0.5, 1.0}};
    std::array<double, numSamples> current{ {0.13, 0.83, 0.96, 0.22, 0.30, 0.54} };

    ASSERT_NO_THROW( [&](){
        Interpolator<double, double, double, numSamples> intp(bf, ts, current);
    });

}

Unfortunately, I get the following error (with or without the lambda function). I wrapped the constructor call in the lambda after getting the same error earlier.

.../test/main.cpp:34: error: macro "ASSERT_NO_THROW" passed 4 arguments, but takes just 1
 });
  ^

It is not a deal-breaking issue. I could wrap my code in a 'normal' function which could then itself return AssertionSuccess() or AssertionFailure(), that could then be checked in the assertion, but it seems not very nice.

I can tell from my experience with the CATCH testing framework that testing for an exception with REQUIRE_NOTHROW() for example, from a constructor is possible straight away in the macro. Even the lambda would've been unnecessary.

I'd be surprised if I am not making a mistake in using the google test framework.

I went over the following two docs looking for solution for my problem, but there seems no referral to it.

https://github.com/google/googletest/blob/master/googletest/docs/advanced.md

https://github.com/google/googletest/blob/master/googletest/docs/primer.md

回答1:

This happens when a macro argument has commas in it - the preprocessor gets "first dibs" on the commas and interprets them as parameter separators.

The solution is to add a pair of parentheses around the argument.