Regularize scoped message lifetime to only consider the object's scope

This means that:
1) Scoped messages are always removed at the end of their scope,
   even if the scope ended due to an exception.
2) Scoped messages outlive section end, if that section's scope is
   enclosed in their own.

Previously neither of these were true, which has led to a number
of surprising behaviour, where e.g. this:
```cpp
TEST_CASE() {
    try {
        INFO( "some info" );
        throw std::runtime_error( "ex" );
    } catch (std::exception const&) {}

    REQUIRE( false );
}
```
would print "some info" as the message for the assertion, while this:
```cpp
TEST_CASE() {
    INFO("Hello");
    SECTION("dummy") {}
    REQUIRE(false);
}
```
would not print out "Hello" as the message for the assertion.

This had an underlying reason, in that it was trying to helpfully
keep the messages around in case of unexpected exceptions, so that
code like this:
```cpp
TEST_CASE() {
    auto [input, expected] = GENERATE(...);
    CAPTURE(input);
    auto result = transform(input); // throws
    REQUIRE(result == expected);
}
```
would report the value of `input` when `transform` throws. However,
it was surprising in practice and was causing various issues around
handling of messages in other cases.

Closes #1759
Closes #2019
Closes #2959
This commit is contained in:
Martin Hořeňovský
2025-07-21 17:47:59 +02:00
parent 98b4bbb35e
commit 10aef62f21
22 changed files with 831 additions and 73 deletions

View File

@@ -1012,7 +1012,6 @@ at Decomposition.tests.cpp:<line number>
FAILED:
{Unknown expression after the reported line}
expected exception
answer := 42
at Exception.tests.cpp:<line number>
</skipped>
</testCase>
@@ -1637,6 +1636,23 @@ at Matchers.tests.cpp:<line number>
<testCase name="CAPTURE can deal with complex expressions" duration="{duration}"/>
<testCase name="CAPTURE can deal with complex expressions involving commas" duration="{duration}"/>
<testCase name="CAPTURE parses string and character constants" duration="{duration}"/>
<testCase name="Captures do not leave block with an exception" duration="{duration}">
<failure message="REQUIRE(false)">
FAILED:
REQUIRE( false )
a := 1
at Message.tests.cpp:<line number>
</failure>
</testCase>
<testCase name="Captures outlive section end" duration="{duration}">
<failure message="REQUIRE(false)">
FAILED:
REQUIRE( false )
a := 1
at Message.tests.cpp:<line number>
</failure>
</testCase>
<testCase name="Captures outlive section end/Dummy section" duration="{duration}"/>
<testCase name="FAIL aborts the test" duration="{duration}">
<failure message="FAIL()">
FAILED:
@@ -1726,6 +1742,37 @@ at Message.tests.cpp:<line number>
</testCase>
<testCase name="SUCCEED counts as a test pass" duration="{duration}"/>
<testCase name="SUCCEED does not require an argument" duration="{duration}"/>
<testCase name="Scoped message applies to all assertions in scope" duration="{duration}">
<failure message="CHECK(false)">
FAILED:
CHECK( false )
This will be reported multiple times
at Message.tests.cpp:<line number>
</failure>
<failure message="CHECK(false)">
FAILED:
CHECK( false )
This will be reported multiple times
at Message.tests.cpp:<line number>
</failure>
</testCase>
<testCase name="Scoped messages do not leave block with an exception" duration="{duration}">
<failure message="REQUIRE(false)">
FAILED:
REQUIRE( false )
Should be in scope at the end
at Message.tests.cpp:<line number>
</failure>
</testCase>
<testCase name="Scoped messages outlive section end" duration="{duration}">
<failure message="REQUIRE(false)">
FAILED:
REQUIRE( false )
Should survive a section end
at Message.tests.cpp:<line number>
</failure>
</testCase>
<testCase name="Scoped messages outlive section end/Dummy section" duration="{duration}"/>
<testCase name="Standard output from all sections is reported" duration="{duration}"/>
<testCase name="Standard output from all sections is reported/one" duration="{duration}"/>
<testCase name="Standard output from all sections is reported/two" duration="{duration}"/>