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

@@ -781,8 +781,7 @@ Exception.tests.cpp:<line number>
Exception.tests.cpp:<line number>: FAILED:
{Unknown expression after the reported line}
due to unexpected exception with messages:
answer := 42
due to unexpected exception with message:
expected exception
-------------------------------------------------------------------------------
@@ -2867,6 +2866,40 @@ ToStringGeneral.tests.cpp:<line number>: PASSED:
with message:
3
-------------------------------------------------------------------------------
Captures do not leave block with an exception
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: FAILED:
REQUIRE( false )
with message:
a := 1
-------------------------------------------------------------------------------
Captures outlive section end
Dummy section
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: PASSED:
CHECK( true )
with message:
a := 1
-------------------------------------------------------------------------------
Captures outlive section end
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: FAILED:
REQUIRE( false )
with message:
a := 1
-------------------------------------------------------------------------------
CaseInsensitiveEqualsTo is case insensitive
Degenerate cases
@@ -11026,6 +11059,56 @@ BDD.tests.cpp:<line number>: PASSED:
with expansion:
0 == 0
-------------------------------------------------------------------------------
Scoped message applies to all assertions in scope
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: FAILED:
CHECK( false )
with message:
This will be reported multiple times
Message.tests.cpp:<line number>: FAILED:
CHECK( false )
with message:
This will be reported multiple times
-------------------------------------------------------------------------------
Scoped messages do not leave block with an exception
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: FAILED:
REQUIRE( false )
with message:
Should be in scope at the end
-------------------------------------------------------------------------------
Scoped messages outlive section end
Dummy section
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: PASSED:
CHECK( true )
with message:
Should survive a section end
-------------------------------------------------------------------------------
Scoped messages outlive section end
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................
Message.tests.cpp:<line number>: FAILED:
REQUIRE( false )
with message:
Should survive a section end
A string sent directly to stdout
A string sent directly to stderr
A string sent to stderr via clog
@@ -19051,6 +19134,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED:
===============================================================================
test cases: 423 | 313 passed | 90 failed | 6 skipped | 14 failed as expected
assertions: 2273 | 2087 passed | 151 failed | 35 failed as expected
test cases: 428 | 313 passed | 95 failed | 6 skipped | 14 failed as expected
assertions: 2281 | 2089 passed | 157 failed | 35 failed as expected