1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Test - The Google C++ Testing and Mocking Framework
34 // This file implements a universal value printer that can print a
35 // value of any type T:
37 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
39 // A user can teach this function how to print a class type T by
40 // defining either operator<<() or PrintTo() in the namespace that
41 // defines T. More specifically, the FIRST defined function in the
42 // following list will be used (assuming T is defined in namespace
45 // 1. foo::PrintTo(const T&, ostream*)
46 // 2. operator<<(ostream&, const T&) defined in either foo or the
49 // However if T is an STL-style container then it is printed element-wise
50 // unless foo::PrintTo(const T&, ostream*) is defined. Note that
51 // operator<<() is ignored for container types.
53 // If none of the above is defined, it will print the debug string of
54 // the value if it is a protocol buffer, or print the raw bytes in the
57 // To aid debugging: when T is a reference type, the address of the
58 // value is also printed; when T is a (const) char pointer, both the
59 // pointer value and the NUL-terminated string it points to are
62 // We also provide some convenient wrappers:
64 // // Prints a value to a string. For a (const or not) char
65 // // pointer, the NUL-terminated string (but not the pointer) is
67 // std::string ::testing::PrintToString(const T& value);
69 // // Prints a value tersely: for a reference type, the referenced
70 // // value (but not the address) is printed; for a (const or not) char
71 // // pointer, the NUL-terminated string (but not the pointer) is
73 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
75 // // Prints value using the type inferred by the compiler. The difference
76 // // from UniversalTersePrint() is that this function prints both the
77 // // pointer and the NUL-terminated string for a (const or not) char pointer.
78 // void ::testing::internal::UniversalPrint(const T& value, ostream*);
80 // // Prints the fields of a tuple tersely to a string vector, one
81 // // element for each field. Tuple support must be enabled in
83 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
84 // const Tuple& value);
88 // The print primitives print the elements of an STL-style container
89 // using the compiler-inferred type of *iter where iter is a
90 // const_iterator of the container. When const_iterator is an input
91 // iterator but not a forward iterator, this inferred type may not
92 // match value_type, and the print output may be incorrect. In
93 // practice, this is rarely a problem as for most containers
94 // const_iterator is a forward iterator. We'll fix this if there's an
95 // actual need for it. Note that this fix cannot rely on value_type
96 // being defined as many user-defined container types don't have
99 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
100 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102 #include <ostream> // NOLINT
107 #include "gtest/internal/gtest-port.h"
108 #include "gtest/internal/gtest-internal.h"
110 #if GTEST_HAS_STD_TUPLE_
115 #include "absl/strings/string_view.h"
116 #include "absl/types/optional.h"
117 #endif // GTEST_HAS_ABSL
121 // Definitions in the 'internal' and 'internal2' name spaces are
122 // subject to change without notice. DO NOT USE THEM IN USER CODE!
123 namespace internal2
{
125 // Prints the given number of bytes in the given object to the given
127 GTEST_API_
void PrintBytesInObjectTo(const unsigned char* obj_bytes
,
131 // For selecting which printer to use when a given type has neither <<
134 kProtobuf
, // a protobuf type
135 kConvertibleToInteger
, // a type implicitly convertible to BiggestInt
136 // (e.g. a named or unnamed enum type)
138 kConvertibleToStringView
, // a type implicitly convertible to
141 kOtherType
// anything else
144 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
145 // by the universal printer to print a value of type T when neither
146 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
147 // "kind" of T as defined by enum TypeKind.
148 template <typename T
, TypeKind kTypeKind
>
149 class TypeWithoutFormatter
{
151 // This default version is called when kTypeKind is kOtherType.
152 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
153 PrintBytesInObjectTo(static_cast<const unsigned char*>(
154 reinterpret_cast<const void*>(&value
)),
159 // We print a protobuf using its ShortDebugString() when the string
160 // doesn't exceed this many characters; otherwise we print it using
161 // DebugString() for better readability.
162 const size_t kProtobufOneLinerMaxLength
= 50;
164 template <typename T
>
165 class TypeWithoutFormatter
<T
, kProtobuf
> {
167 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
168 std::string pretty_str
= value
.ShortDebugString();
169 if (pretty_str
.length() > kProtobufOneLinerMaxLength
) {
170 pretty_str
= "\n" + value
.DebugString();
172 *os
<< ("<" + pretty_str
+ ">");
176 template <typename T
>
177 class TypeWithoutFormatter
<T
, kConvertibleToInteger
> {
179 // Since T has no << operator or PrintTo() but can be implicitly
180 // converted to BiggestInt, we print it as a BiggestInt.
182 // Most likely T is an enum type (either named or unnamed), in which
183 // case printing it as an integer is the desired behavior. In case
184 // T is not an enum, printing it as an integer is the best we can do
185 // given that it has no user-defined printer.
186 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
187 const internal::BiggestInt kBigInt
= value
;
193 template <typename T
>
194 class TypeWithoutFormatter
<T
, kConvertibleToStringView
> {
196 // Since T has neither operator<< nor PrintTo() but can be implicitly
197 // converted to absl::string_view, we print it as a absl::string_view.
199 // Note: the implementation is further below, as it depends on
200 // internal::PrintTo symbol which is defined later in the file.
201 static void PrintValue(const T
& value
, ::std::ostream
* os
);
205 // Prints the given value to the given ostream. If the value is a
206 // protocol message, its debug string is printed; if it's an enum or
207 // of a type implicitly convertible to BiggestInt, it's printed as an
208 // integer; otherwise the bytes in the value are printed. This is
209 // what UniversalPrinter<T>::Print() does when it knows nothing about
210 // type T and T has neither << operator nor PrintTo().
212 // A user can override this behavior for a class type Foo by defining
213 // a << operator in the namespace where Foo is defined.
215 // We put this operator in namespace 'internal2' instead of 'internal'
216 // to simplify the implementation, as much code in 'internal' needs to
217 // use << in STL, which would conflict with our own << were it defined
220 // Note that this operator<< takes a generic std::basic_ostream<Char,
221 // CharTraits> type instead of the more restricted std::ostream. If
222 // we define it to take an std::ostream instead, we'll get an
223 // "ambiguous overloads" compiler error when trying to print a type
224 // Foo that supports streaming to std::basic_ostream<Char,
225 // CharTraits>, as the compiler cannot tell whether
226 // operator<<(std::ostream&, const T&) or
227 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
229 template <typename Char
, typename CharTraits
, typename T
>
230 ::std::basic_ostream
<Char
, CharTraits
>& operator<<(
231 ::std::basic_ostream
<Char
, CharTraits
>& os
, const T
& x
) {
232 TypeWithoutFormatter
<T
, (internal::IsAProtocolMessage
<T
>::value
234 : internal::ImplicitlyConvertible
<
235 const T
&, internal::BiggestInt
>::value
236 ? kConvertibleToInteger
239 internal::ImplicitlyConvertible
<
240 const T
&, absl::string_view
>::value
241 ? kConvertibleToStringView
244 kOtherType
)>::PrintValue(x
, &os
);
248 } // namespace internal2
249 } // namespace testing
251 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
252 // magic needed for implementing UniversalPrinter won't work.
253 namespace testing_internal
{
255 // Used to print a value that is not an STL-style container when the
256 // user doesn't define PrintTo() for it.
257 template <typename T
>
258 void DefaultPrintNonContainerTo(const T
& value
, ::std::ostream
* os
) {
259 // With the following statement, during unqualified name lookup,
260 // testing::internal2::operator<< appears as if it was declared in
261 // the nearest enclosing namespace that contains both
262 // ::testing_internal and ::testing::internal2, i.e. the global
263 // namespace. For more details, refer to the C++ Standard section
264 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
265 // testing::internal2::operator<< in case T doesn't come with a <<
268 // We cannot write 'using ::testing::internal2::operator<<;', which
269 // gcc 3.3 fails to compile due to a compiler bug.
270 using namespace ::testing::internal2
; // NOLINT
272 // Assuming T is defined in namespace foo, in the next statement,
273 // the compiler will consider all of:
275 // 1. foo::operator<< (thanks to Koenig look-up),
276 // 2. ::operator<< (as the current namespace is enclosed in ::),
277 // 3. testing::internal2::operator<< (thanks to the using statement above).
279 // The operator<< whose type matches T best will be picked.
281 // We deliberately allow #2 to be a candidate, as sometimes it's
282 // impossible to define #1 (e.g. when foo is ::std, defining
283 // anything in it is undefined behavior unless you are a compiler
288 } // namespace testing_internal
293 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
294 // value of type ToPrint that is an operand of a comparison assertion
295 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
296 // the comparison, and is used to help determine the best way to
297 // format the value. In particular, when the value is a C string
298 // (char pointer) and the other operand is an STL string object, we
299 // want to format the C string as a string, since we know it is
300 // compared by value with the string object. If the value is a char
301 // pointer but the other operand is not an STL string object, we don't
302 // know whether the pointer is supposed to point to a NUL-terminated
303 // string, and thus want to print it as a pointer to be safe.
305 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
308 template <typename ToPrint
, typename OtherOperand
>
309 class FormatForComparison
{
311 static ::std::string
Format(const ToPrint
& value
) {
312 return ::testing::PrintToString(value
);
317 template <typename ToPrint
, size_t N
, typename OtherOperand
>
318 class FormatForComparison
<ToPrint
[N
], OtherOperand
> {
320 static ::std::string
Format(const ToPrint
* value
) {
321 return FormatForComparison
<const ToPrint
*, OtherOperand
>::Format(value
);
325 // By default, print C string as pointers to be safe, as we don't know
326 // whether they actually point to a NUL-terminated string.
328 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
329 template <typename OtherOperand> \
330 class FormatForComparison<CharType*, OtherOperand> { \
332 static ::std::string Format(CharType* value) { \
333 return ::testing::PrintToString(static_cast<const void*>(value)); \
337 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
338 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
339 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
340 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
342 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
344 // If a C string is compared with an STL string object, we know it's meant
345 // to point to a NUL-terminated string, and thus can print it as a string.
347 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
349 class FormatForComparison<CharType*, OtherStringType> { \
351 static ::std::string Format(CharType* value) { \
352 return ::testing::PrintToString(value); \
356 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string
);
357 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string
);
359 #if GTEST_HAS_GLOBAL_STRING
360 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string
);
361 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string
);
364 #if GTEST_HAS_GLOBAL_WSTRING
365 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring
);
366 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring
);
369 #if GTEST_HAS_STD_WSTRING
370 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring
);
371 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring
);
374 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
376 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
377 // operand to be used in a failure message. The type (but not value)
378 // of the other operand may affect the format. This allows us to
379 // print a char* as a raw pointer when it is compared against another
380 // char* or void*, and print it as a C string when it is compared
381 // against an std::string object, for example.
383 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
384 template <typename T1
, typename T2
>
385 std::string
FormatForComparisonFailureMessage(
386 const T1
& value
, const T2
& /* other_operand */) {
387 return FormatForComparison
<T1
, T2
>::Format(value
);
390 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
391 // value to the given ostream. The caller must ensure that
392 // 'ostream_ptr' is not NULL, or the behavior is undefined.
394 // We define UniversalPrinter as a class template (as opposed to a
395 // function template), as we need to partially specialize it for
396 // reference types, which cannot be done with function templates.
397 template <typename T
>
398 class UniversalPrinter
;
400 template <typename T
>
401 void UniversalPrint(const T
& value
, ::std::ostream
* os
);
403 enum DefaultPrinterType
{
406 kPrintFunctionPointer
,
409 template <DefaultPrinterType type
> struct WrapPrinterType
{};
411 // Used to print an STL-style container when the user doesn't define
412 // a PrintTo() for it.
413 template <typename C
>
414 void DefaultPrintTo(WrapPrinterType
<kPrintContainer
> /* dummy */,
415 const C
& container
, ::std::ostream
* os
) {
416 const size_t kMaxCount
= 32; // The maximum number of elements to print.
419 for (typename
C::const_iterator it
= container
.begin();
420 it
!= container
.end(); ++it
, ++count
) {
423 if (count
== kMaxCount
) { // Enough has been printed.
429 // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
430 // handle *it being a native array.
431 internal::UniversalPrint(*it
, os
);
440 // Used to print a pointer that is neither a char pointer nor a member
441 // pointer, when the user doesn't define PrintTo() for it. (A member
442 // variable pointer or member function pointer doesn't really point to
443 // a location in the address space. Their representation is
444 // implementation-defined. Therefore they will be printed as raw
446 template <typename T
>
447 void DefaultPrintTo(WrapPrinterType
<kPrintPointer
> /* dummy */,
448 T
* p
, ::std::ostream
* os
) {
452 // T is not a function type. We just call << to print p,
453 // relying on ADL to pick up user-defined << for their pointer
458 template <typename T
>
459 void DefaultPrintTo(WrapPrinterType
<kPrintFunctionPointer
> /* dummy */,
460 T
* p
, ::std::ostream
* os
) {
464 // T is a function type, so '*os << p' doesn't do what we want
465 // (it just prints p as bool). We want to print p as a const
467 *os
<< reinterpret_cast<const void*>(p
);
471 // Used to print a non-container, non-pointer value when the user
472 // doesn't define PrintTo() for it.
473 template <typename T
>
474 void DefaultPrintTo(WrapPrinterType
<kPrintOther
> /* dummy */,
475 const T
& value
, ::std::ostream
* os
) {
476 ::testing_internal::DefaultPrintNonContainerTo(value
, os
);
479 // Prints the given value using the << operator if it has one;
480 // otherwise prints the bytes in it. This is what
481 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
482 // or overloaded for type T.
484 // A user can override this behavior for a class type Foo by defining
485 // an overload of PrintTo() in the namespace where Foo is defined. We
486 // give the user this option as sometimes defining a << operator for
487 // Foo is not desirable (e.g. the coding style may prevent doing it,
488 // or there is already a << operator but it doesn't do what the user
490 template <typename T
>
491 void PrintTo(const T
& value
, ::std::ostream
* os
) {
492 // DefaultPrintTo() is overloaded. The type of its first argument
493 // determines which version will be picked.
495 // Note that we check for container types here, prior to we check
496 // for protocol message types in our operator<<. The rationale is:
498 // For protocol messages, we want to give people a chance to
499 // override Google Mock's format by defining a PrintTo() or
500 // operator<<. For STL containers, other formats can be
501 // incompatible with Google Mock's format for the container
502 // elements; therefore we check for container types here to ensure
503 // that our format is used.
505 // Note that MSVC and clang-cl do allow an implicit conversion from
506 // pointer-to-function to pointer-to-object, but clang-cl warns on it.
507 // So don't use ImplicitlyConvertible if it can be helped since it will
508 // cause this warning, and use a separate overload of DefaultPrintTo for
509 // function pointers so that the `*os << p` in the object pointer overload
510 // doesn't cause that warning either.
513 (sizeof(IsContainerTest
<T
>(0)) == sizeof(IsContainer
)) &&
514 !IsRecursiveContainer
<T
>::value
516 : !is_pointer
<T
>::value
519 : std::is_function
<typename
std::remove_pointer
<T
>::type
>::value
521 : !internal::ImplicitlyConvertible
<T
, const void*>::value
523 ? kPrintFunctionPointer
524 : kPrintPointer
> (),
528 // The following list of PrintTo() overloads tells
529 // UniversalPrinter<T>::Print() how to print standard types (built-in
530 // types, strings, plain arrays, and pointers).
532 // Overloads for various char types.
533 GTEST_API_
void PrintTo(unsigned char c
, ::std::ostream
* os
);
534 GTEST_API_
void PrintTo(signed char c
, ::std::ostream
* os
);
535 inline void PrintTo(char c
, ::std::ostream
* os
) {
536 // When printing a plain char, we always treat it as unsigned. This
537 // way, the output won't be affected by whether the compiler thinks
538 // char is signed or not.
539 PrintTo(static_cast<unsigned char>(c
), os
);
542 // Overloads for other simple built-in types.
543 inline void PrintTo(bool x
, ::std::ostream
* os
) {
544 *os
<< (x
? "true" : "false");
547 // Overload for wchar_t type.
548 // Prints a wchar_t as a symbol if it is printable or as its internal
549 // code otherwise and also as its decimal code (except for L'\0').
550 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
551 // as signed integer when wchar_t is implemented by the compiler
552 // as a signed type and is printed as an unsigned integer when wchar_t
553 // is implemented as an unsigned type.
554 GTEST_API_
void PrintTo(wchar_t wc
, ::std::ostream
* os
);
556 // Overloads for C strings.
557 GTEST_API_
void PrintTo(const char* s
, ::std::ostream
* os
);
558 inline void PrintTo(char* s
, ::std::ostream
* os
) {
559 PrintTo(ImplicitCast_
<const char*>(s
), os
);
562 // signed/unsigned char is often used for representing binary data, so
563 // we print pointers to it as void* to be safe.
564 inline void PrintTo(const signed char* s
, ::std::ostream
* os
) {
565 PrintTo(ImplicitCast_
<const void*>(s
), os
);
567 inline void PrintTo(signed char* s
, ::std::ostream
* os
) {
568 PrintTo(ImplicitCast_
<const void*>(s
), os
);
570 inline void PrintTo(const unsigned char* s
, ::std::ostream
* os
) {
571 PrintTo(ImplicitCast_
<const void*>(s
), os
);
573 inline void PrintTo(unsigned char* s
, ::std::ostream
* os
) {
574 PrintTo(ImplicitCast_
<const void*>(s
), os
);
577 // MSVC can be configured to define wchar_t as a typedef of unsigned
578 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
579 // type. When wchar_t is a typedef, defining an overload for const
580 // wchar_t* would cause unsigned short* be printed as a wide string,
581 // possibly causing invalid memory accesses.
582 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
583 // Overloads for wide C strings
584 GTEST_API_
void PrintTo(const wchar_t* s
, ::std::ostream
* os
);
585 inline void PrintTo(wchar_t* s
, ::std::ostream
* os
) {
586 PrintTo(ImplicitCast_
<const wchar_t*>(s
), os
);
590 // Overload for C arrays. Multi-dimensional arrays are printed
593 // Prints the given number of elements in an array, without printing
595 template <typename T
>
596 void PrintRawArrayTo(const T a
[], size_t count
, ::std::ostream
* os
) {
597 UniversalPrint(a
[0], os
);
598 for (size_t i
= 1; i
!= count
; i
++) {
600 UniversalPrint(a
[i
], os
);
604 // Overloads for ::string and ::std::string.
605 #if GTEST_HAS_GLOBAL_STRING
606 GTEST_API_
void PrintStringTo(const ::string
&s
, ::std::ostream
* os
);
607 inline void PrintTo(const ::string
& s
, ::std::ostream
* os
) {
608 PrintStringTo(s
, os
);
610 #endif // GTEST_HAS_GLOBAL_STRING
612 GTEST_API_
void PrintStringTo(const ::std::string
&s
, ::std::ostream
* os
);
613 inline void PrintTo(const ::std::string
& s
, ::std::ostream
* os
) {
614 PrintStringTo(s
, os
);
617 // Overloads for ::wstring and ::std::wstring.
618 #if GTEST_HAS_GLOBAL_WSTRING
619 GTEST_API_
void PrintWideStringTo(const ::wstring
&s
, ::std::ostream
* os
);
620 inline void PrintTo(const ::wstring
& s
, ::std::ostream
* os
) {
621 PrintWideStringTo(s
, os
);
623 #endif // GTEST_HAS_GLOBAL_WSTRING
625 #if GTEST_HAS_STD_WSTRING
626 GTEST_API_
void PrintWideStringTo(const ::std::wstring
&s
, ::std::ostream
* os
);
627 inline void PrintTo(const ::std::wstring
& s
, ::std::ostream
* os
) {
628 PrintWideStringTo(s
, os
);
630 #endif // GTEST_HAS_STD_WSTRING
633 // Overload for absl::string_view.
634 inline void PrintTo(absl::string_view sp
, ::std::ostream
* os
) {
635 PrintTo(::std::string(sp
), os
);
637 #endif // GTEST_HAS_ABSL
640 inline void PrintTo(std::nullptr_t
, ::std::ostream
* os
) { *os
<< "(nullptr)"; }
641 #endif // GTEST_LANG_CXX11
643 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
644 // Helper function for printing a tuple. T must be instantiated with
646 template <typename T
>
647 void PrintTupleTo(const T
& t
, ::std::ostream
* os
);
648 #endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
650 #if GTEST_HAS_TR1_TUPLE
651 // Overload for ::std::tr1::tuple. Needed for printing function arguments,
652 // which are packed as tuples.
654 // Overloaded PrintTo() for tuples of various arities. We support
655 // tuples of up-to 10 fields. The following implementation works
656 // regardless of whether tr1::tuple is implemented using the
657 // non-standard variadic template feature or not.
659 inline void PrintTo(const ::std::tr1::tuple
<>& t
, ::std::ostream
* os
) {
663 template <typename T1
>
664 void PrintTo(const ::std::tr1::tuple
<T1
>& t
, ::std::ostream
* os
) {
668 template <typename T1
, typename T2
>
669 void PrintTo(const ::std::tr1::tuple
<T1
, T2
>& t
, ::std::ostream
* os
) {
673 template <typename T1
, typename T2
, typename T3
>
674 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
>& t
, ::std::ostream
* os
) {
678 template <typename T1
, typename T2
, typename T3
, typename T4
>
679 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
>& t
, ::std::ostream
* os
) {
683 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
>
684 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
>& t
,
685 ::std::ostream
* os
) {
689 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
691 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
>& t
,
692 ::std::ostream
* os
) {
696 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
697 typename T6
, typename T7
>
698 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
>& t
,
699 ::std::ostream
* os
) {
703 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
704 typename T6
, typename T7
, typename T8
>
705 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
>& t
,
706 ::std::ostream
* os
) {
710 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
711 typename T6
, typename T7
, typename T8
, typename T9
>
712 void PrintTo(const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
>& t
,
713 ::std::ostream
* os
) {
717 template <typename T1
, typename T2
, typename T3
, typename T4
, typename T5
,
718 typename T6
, typename T7
, typename T8
, typename T9
, typename T10
>
720 const ::std::tr1::tuple
<T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
, T10
>& t
,
721 ::std::ostream
* os
) {
724 #endif // GTEST_HAS_TR1_TUPLE
726 #if GTEST_HAS_STD_TUPLE_
727 template <typename
... Types
>
728 void PrintTo(const ::std::tuple
<Types
...>& t
, ::std::ostream
* os
) {
731 #endif // GTEST_HAS_STD_TUPLE_
733 // Overload for std::pair.
734 template <typename T1
, typename T2
>
735 void PrintTo(const ::std::pair
<T1
, T2
>& value
, ::std::ostream
* os
) {
737 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
738 // a reference type. The same for printing value.second.
739 UniversalPrinter
<T1
>::Print(value
.first
, os
);
741 UniversalPrinter
<T2
>::Print(value
.second
, os
);
745 // Implements printing a non-reference type T by letting the compiler
746 // pick the right overload of PrintTo() for T.
747 template <typename T
>
748 class UniversalPrinter
{
750 // MSVC warns about adding const to a function type, so we want to
751 // disable the warning.
752 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
754 // Note: we deliberately don't call this PrintTo(), as that name
755 // conflicts with ::testing::internal::PrintTo in the body of the
757 static void Print(const T
& value
, ::std::ostream
* os
) {
758 // By default, ::testing::internal::PrintTo() is used for printing
761 // Thanks to Koenig look-up, if T is a class and has its own
762 // PrintTo() function defined in its namespace, that function will
763 // be visible here. Since it is more specific than the generic ones
764 // in ::testing::internal, it will be picked by the compiler in the
765 // following statement - exactly what we want.
769 GTEST_DISABLE_MSC_WARNINGS_POP_()
774 // Printer for absl::optional
776 template <typename T
>
777 class UniversalPrinter
<::absl::optional
<T
>> {
779 static void Print(const ::absl::optional
<T
>& value
, ::std::ostream
* os
) {
784 UniversalPrint(*value
, os
);
790 #endif // GTEST_HAS_ABSL
792 // UniversalPrintArray(begin, len, os) prints an array of 'len'
793 // elements, starting at address 'begin'.
794 template <typename T
>
795 void UniversalPrintArray(const T
* begin
, size_t len
, ::std::ostream
* os
) {
800 const size_t kThreshold
= 18;
801 const size_t kChunkSize
= 8;
802 // If the array has more than kThreshold elements, we'll have to
803 // omit some details by printing only the first and the last
804 // kChunkSize elements.
805 // TODO(wan@google.com): let the user control the threshold using a flag.
806 if (len
<= kThreshold
) {
807 PrintRawArrayTo(begin
, len
, os
);
809 PrintRawArrayTo(begin
, kChunkSize
, os
);
811 PrintRawArrayTo(begin
+ len
- kChunkSize
, kChunkSize
, os
);
816 // This overload prints a (const) char array compactly.
817 GTEST_API_
void UniversalPrintArray(
818 const char* begin
, size_t len
, ::std::ostream
* os
);
820 // This overload prints a (const) wchar_t array compactly.
821 GTEST_API_
void UniversalPrintArray(
822 const wchar_t* begin
, size_t len
, ::std::ostream
* os
);
824 // Implements printing an array type T[N].
825 template <typename T
, size_t N
>
826 class UniversalPrinter
<T
[N
]> {
828 // Prints the given array, omitting some elements when there are too
830 static void Print(const T (&a
)[N
], ::std::ostream
* os
) {
831 UniversalPrintArray(a
, N
, os
);
835 // Implements printing a reference type T&.
836 template <typename T
>
837 class UniversalPrinter
<T
&> {
839 // MSVC warns about adding const to a function type, so we want to
840 // disable the warning.
841 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
843 static void Print(const T
& value
, ::std::ostream
* os
) {
844 // Prints the address of the value. We use reinterpret_cast here
845 // as static_cast doesn't compile when T is a function type.
846 *os
<< "@" << reinterpret_cast<const void*>(&value
) << " ";
848 // Then prints the value itself.
849 UniversalPrint(value
, os
);
852 GTEST_DISABLE_MSC_WARNINGS_POP_()
855 // Prints a value tersely: for a reference type, the referenced value
856 // (but not the address) is printed; for a (const) char pointer, the
857 // NUL-terminated string (but not the pointer) is printed.
859 template <typename T
>
860 class UniversalTersePrinter
{
862 static void Print(const T
& value
, ::std::ostream
* os
) {
863 UniversalPrint(value
, os
);
866 template <typename T
>
867 class UniversalTersePrinter
<T
&> {
869 static void Print(const T
& value
, ::std::ostream
* os
) {
870 UniversalPrint(value
, os
);
873 template <typename T
, size_t N
>
874 class UniversalTersePrinter
<T
[N
]> {
876 static void Print(const T (&value
)[N
], ::std::ostream
* os
) {
877 UniversalPrinter
<T
[N
]>::Print(value
, os
);
881 class UniversalTersePrinter
<const char*> {
883 static void Print(const char* str
, ::std::ostream
* os
) {
887 UniversalPrint(std::string(str
), os
);
892 class UniversalTersePrinter
<char*> {
894 static void Print(char* str
, ::std::ostream
* os
) {
895 UniversalTersePrinter
<const char*>::Print(str
, os
);
899 #if GTEST_HAS_STD_WSTRING
901 class UniversalTersePrinter
<const wchar_t*> {
903 static void Print(const wchar_t* str
, ::std::ostream
* os
) {
907 UniversalPrint(::std::wstring(str
), os
);
914 class UniversalTersePrinter
<wchar_t*> {
916 static void Print(wchar_t* str
, ::std::ostream
* os
) {
917 UniversalTersePrinter
<const wchar_t*>::Print(str
, os
);
921 template <typename T
>
922 void UniversalTersePrint(const T
& value
, ::std::ostream
* os
) {
923 UniversalTersePrinter
<T
>::Print(value
, os
);
926 // Prints a value using the type inferred by the compiler. The
927 // difference between this and UniversalTersePrint() is that for a
928 // (const) char pointer, this prints both the pointer and the
929 // NUL-terminated string.
930 template <typename T
>
931 void UniversalPrint(const T
& value
, ::std::ostream
* os
) {
932 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
933 // UniversalPrinter with T directly.
935 UniversalPrinter
<T1
>::Print(value
, os
);
938 typedef ::std::vector
< ::std::string
> Strings
;
940 // TuplePolicy<TupleT> must provide:
942 // size of tuple TupleT.
943 // - get<size_t I>(const TupleT& t)
944 // static function extracting element I of tuple TupleT.
945 // - tuple_element<size_t I>::type
946 // type of element I of tuple TupleT.
947 template <typename TupleT
>
950 #if GTEST_HAS_TR1_TUPLE
951 template <typename TupleT
>
953 typedef TupleT Tuple
;
954 static const size_t tuple_size
= ::std::tr1::tuple_size
<Tuple
>::value
;
957 struct tuple_element
: ::std::tr1::tuple_element
<I
, Tuple
> {};
960 static typename AddReference
<
961 const typename ::std::tr1::tuple_element
<I
, Tuple
>::type
>::type
get(
962 const Tuple
& tuple
) {
963 return ::std::tr1::get
<I
>(tuple
);
966 template <typename TupleT
>
967 const size_t TuplePolicy
<TupleT
>::tuple_size
;
968 #endif // GTEST_HAS_TR1_TUPLE
970 #if GTEST_HAS_STD_TUPLE_
971 template <typename
... Types
>
972 struct TuplePolicy
< ::std::tuple
<Types
...> > {
973 typedef ::std::tuple
<Types
...> Tuple
;
974 static const size_t tuple_size
= ::std::tuple_size
<Tuple
>::value
;
977 struct tuple_element
: ::std::tuple_element
<I
, Tuple
> {};
980 static const typename ::std::tuple_element
<I
, Tuple
>::type
& get(
981 const Tuple
& tuple
) {
982 return ::std::get
<I
>(tuple
);
985 template <typename
... Types
>
986 const size_t TuplePolicy
< ::std::tuple
<Types
...> >::tuple_size
;
987 #endif // GTEST_HAS_STD_TUPLE_
989 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
990 // This helper template allows PrintTo() for tuples and
991 // UniversalTersePrintTupleFieldsToStrings() to be defined by
992 // induction on the number of tuple fields. The idea is that
993 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
994 // fields in tuple t, and can be defined in terms of
995 // TuplePrefixPrinter<N - 1>.
997 // The inductive case.
999 struct TuplePrefixPrinter
{
1000 // Prints the first N fields of a tuple.
1001 template <typename Tuple
>
1002 static void PrintPrefixTo(const Tuple
& t
, ::std::ostream
* os
) {
1003 TuplePrefixPrinter
<N
- 1>::PrintPrefixTo(t
, os
);
1004 GTEST_INTENTIONAL_CONST_COND_PUSH_()
1006 GTEST_INTENTIONAL_CONST_COND_POP_()
1010 typename TuplePolicy
<Tuple
>::template tuple_element
<N
- 1>::type
>
1011 ::Print(TuplePolicy
<Tuple
>::template get
<N
- 1>(t
), os
);
1014 // Tersely prints the first N fields of a tuple to a string vector,
1015 // one element for each field.
1016 template <typename Tuple
>
1017 static void TersePrintPrefixToStrings(const Tuple
& t
, Strings
* strings
) {
1018 TuplePrefixPrinter
<N
- 1>::TersePrintPrefixToStrings(t
, strings
);
1019 ::std::stringstream ss
;
1020 UniversalTersePrint(TuplePolicy
<Tuple
>::template get
<N
- 1>(t
), &ss
);
1021 strings
->push_back(ss
.str());
1027 struct TuplePrefixPrinter
<0> {
1028 template <typename Tuple
>
1029 static void PrintPrefixTo(const Tuple
&, ::std::ostream
*) {}
1031 template <typename Tuple
>
1032 static void TersePrintPrefixToStrings(const Tuple
&, Strings
*) {}
1035 // Helper function for printing a tuple.
1036 // Tuple must be either std::tr1::tuple or std::tuple type.
1037 template <typename Tuple
>
1038 void PrintTupleTo(const Tuple
& t
, ::std::ostream
* os
) {
1040 TuplePrefixPrinter
<TuplePolicy
<Tuple
>::tuple_size
>::PrintPrefixTo(t
, os
);
1044 // Prints the fields of a tuple tersely to a string vector, one
1045 // element for each field. See the comment before
1046 // UniversalTersePrint() for how we define "tersely".
1047 template <typename Tuple
>
1048 Strings
UniversalTersePrintTupleFieldsToStrings(const Tuple
& value
) {
1050 TuplePrefixPrinter
<TuplePolicy
<Tuple
>::tuple_size
>::
1051 TersePrintPrefixToStrings(value
, &result
);
1054 #endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
1056 } // namespace internal
1059 namespace internal2
{
1060 template <typename T
>
1061 void TypeWithoutFormatter
<T
, kConvertibleToStringView
>::PrintValue(
1062 const T
& value
, ::std::ostream
* os
) {
1063 internal::PrintTo(absl::string_view(value
), os
);
1065 } // namespace internal2
1068 template <typename T
>
1069 ::std::string
PrintToString(const T
& value
) {
1070 ::std::stringstream ss
;
1071 internal::UniversalTersePrinter
<T
>::Print(value
, &ss
);
1075 } // namespace testing
1077 // Include any custom printer added by the local installation.
1078 // We must include this header at the end to make sure it can use the
1079 // declarations from this file.
1080 #include "gtest/internal/custom/gtest-printers.h"
1082 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_