Set GTEST_INTERNAL_HAS_STRING_VIEW = 1 in googletest as C++17 always supports it

PiperOrigin-RevId: 959873168
Change-Id: I66fc09077383f307c98a62f67a57005ce3c96502
This commit is contained in:
Abseil Team
2026-08-05 14:31:52 -07:00
committed by Copybara-Service
parent b607d9f590
commit 4f472747b1
10 changed files with 30 additions and 96 deletions
@@ -963,7 +963,6 @@ class [[nodiscard]] StrEqualityMatcher {
expect_eq_(expect_eq),
case_sensitive_(case_sensitive) {}
#if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const {
// This should fail to compile if StringView is used with wide
@@ -971,7 +970,6 @@ class [[nodiscard]] StrEqualityMatcher {
const StringType& str = std::string(s);
return MatchAndExplain(str, listener);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
@@ -1031,7 +1029,6 @@ class [[nodiscard]] HasSubstrMatcher {
explicit HasSubstrMatcher(const StringType& substring)
: substring_(substring) {}
#if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const {
// This should fail to compile if StringView is used with wide
@@ -1039,7 +1036,6 @@ class [[nodiscard]] HasSubstrMatcher {
const StringType& str = std::string(s);
return MatchAndExplain(str, listener);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
@@ -1084,7 +1080,6 @@ class [[nodiscard]] StartsWithMatcher {
public:
explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {}
#if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const {
// This should fail to compile if StringView is used with wide
@@ -1092,7 +1087,6 @@ class [[nodiscard]] StartsWithMatcher {
const StringType& str = std::string(s);
return MatchAndExplain(str, listener);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
@@ -1138,7 +1132,6 @@ class [[nodiscard]] EndsWithMatcher {
public:
explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
#if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const {
// This should fail to compile if StringView is used with wide
@@ -1146,7 +1139,6 @@ class [[nodiscard]] EndsWithMatcher {
const StringType& str = std::string(s);
return MatchAndExplain(str, listener);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
@@ -35,6 +35,7 @@
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
@@ -42,10 +43,6 @@
#include "test/gmock-matchers_test.h"
#include "gtest/gtest.h"
#if GTEST_INTERNAL_HAS_STRING_VIEW
#include <string_view>
#endif
// Silence warning C4244: 'initializing': conversion from 'int' to 'short',
// possible loss of data and C4100, unreferenced local parameter
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)
@@ -240,7 +237,6 @@ TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
EXPECT_FALSE(m2.Matches("hello"));
}
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that a C-string literal can be implicitly converted to a
// Matcher<StringView> or Matcher<const StringView&>.
TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
@@ -276,7 +272,6 @@ TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
EXPECT_TRUE(m2.Matches("cats"));
EXPECT_FALSE(m2.Matches("dogs"));
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// 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().
@@ -1265,18 +1260,13 @@ internal::StringType<T> ToString(T str) {
TEST(StringType, TestConversions) {
EXPECT_EQ("foo", ToString("foo"));
EXPECT_EQ("foo", ToString(std::string("foo")));
EXPECT_EQ("foo", ToString(internal::StringView("foo")));
#if GTEST_HAS_STD_WSTRING
EXPECT_EQ(L"foo", ToString(L"foo"));
EXPECT_EQ(L"foo", ToString(std::wstring(L"foo")));
#endif
#if GTEST_INTERNAL_HAS_STRING_VIEW
EXPECT_EQ("foo", ToString(internal::StringView("foo")));
#if GTEST_HAS_STD_WSTRING
EXPECT_EQ(L"foo", ToString(std::wstring_view(L"foo")));
#endif
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Non deducible types.
EXPECT_EQ("", ToString({}));
@@ -1295,7 +1285,6 @@ TEST(StrEqTest, MatchesEqualString) {
EXPECT_TRUE(m2.Matches("Hello"));
EXPECT_FALSE(m2.Matches("Hi"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView&> m3 =
StrEq(internal::StringView("Hello"));
EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
@@ -1306,7 +1295,6 @@ TEST(StrEqTest, MatchesEqualString) {
EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
EXPECT_TRUE(m_empty.Matches(internal::StringView()));
EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(StrEqTest, CanDescribeSelf) {
@@ -1333,12 +1321,10 @@ TEST(StrNeTest, MatchesUnequalString) {
EXPECT_TRUE(m2.Matches("hello"));
EXPECT_FALSE(m2.Matches("Hello"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));
EXPECT_TRUE(m3.Matches(internal::StringView("")));
EXPECT_TRUE(m3.Matches(internal::StringView()));
EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(StrNeTest, CanDescribeSelf) {
@@ -1357,14 +1343,12 @@ TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
EXPECT_TRUE(m2.Matches("hello"));
EXPECT_FALSE(m2.Matches("Hi"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView&> m3 =
StrCaseEq(internal::StringView("Hello"));
EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
EXPECT_TRUE(m3.Matches(internal::StringView("hello")));
EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));
EXPECT_FALSE(m3.Matches(internal::StringView()));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
@@ -1408,14 +1392,12 @@ TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
EXPECT_TRUE(m2.Matches(""));
EXPECT_FALSE(m2.Matches("Hello"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView> m3 =
StrCaseNe(internal::StringView("Hello"));
EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));
EXPECT_TRUE(m3.Matches(internal::StringView()));
EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(StrCaseNeTest, CanDescribeSelf) {
@@ -1456,7 +1438,6 @@ TEST(HasSubstrTest, WorksForCStrings) {
EXPECT_FALSE(m_empty.Matches(nullptr));
}
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr() works for matching StringView-typed values.
TEST(HasSubstrTest, WorksForStringViewClasses) {
const Matcher<internal::StringView> m1 =
@@ -1475,7 +1456,6 @@ TEST(HasSubstrTest, WorksForStringViewClasses) {
EXPECT_TRUE(m3.Matches(internal::StringView("")));
EXPECT_TRUE(m3.Matches(internal::StringView()));
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr(s) describes itself properly.
TEST(HasSubstrTest, CanDescribeSelf) {
@@ -1874,13 +1854,11 @@ TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
EXPECT_FALSE(m2.Matches("H"));
EXPECT_FALSE(m2.Matches(" Hi"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<internal::StringView> m_empty =
StartsWith(internal::StringView(""));
EXPECT_TRUE(m_empty.Matches(internal::StringView()));
EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(StartsWithTest, CanDescribeSelf) {
@@ -1889,12 +1867,8 @@ TEST(StartsWithTest, CanDescribeSelf) {
}
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).
@@ -1912,14 +1886,12 @@ TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
EXPECT_FALSE(m2.Matches("i"));
EXPECT_FALSE(m2.Matches("Hi "));
#if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const internal::StringView&> m4 =
EndsWith(internal::StringView(""));
EXPECT_TRUE(m4.Matches("Hi"));
EXPECT_TRUE(m4.Matches(""));
EXPECT_TRUE(m4.Matches(internal::StringView()));
EXPECT_TRUE(m4.Matches(internal::StringView("")));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(EndsWithTest, CanDescribeSelf) {
@@ -1942,14 +1914,12 @@ TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!
EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf!
#if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const internal::StringView&> m3 =
WhenBase64Unescaped(EndsWith("!"));
EXPECT_FALSE(m3.Matches("invalid base64"));
EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world
EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!
EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf!
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
@@ -1970,7 +1940,6 @@ TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
EXPECT_FALSE(m2.Matches("az1"));
EXPECT_FALSE(m2.Matches("1az"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");
EXPECT_TRUE(m3.Matches(internal::StringView("az")));
EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));
@@ -1980,7 +1949,6 @@ TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
MatchesRegex(internal::StringView(""));
EXPECT_TRUE(m4.Matches(internal::StringView("")));
EXPECT_TRUE(m4.Matches(internal::StringView()));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(MatchesRegexTest, CanDescribeSelf) {
@@ -1990,10 +1958,8 @@ TEST(MatchesRegexTest, CanDescribeSelf) {
Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));
EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
// Tests ContainsRegex().
@@ -2009,7 +1975,6 @@ TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
EXPECT_TRUE(m2.Matches("az1"));
EXPECT_FALSE(m2.Matches("1a"));
#if GTEST_INTERNAL_HAS_STRING_VIEW
const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z"));
EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));
EXPECT_TRUE(m3.Matches(internal::StringView("az1")));
@@ -2019,7 +1984,6 @@ TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
ContainsRegex(internal::StringView(""));
EXPECT_TRUE(m4.Matches(internal::StringView("")));
EXPECT_TRUE(m4.Matches(internal::StringView()));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST(ContainsRegexTest, CanDescribeSelf) {
@@ -2029,10 +1993,8 @@ TEST(ContainsRegexTest, CanDescribeSelf) {
Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
#if GTEST_INTERNAL_HAS_STRING_VIEW
Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));
EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
// Tests for wide strings.
+4 -8
View File
@@ -544,9 +544,8 @@ Matcher<std::string> : public internal::MatcherBase<std::string> {
Matcher(const char* s); // NOLINT
};
#if GTEST_INTERNAL_HAS_STRING_VIEW
// 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 std::string_view
// matcher is expected.
template <>
class GTEST_API_ [[nodiscard]] Matcher<const internal::StringView&>
@@ -570,7 +569,7 @@ class GTEST_API_ [[nodiscard]] Matcher<const internal::StringView&>
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher(const char* s); // NOLINT
// Allows the user to pass absl::string_views or std::string_views directly.
// Allows the user to pass std::string_views directly.
Matcher(internal::StringView s); // NOLINT
};
@@ -597,10 +596,9 @@ class GTEST_API_ [[nodiscard]] Matcher<internal::StringView>
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher(const char* s); // NOLINT
// Allows the user to pass absl::string_views or std::string_views directly.
// Allows the user to pass std::string_views directly.
Matcher(internal::StringView s); // NOLINT
};
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Prints a matcher in a human-readable format.
template <typename T>
@@ -842,12 +840,10 @@ class [[nodiscard]] MatchesRegexMatcher {
MatchesRegexMatcher(const RE* regex, bool full_match)
: regex_(regex), full_match_(full_match) {}
#if GTEST_INTERNAL_HAS_STRING_VIEW
bool MatchAndExplain(const internal::StringView& s,
MatchResultListener* listener) const {
return MatchAndExplain(std::string(s), listener);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
@@ -862,7 +858,7 @@ class [[nodiscard]] MatchesRegexMatcher {
// Matches anything that can convert to std::string.
//
// This is a template, not just a plain function with const std::string&,
// because absl::string_view has some interfering non-explicit constructors.
// because std::string_view has some interfering non-explicit constructors.
template <class MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* /* listener */) const {
+4 -8
View File
@@ -291,11 +291,9 @@ struct ConvertibleToIntegerPrinter {
};
struct ConvertibleToStringViewPrinter {
#if GTEST_INTERNAL_HAS_STRING_VIEW
static void PrintValue(internal::StringView value, ::std::ostream* os) {
internal::UniversalPrint(value, os);
}
#endif
};
#ifdef GTEST_HAS_ABSL
@@ -703,12 +701,12 @@ void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
}
}
// Overloads for ::std::string and ::std::string_view
GTEST_API_ void PrintStringTo(::std::string_view s, ::std::ostream* os);
// Overloads for ::std::string and std::string_view
GTEST_API_ void PrintStringTo(std::string_view s, ::std::ostream* os);
inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
PrintStringTo(s, os);
}
inline void PrintTo(::std::string_view s, ::std::ostream* os) {
inline void PrintTo(std::string_view s, ::std::ostream* os) {
PrintStringTo(s, os);
}
@@ -752,16 +750,14 @@ inline void PrintTo(::std::wstring_view s, ::std::ostream* os) {
}
#endif // GTEST_HAS_STD_WSTRING
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Overload for internal::StringView. Needed for build configurations where
// internal::StringView is an alias for absl::string_view, but absl::string_view
// is a distinct type from std::string_view.
template <int&... ExplicitArgumentBarrier, typename T = internal::StringView,
std::enable_if_t<!std::is_same_v<T, ::std::string_view>, int> = 0>
std::enable_if_t<!std::is_same_v<T, std::string_view>, int> = 0>
inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
PrintStringTo(sp, os);
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
@@ -43,6 +43,7 @@
#include <memory>
#include <string>
#include <string_view>
#include "gtest/gtest-matchers.h"
#include "gtest/internal/gtest-internal.h"
@@ -63,6 +64,10 @@ inline Matcher<const ::std::string&> MakeDeathTestMatcher(
::testing::internal::RE regex) {
return ContainsRegex(regex.pattern());
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(
std::string_view regex) {
return ContainsRegex(regex);
}
inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
return ContainsRegex(regex);
}
+10 -21
View File
@@ -293,9 +293,10 @@
#include <limits>
#include <locale>
#include <memory>
// #include <mutex> // Guarded by GTEST_IS_THREADSAFE below
#include <ostream>
#include <string>
// #include <mutex> // Guarded by GTEST_IS_THREADSAFE below
#include <string_view>
#include <tuple>
#include <type_traits>
#include <vector>
@@ -949,21 +950,21 @@ GTEST_API_ bool IsTrue(bool condition);
#ifdef GTEST_USES_RE2
// This is almost `using RE = ::RE2`, except it is copy-constructible, and it
// needs to disambiguate the `std::string`, `absl::string_view`, and `const
// needs to disambiguate the `std::string`, `std::string_view`, and `const
// char*` constructors.
class GTEST_API_ [[nodiscard]] RE {
public:
RE(absl::string_view regex) : regex_(regex) {} // NOLINT
RE(const char* regex) : RE(absl::string_view(regex)) {} // NOLINT
RE(const std::string& regex) : RE(absl::string_view(regex)) {} // NOLINT
RE(std::string_view regex) : regex_(regex) {} // NOLINT
RE(const char* regex) : RE(std::string_view(regex)) {} // NOLINT
RE(const std::string& regex) : RE(std::string_view(regex)) {} // NOLINT
RE(const RE& other) : RE(other.pattern()) {}
const std::string& pattern() const { return regex_.pattern(); }
static bool FullMatch(absl::string_view str, const RE& re) {
static bool FullMatch(std::string_view str, const RE& re) {
return RE2::FullMatch(str, re.regex_);
}
static bool PartialMatch(absl::string_view str, const RE& re) {
static bool PartialMatch(std::string_view str, const RE& re) {
return RE2::PartialMatch(str, re.regex_);
}
@@ -2396,7 +2397,6 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
#ifdef 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 {
@@ -2404,26 +2404,15 @@ using StringView = ::absl::string_view;
} // namespace internal
} // namespace testing
#else
#if defined(__cpp_lib_string_view) || \
(GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \
GTEST_INTERNAL_CPLUSPLUS_LANG >= 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;
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 // __cpp_lib_string_view
#endif // GTEST_HAS_ABSL
#ifndef GTEST_INTERNAL_HAS_STRING_VIEW
#define GTEST_INTERNAL_HAS_STRING_VIEW 0
#endif
#define GTEST_INTERNAL_HAS_STRING_VIEW 1
#if defined(__cpp_lib_three_way_comparison)
#define GTEST_INTERNAL_HAS_COMPARE_LIB 1
-2
View File
@@ -59,7 +59,6 @@ Matcher<std::string>::Matcher(const std::string& s) { *this = Eq(s); }
// s.
Matcher<std::string>::Matcher(const char* s) { *this = Eq(std::string(s)); }
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Constructs a matcher that matches a const StringView& whose value is
// equal to s.
Matcher<const internal::StringView&>::Matcher(const std::string& s) {
@@ -93,6 +92,5 @@ Matcher<internal::StringView>::Matcher(const char* s) {
Matcher<internal::StringView>::Matcher(internal::StringView s) {
*this = Eq(std::string(s));
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
} // namespace testing
+2 -2
View File
@@ -515,13 +515,13 @@ bool IsValidUTF8(const char* str, size_t length) {
void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {
if (!ContainsUnprintableControlCodes(str, length) &&
IsValidUTF8(str, length)) {
*os << "\n As Text: \"" << ::std::string_view(str, length) << "\"";
*os << "\n As Text: \"" << std::string_view(str, length) << "\"";
}
}
} // anonymous namespace
void PrintStringTo(::std::string_view s, ostream* os) {
void PrintStringTo(std::string_view s, ostream* os) {
if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
if (GTEST_FLAG_GET(print_utf8)) {
ConditionalPrintAsText(s.data(), s.size(), os);
+1 -1
View File
@@ -6930,7 +6930,7 @@ void ParseGoogleTestFlagsOnly(int* argc, char** argv) {
std::vector<char*> positional_args;
std::vector<absl::UnrecognizedFlag> unrecognized_flags;
absl::ParseAbseilFlagsOnly(*argc, argv, positional_args, unrecognized_flags);
absl::flat_hash_set<absl::string_view> unrecognized;
absl::flat_hash_set<std::string_view> unrecognized;
for (const auto& flag : unrecognized_flags) {
unrecognized.insert(flag.flag_name);
}
+2 -6
View File
@@ -940,7 +940,7 @@ TEST(PrintStringTest, StringInStdNamespace) {
TEST(PrintStringTest, StringViewInStdNamespace) {
const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
const ::std::string_view str(s, sizeof(s));
const std::string_view str(s, sizeof(s));
EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
Print(str));
}
@@ -1091,8 +1091,6 @@ TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
}
#if GTEST_INTERNAL_HAS_STRING_VIEW
// Tests printing internal::StringView.
TEST(PrintStringViewTest, SimpleStringView) {
@@ -1106,8 +1104,6 @@ TEST(PrintStringViewTest, UnprintableCharacters) {
EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
}
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
// Tests printing STL containers.
TEST(PrintStlContainerTest, EmptyDeque) {
@@ -1940,7 +1936,7 @@ TEST(UniversalPrintTest, StringViewNonZeroTerminated) {
// `strlen` instead of `str.size()`, it will include 'X' and cause a visible
// difference (in addition to ASAN tests detecting a buffer overflow due to
// the missing 0 at the end).
const ::std::string_view str(s, 3);
const std::string_view str(s, 3);
::std::stringstream ss;
UniversalPrint(str, &ss);
EXPECT_EQ("\"\\xEF\\xA3\\xA2\"\n As Text: \"\xEF\xA3\xA2\"", ss.str());