Comments (7)
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.
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.
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.
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.
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.
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.
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)
- When one subcase fails, other independent sibling subcases are not executed anymore HOT 7
- Cannot include `doctest.h` in multiple translation units for Arduino (avr-gcc-7.3.0) HOT 2
- TYPE_TO_STRING does not work inside a TEST_SUITE / namespace HOT 5
- Access violation on Winlibs MinGW-w64 Clang on Windows HOT 7
- Missing pkg-config file doctest.pc HOT 5
- Feature request: Compare floats and have NaN be true HOT 2
- Feature: Add test-suite support to IReporter interface and JUnit-Reporter HOT 4
- cmake: Warning on init HOT 1
- docs: CMake build-system related -- Use FetchContent() HOT 1
- Feature Request: Support structural types in TEST_CASE_TEMPLATE HOT 4
- Minor Import Guard issue HOT 1
- doctest_discover_tests ADD_LABELS not working HOT 4
- doctest + SDL2main : error LNK2019 (despite using DOCTEST_CONFIG_IMPLEMENT) HOT 2
- doctest crashes on ESP32 at startup (with workaround) HOT 2
- Cosine becomes non-deterministic in unit test HOT 9
- Feature suggestion: Add support for testing equality of floats with precision specified in ulps HOT 1
- Triaging guidelines HOT 1
- "doctest::detail::g_cs was nullptr" exception HOT 2
- Fix broken CI
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from doctest.