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: check or return false #426
Comments
Hi @UnePierre , Sorry for the late reply. Here is how the #define CHECK_OR_RETURN_IMPL(assert_type, cond, ret) \
do { \
DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses") \
doctest::detail::ResultBuilder _DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \
__LINE__, #cond); \
DOCTEST_WRAP_IN_TRY(_DOCTEST_RB.setResult( \
doctest::detail::ExpressionDecomposer(doctest::assertType::assert_type) << cond)) \
DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
if(_DOCTEST_RB.m_failed) return ret; \
DOCTEST_CLANG_SUPPRESS_WARNING_POP \
} while(false)
#define CHECK_OR_RETURN(cond, ret) CHECK_OR_RETURN_IMPL(DT_CHECK, cond, ret) This is basically the implementation of the However, I don't think the library should provide this macro as well - users might want exceptions instead of returns and there is already a way to use standard doctest asserts outside of a testing context (within the actual production codebase) with a custom assert handler: https://github.com/onqtam/doctest/blob/master/doc/markdown/assertions.md#using-asserts-out-of-a-testing-context Let me know if this helps! |
Asserts now return the result of the expression and can be used in if statements ever since #496 was fixed in the I also just made it so that the comparison assertions also work properly when |
Thanks for implementing the "Second option". I, too, consider this issue solved. |
FYI it's not enabled by default - you'll have to define |
…the result even when using DOCTEST_CONFIG_DISABLE - related to doctest#426 and doctest#496
Description
I like the expressiveness of doctest checks and their usage as asserts -- especially their reporting on failure.
I'd like to use them to check untrusted, user-given input or otherwise implement defensive mechanisms:
defined DOCTEST_CONFIG_DISABLE
).Example (before):
This looks like a bunch of code duplication!
And the expressions are evaluated twice.
Note on the side: of course, seeing all failed checks is also worth a try, but sometimes, you check a pointer against
nullptr
before dereferencing it, so the early returns should stay, where they are in this little example.Example (after):
Concise and readable.
Beware! In case
DOCTEST_CONFIG_DISABLE
is defined, this must fall back to:What do you think?
Would this help in cleaning / checking user input and writing defensive code, that is actually talkative in debug mode?
Second option
Have CHECK() return a bool.
But (big BUT) this is a contradiction to "everything testing-related is removed from the binary, when
DOCTEST_CONFIG_DISABLE
is defined".The example would be written as follows and fall back just as in the above code snippet:
The text was updated successfully, but these errors were encountered: