google / google/googletest

support std::string_view for comparisons

Open
#3,829 1 comment 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

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.

```c++
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.

```c++
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`).

Contributor guide

Open the contributing guide

Research direction

Start by locating the ASSERT_STRCASEEQ macro and the comparison helpers it invokes. Review how the existing helpers handle C-style strings, then check the relevant string-comparison tests. Done means the macro accepts std::string_view and compatible string types without requiring manual conversion, while preserving existing C-string behavior and ABI compatibility.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.