Skip to content
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

Validating excception contents #641

Closed
mcraveiro opened this issue Apr 18, 2016 · 3 comments
Closed

Validating excception contents #641

mcraveiro opened this issue Apr 18, 2016 · 3 comments

Comments

@mcraveiro
Copy link

mcraveiro commented Apr 18, 2016

Hi,

Thanks very much for Catch, looks like a very interesting framework. I just started porting across my code from Boost.Test[1] and got stuck with what is most likely a trivial issue: checking the contents of an exception message. I checked the issues and found #563 - a similar problem, pointing to CATCH_CHECK_THROWS_WITH. However, my code is relying on user defined predicates to perform the matches[2]. Is it possible to do something similar to this with catch? i.e.:

bool is_critical( my_exception const& ex ) { return ex.m_error_code < 0; }
...
BOOST_CHECK_EXCEPTION( some_func(1), my_exception, is_critical );

The advantage of this approach is that I can also check additional information supplied by Boost.Exception and also make fuzzier checks on the message string (rather than just equality).

Many thanks for your time.

Marco

[1] http://www.boost.org/doc/libs/1_60_0/libs/test/doc/html/index.html
[2]
http://www.boost.org/doc/libs/1_60_0/libs/test/doc/html/boost_test/utf_reference/testing_tool_ref/assertion_boost_level_exception.html

@horenmar
Copy link
Member

horenmar commented Mar 8, 2017

@mcraveiro I think this would be doable, but is currently not quite supported.

After the recent Matchers rework, they are documented and the interface is likely to be stable for a while, but right now REQUIRE_THROWS_WITH only supports matchers over std::string, which are given stringified exception (result of calling either what or overload of an exception-translation function Catch provides). This is because we can't properly keep exception's type, so we have to work with string that represents it.

There have been couple of requests for combination of REQUIRE_THROWS_WITH and REQUIRE_THROWS_AS, which would allow for arbitrary matchers, so I might implement it in the future.

@lightmare
Copy link
Contributor

I was quite surprised that the matchers stuff is only concerned with error messages, completely ignoring exception type. Correct message in wrong exception type is never desirable. IMO it'd be better if each matcher class had a base exception type, which Catch would capture and pass on to the matcher to do their magic (simply compare what() or check the type with dynamic_cast etc).

@horenmar
Copy link
Member

horenmar commented Jun 5, 2017

This is now in Catch 2.0 branch and should find its way into master at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants