commit
ea31cb15f0
|
@ -104,6 +104,7 @@ cc_library(
|
||||||
deps = select ({
|
deps = select ({
|
||||||
":has_absl": [
|
":has_absl": [
|
||||||
"@com_google_absl//absl/types:optional",
|
"@com_google_absl//absl/types:optional",
|
||||||
|
"@com_google_absl//absl/strings"
|
||||||
],
|
],
|
||||||
"//conditions:default": [],
|
"//conditions:default": [],
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,3 +33,4 @@ set -e
|
||||||
|
|
||||||
bazel build --curses=no //...:all
|
bazel build --curses=no //...:all
|
||||||
bazel test --curses=no //...:all
|
bazel test --curses=no //...:all
|
||||||
|
bazel test --curses=no //...:all --define absl=1
|
||||||
|
|
|
@ -113,6 +113,7 @@
|
||||||
|
|
||||||
#if GTEST_HAS_ABSL
|
#if GTEST_HAS_ABSL
|
||||||
#include "absl/types/optional.h"
|
#include "absl/types/optional.h"
|
||||||
|
#include "absl/strings/string_view.h"
|
||||||
#endif // GTEST_HAS_ABSL
|
#endif // GTEST_HAS_ABSL
|
||||||
|
|
||||||
namespace testing {
|
namespace testing {
|
||||||
|
@ -133,7 +134,11 @@ 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)
|
||||||
kOtherType // anything else
|
#if GTEST_HAS_ABSL
|
||||||
|
kConvertibleToStringView, // a type implicitly convertible to
|
||||||
|
// absl::string_view
|
||||||
|
#endif
|
||||||
|
kOtherType // anything else
|
||||||
};
|
};
|
||||||
|
|
||||||
// TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
|
// TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
|
||||||
|
@ -146,7 +151,7 @@ class TypeWithoutFormatter {
|
||||||
// This default version is called when kTypeKind is kOtherType.
|
// This default version is called when kTypeKind is kOtherType.
|
||||||
static void PrintValue(const T& value, ::std::ostream* os) {
|
static void PrintValue(const T& value, ::std::ostream* os) {
|
||||||
PrintBytesInObjectTo(static_cast<const unsigned char*>(
|
PrintBytesInObjectTo(static_cast<const unsigned char*>(
|
||||||
reinterpret_cast<const void *>(&value)),
|
reinterpret_cast<const void*>(&value)),
|
||||||
sizeof(value), os);
|
sizeof(value), os);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -184,6 +189,19 @@ class TypeWithoutFormatter<T, kConvertibleToInteger> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if GTEST_HAS_ABSL
|
||||||
|
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.
|
||||||
|
//
|
||||||
|
// Note: the implementation is further below, as it depends on
|
||||||
|
// internal::PrintTo symbol which is defined later in the file.
|
||||||
|
static void PrintValue(const T& value, ::std::ostream* os);
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
// Prints the given value to the given ostream. If the value is a
|
// Prints the given value to the given ostream. If the value is a
|
||||||
// protocol message, its debug string is printed; if it's an enum or
|
// protocol message, its debug string is printed; if it's an enum or
|
||||||
// of a type implicitly convertible to BiggestInt, it's printed as an
|
// of a type implicitly convertible to BiggestInt, it's printed as an
|
||||||
|
@ -211,10 +229,19 @@ class TypeWithoutFormatter<T, kConvertibleToInteger> {
|
||||||
template <typename Char, typename CharTraits, typename T>
|
template <typename Char, typename CharTraits, typename T>
|
||||||
::std::basic_ostream<Char, CharTraits>& operator<<(
|
::std::basic_ostream<Char, CharTraits>& operator<<(
|
||||||
::std::basic_ostream<Char, CharTraits>& os, const T& x) {
|
::std::basic_ostream<Char, CharTraits>& os, const T& x) {
|
||||||
TypeWithoutFormatter<T,
|
TypeWithoutFormatter<T, (internal::IsAProtocolMessage<T>::value
|
||||||
(internal::IsAProtocolMessage<T>::value ? kProtobuf :
|
? kProtobuf
|
||||||
internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
|
: internal::ImplicitlyConvertible<
|
||||||
kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
|
const T&, internal::BiggestInt>::value
|
||||||
|
? kConvertibleToInteger
|
||||||
|
:
|
||||||
|
#if GTEST_HAS_ABSL
|
||||||
|
internal::ImplicitlyConvertible<
|
||||||
|
const T&, absl::string_view>::value
|
||||||
|
? kConvertibleToStringView
|
||||||
|
:
|
||||||
|
#endif
|
||||||
|
kOtherType)>::PrintValue(x, &os);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,7 +462,8 @@ void DefaultPrintTo(WrapPrinterType<kPrintFunctionPointer> /* dummy */,
|
||||||
*os << "NULL";
|
*os << "NULL";
|
||||||
} else {
|
} else {
|
||||||
// T is a function type, so '*os << p' doesn't do what we want
|
// T is a function type, so '*os << p' doesn't do what we want
|
||||||
// (it just prints p as bool). Cast p to const void* to print it.
|
// (it just prints p as bool). We want to print p as a const
|
||||||
|
// void*.
|
||||||
*os << reinterpret_cast<const void*>(p);
|
*os << reinterpret_cast<const void*>(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -464,17 +492,15 @@ void PrintTo(const T& value, ::std::ostream* os) {
|
||||||
// DefaultPrintTo() is overloaded. The type of its first argument
|
// DefaultPrintTo() is overloaded. The type of its first argument
|
||||||
// determines which version will be picked.
|
// determines which version will be picked.
|
||||||
//
|
//
|
||||||
// Note that we check for recursive and other container types here, prior
|
// Note that we check for container types here, prior to we check
|
||||||
// to we check for protocol message types in our operator<<. The rationale is:
|
// for protocol message types in our operator<<. The rationale is:
|
||||||
//
|
//
|
||||||
// For protocol messages, we want to give people a chance to
|
// For protocol messages, we want to give people a chance to
|
||||||
// override Google Mock's format by defining a PrintTo() or
|
// override Google Mock's format by defining a PrintTo() or
|
||||||
// operator<<. For STL containers, other formats can be
|
// operator<<. For STL containers, other formats can be
|
||||||
// incompatible with Google Mock's format for the container
|
// incompatible with Google Mock's format for the container
|
||||||
// elements; therefore we check for container types here to ensure
|
// elements; therefore we check for container types here to ensure
|
||||||
// that our format is used. To prevent an infinite runtime recursion
|
// that our format is used.
|
||||||
// during the output of recursive container types, we check first for
|
|
||||||
// those.
|
|
||||||
//
|
//
|
||||||
// Note that MSVC and clang-cl do allow an implicit conversion from
|
// Note that MSVC and clang-cl do allow an implicit conversion from
|
||||||
// pointer-to-function to pointer-to-object, but clang-cl warns on it.
|
// pointer-to-function to pointer-to-object, but clang-cl warns on it.
|
||||||
|
@ -492,8 +518,8 @@ void PrintTo(const T& value, ::std::ostream* os) {
|
||||||
#else
|
#else
|
||||||
: !internal::ImplicitlyConvertible<T, const void*>::value
|
: !internal::ImplicitlyConvertible<T, const void*>::value
|
||||||
#endif
|
#endif
|
||||||
? kPrintFunctionPointer
|
? kPrintFunctionPointer
|
||||||
: kPrintPointer>(),
|
: kPrintPointer>(),
|
||||||
value, os);
|
value, os);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -601,6 +627,13 @@ inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
|
||||||
}
|
}
|
||||||
#endif // GTEST_HAS_STD_WSTRING
|
#endif // GTEST_HAS_STD_WSTRING
|
||||||
|
|
||||||
|
#if GTEST_HAS_ABSL
|
||||||
|
// Overload for absl::string_view.
|
||||||
|
inline void PrintTo(absl::string_view sp, ::std::ostream* os) {
|
||||||
|
PrintTo(::std::string(sp), os);
|
||||||
|
}
|
||||||
|
#endif // GTEST_HAS_ABSL
|
||||||
|
|
||||||
#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
|
#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
|
||||||
// Helper function for printing a tuple. T must be instantiated with
|
// Helper function for printing a tuple. T must be instantiated with
|
||||||
// a tuple type.
|
// a tuple type.
|
||||||
|
@ -896,7 +929,7 @@ void UniversalPrint(const T& value, ::std::ostream* os) {
|
||||||
UniversalPrinter<T1>::Print(value, os);
|
UniversalPrinter<T1>::Print(value, os);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef ::std::vector<string> Strings;
|
typedef ::std::vector< ::std::string> Strings;
|
||||||
|
|
||||||
// TuplePolicy<TupleT> must provide:
|
// TuplePolicy<TupleT> must provide:
|
||||||
// - tuple_size
|
// - tuple_size
|
||||||
|
@ -1016,6 +1049,16 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
|
#if GTEST_HAS_ABSL
|
||||||
|
namespace internal2 {
|
||||||
|
template <typename T>
|
||||||
|
void TypeWithoutFormatter<T, kConvertibleToStringView>::PrintValue(
|
||||||
|
const T& value, ::std::ostream* os) {
|
||||||
|
internal::PrintTo(absl::string_view(value), os);
|
||||||
|
}
|
||||||
|
} // namespace internal2
|
||||||
|
#endif
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
::std::string PrintToString(const T& value) {
|
::std::string PrintToString(const T& value) {
|
||||||
::std::stringstream ss;
|
::std::stringstream ss;
|
||||||
|
|
|
@ -837,22 +837,22 @@ TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
|
||||||
EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
|
EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GTEST_HAS_STRING_PIECE_
|
#if GTEST_HAS_ABSL
|
||||||
|
|
||||||
// Tests printing StringPiece.
|
// Tests printing ::absl::string_view.
|
||||||
|
|
||||||
TEST(PrintStringPieceTest, SimpleStringPiece) {
|
TEST(PrintStringViewTest, SimpleStringView) {
|
||||||
const StringPiece sp = "Hello";
|
const ::absl::string_view sp = "Hello";
|
||||||
EXPECT_EQ("\"Hello\"", Print(sp));
|
EXPECT_EQ("\"Hello\"", Print(sp));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrintStringPieceTest, UnprintableCharacters) {
|
TEST(PrintStringViewTest, UnprintableCharacters) {
|
||||||
const char str[] = "NUL (\0) and \r\t";
|
const char str[] = "NUL (\0) and \r\t";
|
||||||
const StringPiece sp(str, sizeof(str) - 1);
|
const ::absl::string_view 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_STRING_PIECE_
|
#endif // GTEST_HAS_ABSL
|
||||||
|
|
||||||
// Tests printing STL containers.
|
// Tests printing STL containers.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user