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

Native Testing: Google test failure output does not capture custom diagnostics written to result_listener #323

Closed
ryan-holt-1 opened this issue Jan 20, 2022 · 5 comments

Comments

@ryan-holt-1
Copy link

ryan-holt-1 commented Jan 20, 2022

We have a custom GTest Matcher called containsTokens. It does the following:

#include <string>
#include <sstream>

std::string stripWhitespace(const std::string& aString) {
    std::ostringstream tmp;
    auto i = aString.begin();
    auto end = aString.end();
    while (i != end) {
        if (!isspace(*i)) {
            tmp << *i;
        }
        ++i;
    }
    return tmp.str();
}

MATCHER_P(containsTokens, aSubstring, "") {
    auto findMe = stripWhitespace(aSubstring);
    auto withinMe = stripWhitespace(arg);
    if (withinMe.find(findMe) != std::string::npos) {
        return true;
    }
    *result_listener << "\n\n" << aSubstring << "\n\nnot found within\n\n" << arg;
    return false;
}

Note that we append additional failure diagnostics to the result_listener.

which is used as follows:


TEST(MySuite, MyTest) {
    auto input =
        R"(
            func foo() {
                x = 4;
            }
         )";

    auto expected =
        R"(
    func foo() {
        x = 5;
    }
    )";

    ASSERT_THAT(input, containsTokens(expected));
}

This test will fail with the following message (just running the test binary):

[ RUN ] MySuite.MyTest
test/unittest/tTest.cpp:119: Failure
Value of: input
Expected: contains tokens "\n func foo() {\n x = 5;\n }\n "
Actual: 0x5d04e0 pointing to "\n func foo() {\n x = 4;\n }\n " (of type char const*),

func foo() {
    x = 5;
}

not found within

        func foo() {
            x = 4;
        }

[ FAILED ] MySuite.MyTest (0 ms)

However, if I run it in C++ TestMate, It prints the following:

# Failure:
Value of: input
Expected: contains tokens "\n func foo() {\n x = 5;\n }\n "
Actual: 0x5d04e0 pointing to "\n func foo() {\n x = 4;\n }\n " (of type char const*),

Note that C++ TestMate does not print out the extra diagnostics that we wrote to the result_listener. This used to work before the switch to Native Testing. I'm not sure if this is a bug with TestMate or VSCode.

@matepek
Copy link
Owner

matepek commented Feb 1, 2022

please add the definition of stripWhitepace

@ryan-holt-1
Copy link
Author

Added to my original post.

Though, the easiest way to reproduce would be to just do:

MATCHER_P(containsTokens, aSubstring, "") {
    *result_listener << "\nPRINT ME\n";
    // Make the test always fail
    return false;
}

TEST(MySuite, MyTest) {
    auto input =
        R"(
            func foo() {
                x = 4;
            }
         )";

    auto expected =
        R"(
    func foo() {
        x = 5;
    }
    )";

    ASSERT_THAT(input, containsTokens(expected));
}

The test will always fail and you should see "PRINT ME" in the output. However, you won't see it when running with TestMate.

@matepek
Copy link
Owner

matepek commented Feb 5, 2022

Fixed in **v4.0.25**.

This issue was mentioned in CHANGELOG.md under a released entry so it is assumed to be fixed.
User verifications are always welcome.

@matepek matepek closed this as completed Feb 5, 2022
@ryan-holt-1
Copy link
Author

Thanks for working on this. However, I updated to the latest version and still have the same problem. I also still have the same problem as #329 . I do not see cout/cerr messages in the output.

I'm on Linux (Debian 10) if that matters.

@mkuehn94
Copy link

I'm having a very similar problem as in #329 stderr output are shown in terminal when running manually but not in the extension. however stdout output's work as expected.

I'm on windows sadly (windows 10)

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

3 participants