Support string_view for String comparison Macros

Allow string_views (and types implicitly convertable)
to be used in STREQ, STRNE, STRCASEEQ, STRCASENE macros.

Add a testcase using all of the new functions.
This commit is contained in:
Norbert Lange 2022-05-15 22:56:33 +02:00
parent 7041051488
commit 816b3ac0cc
2 changed files with 45 additions and 0 deletions

View file

@ -1487,6 +1487,36 @@ GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
const char* s2_expression,
const wchar_t* s1, const wchar_t* s2);
#if GTEST_INTERNAL_HAS_STRING_VIEW
GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTREQ(
const char* s1_expression, const char* s2_expression,
const internal::StringView& s1, const internal::StringView& s2) {
return CmpHelperSTREQ(s1_expression, s2_expression, std::string(s1).c_str(),
std::string(s2).c_str());
}
GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTRCASEEQ(
const char* s1_expression, const char* s2_expression,
const internal::StringView& s1, const internal::StringView& s2) {
return CmpHelperSTRCASEEQ(s1_expression, s2_expression,
std::string(s1).c_str(), std::string(s2).c_str());
}
GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTRNE(
const char* s1_expression, const char* s2_expression,
const internal::StringView& s1, const internal::StringView& s2) {
return CmpHelperSTRNE(s1_expression, s2_expression, std::string(s1).c_str(),
std::string(s2).c_str());
}
GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTRCASENE(
const char* s1_expression, const char* s2_expression,
const internal::StringView& s1, const internal::StringView& s2) {
return CmpHelperSTRCASENE(s1_expression, s2_expression,
std::string(s1).c_str(), std::string(s2).c_str());
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
} // namespace internal
// IsSubstring() and IsNotSubstring() are intended to be used as the

View file

@ -272,6 +272,10 @@ using testing::internal::GetCapturedStdout;
using testing::internal::ThreadWithParam;
#endif
#if GTEST_INTERNAL_HAS_STRING_VIEW
using testing::internal::StringView;
#endif
class TestingVector : public std::vector<int> {};
::std::ostream& operator<<(::std::ostream& os, const TestingVector& vector) {
@ -2526,6 +2530,17 @@ TEST(StringAssertionTest, ASSERT_STRCASENE) {
EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("Hi", "hi"), "(ignoring case)");
}
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests correct mapping to the C-String functions
TEST(StringAssertionTest, StringView) {
ASSERT_STREQ(StringView("hi"), StringView("hi"));
ASSERT_STREQ(StringView("hi"), "hi");
ASSERT_STRNE("hi", StringView("hi2"));
ASSERT_STRCASEEQ(StringView("hi"), "Hi");
ASSERT_STRCASENE("hi1", StringView("Hi2"));
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests *_STREQ on wide strings.
TEST(StringAssertionTest, STREQ_Wide) {
// NULL strings.