Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNSCOPED_CAPTURE #2954

Open
TysonRayJones opened this issue Feb 11, 2025 · 0 comments
Open

UNSCOPED_CAPTURE #2954

TysonRayJones opened this issue Feb 11, 2025 · 0 comments

Comments

@TysonRayJones
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant