100 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ 101 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ 103 #include <functional> 109 #include <type_traits> 113 #include "gtest/internal/gtest-internal.h" 114 #include "gtest/internal/gtest-port.h" 122 template <
typename T>
123 void UniversalPrint(
const T& value, ::std::ostream* os);
127 struct ContainerPrinter {
128 template <
typename T,
129 typename =
typename std::enable_if<
130 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(IsContainer)) &&
131 !IsRecursiveContainer<T>::value>::type>
132 static void PrintValue(
const T& container, std::ostream* os) {
133 const size_t kMaxCount = 32;
136 for (
auto&& elem : container) {
139 if (count == kMaxCount) {
147 internal::UniversalPrint(elem, os);
164 struct FunctionPointerPrinter {
165 template <
typename T,
typename =
typename std::enable_if<
166 std::is_function<T>::value>::type>
167 static void PrintValue(T* p, ::std::ostream* os) {
174 *os << reinterpret_cast<const void*>(p);
179 struct PointerPrinter {
180 template <
typename T>
181 static void PrintValue(T* p, ::std::ostream* os) {
193 namespace internal_stream_operator_without_lexical_name_lookup {
199 struct LookupBlocker {};
200 void operator<<(LookupBlocker, LookupBlocker);
202 struct StreamPrinter {
203 template <
typename T,
206 typename =
typename std::enable_if<
207 !std::is_member_pointer<T>::value>::type,
210 typename = decltype(std::declval<std::ostream&>()
211 << std::declval<const T&>())>
212 static void PrintValue(
const T& value, ::std::ostream* os) {
221 struct ProtobufPrinter {
225 static const size_t kProtobufOneLinerMaxLength = 50;
227 template <
typename T,
228 typename =
typename std::enable_if<
229 internal::HasDebugStringAndShortDebugString<T>::value>::type>
230 static void PrintValue(
const T& value, ::std::ostream* os) {
231 std::string pretty_str = value.ShortDebugString();
232 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
233 pretty_str =
"\n" + value.DebugString();
235 *os << (
"<" + pretty_str +
">");
239 struct ConvertibleToIntegerPrinter {
247 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
252 struct ConvertibleToStringViewPrinter {
253 #if GTEST_INTERNAL_HAS_STRING_VIEW 254 static void PrintValue(internal::StringView value, ::std::ostream* os) {
255 internal::UniversalPrint(value, os);
263 GTEST_API_
void PrintBytesInObjectTo(
const unsigned char* obj_bytes,
266 struct RawBytesPrinter {
268 template <
typename T,
size_t = sizeof(T)>
269 static void PrintValue(
const T& value, ::std::ostream* os) {
270 PrintBytesInObjectTo(
271 static_cast<const unsigned char*>(
273 reinterpret_cast<const void*>(std::addressof(value))),
278 struct FallbackPrinter {
279 template <
typename T>
280 static void PrintValue(
const T&, ::std::ostream* os) {
281 *os <<
"(incomplete type)";
286 template <
typename T,
typename E,
typename Printer,
typename... Printers>
287 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
289 template <
typename T,
typename Printer,
typename... Printers>
290 struct FindFirstPrinter<
291 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
292 Printer, Printers...> {
293 using type = Printer;
305 template <
typename T>
306 void PrintWithFallback(
const T& value, ::std::ostream* os) {
307 using Printer =
typename FindFirstPrinter<
308 T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
309 internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
310 ProtobufPrinter, ConvertibleToIntegerPrinter,
311 ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type;
312 Printer::PrintValue(value, os);
330 template <
typename ToPr
int,
typename OtherOperand>
331 class FormatForComparison {
333 static ::std::string Format(
const ToPrint& value) {
334 return ::testing::PrintToString(value);
339 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
340 class FormatForComparison<ToPrint[N], OtherOperand> {
342 static ::std::string Format(
const ToPrint* value) {
343 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
350 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ 351 template <typename OtherOperand> \ 352 class FormatForComparison<CharType*, OtherOperand> { \ 354 static ::std::string Format(CharType* value) { \ 355 return ::testing::PrintToString(static_cast<const void*>(value)); \ 359 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
char);
360 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char);
361 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
wchar_t);
362 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const wchar_t);
364 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
365 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char8_t);
367 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
368 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char16_t);
369 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
370 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(
const char32_t);
372 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ 377 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ 379 class FormatForComparison<CharType*, OtherStringType> { \ 381 static ::std::string Format(CharType* value) { \ 382 return ::testing::PrintToString(value); \ 386 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
char, ::std::string);
387 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char, ::std::string);
389 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
390 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char8_t, ::std::u8string);
392 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
393 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char16_t, ::std::u16string);
394 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
395 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const char32_t, ::std::u32string);
397 #if GTEST_HAS_STD_WSTRING 398 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
wchar_t, ::std::wstring);
399 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(
const wchar_t, ::std::wstring);
402 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ 412 template <
typename T1,
typename T2>
413 std::string FormatForComparisonFailureMessage(
414 const T1& value,
const T2& ) {
415 return FormatForComparison<T1, T2>::Format(value);
425 template <
typename T>
426 class UniversalPrinter;
439 template <
typename T>
440 void PrintTo(
const T& value, ::std::ostream* os) {
441 internal::PrintWithFallback(value, os);
449 GTEST_API_
void PrintTo(
unsigned char c, ::std::ostream* os);
450 GTEST_API_
void PrintTo(
signed char c, ::std::ostream* os);
451 inline void PrintTo(
char c, ::std::ostream* os) {
455 PrintTo(static_cast<unsigned char>(c), os);
459 inline void PrintTo(
bool x, ::std::ostream* os) {
460 *os << (x ?
"true" :
"false");
470 GTEST_API_
void PrintTo(
wchar_t wc, ::std::ostream* os);
472 GTEST_API_
void PrintTo(char32_t c, ::std::ostream* os);
473 inline void PrintTo(char16_t c, ::std::ostream* os) {
474 PrintTo(ImplicitCast_<char32_t>(c), os);
477 inline void PrintTo(char8_t c, ::std::ostream* os) {
478 PrintTo(ImplicitCast_<char32_t>(c), os);
483 GTEST_API_
void PrintTo(
const char* s, ::std::ostream* os);
484 inline void PrintTo(
char* s, ::std::ostream* os) {
485 PrintTo(ImplicitCast_<const char*>(s), os);
490 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
491 PrintTo(ImplicitCast_<const void*>(s), os);
493 inline void PrintTo(
signed char* s, ::std::ostream* os) {
494 PrintTo(ImplicitCast_<const void*>(s), os);
496 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
497 PrintTo(ImplicitCast_<const void*>(s), os);
499 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
500 PrintTo(ImplicitCast_<const void*>(s), os);
504 GTEST_API_
void PrintTo(
const char8_t* s, ::std::ostream* os);
505 inline void PrintTo(char8_t* s, ::std::ostream* os) {
506 PrintTo(ImplicitCast_<const char8_t*>(s), os);
510 GTEST_API_
void PrintTo(
const char16_t* s, ::std::ostream* os);
511 inline void PrintTo(char16_t* s, ::std::ostream* os) {
512 PrintTo(ImplicitCast_<const char16_t*>(s), os);
515 GTEST_API_
void PrintTo(
const char32_t* s, ::std::ostream* os);
516 inline void PrintTo(char32_t* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const char32_t*>(s), os);
525 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 527 GTEST_API_
void PrintTo(
const wchar_t* s, ::std::ostream* os);
528 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
529 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
538 template <
typename T>
539 void PrintRawArrayTo(
const T a[],
size_t count, ::std::ostream* os) {
540 UniversalPrint(a[0], os);
541 for (
size_t i = 1; i != count; i++) {
543 UniversalPrint(a[i], os);
548 GTEST_API_
void PrintStringTo(const ::std::string&s, ::std::ostream* os);
549 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
550 PrintStringTo(s, os);
555 GTEST_API_
void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
556 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
557 PrintU8StringTo(s, os);
562 GTEST_API_
void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
563 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
564 PrintU16StringTo(s, os);
568 GTEST_API_
void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
569 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
570 PrintU32StringTo(s, os);
574 #if GTEST_HAS_STD_WSTRING 575 GTEST_API_
void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
576 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
577 PrintWideStringTo(s, os);
579 #endif // GTEST_HAS_STD_WSTRING 581 #if GTEST_INTERNAL_HAS_STRING_VIEW 583 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
584 PrintTo(::std::string(sp), os);
586 #endif // GTEST_INTERNAL_HAS_STRING_VIEW 588 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
590 template <
typename T>
591 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
592 UniversalPrinter<T&>::Print(ref.get(), os);
595 inline const void* VoidifyPointer(
const void* p) {
return p; }
596 inline const void* VoidifyPointer(
volatile const void* p) {
597 return const_cast<const void*
>(p);
600 template <
typename T,
typename Ptr>
601 void PrintSmartPointer(
const Ptr& ptr, std::ostream* os,
char) {
602 if (ptr ==
nullptr) {
606 *os <<
"(" << (VoidifyPointer)(ptr.get()) <<
")";
609 template <
typename T,
typename Ptr,
610 typename =
typename std::enable_if<!std::is_void<T>::value &&
611 !std::is_array<T>::value>::type>
612 void PrintSmartPointer(
const Ptr& ptr, std::ostream* os,
int) {
613 if (ptr ==
nullptr) {
616 *os <<
"(ptr = " << (VoidifyPointer)(ptr.get()) <<
", value = ";
617 UniversalPrinter<T>::Print(*ptr, os);
622 template <
typename T,
typename D>
623 void PrintTo(
const std::unique_ptr<T, D>& ptr, std::ostream* os) {
624 (PrintSmartPointer<T>)(ptr, os, 0);
627 template <
typename T>
628 void PrintTo(
const std::shared_ptr<T>& ptr, std::ostream* os) {
629 (PrintSmartPointer<T>)(ptr, os, 0);
634 template <
typename T>
635 void PrintTupleTo(
const T&, std::integral_constant<size_t, 0>,
638 template <
typename T,
size_t I>
639 void PrintTupleTo(
const T& t, std::integral_constant<size_t, I>,
640 ::std::ostream* os) {
641 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
642 GTEST_INTENTIONAL_CONST_COND_PUSH_()
644 GTEST_INTENTIONAL_CONST_COND_POP_()
647 UniversalPrinter<
typename std::tuple_element<I - 1, T>::type>::Print(
648 std::get<I - 1>(t), os);
651 template <
typename... Types>
652 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
654 PrintTupleTo(t, std::integral_constant<
size_t,
sizeof...(Types)>(), os);
659 template <
typename T1,
typename T2>
660 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
664 UniversalPrinter<T1>::Print(value.first, os);
666 UniversalPrinter<T2>::Print(value.second, os);
672 template <
typename T>
673 class UniversalPrinter {
677 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
682 static
void Print(const T& value, ::
std::ostream* os) {
694 GTEST_DISABLE_MSC_WARNINGS_POP_()
698 template <
typename T>
699 class UniversalPrinter<const T> :
public UniversalPrinter<T> {};
701 #if GTEST_INTERNAL_HAS_ANY 706 class UniversalPrinter<Any> {
708 static void Print(
const Any& value, ::std::ostream* os) {
709 if (value.has_value()) {
710 *os <<
"value of type " << GetTypeName(value);
717 static std::string GetTypeName(
const Any& value) {
719 return internal::GetTypeName(value.type());
721 static_cast<void>(value);
722 return "<unknown_type>";
723 #endif // GTEST_HAS_RTTI 727 #endif // GTEST_INTERNAL_HAS_ANY 729 #if GTEST_INTERNAL_HAS_OPTIONAL 733 template <
typename T>
734 class UniversalPrinter<Optional<T>> {
736 static void Print(
const Optional<T>& value, ::std::ostream* os) {
741 UniversalPrint(*value, os);
747 #endif // GTEST_INTERNAL_HAS_OPTIONAL 749 #if GTEST_INTERNAL_HAS_VARIANT 753 template <
typename... T>
754 class UniversalPrinter<Variant<T...>> {
756 static void Print(
const Variant<T...>& value, ::std::ostream* os) {
759 absl::visit(Visitor{os, value.index()}, value);
761 std::visit(Visitor{os, value.index()}, value);
762 #endif // GTEST_HAS_ABSL 768 template <
typename U>
769 void operator()(
const U& u)
const {
770 *os <<
"'" << GetTypeName<U>() <<
"(index = " << index
772 UniversalPrint(u, os);
779 #endif // GTEST_INTERNAL_HAS_VARIANT 783 template <
typename T>
784 void UniversalPrintArray(
const T* begin,
size_t len, ::std::ostream* os) {
789 const size_t kThreshold = 18;
790 const size_t kChunkSize = 8;
794 if (len <= kThreshold) {
795 PrintRawArrayTo(begin, len, os);
797 PrintRawArrayTo(begin, kChunkSize, os);
799 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
805 GTEST_API_
void UniversalPrintArray(
806 const char* begin,
size_t len, ::std::ostream* os);
810 GTEST_API_
void UniversalPrintArray(
const char8_t* begin,
size_t len,
815 GTEST_API_
void UniversalPrintArray(
const char16_t* begin,
size_t len,
819 GTEST_API_
void UniversalPrintArray(
const char32_t* begin,
size_t len,
823 GTEST_API_
void UniversalPrintArray(
824 const wchar_t* begin,
size_t len, ::std::ostream* os);
827 template <
typename T,
size_t N>
828 class UniversalPrinter<T[N]> {
832 static void Print(
const T (&a)[N], ::std::ostream* os) {
833 UniversalPrintArray(a, N, os);
838 template <
typename T>
839 class UniversalPrinter<T&> {
843 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
845 static
void Print(const T& value, ::
std::ostream* os) {
848 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
851 UniversalPrint(value, os);
854 GTEST_DISABLE_MSC_WARNINGS_POP_()
861 template <
typename T>
862 class UniversalTersePrinter {
864 static void Print(
const T& value, ::std::ostream* os) {
865 UniversalPrint(value, os);
868 template <
typename T>
869 class UniversalTersePrinter<T&> {
871 static void Print(
const T& value, ::std::ostream* os) {
872 UniversalPrint(value, os);
875 template <
typename T,
size_t N>
876 class UniversalTersePrinter<T[N]> {
878 static void Print(
const T (&value)[N], ::std::ostream* os) {
879 UniversalPrinter<T[N]>::Print(value, os);
883 class UniversalTersePrinter<const char*> {
885 static void Print(
const char* str, ::std::ostream* os) {
886 if (str ==
nullptr) {
889 UniversalPrint(std::string(str), os);
894 class UniversalTersePrinter<char*> :
public UniversalTersePrinter<const char*> {
899 class UniversalTersePrinter<const char8_t*> {
901 static void Print(
const char8_t* str, ::std::ostream* os) {
902 if (str ==
nullptr) {
905 UniversalPrint(::std::u8string(str), os);
910 class UniversalTersePrinter<char8_t*>
911 :
public UniversalTersePrinter<const char8_t*> {};
915 class UniversalTersePrinter<const char16_t*> {
917 static void Print(
const char16_t* str, ::std::ostream* os) {
918 if (str ==
nullptr) {
921 UniversalPrint(::std::u16string(str), os);
926 class UniversalTersePrinter<char16_t*>
927 :
public UniversalTersePrinter<const char16_t*> {};
930 class UniversalTersePrinter<const char32_t*> {
932 static void Print(
const char32_t* str, ::std::ostream* os) {
933 if (str ==
nullptr) {
936 UniversalPrint(::std::u32string(str), os);
941 class UniversalTersePrinter<char32_t*>
942 :
public UniversalTersePrinter<const char32_t*> {};
944 #if GTEST_HAS_STD_WSTRING 946 class UniversalTersePrinter<const wchar_t*> {
948 static void Print(
const wchar_t* str, ::std::ostream* os) {
949 if (str ==
nullptr) {
952 UniversalPrint(::std::wstring(str), os);
959 class UniversalTersePrinter<wchar_t*> {
961 static void Print(
wchar_t* str, ::std::ostream* os) {
962 UniversalTersePrinter<const wchar_t*>::Print(str, os);
966 template <
typename T>
967 void UniversalTersePrint(
const T& value, ::std::ostream* os) {
968 UniversalTersePrinter<T>::Print(value, os);
975 template <
typename T>
976 void UniversalPrint(
const T& value, ::std::ostream* os) {
980 UniversalPrinter<T1>::Print(value, os);
983 typedef ::std::vector< ::std::string> Strings;
987 template <
typename Tuple>
988 void TersePrintPrefixToStrings(
const Tuple&, std::integral_constant<size_t, 0>,
990 template <
typename Tuple,
size_t I>
991 void TersePrintPrefixToStrings(
const Tuple& t,
992 std::integral_constant<size_t, I>,
994 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
996 ::std::stringstream ss;
997 UniversalTersePrint(std::get<I - 1>(t), &ss);
998 strings->push_back(ss.str());
1004 template <
typename Tuple>
1005 Strings UniversalTersePrintTupleFieldsToStrings(
const Tuple& value) {
1007 TersePrintPrefixToStrings(
1008 value, std::integral_constant<
size_t, std::tuple_size<Tuple>::value>(),
1015 template <
typename T>
1016 ::std::string PrintToString(
const T& value) {
1017 ::std::stringstream ss;
1018 internal::UniversalTersePrinter<T>::Print(value, &ss);
1027 #include "gtest/internal/custom/gtest-printers.h" 1029 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ JSON_API OStream & operator<<(OStream &, const Value &root)
Output using the StyledStreamWriter.
Definition: gmock-actions.h:154
Definition: gtest-internal.h:1322