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

Feature request: check for exception string (like Catch's CHECK_THROWS_WITH) #97

Closed
nlohmann opened this issue Oct 27, 2017 · 5 comments

Comments

@nlohmann
Copy link

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 call CHECK on the exception's what() function.

nlohmann referenced this issue in nlohmann/json Oct 27, 2017
@onqtam
Copy link
Member

onqtam commented Oct 29, 2017

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

@nlohmann
Copy link
Author

Any news on this?

@onqtam
Copy link
Member

onqtam commented Nov 11, 2018

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.

@onqtam onqtam closed this as completed in 0e6236a Nov 30, 2018
@onqtam
Copy link
Member

onqtam commented Nov 30, 2018

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 <LEVEL>_THROWS_WITH macros - like this:

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 std::exception and calls the .what() method for them. Also C strings are supported as exceptions. The framework can be extended with user-defined exceptions as well - consult the docs:

https://github.com/onqtam/doctest/blob/master/doc/markdown/stringification.md#translating-exceptions

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.

@onqtam
Copy link
Member

onqtam commented Dec 5, 2018

@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

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

No branches or pull requests

2 participants