snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
258 stars 7 forks source link

Capture and section state not reported correctly with manual exception handling followed by unhandled exception #179

Open cschreib opened 2 weeks ago

cschreib commented 2 weeks ago

This is a bit of a niche edge case, but following changes in https://github.com/snitch-org/snitch/pull/178, the following doesn't behave as expected:

For captures:

try {
    int i = 1;
    CAPTURE(i);
    throw std::runtime_error("bad 1");
} catch (...) {
}

throw std::runtime_error("bad 2");

For sections:

try {
    SNITCH_SECTION("section 1") {
        throw std::runtime_error("bad 1");
    }
} catch (...) {
}

throw std::runtime_error("bad 2");

In both cases, the unhandled exception "bad 2" will be reported with the capture/section information of "bad 1".

This will only occur if no snitch macro is encountered between the two throw. So, for example, the following works again as expected:

try {
    int i = 1;
    CAPTURE(i);
    throw std::runtime_error("bad 1");
} catch (...) {
}

// If there is a capture...
int j = 2;
CAPTURE(j);
// ... or a check
CHECK(1 == 2);
// ... or a section
SECTION("subsection 1") { /* ... */ }
// ... then the reported information will be accurate again for unhandled exceptions.

throw std::runtime_error("bad 2");