Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
gtest-printers.h
1// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29
30
31// Google Test - The Google C++ Testing and Mocking Framework
32//
33// This file implements a universal value printer that can print a
34// value of any type T:
35//
36// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
37//
38// A user can teach this function how to print a class type T by
39// defining either operator<<() or PrintTo() in the namespace that
40// defines T. More specifically, the FIRST defined function in the
41// following list will be used (assuming T is defined in namespace
42// foo):
43//
44// 1. foo::PrintTo(const T&, ostream*)
45// 2. operator<<(ostream&, const T&) defined in either foo or the
46// global namespace.
47//
48// However if T is an STL-style container then it is printed element-wise
49// unless foo::PrintTo(const T&, ostream*) is defined. Note that
50// operator<<() is ignored for container types.
51//
52// If none of the above is defined, it will print the debug string of
53// the value if it is a protocol buffer, or print the raw bytes in the
54// value otherwise.
55//
56// To aid debugging: when T is a reference type, the address of the
57// value is also printed; when T is a (const) char pointer, both the
58// pointer value and the NUL-terminated string it points to are
59// printed.
60//
61// We also provide some convenient wrappers:
62//
63// // Prints a value to a string. For a (const or not) char
64// // pointer, the NUL-terminated string (but not the pointer) is
65// // printed.
66// std::string ::testing::PrintToString(const T& value);
67//
68// // Prints a value tersely: for a reference type, the referenced
69// // value (but not the address) is printed; for a (const or not) char
70// // pointer, the NUL-terminated string (but not the pointer) is
71// // printed.
72// void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
73//
74// // Prints value using the type inferred by the compiler. The difference
75// // from UniversalTersePrint() is that this function prints both the
76// // pointer and the NUL-terminated string for a (const or not) char pointer.
77// void ::testing::internal::UniversalPrint(const T& value, ostream*);
78//
79// // Prints the fields of a tuple tersely to a string vector, one
80// // element for each field. Tuple support must be enabled in
81// // gtest-port.h.
82// std::vector<string> UniversalTersePrintTupleFieldsToStrings(
83// const Tuple& value);
84//
85// Known limitation:
86//
87// The print primitives print the elements of an STL-style container
88// using the compiler-inferred type of *iter where iter is a
89// const_iterator of the container. When const_iterator is an input
90// iterator but not a forward iterator, this inferred type may not
91// match value_type, and the print output may be incorrect. In
92// practice, this is rarely a problem as for most containers
93// const_iterator is a forward iterator. We'll fix this if there's an
94// actual need for it. Note that this fix cannot rely on value_type
95// being defined as many user-defined container types don't have
96// value_type.
97
98// GOOGLETEST_CM0001 DO NOT DELETE
99
100#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102
103#include <functional>
104#include <memory>
105#include <ostream> // NOLINT
106#include <sstream>
107#include <string>
108#include <tuple>
109#include <type_traits>
110#include <utility>
111#include <vector>
112
113#include "gtest/internal/gtest-internal.h"
114#include "gtest/internal/gtest-port.h"
115
116#if GTEST_HAS_RTTI
117#include <typeindex>
118#include <typeinfo>
119#endif // GTEST_HAS_RTTI
120
121namespace testing {
122
123// Definitions in the internal* namespaces are subject to change without notice.
124// DO NOT USE THEM IN USER CODE!
125namespace internal {
126
127template <typename T>
128void UniversalPrint(const T& value, ::std::ostream* os);
129
130// Used to print an STL-style container when the user doesn't define
131// a PrintTo() for it.
133 template <typename T,
134 typename = typename std::enable_if<
135 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
137 static void PrintValue(const T& container, std::ostream* os) {
138 const size_t kMaxCount = 32; // The maximum number of elements to print.
139 *os << '{';
140 size_t count = 0;
141 for (auto&& elem : container) {
142 if (count > 0) {
143 *os << ',';
144 if (count == kMaxCount) { // Enough has been printed.
145 *os << " ...";
146 break;
147 }
148 }
149 *os << ' ';
150 // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
151 // handle `elem` being a native array.
152 internal::UniversalPrint(elem, os);
153 ++count;
154 }
155
156 if (count > 0) {
157 *os << ' ';
158 }
159 *os << '}';
160 }
161};
162
163// Used to print a pointer that is neither a char pointer nor a member
164// pointer, when the user doesn't define PrintTo() for it. (A member
165// variable pointer or member function pointer doesn't really point to
166// a location in the address space. Their representation is
167// implementation-defined. Therefore they will be printed as raw
168// bytes.)
170 template <typename T, typename = typename std::enable_if<
171 std::is_function<T>::value>::type>
172 static void PrintValue(T* p, ::std::ostream* os) {
173 if (p == nullptr) {
174 *os << "NULL";
175 } else {
176 // T is a function type, so '*os << p' doesn't do what we want
177 // (it just prints p as bool). We want to print p as a const
178 // void*.
179 *os << reinterpret_cast<const void*>(p);
180 }
181 }
182};
183
185 template <typename T>
186 static void PrintValue(T* p, ::std::ostream* os) {
187 if (p == nullptr) {
188 *os << "NULL";
189 } else {
190 // T is not a function type. We just call << to print p,
191 // relying on ADL to pick up user-defined << for their pointer
192 // types, if any.
193 *os << p;
194 }
195 }
196};
197
198namespace internal_stream_operator_without_lexical_name_lookup {
199
200// The presence of an operator<< here will terminate lexical scope lookup
201// straight away (even though it cannot be a match because of its argument
202// types). Thus, the two operator<< calls in StreamPrinter will find only ADL
203// candidates.
205void operator<<(LookupBlocker, LookupBlocker);
206
208 template <typename T,
209 // Don't accept member pointers here. We'd print them via implicit
210 // conversion to bool, which isn't useful.
211 typename = typename std::enable_if<
212 !std::is_member_pointer<T>::value>::type,
213 // Only accept types for which we can find a streaming operator via
214 // ADL (possibly involving implicit conversions).
215 typename = decltype(std::declval<std::ostream&>()
216 << std::declval<const T&>())>
217 static void PrintValue(const T& value, ::std::ostream* os) {
218 // Call streaming operator found by ADL, possibly with implicit conversions
219 // of the arguments.
220 *os << value;
221 }
222};
223
224} // namespace internal_stream_operator_without_lexical_name_lookup
225
227 // We print a protobuf using its ShortDebugString() when the string
228 // doesn't exceed this many characters; otherwise we print it using
229 // DebugString() for better readability.
230 static const size_t kProtobufOneLinerMaxLength = 50;
231
232 template <typename T,
233 typename = typename std::enable_if<
235 static void PrintValue(const T& value, ::std::ostream* os) {
236 std::string pretty_str = value.ShortDebugString();
237 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
238 pretty_str = "\n" + value.DebugString();
239 }
240 *os << ("<" + pretty_str + ">");
241 }
242};
243
245 // Since T has no << operator or PrintTo() but can be implicitly
246 // converted to BiggestInt, we print it as a BiggestInt.
247 //
248 // Most likely T is an enum type (either named or unnamed), in which
249 // case printing it as an integer is the desired behavior. In case
250 // T is not an enum, printing it as an integer is the best we can do
251 // given that it has no user-defined printer.
252 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
253 *os << value;
254 }
255};
256
258#if GTEST_INTERNAL_HAS_STRING_VIEW
259 static void PrintValue(internal::StringView value, ::std::ostream* os) {
260 internal::UniversalPrint(value, os);
261 }
262#endif
263};
264
265
266// Prints the given number of bytes in the given object to the given
267// ostream.
268GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
269 size_t count,
270 ::std::ostream* os);
272 // SFINAE on `sizeof` to make sure we have a complete type.
273 template <typename T, size_t = sizeof(T)>
274 static void PrintValue(const T& value, ::std::ostream* os) {
275 PrintBytesInObjectTo(
276 static_cast<const unsigned char*>(
277 // Load bearing cast to void* to support iOS
278 reinterpret_cast<const void*>(std::addressof(value))),
279 sizeof(value), os);
280 }
281};
282
284 template <typename T>
285 static void PrintValue(const T&, ::std::ostream* os) {
286 *os << "(incomplete type)";
287 }
288};
289
290// Try every printer in order and return the first one that works.
291template <typename T, typename E, typename Printer, typename... Printers>
292struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
293
294template <typename T, typename Printer, typename... Printers>
296 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
297 Printer, Printers...> {
298 using type = Printer;
299};
300
301// Select the best printer in the following order:
302// - Print containers (they have begin/end/etc).
303// - Print function pointers.
304// - Print object pointers.
305// - Use the stream operator, if available.
306// - Print protocol buffers.
307// - Print types convertible to BiggestInt.
308// - Print types convertible to StringView, if available.
309// - Fallback to printing the raw bytes of the object.
310template <typename T>
311void PrintWithFallback(const T& value, ::std::ostream* os) {
312 using Printer = typename FindFirstPrinter<
317 Printer::PrintValue(value, os);
318}
319
320// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
321// value of type ToPrint that is an operand of a comparison assertion
322// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
323// the comparison, and is used to help determine the best way to
324// format the value. In particular, when the value is a C string
325// (char pointer) and the other operand is an STL string object, we
326// want to format the C string as a string, since we know it is
327// compared by value with the string object. If the value is a char
328// pointer but the other operand is not an STL string object, we don't
329// know whether the pointer is supposed to point to a NUL-terminated
330// string, and thus want to print it as a pointer to be safe.
331//
332// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
333
334// The default case.
335template <typename ToPrint, typename OtherOperand>
337 public:
338 static ::std::string Format(const ToPrint& value) {
339 return ::testing::PrintToString(value);
340 }
341};
342
343// Array.
344template <typename ToPrint, size_t N, typename OtherOperand>
345class FormatForComparison<ToPrint[N], OtherOperand> {
346 public:
347 static ::std::string Format(const ToPrint* value) {
349 }
350};
351
352// By default, print C string as pointers to be safe, as we don't know
353// whether they actually point to a NUL-terminated string.
354
355#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
356 template <typename OtherOperand> \
357 class FormatForComparison<CharType*, OtherOperand> { \
358 public: \
359 static ::std::string Format(CharType* value) { \
360 return ::testing::PrintToString(static_cast<const void*>(value)); \
361 } \
362 }
363
364GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
365GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
366GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
367GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
368#ifdef __cpp_char8_t
369GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
370GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
371#endif
372GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
373GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
374GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
375GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
376
377#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
378
379// If a C string is compared with an STL string object, we know it's meant
380// to point to a NUL-terminated string, and thus can print it as a string.
381
382#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
383 template <> \
384 class FormatForComparison<CharType*, OtherStringType> { \
385 public: \
386 static ::std::string Format(CharType* value) { \
387 return ::testing::PrintToString(value); \
388 } \
389 }
390
391GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
392GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
393#ifdef __cpp_char8_t
394GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
395GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
396#endif
397GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
398GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
399GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
400GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
401
402#if GTEST_HAS_STD_WSTRING
403GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
404GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
405#endif
406
407#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
408
409// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
410// operand to be used in a failure message. The type (but not value)
411// of the other operand may affect the format. This allows us to
412// print a char* as a raw pointer when it is compared against another
413// char* or void*, and print it as a C string when it is compared
414// against an std::string object, for example.
415//
416// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
417template <typename T1, typename T2>
418std::string FormatForComparisonFailureMessage(
419 const T1& value, const T2& /* other_operand */) {
421}
422
423// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
424// value to the given ostream. The caller must ensure that
425// 'ostream_ptr' is not NULL, or the behavior is undefined.
426//
427// We define UniversalPrinter as a class template (as opposed to a
428// function template), as we need to partially specialize it for
429// reference types, which cannot be done with function templates.
430template <typename T>
431class UniversalPrinter;
432
433// Prints the given value using the << operator if it has one;
434// otherwise prints the bytes in it. This is what
435// UniversalPrinter<T>::Print() does when PrintTo() is not specialized
436// or overloaded for type T.
437//
438// A user can override this behavior for a class type Foo by defining
439// an overload of PrintTo() in the namespace where Foo is defined. We
440// give the user this option as sometimes defining a << operator for
441// Foo is not desirable (e.g. the coding style may prevent doing it,
442// or there is already a << operator but it doesn't do what the user
443// wants).
444template <typename T>
445void PrintTo(const T& value, ::std::ostream* os) {
446 internal::PrintWithFallback(value, os);
447}
448
449// The following list of PrintTo() overloads tells
450// UniversalPrinter<T>::Print() how to print standard types (built-in
451// types, strings, plain arrays, and pointers).
452
453// Overloads for various char types.
454GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
455GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
456inline void PrintTo(char c, ::std::ostream* os) {
457 // When printing a plain char, we always treat it as unsigned. This
458 // way, the output won't be affected by whether the compiler thinks
459 // char is signed or not.
460 PrintTo(static_cast<unsigned char>(c), os);
461}
462
463// Overloads for other simple built-in types.
464inline void PrintTo(bool x, ::std::ostream* os) {
465 *os << (x ? "true" : "false");
466}
467
468// Overload for wchar_t type.
469// Prints a wchar_t as a symbol if it is printable or as its internal
470// code otherwise and also as its decimal code (except for L'\0').
471// The L'\0' char is printed as "L'\\0'". The decimal code is printed
472// as signed integer when wchar_t is implemented by the compiler
473// as a signed type and is printed as an unsigned integer when wchar_t
474// is implemented as an unsigned type.
475GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
476
477GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
478inline void PrintTo(char16_t c, ::std::ostream* os) {
479 PrintTo(ImplicitCast_<char32_t>(c), os);
480}
481#ifdef __cpp_char8_t
482inline void PrintTo(char8_t c, ::std::ostream* os) {
483 PrintTo(ImplicitCast_<char32_t>(c), os);
484}
485#endif
486
487// Overloads for C strings.
488GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
489inline void PrintTo(char* s, ::std::ostream* os) {
490 PrintTo(ImplicitCast_<const char*>(s), os);
491}
492
493// signed/unsigned char is often used for representing binary data, so
494// we print pointers to it as void* to be safe.
495inline void PrintTo(const signed char* s, ::std::ostream* os) {
496 PrintTo(ImplicitCast_<const void*>(s), os);
497}
498inline void PrintTo(signed char* s, ::std::ostream* os) {
499 PrintTo(ImplicitCast_<const void*>(s), os);
500}
501inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
502 PrintTo(ImplicitCast_<const void*>(s), os);
503}
504inline void PrintTo(unsigned char* s, ::std::ostream* os) {
505 PrintTo(ImplicitCast_<const void*>(s), os);
506}
507#ifdef __cpp_char8_t
508// Overloads for u8 strings.
509void PrintTo(const char8_t* s, ::std::ostream* os);
510inline void PrintTo(char8_t* s, ::std::ostream* os) {
511 PrintTo(ImplicitCast_<const char8_t*>(s), os);
512}
513#endif
514// Overloads for u16 strings.
515void PrintTo(const char16_t* s, ::std::ostream* os);
516inline void PrintTo(char16_t* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const char16_t*>(s), os);
518}
519// Overloads for u32 strings.
520void PrintTo(const char32_t* s, ::std::ostream* os);
521inline void PrintTo(char32_t* s, ::std::ostream* os) {
522 PrintTo(ImplicitCast_<const char32_t*>(s), os);
523}
524
525// MSVC can be configured to define wchar_t as a typedef of unsigned
526// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
527// type. When wchar_t is a typedef, defining an overload for const
528// wchar_t* would cause unsigned short* be printed as a wide string,
529// possibly causing invalid memory accesses.
530#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
531// Overloads for wide C strings
532GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
533inline void PrintTo(wchar_t* s, ::std::ostream* os) {
534 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
535}
536#endif
537
538// Overload for C arrays. Multi-dimensional arrays are printed
539// properly.
540
541// Prints the given number of elements in an array, without printing
542// the curly braces.
543template <typename T>
544void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
545 UniversalPrint(a[0], os);
546 for (size_t i = 1; i != count; i++) {
547 *os << ", ";
548 UniversalPrint(a[i], os);
549 }
550}
551
552// Overloads for ::std::string.
553GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
554inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
555 PrintStringTo(s, os);
556}
557
558// Overloads for ::std::u8string
559#ifdef __cpp_char8_t
560GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
561inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
562 PrintU8StringTo(s, os);
563}
564#endif
565
566// Overloads for ::std::u16string
567GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
568inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
569 PrintU16StringTo(s, os);
570}
571
572// Overloads for ::std::u32string
573GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
574inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
575 PrintU32StringTo(s, os);
576}
577
578// Overloads for ::std::wstring.
579#if GTEST_HAS_STD_WSTRING
580GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
581inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
582 PrintWideStringTo(s, os);
583}
584#endif // GTEST_HAS_STD_WSTRING
585
586#if GTEST_INTERNAL_HAS_STRING_VIEW
587// Overload for internal::StringView.
588inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
589 PrintTo(::std::string(sp), os);
590}
591#endif // GTEST_INTERNAL_HAS_STRING_VIEW
592
593inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
594
595template <typename T>
596void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
597 UniversalPrinter<T&>::Print(ref.get(), os);
598}
599
600inline const void* VoidifyPointer(const void* p) { return p; }
601inline const void* VoidifyPointer(volatile const void* p) {
602 return const_cast<const void*>(p);
603}
604
605template <typename T, typename Ptr>
606void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
607 if (ptr == nullptr) {
608 *os << "(nullptr)";
609 } else {
610 // We can't print the value. Just print the pointer..
611 *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
612 }
613}
614template <typename T, typename Ptr,
615 typename = typename std::enable_if<!std::is_void<T>::value &&
616 !std::is_array<T>::value>::type>
617void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
618 if (ptr == nullptr) {
619 *os << "(nullptr)";
620 } else {
621 *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
622 UniversalPrinter<T>::Print(*ptr, os);
623 *os << ")";
624 }
625}
626
627template <typename T, typename D>
628void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
629 (PrintSmartPointer<T>)(ptr, os, 0);
630}
631
632template <typename T>
633void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
634 (PrintSmartPointer<T>)(ptr, os, 0);
635}
636
637// Helper function for printing a tuple. T must be instantiated with
638// a tuple type.
639template <typename T>
640void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
641 ::std::ostream*) {}
642
643template <typename T, size_t I>
644void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
645 ::std::ostream* os) {
646 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
647 GTEST_INTENTIONAL_CONST_COND_PUSH_()
648 if (I > 1) {
649 GTEST_INTENTIONAL_CONST_COND_POP_()
650 *os << ", ";
651 }
652 UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
653 std::get<I - 1>(t), os);
654}
655
656template <typename... Types>
657void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
658 *os << "(";
659 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
660 *os << ")";
661}
662
663// Overload for std::pair.
664template <typename T1, typename T2>
665void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
666 *os << '(';
667 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
668 // a reference type. The same for printing value.second.
669 UniversalPrinter<T1>::Print(value.first, os);
670 *os << ", ";
671 UniversalPrinter<T2>::Print(value.second, os);
672 *os << ')';
673}
674
675#if GTEST_HAS_RTTI
676inline void PrintTo(const ::std::type_info& value, ::std::ostream* os) {
677 internal::PrintTo<::std::type_info>(value, os);
678 *os << " (\"" << value.name() << "\")";
679}
680
681inline void PrintTo(const ::std::type_index& value, ::std::ostream* os) {
682 internal::PrintTo<::std::type_index>(value, os);
683 *os << " (\"" << value.name() << "\")";
684}
685#endif // GTEST_HAS_RTTI
686
687// Implements printing a non-reference type T by letting the compiler
688// pick the right overload of PrintTo() for T.
689template <typename T>
691 public:
692 // MSVC warns about adding const to a function type, so we want to
693 // disable the warning.
694 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
695
696 // Note: we deliberately don't call this PrintTo(), as that name
697 // conflicts with ::testing::internal::PrintTo in the body of the
698 // function.
699 static void Print(const T& value, ::std::ostream* os) {
700 // By default, ::testing::internal::PrintTo() is used for printing
701 // the value.
702 //
703 // Thanks to Koenig look-up, if T is a class and has its own
704 // PrintTo() function defined in its namespace, that function will
705 // be visible here. Since it is more specific than the generic ones
706 // in ::testing::internal, it will be picked by the compiler in the
707 // following statement - exactly what we want.
708 PrintTo(value, os);
709 }
710
711 GTEST_DISABLE_MSC_WARNINGS_POP_()
712};
713
714// Remove any const-qualifiers before passing a type to UniversalPrinter.
715template <typename T>
716class UniversalPrinter<const T> : public UniversalPrinter<T> {};
717
718#if GTEST_INTERNAL_HAS_ANY
719
720// Printer for std::any / absl::any
721
722template <>
723class UniversalPrinter<Any> {
724 public:
725 static void Print(const Any& value, ::std::ostream* os) {
726 if (value.has_value()) {
727 *os << "value of type " << GetTypeName(value);
728 } else {
729 *os << "no value";
730 }
731 }
732
733 private:
734 static std::string GetTypeName(const Any& value) {
735#if GTEST_HAS_RTTI
736 return internal::GetTypeName(value.type());
737#else
738 static_cast<void>(value); // possibly unused
739 return "<unknown_type>";
740#endif // GTEST_HAS_RTTI
741 }
742};
743
744#endif // GTEST_INTERNAL_HAS_ANY
745
746#if GTEST_INTERNAL_HAS_OPTIONAL
747
748// Printer for std::optional / absl::optional
749
750template <typename T>
751class UniversalPrinter<Optional<T>> {
752 public:
753 static void Print(const Optional<T>& value, ::std::ostream* os) {
754 *os << '(';
755 if (!value) {
756 *os << "nullopt";
757 } else {
758 UniversalPrint(*value, os);
759 }
760 *os << ')';
761 }
762};
763
764#endif // GTEST_INTERNAL_HAS_OPTIONAL
765
766#if GTEST_INTERNAL_HAS_VARIANT
767
768// Printer for std::variant / absl::variant
769
770template <typename... T>
771class UniversalPrinter<Variant<T...>> {
772 public:
773 static void Print(const Variant<T...>& value, ::std::ostream* os) {
774 *os << '(';
775#if GTEST_HAS_ABSL
776 absl::visit(Visitor{os, value.index()}, value);
777#else
778 std::visit(Visitor{os, value.index()}, value);
779#endif // GTEST_HAS_ABSL
780 *os << ')';
781 }
782
783 private:
784 struct Visitor {
785 template <typename U>
786 void operator()(const U& u) const {
787 *os << "'" << GetTypeName<U>() << "(index = " << index
788 << ")' with value ";
789 UniversalPrint(u, os);
790 }
791 ::std::ostream* os;
792 std::size_t index;
793 };
794};
795
796#endif // GTEST_INTERNAL_HAS_VARIANT
797
798// UniversalPrintArray(begin, len, os) prints an array of 'len'
799// elements, starting at address 'begin'.
800template <typename T>
801void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
802 if (len == 0) {
803 *os << "{}";
804 } else {
805 *os << "{ ";
806 const size_t kThreshold = 18;
807 const size_t kChunkSize = 8;
808 // If the array has more than kThreshold elements, we'll have to
809 // omit some details by printing only the first and the last
810 // kChunkSize elements.
811 if (len <= kThreshold) {
812 PrintRawArrayTo(begin, len, os);
813 } else {
814 PrintRawArrayTo(begin, kChunkSize, os);
815 *os << ", ..., ";
816 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
817 }
818 *os << " }";
819 }
820}
821// This overload prints a (const) char array compactly.
822GTEST_API_ void UniversalPrintArray(
823 const char* begin, size_t len, ::std::ostream* os);
824
825#ifdef __cpp_char8_t
826// This overload prints a (const) char8_t array compactly.
827GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
828 ::std::ostream* os);
829#endif
830
831// This overload prints a (const) char16_t array compactly.
832GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
833 ::std::ostream* os);
834
835// This overload prints a (const) char32_t array compactly.
836GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
837 ::std::ostream* os);
838
839// This overload prints a (const) wchar_t array compactly.
840GTEST_API_ void UniversalPrintArray(
841 const wchar_t* begin, size_t len, ::std::ostream* os);
842
843// Implements printing an array type T[N].
844template <typename T, size_t N>
845class UniversalPrinter<T[N]> {
846 public:
847 // Prints the given array, omitting some elements when there are too
848 // many.
849 static void Print(const T (&a)[N], ::std::ostream* os) {
850 UniversalPrintArray(a, N, os);
851 }
852};
853
854// Implements printing a reference type T&.
855template <typename T>
857 public:
858 // MSVC warns about adding const to a function type, so we want to
859 // disable the warning.
860 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
861
862 static void Print(const T& value, ::std::ostream* os) {
863 // Prints the address of the value. We use reinterpret_cast here
864 // as static_cast doesn't compile when T is a function type.
865 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
866
867 // Then prints the value itself.
868 UniversalPrint(value, os);
869 }
870
871 GTEST_DISABLE_MSC_WARNINGS_POP_()
872};
873
874// Prints a value tersely: for a reference type, the referenced value
875// (but not the address) is printed; for a (const) char pointer, the
876// NUL-terminated string (but not the pointer) is printed.
877
878template <typename T>
880 public:
881 static void Print(const T& value, ::std::ostream* os) {
882 UniversalPrint(value, os);
883 }
884};
885template <typename T>
887 public:
888 static void Print(const T& value, ::std::ostream* os) {
889 UniversalPrint(value, os);
890 }
891};
892template <typename T, size_t N>
894 public:
895 static void Print(const T (&value)[N], ::std::ostream* os) {
897 }
898};
899template <>
900class UniversalTersePrinter<const char*> {
901 public:
902 static void Print(const char* str, ::std::ostream* os) {
903 if (str == nullptr) {
904 *os << "NULL";
905 } else {
906 UniversalPrint(std::string(str), os);
907 }
908 }
909};
910template <>
913
914#ifdef __cpp_char8_t
915template <>
916class UniversalTersePrinter<const char8_t*> {
917 public:
918 static void Print(const char8_t* str, ::std::ostream* os) {
919 if (str == nullptr) {
920 *os << "NULL";
921 } else {
922 UniversalPrint(::std::u8string(str), os);
923 }
924 }
925};
926template <>
927class UniversalTersePrinter<char8_t*>
928 : public UniversalTersePrinter<const char8_t*> {};
929#endif
930
931template <>
932class UniversalTersePrinter<const char16_t*> {
933 public:
934 static void Print(const char16_t* str, ::std::ostream* os) {
935 if (str == nullptr) {
936 *os << "NULL";
937 } else {
938 UniversalPrint(::std::u16string(str), os);
939 }
940 }
941};
942template <>
945
946template <>
947class UniversalTersePrinter<const char32_t*> {
948 public:
949 static void Print(const char32_t* str, ::std::ostream* os) {
950 if (str == nullptr) {
951 *os << "NULL";
952 } else {
953 UniversalPrint(::std::u32string(str), os);
954 }
955 }
956};
957template <>
960
961#if GTEST_HAS_STD_WSTRING
962template <>
963class UniversalTersePrinter<const wchar_t*> {
964 public:
965 static void Print(const wchar_t* str, ::std::ostream* os) {
966 if (str == nullptr) {
967 *os << "NULL";
968 } else {
969 UniversalPrint(::std::wstring(str), os);
970 }
971 }
972};
973#endif
974
975template <>
976class UniversalTersePrinter<wchar_t*> {
977 public:
978 static void Print(wchar_t* str, ::std::ostream* os) {
980 }
981};
982
983template <typename T>
984void UniversalTersePrint(const T& value, ::std::ostream* os) {
986}
987
988// Prints a value using the type inferred by the compiler. The
989// difference between this and UniversalTersePrint() is that for a
990// (const) char pointer, this prints both the pointer and the
991// NUL-terminated string.
992template <typename T>
993void UniversalPrint(const T& value, ::std::ostream* os) {
994 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
995 // UniversalPrinter with T directly.
996 typedef T T1;
997 UniversalPrinter<T1>::Print(value, os);
998}
999
1000typedef ::std::vector< ::std::string> Strings;
1001
1002 // Tersely prints the first N fields of a tuple to a string vector,
1003 // one element for each field.
1004template <typename Tuple>
1005void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1006 Strings*) {}
1007template <typename Tuple, size_t I>
1008void TersePrintPrefixToStrings(const Tuple& t,
1009 std::integral_constant<size_t, I>,
1010 Strings* strings) {
1011 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1012 strings);
1013 ::std::stringstream ss;
1014 UniversalTersePrint(std::get<I - 1>(t), &ss);
1015 strings->push_back(ss.str());
1016}
1017
1018// Prints the fields of a tuple tersely to a string vector, one
1019// element for each field. See the comment before
1020// UniversalTersePrint() for how we define "tersely".
1021template <typename Tuple>
1022Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1023 Strings result;
1024 TersePrintPrefixToStrings(
1025 value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
1026 &result);
1027 return result;
1028}
1029
1030} // namespace internal
1031
1032template <typename T>
1033::std::string PrintToString(const T& value) {
1034 ::std::stringstream ss;
1035 internal::UniversalTersePrinter<T>::Print(value, &ss);
1036 return ss.str();
1037}
1038
1039} // namespace testing
1040
1041// Include any custom printer added by the local installation.
1042// We must include this header at the end to make sure it can use the
1043// declarations from this file.
1044#include "gtest/internal/custom/gtest-printers.h"
1045
1046#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
Definition gtest-printers.h:336
Definition gtest-printers.h:690
Definition gtest-printers.h:879
Definition gtest-printers.h:132
Definition gtest-printers.h:244
Definition gtest-printers.h:283
Definition gtest-printers.h:292
Definition gtest-printers.h:169
Definition gtest-internal.h:1007
Definition gtest-printers.h:184
Definition gtest-printers.h:226
Definition gtest-printers.h:271