-
-
Notifications
You must be signed in to change notification settings - Fork 649
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
Feature request: check for exception string (like Catch's CHECK_THROWS_WITH) #97
Comments
Matchers are on the roadmap but I won't have the time to implement them properly in the next 2-3 months - just signed a 3 month contract to get some cash... Until then you could use this solution: #include "doctest.h"
#include <stdexcept>
#define DOCTEST_EXT_THROWS_WITH_IMPL(the_assert, expr, message) \
do { \
doctest::String _doctest_exception_string = "Didn't throw at all"; \
try { \
expr; \
} catch(std::exception & e) { _doctest_exception_string = e.what(); } catch(...) { \
_doctest_exception_string = "Unknown exception"; \
} \
DOCTEST_EXPAND_VA_ARGS(the_assert)(_doctest_exception_string == doctest::String(message)); \
} while((void)0, 0)
#define CHECK_THROWS_WITH(expr, message) DOCTEST_EXT_THROWS_WITH_IMPL(DOCTEST_CHECK, expr, message)
#define REQUIRE_THROWS_WITH(expr, message) DOCTEST_EXT_THROWS_WITH_IMPL(DOCTEST_REQUIRE, expr, message)
TEST_CASE("using a custom exception assert macro") {
CHECK_THROWS_WITH(throw std::runtime_error("omg"), "omg 2!!!");
REQUIRE_THROWS_WITH(throw std::runtime_error("wtf"), "wtf");
CHECK_THROWS_WITH(throw 5, "wtf");
CHECK_THROWS_WITH(((void)0), "wtf");
} the output message will contain the exception result and what was expected - its not perfect, but should do the trick |
Any news on this? |
This is a high priority feature. I'll try to get version 2.1 out by the end of the year but cannot promise it - will notify here when done. Thanks for waiting. |
This is now in the master branch - version 2.1.0 is released! Thanks for the patience! So currently you can pass only C strings to the CHECK_THROWS_WITH(do_parsing(), "bad parse!"); In the future support for matchers will be added (the ability to use objects instead of just plain old C strings) but the interface of the macros will not change. Currently the framework translates exceptions deriving from I also did a small change in version 2.1 to allow 2 ways of passing exception types to _THROWS_AS - with and without a reference (but the result is always catch by reference) so it is easier to migrate: 8cf9041 Let me know if you need anything else. |
@nlohmann aaand a bit unrelated to this issue, but I just released version 2.2 and now the DOCTEST_CONFIG_SUPER_FAST_ASSERTS config option also affects the normal asserts and not just the binary - see the results in the benchmarks |
Description
I would like to have a macro equivalent to Catch's
CHECK_THROWS_WITH
, see https://github.com/philsquared/Catch/blob/master/docs/assertions.md#exceptions.Currently, I would need to provide a
try
/catch
block on my own and callCHECK
on the exception'swhat()
function.The text was updated successfully, but these errors were encountered: