简体   繁体   English

Google Test宏似乎不适用于Lambda函数

[英]Google Test macros seem not to work with Lambda functions

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. 无论Interpolator类的细节是什么,在这种情况下都应该抛出异常,这就是我想要测试的。

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). 不幸的是,我得到以下错误(有或没有lambda函数)。 I wrapped the constructor call in the lambda after getting the same error earlier. 之前得到相同的错误后,我在lambda中包装了构造函数调用。

.../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. 我可以将我的代码包装在一个'普通'函数中,然后该函数本身可以返回AssertionSuccess()或AssertionFailure(),然后可以在断言中检查它,但它看起来不是很好。

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. 我可以从我对CATCH测试框架的经验中看出,使用REQUIRE_NOTHROW()测试异常,例如,从构造函数中可以直接在宏中进行测试。 Even the lambda would've been unnecessary. 即使是lambda也没必要。

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/advanced.md

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

This happens when a macro argument has commas in it - the preprocessor gets "first dibs" on the commas and interprets them as parameter separators. 当宏参数中包含逗号时会发生这种情况 - 预处理器在逗号上获取“first dibs”并将它们解释为参数分隔符。

The solution is to add a pair of parentheses around the argument. 解决方案是在参数周围添加一对括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM