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

So, I have the following bit of code. Whatever the information about the Interpolator class is, it should NOT throw an exception in that case, which 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 am getting the following error (with or without lambda function). I wrapped the constructor call in a lambda after getting the same error earlier.

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

      

This is not a business issue. I could wrap my code in a "normal" function that could then return AssertionSuccess () or AssertionFailure () on its own, which could then be verified in the assertion, but that doesn't seem very nice.

From my experience with the CATCH testing framework, I can say that testing an exception using REQUIRE_NOTHROW (), for example, from the constructor, is possible right in the macro. Even a lambda wouldn't be superfluous.

I would be surprised if it weren't wrong when using google test framework.

I've looked at the following two documents looking for a solution to my problem, but there doesn't seem to be a link to it.

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

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

0


source to share


1 answer


This happens when there are commas in the macro argument - the preprocessor gets the "first dibs" in commas and interprets them as parameter separators.



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

+2


source







All Articles