diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 7be0caaf..17a74e45 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -1523,6 +1523,52 @@ 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 + +#if __cpp_lib_string_view >= 201803L +GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTREQ( + const char* s1_expression, const char* s2_expression, + const std::wstring_view& s1, const std::wstring_view& s2) { + return CmpHelperSTREQ(s1_expression, s2_expression, std::wstring(s1).c_str(), + std::wstring(s2).c_str()); +} + +GTEST_ATTRIBUTE_UNUSED_ static inline AssertionResult CmpHelperSTRNE( + const char* s1_expression, const char* s2_expression, + const std::wstring_view& s1, const std::wstring_view& s2) { + return CmpHelperSTRNE(s1_expression, s2_expression, std::wstring(s1).c_str(), + std::wstring(s2).c_str()); +} +#endif + } // namespace internal // IsSubstring() and IsNotSubstring() are intended to be used as the diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index 559d34c0..82130e5f 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -286,6 +286,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 {}; ::std::ostream& operator<<(::std::ostream& os, const TestingVector& vector) { @@ -2553,6 +2557,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. @@ -2609,6 +2624,16 @@ TEST(StringAssertionTest, STRNE_Wide) { ASSERT_STRNE(L"abc\x8119", L"abc\x8120") << "This shouldn't happen"; } +#if __cpp_lib_string_view >= 201803L +// Tests correct mapping to the C-String functions +TEST(StringAssertionTest, StringView_Wide) { + using std::wstring_view; + ASSERT_STREQ(wstring_view(L"hi"), wstring_view(L"hi")); + ASSERT_STREQ(wstring_view(L"hi"), L"hi"); + ASSERT_STRNE(L"hi", wstring_view(L"hi2")); +} +#endif + // Tests for ::testing::IsSubstring(). // Tests that IsSubstring() returns the correct result when the input