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

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="<exe-name>" errors="17" failures="134" skipped="12" tests="2285" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="140" skipped="12" tests="2293" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="random-seed" value="1"/>
<property name="filters" value="&quot;*&quot; ~[!nonportable] ~[!benchmark] ~[approvals]"/>
@@ -66,7 +66,6 @@ at Generators.tests.cpp:<line number>
FAILED:
{Unknown expression after the reported line}
expected exception
answer := 42
at Exception.tests.cpp:<line number>
</error>
</testcase>
@@ -411,6 +410,23 @@ at Exception.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Capture and info messages" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Capture and info messages/Capture should stringify like assertions" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Capture and info messages/Info should NOT stringify the way assertions do" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Captures do not leave block with an exception" time="{duration}" status="run">
<failure message="false" type="REQUIRE">
FAILED:
REQUIRE( false )
a := 1
at Message.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Captures outlive section end" time="{duration}" status="run">
<failure message="false" type="REQUIRE">
FAILED:
REQUIRE( false )
a := 1
at Message.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Captures outlive section end/Dummy section" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="CaseInsensitiveEqualsTo is case insensitive" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="CaseInsensitiveEqualsTo is case insensitive/Degenerate cases" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="CaseInsensitiveEqualsTo is case insensitive/Plain comparisons" time="{duration}" status="run"/>
@@ -1349,6 +1365,37 @@ at Matchers.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Scenario: Vector resizing affects size and capacity/Given: an empty vector/When: it is made larger/Then: the size and capacity go up/And when: it is made smaller again/Then: the size goes down but the capacity stays the same" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Scenario: Vector resizing affects size and capacity/Given: an empty vector/When: we reserve more space" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Scenario: Vector resizing affects size and capacity/Given: an empty vector/When: we reserve more space/Then: The capacity is increased but the size remains the same" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Scoped message applies to all assertions in scope" time="{duration}" status="run">
<failure message="false" type="CHECK">
FAILED:
CHECK( false )
This will be reported multiple times
at Message.tests.cpp:<line number>
</failure>
<failure message="false" type="CHECK">
FAILED:
CHECK( false )
This will be reported multiple times
at Message.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Scoped messages do not leave block with an exception" time="{duration}" status="run">
<failure message="false" type="REQUIRE">
FAILED:
REQUIRE( false )
Should be in scope at the end
at Message.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Scoped messages outlive section end" time="{duration}" status="run">
<failure message="false" type="REQUIRE">
FAILED:
REQUIRE( false )
Should survive a section end
at Message.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Scoped messages outlive section end/Dummy section" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Sends stuff to stdout and stderr" time="{duration}" status="run">
<system-out>
A string sent directly to stdout