100 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
103 #include <functional>
108 #include <type_traits>
115 #include "absl/strings/string_view.h"
116 #include "absl/types/optional.h"
117 #include "absl/types/variant.h"
124 namespace internal2 {
139 kConvertibleToStringView,
149 template <
typename T, TypeKind kTypeKind>
155 reinterpret_cast<const void*
>(&value)),
165 template <
typename T>
171 pretty_str =
"\n" + value.DebugString();
173 *os << (
"<" + pretty_str +
">");
177 template <
typename T>
194 template <
typename T>
195 class TypeWithoutFormatter<T, kConvertibleToStringView> {
202 static void PrintValue(
const T& value, ::std::ostream* os);
230 template <
typename Char,
typename CharTraits,
typename T>
231 ::std::basic_ostream<Char, CharTraits>&
operator<<(
232 ::std::basic_ostream<Char, CharTraits>& os,
const T& x) {
235 : std::is_convertible<
241 const T&, absl::string_view>::value
242 ? kConvertibleToStringView
258 template <
typename T>
309 template <
typename ToPr
int,
typename OtherOperand>
318 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
329 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
330 template <typename OtherOperand> \
331 class FormatForComparison<CharType*, OtherOperand> { \
333 static ::std::string Format(CharType* value) { \
334 return ::testing::PrintToString(static_cast<const void*>(value)); \
343 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
348 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
350 class FormatForComparison<CharType*, OtherStringType> { \
352 static ::std::string Format(CharType* value) { \
353 return ::testing::PrintToString(value); \
360 #if GTEST_HAS_GLOBAL_STRING
365 #if GTEST_HAS_GLOBAL_WSTRING
370 #if GTEST_HAS_STD_WSTRING
375 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
385 template <
typename T1,
typename T2>
387 const T1& value,
const T2& ) {
398 template <
typename T>
399 class UniversalPrinter;
401 template <
typename T>
414 template <
typename C>
416 const C& container, ::std::ostream* os) {
417 const size_t kMaxCount = 32;
420 for (
typename C::const_iterator it = container.begin();
421 it != container.end(); ++it, ++count) {
424 if (count == kMaxCount) {
447 template <
typename T>
449 T* p, ::std::ostream* os) {
459 template <
typename T>
461 T* p, ::std::ostream* os) {
468 *os << reinterpret_cast<const void*>(p);
474 template <
typename T>
476 const T& value, ::std::ostream* os) {
491 template <
typename T>
492 void PrintTo(
const T& value, ::std::ostream* os) {
514 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
517 : !std::is_pointer<T>::value
519 : std::is_function<
typename std::remove_pointer<T>::type>::value
532 inline void PrintTo(
char c, ::std::ostream* os) {
536 PrintTo(
static_cast<unsigned char>(c), os);
540 inline void PrintTo(
bool x, ::std::ostream* os) {
541 *os << (x ?
"true" :
"false");
555 inline void PrintTo(
char* s, ::std::ostream* os) {
556 PrintTo(ImplicitCast_<const char*>(s), os);
561 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
562 PrintTo(ImplicitCast_<const void*>(s), os);
564 inline void PrintTo(
signed char* s, ::std::ostream* os) {
565 PrintTo(ImplicitCast_<const void*>(s), os);
567 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
568 PrintTo(ImplicitCast_<const void*>(s), os);
570 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
571 PrintTo(ImplicitCast_<const void*>(s), os);
579 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
582 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
583 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
592 template <
typename T>
595 for (
size_t i = 1; i != count; i++) {
602 #if GTEST_HAS_GLOBAL_STRING
615 #if GTEST_HAS_GLOBAL_WSTRING
618 PrintWideStringTo(s, os);
622 #if GTEST_HAS_STD_WSTRING
625 PrintWideStringTo(s, os);
631 inline void PrintTo(absl::string_view sp, ::std::ostream* os) {
636 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
638 template <
typename T>
639 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
645 template <
typename T>
649 template <
typename T,
size_t I>
651 ::std::ostream* os) {
652 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
659 std::get<I - 1>(t), os);
662 template <
typename... Types>
663 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
665 PrintTupleTo(t, std::integral_constant<
size_t,
sizeof...(Types)>(), os);
670 template <
typename T1,
typename T2>
671 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
683 template <
typename T>
693 static
void Print(const T& value, ::std::ostream* os) {
712 template <
typename T>
713 class UniversalPrinter<::absl::optional<T>> {
715 static void Print(const ::absl::optional<T>& value, ::std::ostream* os) {
728 template <
typename... T>
729 class UniversalPrinter<::absl::variant<T...>> {
731 static void Print(const ::absl::variant<T...>& value, ::std::ostream* os) {
733 absl::visit(Visitor{os}, value);
739 template <
typename U>
740 void operator()(
const U& u)
const {
741 *os <<
"'" << GetTypeName<U>() <<
"' with value ";
752 template <
typename T>
758 const size_t kThreshold = 18;
759 const size_t kChunkSize = 8;
763 if (len <= kThreshold) {
775 const char* begin,
size_t len, ::std::ostream* os);
779 const wchar_t* begin,
size_t len, ::std::ostream* os);
782 template <
typename T,
size_t N>
787 static void Print(
const T (&a)[N], ::std::ostream* os) {
793 template <
typename T>
800 static
void Print(const T& value, ::std::ostream* os) {
803 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
816 template <
typename T>
819 static void Print(
const T& value, ::std::ostream* os) {
823 template <
typename T>
826 static void Print(
const T& value, ::std::ostream* os) {
830 template <
typename T,
size_t N>
833 static void Print(
const T (&value)[N], ::std::ostream* os) {
840 static void Print(
const char* str, ::std::ostream* os) {
841 if (str ==
nullptr) {
851 static void Print(
char* str, ::std::ostream* os) {
856 #if GTEST_HAS_STD_WSTRING
858 class UniversalTersePrinter<const wchar_t*> {
860 static void Print(
const wchar_t* str, ::std::ostream* os) {
861 if (str ==
nullptr) {
873 static void Print(
wchar_t* str, ::std::ostream* os) {
878 template <
typename T>
887 template <
typename T>
895 typedef ::std::vector< ::std::string>
Strings;
899 template <
typename Tuple>
902 template <
typename Tuple,
size_t I>
904 std::integral_constant<size_t, I>,
908 ::std::stringstream ss;
910 strings->push_back(ss.str());
916 template <
typename Tuple>
920 value, std::integral_constant<
size_t, std::tuple_size<Tuple>::value>(),
928 namespace internal2 {
929 template <
typename T>
931 const T& value, ::std::ostream* os) {
937 template <
typename T>
939 ::std::stringstream ss;
static void Print(const T(&a)[N], ::std::ostream *os)
static void Print(const T &value, ::std::ostream *os)
static void Print(const T &value, ::std::ostream *os)
static void Print(const T(&value)[N], ::std::ostream *os)
static void Print(char *str, ::std::ostream *os)
static void Print(const char *str, ::std::ostream *os)
static void Print(wchar_t *str, ::std::ostream *os)
static void Print(const T &value, ::std::ostream *os)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
#define GTEST_INTENTIONAL_CONST_COND_POP_()
::std::basic_ostream< Char, CharTraits > & operator<<(::std::basic_ostream< Char, CharTraits > &os, const T &x)
const size_t kProtobufOneLinerMaxLength
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count, ::std::ostream *os)
void DefaultPrintTo(WrapPrinterType< kPrintContainer >, const C &container, ::std::ostream *os)
void UniversalPrint(const T &value, ::std::ostream *os)
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char)
::std::vector< ::std::string > Strings
void TersePrintPrefixToStrings(const Tuple &, std::integral_constant< size_t, 0 >, Strings *)
void PrintTupleTo(const T &, std::integral_constant< size_t, 0 >, ::std::ostream *)
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
GTEST_API_ void PrintStringTo(const ::std::string &s, ::std::ostream *os)
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
void PrintRawArrayTo(const T a[], size_t count, ::std::ostream *os)
void UniversalPrintArray(const T *begin, size_t len, ::std::ostream *os)
void PrintTo(const T &value, ::std::ostream *os)
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string)
void UniversalTersePrint(const T &value, ::std::ostream *os)
void DefaultPrintNonContainerTo(const T &value, ::std::ostream *os)
::std::string PrintToString(const T &value)