Googletest export
Treat default-constructed string_view same as constructed from "". In the context of string comparison (e.g. HasSubstr, StartsWith, EndsWith, etc.), a default-constructed string_view (nullptr) should be semantically same as a empty string "". PiperOrigin-RevId: 212816839
This commit is contained in:
		
				
					committed by
					
						
						Gennadiy Civil
					
				
			
			
				
	
			
			
			
						parent
						
							cfe0ae8678
						
					
				
				
					commit
					ffc9baeb4c
				
			@@ -1307,9 +1307,6 @@ class StrEqualityMatcher {
 | 
				
			|||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
  bool MatchAndExplain(const absl::string_view& s,
 | 
					  bool MatchAndExplain(const absl::string_view& s,
 | 
				
			||||||
                       MatchResultListener* listener) const {
 | 
					                       MatchResultListener* listener) const {
 | 
				
			||||||
    if (s.data() == NULL) {
 | 
					 | 
				
			||||||
      return !expect_eq_;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    // This should fail to compile if absl::string_view is used with wide
 | 
					    // This should fail to compile if absl::string_view is used with wide
 | 
				
			||||||
    // strings.
 | 
					    // strings.
 | 
				
			||||||
    const StringType& str = string(s);
 | 
					    const StringType& str = string(s);
 | 
				
			||||||
@@ -1380,9 +1377,6 @@ class HasSubstrMatcher {
 | 
				
			|||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
  bool MatchAndExplain(const absl::string_view& s,
 | 
					  bool MatchAndExplain(const absl::string_view& s,
 | 
				
			||||||
                       MatchResultListener* listener) const {
 | 
					                       MatchResultListener* listener) const {
 | 
				
			||||||
    if (s.data() == NULL) {
 | 
					 | 
				
			||||||
      return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    // This should fail to compile if absl::string_view is used with wide
 | 
					    // This should fail to compile if absl::string_view is used with wide
 | 
				
			||||||
    // strings.
 | 
					    // strings.
 | 
				
			||||||
    const StringType& str = string(s);
 | 
					    const StringType& str = string(s);
 | 
				
			||||||
@@ -1440,9 +1434,6 @@ class StartsWithMatcher {
 | 
				
			|||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
  bool MatchAndExplain(const absl::string_view& s,
 | 
					  bool MatchAndExplain(const absl::string_view& s,
 | 
				
			||||||
                       MatchResultListener* listener) const {
 | 
					                       MatchResultListener* listener) const {
 | 
				
			||||||
    if (s.data() == NULL) {
 | 
					 | 
				
			||||||
      return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    // This should fail to compile if absl::string_view is used with wide
 | 
					    // This should fail to compile if absl::string_view is used with wide
 | 
				
			||||||
    // strings.
 | 
					    // strings.
 | 
				
			||||||
    const StringType& str = string(s);
 | 
					    const StringType& str = string(s);
 | 
				
			||||||
@@ -1499,9 +1490,6 @@ class EndsWithMatcher {
 | 
				
			|||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
  bool MatchAndExplain(const absl::string_view& s,
 | 
					  bool MatchAndExplain(const absl::string_view& s,
 | 
				
			||||||
                       MatchResultListener* listener) const {
 | 
					                       MatchResultListener* listener) const {
 | 
				
			||||||
    if (s.data() == NULL) {
 | 
					 | 
				
			||||||
      return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    // This should fail to compile if absl::string_view is used with wide
 | 
					    // This should fail to compile if absl::string_view is used with wide
 | 
				
			||||||
    // strings.
 | 
					    // strings.
 | 
				
			||||||
    const StringType& str = string(s);
 | 
					    const StringType& str = string(s);
 | 
				
			||||||
@@ -1558,7 +1546,7 @@ class MatchesRegexMatcher {
 | 
				
			|||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
  bool MatchAndExplain(const absl::string_view& s,
 | 
					  bool MatchAndExplain(const absl::string_view& s,
 | 
				
			||||||
                       MatchResultListener* listener) const {
 | 
					                       MatchResultListener* listener) const {
 | 
				
			||||||
    return s.data() && MatchAndExplain(string(s), listener);
 | 
					    return MatchAndExplain(string(s), listener);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1335,6 +1335,11 @@ TEST(StrEqTest, MatchesEqualString) {
 | 
				
			|||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("Hello")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("Hello")));
 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view("hello")));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view("hello")));
 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Matcher<const absl::string_view&> m_empty = StrEq("");
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(absl::string_view("")));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(absl::string_view()));
 | 
				
			||||||
 | 
					  EXPECT_FALSE(m_empty.Matches(absl::string_view("hello")));
 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1459,6 +1464,10 @@ TEST(HasSubstrTest, WorksForStringClasses) {
 | 
				
			|||||||
  const Matcher<const std::string&> m2 = HasSubstr("foo");
 | 
					  const Matcher<const std::string&> m2 = HasSubstr("foo");
 | 
				
			||||||
  EXPECT_TRUE(m2.Matches(std::string("I love food.")));
 | 
					  EXPECT_TRUE(m2.Matches(std::string("I love food.")));
 | 
				
			||||||
  EXPECT_FALSE(m2.Matches(std::string("tofo")));
 | 
					  EXPECT_FALSE(m2.Matches(std::string("tofo")));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const Matcher<std::string> m_empty = HasSubstr("");
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(std::string()));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(std::string("not empty")));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Tests that HasSubstr() works for matching C-string-typed values.
 | 
					// Tests that HasSubstr() works for matching C-string-typed values.
 | 
				
			||||||
@@ -1472,6 +1481,11 @@ TEST(HasSubstrTest, WorksForCStrings) {
 | 
				
			|||||||
  EXPECT_TRUE(m2.Matches("I love food."));
 | 
					  EXPECT_TRUE(m2.Matches("I love food."));
 | 
				
			||||||
  EXPECT_FALSE(m2.Matches("tofo"));
 | 
					  EXPECT_FALSE(m2.Matches("tofo"));
 | 
				
			||||||
  EXPECT_FALSE(m2.Matches(NULL));
 | 
					  EXPECT_FALSE(m2.Matches(NULL));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const Matcher<const char*> m_empty = HasSubstr("");
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches("not empty"));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(""));
 | 
				
			||||||
 | 
					  EXPECT_FALSE(m_empty.Matches(NULL));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if GTEST_HAS_ABSL
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
@@ -1489,7 +1503,8 @@ TEST(HasSubstrTest, WorksForStringViewClasses) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  const Matcher<const absl::string_view&> m3 = HasSubstr("");
 | 
					  const Matcher<const absl::string_view&> m3 = HasSubstr("");
 | 
				
			||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("foo")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("foo")));
 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("")));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view()));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1713,6 +1728,13 @@ TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
 | 
				
			|||||||
  EXPECT_TRUE(m2.Matches("High"));
 | 
					  EXPECT_TRUE(m2.Matches("High"));
 | 
				
			||||||
  EXPECT_FALSE(m2.Matches("H"));
 | 
					  EXPECT_FALSE(m2.Matches("H"));
 | 
				
			||||||
  EXPECT_FALSE(m2.Matches(" Hi"));
 | 
					  EXPECT_FALSE(m2.Matches(" Hi"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if GTEST_HAS_ABSL
 | 
				
			||||||
 | 
					  const Matcher<absl::string_view> m_empty = StartsWith("");
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(absl::string_view()));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(absl::string_view("")));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m_empty.Matches(absl::string_view("not empty")));
 | 
				
			||||||
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST(StartsWithTest, CanDescribeSelf) {
 | 
					TEST(StartsWithTest, CanDescribeSelf) {
 | 
				
			||||||
@@ -1748,9 +1770,8 @@ TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
 | 
				
			|||||||
  const Matcher<const absl::string_view&> m4 = EndsWith("");
 | 
					  const Matcher<const absl::string_view&> m4 = EndsWith("");
 | 
				
			||||||
  EXPECT_TRUE(m4.Matches("Hi"));
 | 
					  EXPECT_TRUE(m4.Matches("Hi"));
 | 
				
			||||||
  EXPECT_TRUE(m4.Matches(""));
 | 
					  EXPECT_TRUE(m4.Matches(""));
 | 
				
			||||||
  // Default-constructed absl::string_view should not match anything, in order
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view()));
 | 
				
			||||||
  // to distinguish it from an empty string.
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view("")));
 | 
				
			||||||
  EXPECT_FALSE(m4.Matches(absl::string_view()));
 | 
					 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1777,11 +1798,10 @@ TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
 | 
				
			|||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("az")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("az")));
 | 
				
			||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("abcz")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("abcz")));
 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view("1az")));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view("1az")));
 | 
				
			||||||
  // Default-constructed absl::string_view should not match anything, in order
 | 
					 | 
				
			||||||
  // to distinguish it from an empty string.
 | 
					 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
				
			||||||
  const Matcher<const absl::string_view&> m4 = MatchesRegex("");
 | 
					  const Matcher<const absl::string_view&> m4 = MatchesRegex("");
 | 
				
			||||||
  EXPECT_FALSE(m4.Matches(absl::string_view()));
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view("")));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view()));
 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1816,11 +1836,10 @@ TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
 | 
				
			|||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("azbz")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("azbz")));
 | 
				
			||||||
  EXPECT_TRUE(m3.Matches(absl::string_view("az1")));
 | 
					  EXPECT_TRUE(m3.Matches(absl::string_view("az1")));
 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view("1a")));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view("1a")));
 | 
				
			||||||
  // Default-constructed absl::string_view should not match anything, in order
 | 
					 | 
				
			||||||
  // to distinguish it from an empty string.
 | 
					 | 
				
			||||||
  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
					  EXPECT_FALSE(m3.Matches(absl::string_view()));
 | 
				
			||||||
  const Matcher<const absl::string_view&> m4 = ContainsRegex("");
 | 
					  const Matcher<const absl::string_view&> m4 = ContainsRegex("");
 | 
				
			||||||
  EXPECT_FALSE(m4.Matches(absl::string_view()));
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view("")));
 | 
				
			||||||
 | 
					  EXPECT_TRUE(m4.Matches(absl::string_view()));
 | 
				
			||||||
#endif  // GTEST_HAS_ABSL
 | 
					#endif  // GTEST_HAS_ABSL
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user