catchorg / catchorg/Catch2

Optional callback for `REQUIRE`

Open
#1,431 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

Some libraries, mostly those that expose a C api like the system API, `zlib`, `openssl`, and many others, gives the possibility to access to more detailed error information stored in global objects, or by calling a separate function with an object handle.

Consider for example following test case:
````
TEST_CASE("dummy){
auto f = CreateFile(/* ... */);
REQUIRE_FALSE(f == INVALID_HANDLE_VALUE);
}
````
if the test fails, no useful information are shown, which is a pity since it is the selling point of `REQUIRE` et al compared to other test suites.

In order to have an useful error message, we need to write the test case more similar to (pseudocode)
````
TEST_CASE("dummy){
auto f = CreateFile(/* ... */);
if(f == INVALID_HANDLE_VALUE){
CAPTURE(hComm) ; // replicate output of "hComm != INVALID_HANDLE_VALUE"
FAIL(GetLastError()) // or gather other information
}
}
````

which works, but removes the benefit of using `REQUIRE`, `REQUIRE_FALSE`, and other facilities.

It would be great, if `REQUIRE` and friends could accept an optional callback to call in case an assertion fails, for example
````
TEST_CASE("dummy){
auto f = CreateFile(/* ... */);
REQUIRE(f != INVALID_HANDLE_VALUE, [](const auto& lhs, const auto& rhs){
// lhs, rhs: optionally give in the callback the possibility to inspect the tested value, in case a function needs to access it in order to provide detailed information
FAIL(GetLastError())
}
}
````
If there a more elegant solution to this issue, that makes such a feature superfluous, then I completely missed it, otherwise I think it would be a great addition for `catch` to have something like that.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.