Skip to content

UNSCOPED_CAPTURE #2954

Open
Open
@TysonRayJones

Description

It might be useful to have a new logger called UNSCOPED_CAPTURE, which relates to CAPTURE in the same way that UNSCOPED_INFO relates to INFO. That is, it works like CAPTURE but its lifetime is not limited to its scope. This would permit invoking CAPTURE inside control flow or inner functions which do not encounter an assertion (like REQUIRE) before the end-of-scope, yet still have the output displayed when the calling scope (like a TEST_CASE) hits the assertion.

Motivation
Currently, CAPTURE's limited lifetime has the same limitations of INFO. This means running code like:

void inner(x) {
    CAPTURE( x );

    // no assertions here, so CAPTURE silent
}

TEST_CASE( "demo" ) {

    int x = 2;
    inner(x);

    FAIL( );
}

will not display x := 2. Such a use-case is motivated by inner() being a customisation of CAPTURE which (for example) chooses which variables to display based on compile-time constants. For example:

template <bool Show>
void runTest() {

    int x = 2;

    // this will never show
    if constexpr (Show) {
        CAPTURE( x );
    }

    FAIL();
}

TEST_CASE( "demoA" ) {

    runTest<true>();
}

TEST_CASE( "demoB" ) {

    runTest<false>();
}

Even the constexpr branch has a scope, so CAPTURE's output is lost. Above is akin to my situation; I have a templated function used to automate many TEST_CASEs (yucky I know - woe scientific software) and I must consult the template parameters to choose which variables CAPTURE should show if/when the test subsequently fails.

In principle, I can manually invoke UNSCOPED_INFO myself, but then I have to re-implement CAPTURE's lovely printing of nested vectors, for example.

Description
A new macro UNSCOPED_CAPTURE which works exactly like CAPTURE, but which has the scoping behaviour of UNSCOPED_INFO.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions