Coder Social home page Coder Social logo

Comments (7)

cdeln avatar cdeln commented on September 27, 2024 1

Yes I think it is a good feature, and this issue clearly demonstrate the need for it.

It might take some time before that happens so I always try to document workarounds for other people to use before it's in the library.

from doctest.

cdeln avatar cdeln commented on September 27, 2024

Can you write an example? It sounds like you have a custom exception class with some attached data that you want to verify.

from doctest.

netheril96 avatar netheril96 commented on September 27, 2024

Yes, I have a custom exception with error_code method.

I'm thinking of either

const auto& e = CHECK_THROWS_AS(func(), const MyException&);
CHECK_EQ(e.error_code(), 3);

or

CHECK_THROWS_WITH_PREDICATE(func(), [](const auto& e) {
  const auto* p = dynamic_cast<const MyException*>(&e);
  CHECK(p != nullptr);
  CHECK_EQ(p->error_code(), 3);
});

from doctest.

cdeln avatar cdeln commented on September 27, 2024

I like the idea of being able to further examine the exception.
If this is to be implemented I think the second version with CHECK_THROWS_WITH_PREDICATE is more viable since it's a macro and not a function call. Small improvement on how it would work below

CHECK_THROWS_WITH_PREDICATE(func(), MyException, [](auto& e) {
  CHECK_EQ(e.error_code(), 3);
});

Out of curiosity, is it possible for you to encode the error code using types instead? Or can the code be any number generated at runtime? If you have a finite number of error codes you can solve it with existing methods.

from doctest.

netheril96 avatar netheril96 commented on September 27, 2024

If this is to be implemented I think the second version with CHECK_THROWS_WITH_PREDICATE is more viable since it's a macro and not a function call.

I think this can be worked around if CHECK_THROWS_AS expands as a lamdba call, e.g.

CHECK_THROWS_AS(func(), ExceptionType) => [&](){ MACRO_CONTENTS }();

Out of curiosity, is it possible for you to encode the error code using types instead? Or can the code be any number generated at runtime? If you have a finite number of error codes you can solve it with existing methods.

I have three major exceptions, PosixException with all possile errno codes, WindowsException with all possible GetLastError() codes, and NtException with all possible NTSTATUS values. It should be plain to you why your suggestion is infeasible in my case.

from doctest.

cdeln avatar cdeln commented on September 27, 2024

I see, that makes this feature more important.
Here is a workaround draft

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

struct PosixException : std::exception {
    int code;
    PosixException(int code) : code{code} {}
};

struct WindowsException : std::exception {
    int code;
    WindowsException(int code) : code{code} {}
};

#define REQUIRE_THROWS_WITH_PREDICATE(Expression, ExceptionType, Predicate) \
    try {                                                               \
        Expression ;                                                    \
    }                                                                   \
    catch (const ExceptionType& doctest_e) {                            \
        Predicate(doctest_e);                                           \
    }                                                                   \
    catch (...) {                                                       \
        FAIL("Unexpected exception, expected " #ExceptionType " but something else happened"); \
    }

void throwPosixException() {
    throw PosixException(1337);
}

TEST_CASE("846") {
    REQUIRE_THROWS_WITH_PREDICATE(throwPosixException(), PosixException, [](auto& e) {
        REQUIRE(e.code == 1337);
    });
    REQUIRE_THROWS_WITH_PREDICATE(throwPosixException(), WindowsException, [](auto &e) {
        REQUIRE(e.code == 1337);
    });
}

gives me

[doctest] doctest version is "2.4.11"
[doctest] run with "--help" for options
===============================================================================
main.cpp:29:
TEST CASE:  846

main.cpp:33: FATAL ERROR: Unexpected exception, expected WindowsException but something else happened

===============================================================================
[doctest] test cases: 1 | 0 passed | 1 failed | 0 skipped
[doctest] assertions: 2 | 1 passed | 1 failed |
[doctest] Status: FAILURE!

from doctest.

netheril96 avatar netheril96 commented on September 27, 2024

That's very close to what I'm doing now, but it is better if it is in the library, isn't it?

from doctest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.