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

@@ -167,7 +167,7 @@ ok {test-number} - smallest_non_zero, !WithinULP( -smallest_non_zero, 1 ) for: 0
# #2615 - Throwing in constructor generator fails test case but does not abort
not ok {test-number} - unexpected exception with message: 'failure to init'; expression was: {Unknown expression after the reported line}
# #748 - captures with unexpected exceptions
not ok {test-number} - unexpected exception with message: 'answer := 42'; expression was: {Unknown expression after the reported line} with 1 message: 'expected exception'
not ok {test-number} - unexpected exception with message: 'expected exception'; expression was: {Unknown expression after the reported line}
# #748 - captures with unexpected exceptions
not ok {test-number} - unexpected exception with message: 'answer := 42'; expression was: thisThrows() with 1 message: 'expected exception'
# #748 - captures with unexpected exceptions
@@ -674,6 +674,12 @@ ok {test-number} - with 11 messages: '("comma, in string", "escaped, \", ") := "
ok {test-number} - true with 1 message: 'i := 2'
# Capture and info messages
ok {test-number} - true with 1 message: '3'
# Captures do not leave block with an exception
not ok {test-number} - false with 1 message: 'a := 1'
# Captures outlive section end
ok {test-number} - true with 1 message: 'a := 1'
# Captures outlive section end
not ok {test-number} - false with 1 message: 'a := 1'
# CaseInsensitiveEqualsTo is case insensitive
ok {test-number} - eq( "", "" ) for: true
# CaseInsensitiveEqualsTo is case insensitive
@@ -2616,6 +2622,16 @@ ok {test-number} - v.size() == 0 for: 0 == 0
ok {test-number} - v.capacity() >= 10 for: 10 >= 10
# Scenario: Vector resizing affects size and capacity
ok {test-number} - v.size() == 0 for: 0 == 0
# Scoped message applies to all assertions in scope
not ok {test-number} - false with 1 message: 'This will be reported multiple times'
# Scoped message applies to all assertions in scope
not ok {test-number} - false with 1 message: 'This will be reported multiple times'
# Scoped messages do not leave block with an exception
not ok {test-number} - false with 1 message: 'Should be in scope at the end'
# Scoped messages outlive section end
ok {test-number} - true with 1 message: 'Should survive a section end'
# Scoped messages outlive section end
not ok {test-number} - false with 1 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
@@ -4567,5 +4583,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
ok {test-number} -
# xmlentitycheck
ok {test-number} -
1..2285
1..2293