google / googletest

GoogleTest - Google Testing and Mocking Framework

Home Page:https://google.github.io/googletest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

support std::string_view for comparisons

nolange opened this issue · comments

Hello,

The existing macros (or rather their helpers) still expect C-style strings,
which means you will have to make sure your string-types can offer zero-terminated strings.
and often manually request a conversion.

The basic premise is that I would want to write tests simply stating the intent,
no matter what type is returned.

ASSERT_STRCASEEQ(myclass.getName(), "hugo");

Implementation could be done without affecting the ABI, allowing gtest being compiled with C++11,
just offering some static inline functions.

namespace testing::internal {
#if __cpp_lib_string_view >= 201606L

GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression,
                                              const char* s2_expression,
                                              std::string_view s1,
                                              std::string_view s2)
{
    return CmpHelperSTRCASEEQ(s1_expression, s2_expression, std::string(s1).c_str(), std::string(s2).c_str());
}
#endif
}

This would allow the ASSERT_STRCASEEQ Macro to work transparently with C-Strings, std::string_view and any class convertible to std::string_view (like std::string).

Sounds very reasonable. We already have a macro GTEST_INTERNAL_HAS_STRING_VIEW that could be used to implement string_view overloads.

We do not have plans to work on this at the moment. But we would definitely consider accepting pull requests.