-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the proposed way to test a try block? #671
Comments
|
I use
|
Why would you want to hide which of those calls threw? I mean, wouldn't it be better to REQUIRE_NOTHROW each of them, so that when something fails, you know where the trouble is, instead of just "somewhere in this block"? |
@lightmare What if you want to test declarations? They aren't expressions. IMO you can get enough context from the name of the exception. If you can't, then your test is too big :D |
@milleniumbug How about SECTION? |
Hi all thanks for the answers, my main motivation for this are throwing constructors of classes without default constructors and then using these to make other objects/calls etc. |
@milleniumbug good idea if using C++11 |
@milleniumbug You need to wrap the anonymous function in parentheses, otherwise it will suffer the same problem with commas as a plain block: REQUIRE_NOTHROW(
{
// if there is a comma outside parentheses, it won't even pass preprocessing
std::pair<int, int> nono;
});
REQUIRE_NOTHROW(([&]
{
std::pair<int, int> ok;
})()); @dpservis You can do that with sections: SECTION("foo and bar with some input")
{
ThrowingFoo foo("input");
ThrowingBar bar(foo, "more input");
REQUIRE(bar.isValid());
REQUIRE_NOTHROW(bar.doStuff());
// etc.
} If a constructor throws, it terminates the section and you get exactly as much information as you'd get from REQUIRE_NOTHROW with a block, i.e. only line number of the start of the section/block and exception.what(); |
@milleniumbug great! |
Although the lambda suggestion works (assuming C++11 on) I don't think it adds much value in this case. So I would recommend either use Either way I'm going to close this issue now as I think it's been resolved in one of several ways. |
REQUIRE_NOTHROW is a great way to work with exceptions but what if I want to test a larger block where there may be several calls that throw, e.g.
try
{
// Make a lot of things here
}
catch(...)
{
FAIL("an exception was thrown");
}
The text was updated successfully, but these errors were encountered: