2

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

rold2007
  • 1,297
  • 1
  • 12
  • 25
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43

1 Answers1

5

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Ok. That worked but only in that specific case. GoogleTest Framework really seems not to work with lambda functions. I tried this ASSERT_ANY_THROW([](){throw;}); , and no exception was registered. Then I tried, ASSERT_ANY_THROW(([](){throw;})); (see extra parentheses) and again no exception was registered. If however, I had a 'normal' function throw an exception, then the above assertion registers it fine. But seems very strange to me that something as widely used and touted like the GoogleTest Framework, does not support lambda functions like that. – Duck Dodgers Jun 09 '17 at 15:20
  • @JoeyMallone The function won't throw unless you call it. – molbdnilo Jun 09 '17 at 16:00
  • I am calling it. I have it in a TEST() and if I switch just that one line from lambda functions to 'normal' functions, the exception gets registered. void dummy() {throw;} ASSERT_ANY_THROW(dummy()); How would I not be calling it? – Duck Dodgers Jun 12 '17 at 07:18
  • Let me maybe ask another question, so I can post the code clearly. – Duck Dodgers Jun 12 '17 at 07:19
  • 3
    @JoeyMallone `[](){throw;}` only defines the function. `[](){throw;}()` would call it. – molbdnilo Jun 12 '17 at 18:51
  • Indeed. I was not aware of that. I had been so used to passing lambda functions in the parameters of functions, that I totally forgot about calling them. Also, I didn't know that this syntax was possible with the parentheses right after the lambda definition. Thanks. Now, I know and it solved my problem. – Duck Dodgers Jun 13 '17 at 13:56