diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 0f677137..8052c74a 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -1048,7 +1048,7 @@ class StartsWithMatcher { template bool MatchAndExplain(const MatcheeStringType& s, MatchResultListener* /* listener */) const { - const StringType& s2(s); + const StringType s2(s); return s2.length() >= prefix_.length() && s2.substr(0, prefix_.length()) == prefix_; } @@ -1102,7 +1102,7 @@ class EndsWithMatcher { template bool MatchAndExplain(const MatcheeStringType& s, MatchResultListener* /* listener */) const { - const StringType& s2(s); + const StringType s2(s); return s2.length() >= suffix_.length() && s2.substr(s2.length() - suffix_.length()) == suffix_; } diff --git a/googlemock/test/gmock-matchers-comparisons_test.cc b/googlemock/test/gmock-matchers-comparisons_test.cc index b2ce99e1..5b75b457 100644 --- a/googlemock/test/gmock-matchers-comparisons_test.cc +++ b/googlemock/test/gmock-matchers-comparisons_test.cc @@ -1769,6 +1769,15 @@ TEST(StartsWithTest, CanDescribeSelf) { EXPECT_EQ("starts with \"Hi\"", Describe(m)); } +TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) { +#if GTEST_INTERNAL_HAS_STRING_VIEW + EXPECT_THAT(internal::StringView("talk to me goose"), + StartsWith(std::string("talk"))); +#else + GTEST_SKIP() << "Not applicable without internal::StringView."; +#endif // GTEST_INTERNAL_HAS_STRING_VIEW +} + // Tests EndsWith(s). TEST(EndsWithTest, MatchesStringWithGivenSuffix) {