diff options
Diffstat (limited to 'googletest/include/gtest')
-rw-r--r-- | googletest/include/gtest/gtest-matchers.h | 38 | ||||
-rw-r--r-- | googletest/include/gtest/gtest-printers.h | 25 | ||||
-rw-r--r-- | googletest/include/gtest/internal/gtest-port.h | 31 |
3 files changed, 63 insertions, 31 deletions
diff --git a/googletest/include/gtest/gtest-matchers.h b/googletest/include/gtest/gtest-matchers.h index d9b28e0..a61cef4 100644 --- a/googletest/include/gtest/gtest-matchers.h +++ b/googletest/include/gtest/gtest-matchers.h @@ -384,18 +384,18 @@ class GTEST_API_ Matcher<std::string> 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 // instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view // matcher is expected. template <> -class GTEST_API_ Matcher<const absl::string_view&> - : public internal::MatcherBase<const absl::string_view&> { +class GTEST_API_ Matcher<const internal::StringView&> + : public internal::MatcherBase<const internal::StringView&> { public: Matcher() {} - explicit Matcher(const MatcherInterface<const absl::string_view&>* impl) - : internal::MatcherBase<const absl::string_view&>(impl) {} + explicit Matcher(const MatcherInterface<const internal::StringView&>* impl) + : internal::MatcherBase<const internal::StringView&>(impl) {} // Allows the user to write str instead of Eq(str) sometimes, where // 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. Matcher(const char* s); // NOLINT - // Allows the user to pass absl::string_views directly. - Matcher(absl::string_view s); // NOLINT + // Allows the user to pass absl::string_views or std::string_views directly. + Matcher(internal::StringView s); // NOLINT }; template <> -class GTEST_API_ Matcher<absl::string_view> - : public internal::MatcherBase<absl::string_view> { +class GTEST_API_ Matcher<internal::StringView> + : public internal::MatcherBase<internal::StringView> { public: Matcher() {} - explicit Matcher(const MatcherInterface<const absl::string_view&>* impl) - : internal::MatcherBase<absl::string_view>(impl) {} - explicit Matcher(const MatcherInterface<absl::string_view>* impl) - : internal::MatcherBase<absl::string_view>(impl) {} + explicit Matcher(const MatcherInterface<const internal::StringView&>* impl) + : internal::MatcherBase<internal::StringView>(impl) {} + explicit Matcher(const MatcherInterface<internal::StringView>* impl) + : internal::MatcherBase<internal::StringView>(impl) {} // Allows the user to write str instead of Eq(str) sometimes, where // 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. Matcher(const char* s); // NOLINT - // Allows the user to pass absl::string_views directly. - Matcher(absl::string_view s); // NOLINT + // Allows the user to pass absl::string_views or std::string_views directly. + Matcher(internal::StringView s); // NOLINT }; -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_STRING_VIEW // Prints a matcher in a human-readable format. template <typename T> @@ -620,12 +620,12 @@ class MatchesRegexMatcher { MatchesRegexMatcher(const RE* regex, bool full_match) : regex_(regex), full_match_(full_match) {} -#if GTEST_HAS_ABSL - bool MatchAndExplain(const absl::string_view& s, +#if GTEST_INTERNAL_HAS_STRING_VIEW + bool MatchAndExplain(const internal::StringView& s, MatchResultListener* listener) const { return MatchAndExplain(std::string(s), listener); } -#endif // GTEST_HAS_ABSL +#endif // GTEST_INTERNAL_HAS_STRING_VIEW // Accepts pointer types, particularly: // const char* diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index c443625..407d1f1 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -135,9 +135,9 @@ enum TypeKind { kProtobuf, // a protobuf type kConvertibleToInteger, // a type implicitly convertible to BiggestInt // (e.g. a named or unnamed enum type) -#if GTEST_HAS_ABSL +#if GTEST_INTERNAL_HAS_STRING_VIEW kConvertibleToStringView, // a type implicitly convertible to - // absl::string_view + // absl::string_view or std::string_view #endif kOtherType // anything else }; @@ -191,12 +191,13 @@ class TypeWithoutFormatter<T, kConvertibleToInteger> { } }; -#if GTEST_HAS_ABSL +#if GTEST_INTERNAL_HAS_STRING_VIEW template <typename T> class TypeWithoutFormatter<T, kConvertibleToStringView> { public: // 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 // 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 ? kConvertibleToInteger : -#if GTEST_HAS_ABSL +#if GTEST_INTERNAL_HAS_STRING_VIEW std::is_convertible< - const T&, absl::string_view>::value + const T&, internal::StringView>::value ? kConvertibleToStringView : #endif @@ -601,12 +602,12 @@ inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { } #endif // GTEST_HAS_STD_WSTRING -#if GTEST_HAS_ABSL -// Overload for absl::string_view. -inline void PrintTo(absl::string_view sp, ::std::ostream* os) { +#if GTEST_INTERNAL_HAS_STRING_VIEW +// Overload for internal::StringView. +inline void PrintTo(internal::StringView sp, ::std::ostream* 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)"; } @@ -899,12 +900,12 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { } // namespace internal -#if GTEST_HAS_ABSL +#if GTEST_INTERNAL_HAS_STRING_VIEW namespace internal2 { template <typename T> void TypeWithoutFormatter<T, kConvertibleToStringView>::PrintValue( const T& value, ::std::ostream* os) { - internal::PrintTo(absl::string_view(value), os); + internal::PrintTo(internal::StringView(value), os); } } // namespace internal2 #endif diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 0543da5..60ff471 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -199,6 +199,9 @@ // suppressed (constant conditional). // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 // is suppressed. +// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or +// Matcher<absl::string_view> +// specializations. // // Synchronization: // Mutex, MutexLock, ThreadLocal, GetThreadCount() @@ -2220,4 +2223,32 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val); #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_ |