- 3.0.1 core module.
gtest-printers.h
Go to the documentation of this file.
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 GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
101 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102 
103 #include <functional>
104 #include <ostream> // NOLINT
105 #include <sstream>
106 #include <string>
107 #include <tuple>
108 #include <type_traits>
109 #include <utility>
110 #include <vector>
113 
114 #if GTEST_HAS_ABSL
115 #include "absl/strings/string_view.h"
116 #include "absl/types/optional.h"
117 #include "absl/types/variant.h"
118 #endif // GTEST_HAS_ABSL
119 
120 namespace testing {
121 
122 // Definitions in the 'internal' and 'internal2' name spaces are
123 // subject to change without notice. DO NOT USE THEM IN USER CODE!
124 namespace internal2 {
125 
126 // Prints the given number of bytes in the given object to the given
127 // ostream.
128 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
129  size_t count,
130  ::std::ostream* os);
131 
132 // For selecting which printer to use when a given type has neither <<
133 // nor PrintTo().
134 enum TypeKind {
135  kProtobuf, // a protobuf type
136  kConvertibleToInteger, // a type implicitly convertible to BiggestInt
137  // (e.g. a named or unnamed enum type)
138 #if GTEST_HAS_ABSL
139  kConvertibleToStringView, // a type implicitly convertible to
140  // absl::string_view
141 #endif
142  kOtherType // anything else
143 };
144 
145 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
146 // by the universal printer to print a value of type T when neither
147 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
148 // "kind" of T as defined by enum TypeKind.
149 template <typename T, TypeKind kTypeKind>
151  public:
152  // This default version is called when kTypeKind is kOtherType.
153  static void PrintValue(const T& value, ::std::ostream* os) {
154  PrintBytesInObjectTo(static_cast<const unsigned char*>(
155  reinterpret_cast<const void*>(&value)),
156  sizeof(value), os);
157  }
158 };
159 
160 // We print a protobuf using its ShortDebugString() when the string
161 // doesn't exceed this many characters; otherwise we print it using
162 // DebugString() for better readability.
163 const size_t kProtobufOneLinerMaxLength = 50;
164 
165 template <typename T>
167  public:
168  static void PrintValue(const T& value, ::std::ostream* os) {
169  std::string pretty_str = value.ShortDebugString();
170  if (pretty_str.length() > kProtobufOneLinerMaxLength) {
171  pretty_str = "\n" + value.DebugString();
172  }
173  *os << ("<" + pretty_str + ">");
174  }
175 };
176 
177 template <typename T>
179  public:
180  // Since T has no << operator or PrintTo() but can be implicitly
181  // converted to BiggestInt, we print it as a BiggestInt.
182  //
183  // Most likely T is an enum type (either named or unnamed), in which
184  // case printing it as an integer is the desired behavior. In case
185  // T is not an enum, printing it as an integer is the best we can do
186  // given that it has no user-defined printer.
187  static void PrintValue(const T& value, ::std::ostream* os) {
188  const internal::BiggestInt kBigInt = value;
189  *os << kBigInt;
190  }
191 };
192 
193 #if GTEST_HAS_ABSL
194 template <typename T>
195 class TypeWithoutFormatter<T, kConvertibleToStringView> {
196  public:
197  // Since T has neither operator<< nor PrintTo() but can be implicitly
198  // converted to absl::string_view, we print it as a absl::string_view.
199  //
200  // Note: the implementation is further below, as it depends on
201  // internal::PrintTo symbol which is defined later in the file.
202  static void PrintValue(const T& value, ::std::ostream* os);
203 };
204 #endif
205 
206 // Prints the given value to the given ostream. If the value is a
207 // protocol message, its debug string is printed; if it's an enum or
208 // of a type implicitly convertible to BiggestInt, it's printed as an
209 // integer; otherwise the bytes in the value are printed. This is
210 // what UniversalPrinter<T>::Print() does when it knows nothing about
211 // type T and T has neither << operator nor PrintTo().
212 //
213 // A user can override this behavior for a class type Foo by defining
214 // a << operator in the namespace where Foo is defined.
215 //
216 // We put this operator in namespace 'internal2' instead of 'internal'
217 // to simplify the implementation, as much code in 'internal' needs to
218 // use << in STL, which would conflict with our own << were it defined
219 // in 'internal'.
220 //
221 // Note that this operator<< takes a generic std::basic_ostream<Char,
222 // CharTraits> type instead of the more restricted std::ostream. If
223 // we define it to take an std::ostream instead, we'll get an
224 // "ambiguous overloads" compiler error when trying to print a type
225 // Foo that supports streaming to std::basic_ostream<Char,
226 // CharTraits>, as the compiler cannot tell whether
227 // operator<<(std::ostream&, const T&) or
228 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
229 // specific.
230 template <typename Char, typename CharTraits, typename T>
231 ::std::basic_ostream<Char, CharTraits>& operator<<(
232  ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
234  ? kProtobuf
235  : std::is_convertible<
236  const T&, internal::BiggestInt>::value
238  :
239 #if GTEST_HAS_ABSL
240  std::is_convertible<
241  const T&, absl::string_view>::value
242  ? kConvertibleToStringView
243  :
244 #endif
245  kOtherType)>::PrintValue(x, &os);
246  return os;
247 }
248 
249 } // namespace internal2
250 } // namespace testing
251 
252 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
253 // magic needed for implementing UniversalPrinter won't work.
254 namespace testing_internal {
255 
256 // Used to print a value that is not an STL-style container when the
257 // user doesn't define PrintTo() for it.
258 template <typename T>
259 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
260  // With the following statement, during unqualified name lookup,
261  // testing::internal2::operator<< appears as if it was declared in
262  // the nearest enclosing namespace that contains both
263  // ::testing_internal and ::testing::internal2, i.e. the global
264  // namespace. For more details, refer to the C++ Standard section
265  // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
266  // testing::internal2::operator<< in case T doesn't come with a <<
267  // operator.
268  //
269  // We cannot write 'using ::testing::internal2::operator<<;', which
270  // gcc 3.3 fails to compile due to a compiler bug.
271  using namespace ::testing::internal2; // NOLINT
272 
273  // Assuming T is defined in namespace foo, in the next statement,
274  // the compiler will consider all of:
275  //
276  // 1. foo::operator<< (thanks to Koenig look-up),
277  // 2. ::operator<< (as the current namespace is enclosed in ::),
278  // 3. testing::internal2::operator<< (thanks to the using statement above).
279  //
280  // The operator<< whose type matches T best will be picked.
281  //
282  // We deliberately allow #2 to be a candidate, as sometimes it's
283  // impossible to define #1 (e.g. when foo is ::std, defining
284  // anything in it is undefined behavior unless you are a compiler
285  // vendor.).
286  *os << value;
287 }
288 
289 } // namespace testing_internal
290 
291 namespace testing {
292 namespace internal {
293 
294 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
295 // value of type ToPrint that is an operand of a comparison assertion
296 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
297 // the comparison, and is used to help determine the best way to
298 // format the value. In particular, when the value is a C string
299 // (char pointer) and the other operand is an STL string object, we
300 // want to format the C string as a string, since we know it is
301 // compared by value with the string object. If the value is a char
302 // pointer but the other operand is not an STL string object, we don't
303 // know whether the pointer is supposed to point to a NUL-terminated
304 // string, and thus want to print it as a pointer to be safe.
305 //
306 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
307 
308 // The default case.
309 template <typename ToPrint, typename OtherOperand>
311  public:
312  static ::std::string Format(const ToPrint& value) {
314  }
315 };
316 
317 // Array.
318 template <typename ToPrint, size_t N, typename OtherOperand>
319 class FormatForComparison<ToPrint[N], OtherOperand> {
320  public:
321  static ::std::string Format(const ToPrint* value) {
323  }
324 };
325 
326 // By default, print C string as pointers to be safe, as we don't know
327 // whether they actually point to a NUL-terminated string.
328 
329 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
330  template <typename OtherOperand> \
331  class FormatForComparison<CharType*, OtherOperand> { \
332  public: \
333  static ::std::string Format(CharType* value) { \
334  return ::testing::PrintToString(static_cast<const void*>(value)); \
335  } \
336  }
337 
342 
343 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
344 
345 // If a C string is compared with an STL string object, we know it's meant
346 // to point to a NUL-terminated string, and thus can print it as a string.
347 
348 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
349  template <> \
350  class FormatForComparison<CharType*, OtherStringType> { \
351  public: \
352  static ::std::string Format(CharType* value) { \
353  return ::testing::PrintToString(value); \
354  } \
355  }
356 
357 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
358 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
359 
360 #if GTEST_HAS_STD_WSTRING
361 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
362 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
363 #endif
364 
365 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
366 
367 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
368 // operand to be used in a failure message. The type (but not value)
369 // of the other operand may affect the format. This allows us to
370 // print a char* as a raw pointer when it is compared against another
371 // char* or void*, and print it as a C string when it is compared
372 // against an std::string object, for example.
373 //
374 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
375 template <typename T1, typename T2>
377  const T1& value, const T2& /* other_operand */) {
379 }
380 
381 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
382 // value to the given ostream. The caller must ensure that
383 // 'ostream_ptr' is not NULL, or the behavior is undefined.
384 //
385 // We define UniversalPrinter as a class template (as opposed to a
386 // function template), as we need to partially specialize it for
387 // reference types, which cannot be done with function templates.
388 template <typename T>
390 
391 template <typename T>
392 void UniversalPrint(const T& value, ::std::ostream* os);
393 
399 };
400 template <DefaultPrinterType type> struct WrapPrinterType {};
401 
402 // Used to print an STL-style container when the user doesn't define
403 // a PrintTo() for it.
404 template <typename C>
406  const C& container, ::std::ostream* os) {
407  const size_t kMaxCount = 32; // The maximum number of elements to print.
408  *os << '{';
409  size_t count = 0;
410  for (typename C::const_iterator it = container.begin();
411  it != container.end(); ++it, ++count) {
412  if (count > 0) {
413  *os << ',';
414  if (count == kMaxCount) { // Enough has been printed.
415  *os << " ...";
416  break;
417  }
418  }
419  *os << ' ';
420  // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
421  // handle *it being a native array.
422  internal::UniversalPrint(*it, os);
423  }
424 
425  if (count > 0) {
426  *os << ' ';
427  }
428  *os << '}';
429 }
430 
431 // Used to print a pointer that is neither a char pointer nor a member
432 // pointer, when the user doesn't define PrintTo() for it. (A member
433 // variable pointer or member function pointer doesn't really point to
434 // a location in the address space. Their representation is
435 // implementation-defined. Therefore they will be printed as raw
436 // bytes.)
437 template <typename T>
439  T* p, ::std::ostream* os) {
440  if (p == nullptr) {
441  *os << "NULL";
442  } else {
443  // T is not a function type. We just call << to print p,
444  // relying on ADL to pick up user-defined << for their pointer
445  // types, if any.
446  *os << p;
447  }
448 }
449 template <typename T>
451  T* p, ::std::ostream* os) {
452  if (p == nullptr) {
453  *os << "NULL";
454  } else {
455  // T is a function type, so '*os << p' doesn't do what we want
456  // (it just prints p as bool). We want to print p as a const
457  // void*.
458  *os << reinterpret_cast<const void*>(p);
459  }
460 }
461 
462 // Used to print a non-container, non-pointer value when the user
463 // doesn't define PrintTo() for it.
464 template <typename T>
466  const T& value, ::std::ostream* os) {
468 }
469 
470 // Prints the given value using the << operator if it has one;
471 // otherwise prints the bytes in it. This is what
472 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
473 // or overloaded for type T.
474 //
475 // A user can override this behavior for a class type Foo by defining
476 // an overload of PrintTo() in the namespace where Foo is defined. We
477 // give the user this option as sometimes defining a << operator for
478 // Foo is not desirable (e.g. the coding style may prevent doing it,
479 // or there is already a << operator but it doesn't do what the user
480 // wants).
481 template <typename T>
482 void PrintTo(const T& value, ::std::ostream* os) {
483  // DefaultPrintTo() is overloaded. The type of its first argument
484  // determines which version will be picked.
485  //
486  // Note that we check for container types here, prior to we check
487  // for protocol message types in our operator<<. The rationale is:
488  //
489  // For protocol messages, we want to give people a chance to
490  // override Google Mock's format by defining a PrintTo() or
491  // operator<<. For STL containers, other formats can be
492  // incompatible with Google Mock's format for the container
493  // elements; therefore we check for container types here to ensure
494  // that our format is used.
495  //
496  // Note that MSVC and clang-cl do allow an implicit conversion from
497  // pointer-to-function to pointer-to-object, but clang-cl warns on it.
498  // So don't use ImplicitlyConvertible if it can be helped since it will
499  // cause this warning, and use a separate overload of DefaultPrintTo for
500  // function pointers so that the `*os << p` in the object pointer overload
501  // doesn't cause that warning either.
504  (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
508  ? kPrintOther
509  : std::is_function<typename std::remove_pointer<T>::type>::value
511  : kPrintPointer > (),
512  value, os);
513 }
514 
515 // The following list of PrintTo() overloads tells
516 // UniversalPrinter<T>::Print() how to print standard types (built-in
517 // types, strings, plain arrays, and pointers).
518 
519 // Overloads for various char types.
520 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
521 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
522 inline void PrintTo(char c, ::std::ostream* os) {
523  // When printing a plain char, we always treat it as unsigned. This
524  // way, the output won't be affected by whether the compiler thinks
525  // char is signed or not.
526  PrintTo(static_cast<unsigned char>(c), os);
527 }
528 
529 // Overloads for other simple built-in types.
530 inline void PrintTo(bool x, ::std::ostream* os) {
531  *os << (x ? "true" : "false");
532 }
533 
534 // Overload for wchar_t type.
535 // Prints a wchar_t as a symbol if it is printable or as its internal
536 // code otherwise and also as its decimal code (except for L'\0').
537 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
538 // as signed integer when wchar_t is implemented by the compiler
539 // as a signed type and is printed as an unsigned integer when wchar_t
540 // is implemented as an unsigned type.
541 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
542 
543 // Overloads for C strings.
544 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
545 inline void PrintTo(char* s, ::std::ostream* os) {
546  PrintTo(ImplicitCast_<const char*>(s), os);
547 }
548 
549 // signed/unsigned char is often used for representing binary data, so
550 // we print pointers to it as void* to be safe.
551 inline void PrintTo(const signed char* s, ::std::ostream* os) {
552  PrintTo(ImplicitCast_<const void*>(s), os);
553 }
554 inline void PrintTo(signed char* s, ::std::ostream* os) {
555  PrintTo(ImplicitCast_<const void*>(s), os);
556 }
557 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
558  PrintTo(ImplicitCast_<const void*>(s), os);
559 }
560 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
561  PrintTo(ImplicitCast_<const void*>(s), os);
562 }
563 
564 // MSVC can be configured to define wchar_t as a typedef of unsigned
565 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
566 // type. When wchar_t is a typedef, defining an overload for const
567 // wchar_t* would cause unsigned short* be printed as a wide string,
568 // possibly causing invalid memory accesses.
569 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
570 // Overloads for wide C strings
571 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
572 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
573  PrintTo(ImplicitCast_<const wchar_t*>(s), os);
574 }
575 #endif
576 
577 // Overload for C arrays. Multi-dimensional arrays are printed
578 // properly.
579 
580 // Prints the given number of elements in an array, without printing
581 // the curly braces.
582 template <typename T>
583 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
584  UniversalPrint(a[0], os);
585  for (size_t i = 1; i != count; i++) {
586  *os << ", ";
587  UniversalPrint(a[i], os);
588  }
589 }
590 
591 // Overloads for ::std::string.
592 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
593 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
594  PrintStringTo(s, os);
595 }
596 
597 // Overloads for ::std::wstring.
598 #if GTEST_HAS_STD_WSTRING
599 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
600 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
601  PrintWideStringTo(s, os);
602 }
603 #endif // GTEST_HAS_STD_WSTRING
604 
605 #if GTEST_HAS_ABSL
606 // Overload for absl::string_view.
607 inline void PrintTo(absl::string_view sp, ::std::ostream* os) {
608  PrintTo(::std::string(sp), os);
609 }
610 #endif // GTEST_HAS_ABSL
611 
612 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
613 
614 template <typename T>
615 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
616  UniversalPrinter<T&>::Print(ref.get(), os);
617 }
618 
619 // Helper function for printing a tuple. T must be instantiated with
620 // a tuple type.
621 template <typename T>
622 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
623  ::std::ostream*) {}
624 
625 template <typename T, size_t I>
626 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
627  ::std::ostream* os) {
628  PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
630  if (I > 1) {
632  *os << ", ";
633  }
634  UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
635  std::get<I - 1>(t), os);
636 }
637 
638 template <typename... Types>
639 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
640  *os << "(";
641  PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
642  *os << ")";
643 }
644 
645 // Overload for std::pair.
646 template <typename T1, typename T2>
647 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
648  *os << '(';
649  // We cannot use UniversalPrint(value.first, os) here, as T1 may be
650  // a reference type. The same for printing value.second.
651  UniversalPrinter<T1>::Print(value.first, os);
652  *os << ", ";
653  UniversalPrinter<T2>::Print(value.second, os);
654  *os << ')';
655 }
656 
657 // Implements printing a non-reference type T by letting the compiler
658 // pick the right overload of PrintTo() for T.
659 template <typename T>
660 class UniversalPrinter {
661  public:
662  // MSVC warns about adding const to a function type, so we want to
663  // disable the warning.
665 
666  // Note: we deliberately don't call this PrintTo(), as that name
667  // conflicts with ::testing::internal::PrintTo in the body of the
668  // function.
669  static void Print(const T& value, ::std::ostream* os) {
670  // By default, ::testing::internal::PrintTo() is used for printing
671  // the value.
672  //
673  // Thanks to Koenig look-up, if T is a class and has its own
674  // PrintTo() function defined in its namespace, that function will
675  // be visible here. Since it is more specific than the generic ones
676  // in ::testing::internal, it will be picked by the compiler in the
677  // following statement - exactly what we want.
678  PrintTo(value, os);
679  }
680 
682 };
683 
684 #if GTEST_HAS_ABSL
685 
686 // Printer for absl::optional
687 
688 template <typename T>
689 class UniversalPrinter<::absl::optional<T>> {
690  public:
691  static void Print(const ::absl::optional<T>& value, ::std::ostream* os) {
692  *os << '(';
693  if (!value) {
694  *os << "nullopt";
695  } else {
696  UniversalPrint(*value, os);
697  }
698  *os << ')';
699  }
700 };
701 
702 // Printer for absl::variant
703 
704 template <typename... T>
705 class UniversalPrinter<::absl::variant<T...>> {
706  public:
707  static void Print(const ::absl::variant<T...>& value, ::std::ostream* os) {
708  *os << '(';
709  absl::visit(Visitor{os}, value);
710  *os << ')';
711  }
712 
713  private:
714  struct Visitor {
715  template <typename U>
716  void operator()(const U& u) const {
717  *os << "'" << GetTypeName<U>() << "' with value ";
718  UniversalPrint(u, os);
719  }
720  ::std::ostream* os;
721  };
722 };
723 
724 #endif // GTEST_HAS_ABSL
725 
726 // UniversalPrintArray(begin, len, os) prints an array of 'len'
727 // elements, starting at address 'begin'.
728 template <typename T>
729 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
730  if (len == 0) {
731  *os << "{}";
732  } else {
733  *os << "{ ";
734  const size_t kThreshold = 18;
735  const size_t kChunkSize = 8;
736  // If the array has more than kThreshold elements, we'll have to
737  // omit some details by printing only the first and the last
738  // kChunkSize elements.
739  if (len <= kThreshold) {
740  PrintRawArrayTo(begin, len, os);
741  } else {
742  PrintRawArrayTo(begin, kChunkSize, os);
743  *os << ", ..., ";
744  PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
745  }
746  *os << " }";
747  }
748 }
749 // This overload prints a (const) char array compactly.
751  const char* begin, size_t len, ::std::ostream* os);
752 
753 // This overload prints a (const) wchar_t array compactly.
755  const wchar_t* begin, size_t len, ::std::ostream* os);
756 
757 // Implements printing an array type T[N].
758 template <typename T, size_t N>
759 class UniversalPrinter<T[N]> {
760  public:
761  // Prints the given array, omitting some elements when there are too
762  // many.
763  static void Print(const T (&a)[N], ::std::ostream* os) {
764  UniversalPrintArray(a, N, os);
765  }
766 };
767 
768 // Implements printing a reference type T&.
769 template <typename T>
770 class UniversalPrinter<T&> {
771  public:
772  // MSVC warns about adding const to a function type, so we want to
773  // disable the warning.
775 
776  static void Print(const T& value, ::std::ostream* os) {
777  // Prints the address of the value. We use reinterpret_cast here
778  // as static_cast doesn't compile when T is a function type.
779  *os << "@" << reinterpret_cast<const void*>(&value) << " ";
780 
781  // Then prints the value itself.
782  UniversalPrint(value, os);
783  }
784 
786 };
787 
788 // Prints a value tersely: for a reference type, the referenced value
789 // (but not the address) is printed; for a (const) char pointer, the
790 // NUL-terminated string (but not the pointer) is printed.
791 
792 template <typename T>
794  public:
795  static void Print(const T& value, ::std::ostream* os) {
796  UniversalPrint(value, os);
797  }
798 };
799 template <typename T>
801  public:
802  static void Print(const T& value, ::std::ostream* os) {
803  UniversalPrint(value, os);
804  }
805 };
806 template <typename T, size_t N>
808  public:
809  static void Print(const T (&value)[N], ::std::ostream* os) {
811  }
812 };
813 template <>
815  public:
816  static void Print(const char* str, ::std::ostream* os) {
817  if (str == nullptr) {
818  *os << "NULL";
819  } else {
820  UniversalPrint(std::string(str), os);
821  }
822  }
823 };
824 template <>
825 class UniversalTersePrinter<char*> {
826  public:
827  static void Print(char* str, ::std::ostream* os) {
829  }
830 };
831 
832 #if GTEST_HAS_STD_WSTRING
833 template <>
834 class UniversalTersePrinter<const wchar_t*> {
835  public:
836  static void Print(const wchar_t* str, ::std::ostream* os) {
837  if (str == nullptr) {
838  *os << "NULL";
839  } else {
840  UniversalPrint(::std::wstring(str), os);
841  }
842  }
843 };
844 #endif
845 
846 template <>
847 class UniversalTersePrinter<wchar_t*> {
848  public:
849  static void Print(wchar_t* str, ::std::ostream* os) {
851  }
852 };
853 
854 template <typename T>
855 void UniversalTersePrint(const T& value, ::std::ostream* os) {
857 }
858 
859 // Prints a value using the type inferred by the compiler. The
860 // difference between this and UniversalTersePrint() is that for a
861 // (const) char pointer, this prints both the pointer and the
862 // NUL-terminated string.
863 template <typename T>
864 void UniversalPrint(const T& value, ::std::ostream* os) {
865  // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
866  // UniversalPrinter with T directly.
867  typedef T T1;
868  UniversalPrinter<T1>::Print(value, os);
869 }
870 
871 typedef ::std::vector< ::std::string> Strings;
872 
873  // Tersely prints the first N fields of a tuple to a string vector,
874  // one element for each field.
875 template <typename Tuple>
876 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
877  Strings*) {}
878 template <typename Tuple, size_t I>
879 void TersePrintPrefixToStrings(const Tuple& t,
880  std::integral_constant<size_t, I>,
881  Strings* strings) {
882  TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
883  strings);
884  ::std::stringstream ss;
885  UniversalTersePrint(std::get<I - 1>(t), &ss);
886  strings->push_back(ss.str());
887 }
888 
889 // Prints the fields of a tuple tersely to a string vector, one
890 // element for each field. See the comment before
891 // UniversalTersePrint() for how we define "tersely".
892 template <typename Tuple>
894  Strings result;
896  value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
897  &result);
898  return result;
899 }
900 
901 } // namespace internal
902 
903 #if GTEST_HAS_ABSL
904 namespace internal2 {
905 template <typename T>
907  const T& value, ::std::ostream* os) {
908  internal::PrintTo(absl::string_view(value), os);
909 }
910 } // namespace internal2
911 #endif
912 
913 template <typename T>
914 ::std::string PrintToString(const T& value) {
915  ::std::stringstream ss;
917  return ss.str();
918 }
919 
920 } // namespace testing
921 
922 // Include any custom printer added by the local installation.
923 // We must include this header at the end to make sure it can use the
924 // declarations from this file.
926 
927 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
static void Print(const T &value,::std::ostream *os)
Definition: gtest-printers.h:795
const char * p
Definition: gmock-matchers_test.cc:3613
void TersePrintPrefixToStrings(const Tuple &t, std::integral_constant< size_t, I >, Strings *strings)
Definition: gtest-printers.h:879
Definition: gtest-printers.h:396
::std::string Format(const ToPrint &value)
Definition: gtest-printers.h:312
void PrintTupleTo(const T &t, std::integral_constant< size_t, I >,::std::ostream *os)
Definition: gtest-printers.h:626
Definition: gtest-printers.h:142
Definition: gmock-actions.h:59
static void PrintValue(const T &value,::std::ostream *os)
Definition: gtest-printers.h:187
int * count
Definition: gmock_stress_test.cc:96
void DefaultPrintTo(WrapPrinterType< kPrintOther >, const T &value,::std::ostream *os)
Definition: gtest-printers.h:465
void PrintRawArrayTo(const T a[], size_t count,::std::ostream *os)
Definition: gtest-printers.h:583
static void Print(wchar_t *str,::std::ostream *os)
Definition: gtest-printers.h:849
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition: gtest-port.h:711
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:314
int IsContainer
Definition: gtest-internal.h:929
static void Print(const T &value,::std::ostream *os)
Definition: gtest-printers.h:802
GTEST_API_ void PrintStringTo(const ::std::string &s,::std::ostream *os)
::std::string PrintToString(const T &value)
Definition: gtest-printers.h:914
ct::core::ControlVector< control_dim > u
static void Print(const T(&value)[N],::std::ostream *os)
Definition: gtest-printers.h:809
Definition: gtest-printers.h:150
void UniversalTersePrint(const T &value,::std::ostream *os)
Definition: gtest-printers.h:855
Definition: gtest-printers.h:793
std::string Print(const T &value)
Definition: googletest-printers-test.cc:233
void DefaultPrintNonContainerTo(const T &value,::std::ostream *os)
Definition: gtest-printers.h:259
::std::vector< ::std::string > Strings
Definition: gtest-printers.h:871
static void Print(const T(&a)[N],::std::ostream *os)
Definition: gtest-printers.h:763
::std::basic_ostream< Char, CharTraits > & operator<<(::std::basic_ostream< Char, CharTraits > &os, const T &x)
Definition: gtest-printers.h:231
#define GTEST_API_
Definition: gtest-port.h:759
static void Print(const char *str,::std::ostream *os)
Definition: gtest-printers.h:816
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
Definition: gtest-printers.h:376
int i
Definition: gmock-matchers_test.cc:711
static void PrintValue(const T &value,::std::ostream *os)
Definition: gtest-printers.h:153
const size_t kProtobufOneLinerMaxLength
Definition: gtest-printers.h:163
#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType)
Definition: gtest-printers.h:348
TypeKind
Definition: gtest-printers.h:134
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:313
GTEST_API_ void UniversalPrintArray(const wchar_t *begin, size_t len,::std::ostream *os)
DefaultPrinterType
Definition: gtest-printers.h:394
Definition: gtest-printers.h:395
void UniversalPrint(const T &value,::std::ostream *os)
Definition: gtest-printers.h:864
long long BiggestInt
Definition: gtest-port.h:1964
Definition: gtest-printers.h:400
Definition: gtest-printers.h:389
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count,::std::ostream *os)
Definition: gtest-printers.h:254
Definition: gtest-printers.h:398
void PrintTo(const T &value,::std::ostream *os)
Definition: gtest-printers.h:482
int value
Definition: gmock-matchers_test.cc:657
#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)
Definition: gtest-printers.h:329
Definition: gtest-printers.h:136
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
Definition: gtest-printers.h:893
int x
Definition: gmock-matchers_test.cc:3610
type
Definition: upload.py:443
static void Print(char *str,::std::ostream *os)
Definition: gtest-printers.h:827
const
Definition: upload.py:398
void PrintTo(const ::std::pair< T1, T2 > &value,::std::ostream *os)
Definition: gtest-printers.h:647
::std::string Format(const ToPrint *value)
Definition: gtest-printers.h:321
static void PrintValue(const T &value,::std::ostream *os)
Definition: gtest-printers.h:168
Definition: gtest-internal.h:992
Definition: gtest-printers.h:124
Definition: gtest-printers.h:397
Definition: gtest-printers.h:310
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition: gtest-port.h:713
Definition: gtest-printers.h:135