Googletest export

Add gmock Matcher<std::string_view> specialization.

PiperOrigin-RevId: 294443240
This commit is contained in:
Abseil Team 2020-02-11 11:22:17 -05:00 committed by Mark Barolak
parent d0930731d6
commit 6f5fd0d719
7 changed files with 197 additions and 164 deletions

View File

@ -906,15 +906,15 @@ class StrEqualityMatcher {
bool case_sensitive) bool case_sensitive)
: string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const absl::string_view& s, bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const { MatchResultListener* listener) const {
// This should fail to compile if absl::string_view is used with wide // This should fail to compile if StringView is used with wide
// strings. // strings.
const StringType& str = std::string(s); const StringType& str = std::string(s);
return MatchAndExplain(str, listener); return MatchAndExplain(str, listener);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly: // Accepts pointer types, particularly:
// const char* // const char*
@ -932,7 +932,7 @@ class StrEqualityMatcher {
// Matches anything that can convert to StringType. // Matches anything that can convert to StringType.
// //
// This is a template, not just a plain function with const StringType&, // This is a template, not just a plain function with const StringType&,
// because absl::string_view has some interfering non-explicit constructors. // because StringView has some interfering non-explicit constructors.
template <typename MatcheeStringType> template <typename MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s, bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* /* listener */) const { MatchResultListener* /* listener */) const {
@ -976,15 +976,15 @@ class HasSubstrMatcher {
explicit HasSubstrMatcher(const StringType& substring) explicit HasSubstrMatcher(const StringType& substring)
: substring_(substring) {} : substring_(substring) {}
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const absl::string_view& s, bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const { MatchResultListener* listener) const {
// This should fail to compile if absl::string_view is used with wide // This should fail to compile if StringView is used with wide
// strings. // strings.
const StringType& str = std::string(s); const StringType& str = std::string(s);
return MatchAndExplain(str, listener); return MatchAndExplain(str, listener);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly: // Accepts pointer types, particularly:
// const char* // const char*
@ -999,7 +999,7 @@ class HasSubstrMatcher {
// Matches anything that can convert to StringType. // Matches anything that can convert to StringType.
// //
// This is a template, not just a plain function with const StringType&, // This is a template, not just a plain function with const StringType&,
// because absl::string_view has some interfering non-explicit constructors. // because StringView has some interfering non-explicit constructors.
template <typename MatcheeStringType> template <typename MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s, bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* /* listener */) const { MatchResultListener* /* listener */) const {
@ -1032,15 +1032,15 @@ class StartsWithMatcher {
explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
} }
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const absl::string_view& s, bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const { MatchResultListener* listener) const {
// This should fail to compile if absl::string_view is used with wide // This should fail to compile if StringView is used with wide
// strings. // strings.
const StringType& str = std::string(s); const StringType& str = std::string(s);
return MatchAndExplain(str, listener); return MatchAndExplain(str, listener);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly: // Accepts pointer types, particularly:
// const char* // const char*
@ -1055,7 +1055,7 @@ class StartsWithMatcher {
// Matches anything that can convert to StringType. // Matches anything that can convert to StringType.
// //
// This is a template, not just a plain function with const StringType&, // This is a template, not just a plain function with const StringType&,
// because absl::string_view has some interfering non-explicit constructors. // because StringView has some interfering non-explicit constructors.
template <typename MatcheeStringType> template <typename MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s, bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* /* listener */) const { MatchResultListener* /* listener */) const {
@ -1088,15 +1088,15 @@ class EndsWithMatcher {
public: public:
explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const absl::string_view& s, bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const { MatchResultListener* listener) const {
// This should fail to compile if absl::string_view is used with wide // This should fail to compile if StringView is used with wide
// strings. // strings.
const StringType& str = std::string(s); const StringType& str = std::string(s);
return MatchAndExplain(str, listener); return MatchAndExplain(str, listener);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly: // Accepts pointer types, particularly:
// const char* // const char*
@ -1111,7 +1111,7 @@ class EndsWithMatcher {
// Matches anything that can convert to StringType. // Matches anything that can convert to StringType.
// //
// This is a template, not just a plain function with const StringType&, // This is a template, not just a plain function with const StringType&,
// because absl::string_view has some interfering non-explicit constructors. // because StringView has some interfering non-explicit constructors.
template <typename MatcheeStringType> template <typename MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s, bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* /* listener */) const { MatchResultListener* /* listener */) const {

View File

@ -351,43 +351,43 @@ TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
EXPECT_FALSE(m2.Matches("hello")); EXPECT_FALSE(m2.Matches("hello"));
} }
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that a C-string literal can be implicitly converted to a // Tests that a C-string literal can be implicitly converted to a
// Matcher<absl::string_view> or Matcher<const absl::string_view&>. // Matcher<StringView> or Matcher<const StringView&>.
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
Matcher<absl::string_view> m1 = "cats"; Matcher<internal::StringView> m1 = "cats";
EXPECT_TRUE(m1.Matches("cats")); EXPECT_TRUE(m1.Matches("cats"));
EXPECT_FALSE(m1.Matches("dogs")); EXPECT_FALSE(m1.Matches("dogs"));
Matcher<const absl::string_view&> m2 = "cats"; Matcher<const internal::StringView&> m2 = "cats";
EXPECT_TRUE(m2.Matches("cats")); EXPECT_TRUE(m2.Matches("cats"));
EXPECT_FALSE(m2.Matches("dogs")); EXPECT_FALSE(m2.Matches("dogs"));
} }
// Tests that a std::string object can be implicitly converted to a // Tests that a std::string object can be implicitly converted to a
// Matcher<absl::string_view> or Matcher<const absl::string_view&>. // Matcher<StringView> or Matcher<const StringView&>.
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) { TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
Matcher<absl::string_view> m1 = std::string("cats"); Matcher<internal::StringView> m1 = std::string("cats");
EXPECT_TRUE(m1.Matches("cats")); EXPECT_TRUE(m1.Matches("cats"));
EXPECT_FALSE(m1.Matches("dogs")); EXPECT_FALSE(m1.Matches("dogs"));
Matcher<const absl::string_view&> m2 = std::string("cats"); Matcher<const internal::StringView&> m2 = std::string("cats");
EXPECT_TRUE(m2.Matches("cats")); EXPECT_TRUE(m2.Matches("cats"));
EXPECT_FALSE(m2.Matches("dogs")); EXPECT_FALSE(m2.Matches("dogs"));
} }
// Tests that a absl::string_view object can be implicitly converted to a // Tests that a StringView object can be implicitly converted to a
// Matcher<absl::string_view> or Matcher<const absl::string_view&>. // Matcher<StringView> or Matcher<const StringView&>.
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
Matcher<absl::string_view> m1 = absl::string_view("cats"); Matcher<internal::StringView> m1 = internal::StringView("cats");
EXPECT_TRUE(m1.Matches("cats")); EXPECT_TRUE(m1.Matches("cats"));
EXPECT_FALSE(m1.Matches("dogs")); EXPECT_FALSE(m1.Matches("dogs"));
Matcher<const absl::string_view&> m2 = absl::string_view("cats"); Matcher<const internal::StringView&> m2 = internal::StringView("cats");
EXPECT_TRUE(m2.Matches("cats")); EXPECT_TRUE(m2.Matches("cats"));
EXPECT_FALSE(m2.Matches("dogs")); EXPECT_FALSE(m2.Matches("dogs"));
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that a std::reference_wrapper<std::string> object can be implicitly // Tests that a std::reference_wrapper<std::string> object can be implicitly
// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq(). // converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
@ -1235,17 +1235,17 @@ TEST(StrEqTest, MatchesEqualString) {
EXPECT_TRUE(m2.Matches("Hello")); EXPECT_TRUE(m2.Matches("Hello"));
EXPECT_FALSE(m2.Matches("Hi")); EXPECT_FALSE(m2.Matches("Hi"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view&> m3 = StrEq("Hello"); Matcher<const internal::StringView&> m3 = StrEq("Hello");
EXPECT_TRUE(m3.Matches(absl::string_view("Hello"))); EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
EXPECT_FALSE(m3.Matches(absl::string_view("hello"))); EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
EXPECT_FALSE(m3.Matches(absl::string_view())); EXPECT_FALSE(m3.Matches(internal::StringView()));
Matcher<const absl::string_view&> m_empty = StrEq(""); Matcher<const internal::StringView&> m_empty = StrEq("");
EXPECT_TRUE(m_empty.Matches(absl::string_view(""))); EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
EXPECT_TRUE(m_empty.Matches(absl::string_view())); EXPECT_TRUE(m_empty.Matches(internal::StringView()));
EXPECT_FALSE(m_empty.Matches(absl::string_view("hello"))); EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(StrEqTest, CanDescribeSelf) { TEST(StrEqTest, CanDescribeSelf) {
@ -1272,12 +1272,12 @@ TEST(StrNeTest, MatchesUnequalString) {
EXPECT_TRUE(m2.Matches("hello")); EXPECT_TRUE(m2.Matches("hello"));
EXPECT_FALSE(m2.Matches("Hello")); EXPECT_FALSE(m2.Matches("Hello"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view> m3 = StrNe("Hello"); Matcher<const internal::StringView> m3 = StrNe("Hello");
EXPECT_TRUE(m3.Matches(absl::string_view(""))); EXPECT_TRUE(m3.Matches(internal::StringView("")));
EXPECT_TRUE(m3.Matches(absl::string_view())); EXPECT_TRUE(m3.Matches(internal::StringView()));
EXPECT_FALSE(m3.Matches(absl::string_view("Hello"))); EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(StrNeTest, CanDescribeSelf) { TEST(StrNeTest, CanDescribeSelf) {
@ -1296,13 +1296,13 @@ TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
EXPECT_TRUE(m2.Matches("hello")); EXPECT_TRUE(m2.Matches("hello"));
EXPECT_FALSE(m2.Matches("Hi")); EXPECT_FALSE(m2.Matches("Hi"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view&> m3 = StrCaseEq(std::string("Hello")); Matcher<const internal::StringView&> m3 = StrCaseEq(std::string("Hello"));
EXPECT_TRUE(m3.Matches(absl::string_view("Hello"))); EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
EXPECT_TRUE(m3.Matches(absl::string_view("hello"))); EXPECT_TRUE(m3.Matches(internal::StringView("hello")));
EXPECT_FALSE(m3.Matches(absl::string_view("Hi"))); EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));
EXPECT_FALSE(m3.Matches(absl::string_view())); EXPECT_FALSE(m3.Matches(internal::StringView()));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
@ -1346,13 +1346,13 @@ TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
EXPECT_TRUE(m2.Matches("")); EXPECT_TRUE(m2.Matches(""));
EXPECT_FALSE(m2.Matches("Hello")); EXPECT_FALSE(m2.Matches("Hello"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view> m3 = StrCaseNe("Hello"); Matcher<const internal::StringView> m3 = StrCaseNe("Hello");
EXPECT_TRUE(m3.Matches(absl::string_view("Hi"))); EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));
EXPECT_TRUE(m3.Matches(absl::string_view())); EXPECT_TRUE(m3.Matches(internal::StringView()));
EXPECT_FALSE(m3.Matches(absl::string_view("Hello"))); EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
EXPECT_FALSE(m3.Matches(absl::string_view("hello"))); EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(StrCaseNeTest, CanDescribeSelf) { TEST(StrCaseNeTest, CanDescribeSelf) {
@ -1393,25 +1393,25 @@ TEST(HasSubstrTest, WorksForCStrings) {
EXPECT_FALSE(m_empty.Matches(nullptr)); EXPECT_FALSE(m_empty.Matches(nullptr));
} }
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr() works for matching absl::string_view-typed values. // Tests that HasSubstr() works for matching StringView-typed values.
TEST(HasSubstrTest, WorksForStringViewClasses) { TEST(HasSubstrTest, WorksForStringViewClasses) {
const Matcher<absl::string_view> m1 = HasSubstr("foo"); const Matcher<internal::StringView> m1 = HasSubstr("foo");
EXPECT_TRUE(m1.Matches(absl::string_view("I love food."))); EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));
EXPECT_FALSE(m1.Matches(absl::string_view("tofo"))); EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));
EXPECT_FALSE(m1.Matches(absl::string_view())); EXPECT_FALSE(m1.Matches(internal::StringView()));
const Matcher<const absl::string_view&> m2 = HasSubstr("foo"); const Matcher<const internal::StringView&> m2 = HasSubstr("foo");
EXPECT_TRUE(m2.Matches(absl::string_view("I love food."))); EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));
EXPECT_FALSE(m2.Matches(absl::string_view("tofo"))); EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));
EXPECT_FALSE(m2.Matches(absl::string_view())); EXPECT_FALSE(m2.Matches(internal::StringView()));
const Matcher<const absl::string_view&> m3 = HasSubstr(""); const Matcher<const internal::StringView&> m3 = HasSubstr("");
EXPECT_TRUE(m3.Matches(absl::string_view("foo"))); EXPECT_TRUE(m3.Matches(internal::StringView("foo")));
EXPECT_TRUE(m3.Matches(absl::string_view(""))); EXPECT_TRUE(m3.Matches(internal::StringView("")));
EXPECT_TRUE(m3.Matches(absl::string_view())); EXPECT_TRUE(m3.Matches(internal::StringView()));
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr(s) describes itself properly. // Tests that HasSubstr(s) describes itself properly.
TEST(HasSubstrTest, CanDescribeSelf) { TEST(HasSubstrTest, CanDescribeSelf) {
@ -1648,12 +1648,12 @@ TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
EXPECT_FALSE(m2.Matches("H")); EXPECT_FALSE(m2.Matches("H"));
EXPECT_FALSE(m2.Matches(" Hi")); EXPECT_FALSE(m2.Matches(" Hi"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<absl::string_view> m_empty = StartsWith(""); const Matcher<internal::StringView> m_empty = StartsWith("");
EXPECT_TRUE(m_empty.Matches(absl::string_view())); EXPECT_TRUE(m_empty.Matches(internal::StringView()));
EXPECT_TRUE(m_empty.Matches(absl::string_view(""))); EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
EXPECT_TRUE(m_empty.Matches(absl::string_view("not empty"))); EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(StartsWithTest, CanDescribeSelf) { TEST(StartsWithTest, CanDescribeSelf) {
@ -1676,13 +1676,13 @@ TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
EXPECT_FALSE(m2.Matches("i")); EXPECT_FALSE(m2.Matches("i"));
EXPECT_FALSE(m2.Matches("Hi ")); EXPECT_FALSE(m2.Matches("Hi "));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const absl::string_view&> m4 = EndsWith(""); const Matcher<const internal::StringView&> m4 = EndsWith("");
EXPECT_TRUE(m4.Matches("Hi")); EXPECT_TRUE(m4.Matches("Hi"));
EXPECT_TRUE(m4.Matches("")); EXPECT_TRUE(m4.Matches(""));
EXPECT_TRUE(m4.Matches(absl::string_view())); EXPECT_TRUE(m4.Matches(internal::StringView()));
EXPECT_TRUE(m4.Matches(absl::string_view(""))); EXPECT_TRUE(m4.Matches(internal::StringView("")));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(EndsWithTest, CanDescribeSelf) { TEST(EndsWithTest, CanDescribeSelf) {
@ -1703,16 +1703,16 @@ TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
EXPECT_FALSE(m2.Matches("az1")); EXPECT_FALSE(m2.Matches("az1"));
EXPECT_FALSE(m2.Matches("1az")); EXPECT_FALSE(m2.Matches("1az"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const absl::string_view&> m3 = MatchesRegex("a.*z"); const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");
EXPECT_TRUE(m3.Matches(absl::string_view("az"))); EXPECT_TRUE(m3.Matches(internal::StringView("az")));
EXPECT_TRUE(m3.Matches(absl::string_view("abcz"))); EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));
EXPECT_FALSE(m3.Matches(absl::string_view("1az"))); EXPECT_FALSE(m3.Matches(internal::StringView("1az")));
EXPECT_FALSE(m3.Matches(absl::string_view())); EXPECT_FALSE(m3.Matches(internal::StringView()));
const Matcher<const absl::string_view&> m4 = MatchesRegex(""); const Matcher<const internal::StringView&> m4 = MatchesRegex("");
EXPECT_TRUE(m4.Matches(absl::string_view(""))); EXPECT_TRUE(m4.Matches(internal::StringView("")));
EXPECT_TRUE(m4.Matches(absl::string_view())); EXPECT_TRUE(m4.Matches(internal::StringView()));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(MatchesRegexTest, CanDescribeSelf) { TEST(MatchesRegexTest, CanDescribeSelf) {
@ -1722,10 +1722,10 @@ TEST(MatchesRegexTest, CanDescribeSelf) {
Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view> m3 = MatchesRegex(new RE("0.*")); Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));
EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3)); EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
// Tests ContainsRegex(). // Tests ContainsRegex().
@ -1741,16 +1741,17 @@ TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
EXPECT_TRUE(m2.Matches("az1")); EXPECT_TRUE(m2.Matches("az1"));
EXPECT_FALSE(m2.Matches("1a")); EXPECT_FALSE(m2.Matches("1a"));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const absl::string_view&> m3 = ContainsRegex(new RE("a.*z")); const Matcher<const internal::StringView&> m3 =
EXPECT_TRUE(m3.Matches(absl::string_view("azbz"))); ContainsRegex(new RE("a.*z"));
EXPECT_TRUE(m3.Matches(absl::string_view("az1"))); EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));
EXPECT_FALSE(m3.Matches(absl::string_view("1a"))); EXPECT_TRUE(m3.Matches(internal::StringView("az1")));
EXPECT_FALSE(m3.Matches(absl::string_view())); EXPECT_FALSE(m3.Matches(internal::StringView("1a")));
const Matcher<const absl::string_view&> m4 = ContainsRegex(""); EXPECT_FALSE(m3.Matches(internal::StringView()));
EXPECT_TRUE(m4.Matches(absl::string_view(""))); const Matcher<const internal::StringView&> m4 = ContainsRegex("");
EXPECT_TRUE(m4.Matches(absl::string_view())); EXPECT_TRUE(m4.Matches(internal::StringView("")));
#endif // GTEST_HAS_ABSL EXPECT_TRUE(m4.Matches(internal::StringView()));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
TEST(ContainsRegexTest, CanDescribeSelf) { TEST(ContainsRegexTest, CanDescribeSelf) {
@ -1760,10 +1761,10 @@ TEST(ContainsRegexTest, CanDescribeSelf) {
Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const absl::string_view> m3 = ContainsRegex(new RE("0.*")); Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));
EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3)); EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} }
// Tests for wide strings. // Tests for wide strings.

View File

@ -384,18 +384,18 @@ class GTEST_API_ Matcher<std::string>
Matcher(const char* s); // NOLINT Matcher(const char* s); // NOLINT
}; };
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// The following two specializations allow the user to write str // The following two specializations allow the user to write str
// instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view // instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view
// matcher is expected. // matcher is expected.
template <> template <>
class GTEST_API_ Matcher<const absl::string_view&> class GTEST_API_ Matcher<const internal::StringView&>
: public internal::MatcherBase<const absl::string_view&> { : public internal::MatcherBase<const internal::StringView&> {
public: public:
Matcher() {} Matcher() {}
explicit Matcher(const MatcherInterface<const absl::string_view&>* impl) explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
: internal::MatcherBase<const absl::string_view&>(impl) {} : internal::MatcherBase<const internal::StringView&>(impl) {}
// Allows the user to write str instead of Eq(str) sometimes, where // Allows the user to write str instead of Eq(str) sometimes, where
// str is a std::string object. // str is a std::string object.
@ -404,20 +404,20 @@ class GTEST_API_ Matcher<const absl::string_view&>
// Allows the user to write "foo" instead of Eq("foo") sometimes. // Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher(const char* s); // NOLINT Matcher(const char* s); // NOLINT
// Allows the user to pass absl::string_views directly. // Allows the user to pass absl::string_views or std::string_views directly.
Matcher(absl::string_view s); // NOLINT Matcher(internal::StringView s); // NOLINT
}; };
template <> template <>
class GTEST_API_ Matcher<absl::string_view> class GTEST_API_ Matcher<internal::StringView>
: public internal::MatcherBase<absl::string_view> { : public internal::MatcherBase<internal::StringView> {
public: public:
Matcher() {} Matcher() {}
explicit Matcher(const MatcherInterface<const absl::string_view&>* impl) explicit Matcher(const MatcherInterface<const internal::StringView&>* impl)
: internal::MatcherBase<absl::string_view>(impl) {} : internal::MatcherBase<internal::StringView>(impl) {}
explicit Matcher(const MatcherInterface<absl::string_view>* impl) explicit Matcher(const MatcherInterface<internal::StringView>* impl)
: internal::MatcherBase<absl::string_view>(impl) {} : internal::MatcherBase<internal::StringView>(impl) {}
// Allows the user to write str instead of Eq(str) sometimes, where // Allows the user to write str instead of Eq(str) sometimes, where
// str is a std::string object. // str is a std::string object.
@ -426,10 +426,10 @@ class GTEST_API_ Matcher<absl::string_view>
// Allows the user to write "foo" instead of Eq("foo") sometimes. // Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher(const char* s); // NOLINT Matcher(const char* s); // NOLINT
// Allows the user to pass absl::string_views directly. // Allows the user to pass absl::string_views or std::string_views directly.
Matcher(absl::string_view s); // NOLINT Matcher(internal::StringView s); // NOLINT
}; };
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Prints a matcher in a human-readable format. // Prints a matcher in a human-readable format.
template <typename T> template <typename T>
@ -620,12 +620,12 @@ class MatchesRegexMatcher {
MatchesRegexMatcher(const RE* regex, bool full_match) MatchesRegexMatcher(const RE* regex, bool full_match)
: regex_(regex), full_match_(full_match) {} : regex_(regex), full_match_(full_match) {}
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const absl::string_view& s, bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const { MatchResultListener* listener) const {
return MatchAndExplain(std::string(s), listener); return MatchAndExplain(std::string(s), listener);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly: // Accepts pointer types, particularly:
// const char* // const char*

View File

@ -135,9 +135,9 @@ enum TypeKind {
kProtobuf, // a protobuf type kProtobuf, // a protobuf type
kConvertibleToInteger, // a type implicitly convertible to BiggestInt kConvertibleToInteger, // a type implicitly convertible to BiggestInt
// (e.g. a named or unnamed enum type) // (e.g. a named or unnamed enum type)
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
kConvertibleToStringView, // a type implicitly convertible to kConvertibleToStringView, // a type implicitly convertible to
// absl::string_view // absl::string_view or std::string_view
#endif #endif
kOtherType // anything else kOtherType // anything else
}; };
@ -191,12 +191,13 @@ class TypeWithoutFormatter<T, kConvertibleToInteger> {
} }
}; };
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
template <typename T> template <typename T>
class TypeWithoutFormatter<T, kConvertibleToStringView> { class TypeWithoutFormatter<T, kConvertibleToStringView> {
public: public:
// Since T has neither operator<< nor PrintTo() but can be implicitly // Since T has neither operator<< nor PrintTo() but can be implicitly
// converted to absl::string_view, we print it as a absl::string_view. // converted to absl::string_view, we print it as a absl::string_view
// (or std::string_view).
// //
// Note: the implementation is further below, as it depends on // Note: the implementation is further below, as it depends on
// internal::PrintTo symbol which is defined later in the file. // internal::PrintTo symbol which is defined later in the file.
@ -237,9 +238,9 @@ template <typename Char, typename CharTraits, typename T>
const T&, internal::BiggestInt>::value const T&, internal::BiggestInt>::value
? kConvertibleToInteger ? kConvertibleToInteger
: :
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
std::is_convertible< std::is_convertible<
const T&, absl::string_view>::value const T&, internal::StringView>::value
? kConvertibleToStringView ? kConvertibleToStringView
: :
#endif #endif
@ -601,12 +602,12 @@ inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
} }
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// Overload for absl::string_view. // Overload for internal::StringView.
inline void PrintTo(absl::string_view sp, ::std::ostream* os) { inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
PrintTo(::std::string(sp), os); PrintTo(::std::string(sp), os);
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
@ -899,12 +900,12 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
} // namespace internal } // namespace internal
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
namespace internal2 { namespace internal2 {
template <typename T> template <typename T>
void TypeWithoutFormatter<T, kConvertibleToStringView>::PrintValue( void TypeWithoutFormatter<T, kConvertibleToStringView>::PrintValue(
const T& value, ::std::ostream* os) { const T& value, ::std::ostream* os) {
internal::PrintTo(absl::string_view(value), os); internal::PrintTo(internal::StringView(value), os);
} }
} // namespace internal2 } // namespace internal2
#endif #endif

View File

@ -199,6 +199,9 @@
// suppressed (constant conditional). // suppressed (constant conditional).
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
// is suppressed. // is suppressed.
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
// Matcher<absl::string_view>
// specializations.
// //
// Synchronization: // Synchronization:
// Mutex, MutexLock, ThreadLocal, GetThreadCount() // Mutex, MutexLock, ThreadLocal, GetThreadCount()
@ -2220,4 +2223,32 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
#endif // !defined(GTEST_INTERNAL_DEPRECATED) #endif // !defined(GTEST_INTERNAL_DEPRECATED)
#if GTEST_HAS_ABSL
// Always use absl::string_view for Matcher<> specializations if googletest
// is built with absl support.
# define GTEST_INTERNAL_HAS_STRING_VIEW 1
#include "absl/strings/string_view.h"
namespace testing {
namespace internal {
using StringView = ::absl::string_view;
} // namespace internal
} // namespace testing
#else
# ifdef __has_include
# if __has_include(<string_view>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::string_view for Matcher<>
// specializations.
# define GTEST_INTERNAL_HAS_STRING_VIEW 1
#include <string_view>
namespace testing {
namespace internal {
using StringView = ::std::string_view;
} // namespace internal
} // namespace testing
// The case where absl is configured NOT to alias std::string_view is not
// supported.
# endif // __has_include(<string_view>) && __cplusplus >= 201703L
# endif // __has_include
#endif // GTEST_HAS_ABSL
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_

View File

@ -58,40 +58,40 @@ Matcher<std::string>::Matcher(const std::string& s) { *this = Eq(s); }
// s. // s.
Matcher<std::string>::Matcher(const char* s) { *this = Eq(std::string(s)); } Matcher<std::string>::Matcher(const char* s) { *this = Eq(std::string(s)); }
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// Constructs a matcher that matches a const absl::string_view& whose value is // Constructs a matcher that matches a const StringView& whose value is
// equal to s. // equal to s.
Matcher<const absl::string_view&>::Matcher(const std::string& s) { Matcher<const internal::StringView&>::Matcher(const std::string& s) {
*this = Eq(s); *this = Eq(s);
} }
// Constructs a matcher that matches a const absl::string_view& whose value is // Constructs a matcher that matches a const StringView& whose value is
// equal to s. // equal to s.
Matcher<const absl::string_view&>::Matcher(const char* s) { Matcher<const internal::StringView&>::Matcher(const char* s) {
*this = Eq(std::string(s)); *this = Eq(std::string(s));
} }
// Constructs a matcher that matches a const absl::string_view& whose value is // Constructs a matcher that matches a const StringView& whose value is
// equal to s. // equal to s.
Matcher<const absl::string_view&>::Matcher(absl::string_view s) { Matcher<const internal::StringView&>::Matcher(internal::StringView s) {
*this = Eq(std::string(s)); *this = Eq(std::string(s));
} }
// Constructs a matcher that matches a absl::string_view whose value is equal to // Constructs a matcher that matches a StringView whose value is equal to
// s. // s.
Matcher<absl::string_view>::Matcher(const std::string& s) { *this = Eq(s); } Matcher<internal::StringView>::Matcher(const std::string& s) { *this = Eq(s); }
// Constructs a matcher that matches a absl::string_view whose value is equal to // Constructs a matcher that matches a StringView whose value is equal to
// s. // s.
Matcher<absl::string_view>::Matcher(const char* s) { Matcher<internal::StringView>::Matcher(const char* s) {
*this = Eq(std::string(s)); *this = Eq(std::string(s));
} }
// Constructs a matcher that matches a absl::string_view whose value is equal to // Constructs a matcher that matches a StringView whose value is equal to
// s. // s.
Matcher<absl::string_view>::Matcher(absl::string_view s) { Matcher<internal::StringView>::Matcher(internal::StringView s) {
*this = Eq(std::string(s)); *this = Eq(std::string(s));
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
} // namespace testing } // namespace testing

View File

@ -760,22 +760,22 @@ TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
} }
#if GTEST_HAS_ABSL #if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests printing ::absl::string_view. // Tests printing internal::StringView.
TEST(PrintStringViewTest, SimpleStringView) { TEST(PrintStringViewTest, SimpleStringView) {
const ::absl::string_view sp = "Hello"; const internal::StringView sp = "Hello";
EXPECT_EQ("\"Hello\"", Print(sp)); EXPECT_EQ("\"Hello\"", Print(sp));
} }
TEST(PrintStringViewTest, UnprintableCharacters) { TEST(PrintStringViewTest, UnprintableCharacters) {
const char str[] = "NUL (\0) and \r\t"; const char str[] = "NUL (\0) and \r\t";
const ::absl::string_view sp(str, sizeof(str) - 1); const internal::StringView sp(str, sizeof(str) - 1);
EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp)); EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
} }
#endif // GTEST_HAS_ABSL #endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests printing STL containers. // Tests printing STL containers.