100 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
103 #include <functional>
108 #include <type_traits>
111 #include "gtest/internal/gtest-internal.h"
112 #include "gtest/internal/gtest-port.h"
115 #include "absl/strings/string_view.h"
116 #include "absl/types/optional.h"
117 #include "absl/types/variant.h"
118 #endif // GTEST_HAS_ABSL
124 namespace internal2 {
139 kConvertibleToStringView,
149 template <
typename T, TypeKind kTypeKind>
155 static_cast<const unsigned char*
>(
156 reinterpret_cast<const void*
>(std::addressof(
value))),
166 template <
typename T>
172 pretty_str =
"\n" +
value.DebugString();
174 *os << (
"<" + pretty_str +
">");
178 template <
typename T>
195 template <
typename T>
196 class TypeWithoutFormatter<
T, kConvertibleToStringView> {
231 template <
typename Char,
typename CharTraits,
typename T>
233 ::std::basic_ostream<Char, CharTraits>& os,
const T& x) {
236 : std::is_convertible<
243 ? kConvertibleToStringView
259 template <
typename T>
310 template <
typename ToPr
int,
typename OtherOperand>
319 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
330 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
331 template <typename OtherOperand> \
332 class FormatForComparison<CharType*, OtherOperand> { \
334 static ::std::string Format(CharType* value) { \
335 return ::testing::PrintToString(static_cast<const void*>(value)); \
344 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
349 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
351 class FormatForComparison<CharType*, OtherStringType> { \
353 static ::std::string Format(CharType* value) { \
354 return ::testing::PrintToString(value); \
361 #if GTEST_HAS_STD_WSTRING
366 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
376 template <
typename T1,
typename T2>
389 template <
typename T>
392 template <
typename T>
405 template <
typename C>
407 const C&
container, ::std::ostream* os) {
408 const size_t kMaxCount = 32;
411 for (
typename C::const_iterator
it =
container.begin();
415 if (
count == kMaxCount) {
438 template <
typename T>
440 T* p, ::std::ostream* os) {
450 template <
typename T>
452 T* p, ::std::ostream* os) {
459 *os << reinterpret_cast<const void*>(p);
465 template <
typename T>
467 const T&
value, ::std::ostream* os) {
482 template <
typename T>
505 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
523 inline void PrintTo(
char c, ::std::ostream* os) {
527 PrintTo(
static_cast<unsigned char>(c), os);
531 inline void PrintTo(
bool x, ::std::ostream* os) {
532 *os << (
x ?
"true" :
"false");
546 inline void PrintTo(
char* s, ::std::ostream* os) {
547 PrintTo(ImplicitCast_<const char*>(s), os);
552 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
553 PrintTo(ImplicitCast_<const void*>(s), os);
555 inline void PrintTo(
signed char* s, ::std::ostream* os) {
556 PrintTo(ImplicitCast_<const void*>(s), os);
558 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
559 PrintTo(ImplicitCast_<const void*>(s), os);
561 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
562 PrintTo(ImplicitCast_<const void*>(s), os);
570 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
573 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
574 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
583 template <
typename T>
586 for (
size_t i = 1;
i !=
count;
i++) {
599 #if GTEST_HAS_STD_WSTRING
604 #endif // GTEST_HAS_STD_WSTRING
611 #endif // GTEST_HAS_ABSL
613 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
615 template <
typename T>
616 void PrintTo(std::reference_wrapper<T>
ref, ::std::ostream* os) {
622 template <
typename T>
626 template <
typename T,
size_t I>
628 ::std::ostream* os) {
629 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
636 std::get<I - 1>(t), os);
639 template <
typename...
Types>
640 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
647 template <
typename T1,
typename T2>
660 template <
typename T>
661 class UniversalPrinter {
689 template <
typename T>
692 static void Print(const ::absl::optional<T>&
value, ::std::ostream* os) {
705 template <
typename...
T>
708 static void Print(const ::absl::variant<T...>&
value, ::std::ostream* os) {
716 template <
typename U>
717 void operator()(
const U& u)
const {
718 *os <<
"'" << GetTypeName<U>() <<
"' with value ";
725 #endif // GTEST_HAS_ABSL
729 template <
typename T>
735 const size_t kThreshold = 18;
740 if (
len <= kThreshold) {
752 const char*
begin,
size_t len, ::std::ostream* os);
756 const wchar_t*
begin,
size_t len, ::std::ostream* os);
759 template <
typename T,
size_t N>
764 static void Print(
const T (&a)[
N], ::std::ostream* os) {
770 template <
typename T>
780 *os <<
"@" <<
reinterpret_cast<const void*
>(&
value) <<
" ";
793 template <
typename T>
800 template <
typename T>
807 template <
typename T,
size_t N>
817 static void Print(
const char*
str, ::std::ostream* os) {
818 if (
str ==
nullptr) {
833 #if GTEST_HAS_STD_WSTRING
835 class UniversalTersePrinter<
const wchar_t*> {
837 static void Print(
const wchar_t*
str, ::std::ostream* os) {
838 if (
str ==
nullptr) {
850 static void Print(
wchar_t*
str, ::std::ostream* os) {
855 template <
typename T>
864 template <
typename T>
872 typedef ::std::vector< ::std::string>
Strings;
876 template <
typename Tuple>
879 template <
typename Tuple,
size_t I>
881 std::integral_constant<size_t, I>,
885 ::std::stringstream ss;
887 strings->push_back(ss.str());
893 template <
typename Tuple>
905 namespace internal2 {
906 template <
typename T>
908 const T&
value, ::std::ostream* os) {
914 template <
typename T>
916 ::std::stringstream ss;
926 #include "gtest/internal/custom/gtest-printers.h"
928 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_