blob: be793bbb151e104a11c5b21cbb55fa4b0cfdab31 [file] [log] [blame]
Jay Foad22a83d62011-07-27 09:25:14 +00001// 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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Test - The Google C++ Testing Framework
33//
34// This file implements a universal value printer that can print a
35// value of any type T:
36//
37// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38//
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
43// foo):
44//
45// 1. foo::PrintTo(const T&, ostream*)
46// 2. operator<<(ostream&, const T&) defined in either foo or the
47// global namespace.
48//
49// If none of the above is defined, it will print the debug string of
50// the value if it is a protocol buffer, or print the raw bytes in the
51// value otherwise.
52//
53// To aid debugging: when T is a reference type, the address of the
54// value is also printed; when T is a (const) char pointer, both the
55// pointer value and the NUL-terminated string it points to are
56// printed.
57//
58// We also provide some convenient wrappers:
59//
60// // Prints a value to a string. For a (const or not) char
61// // pointer, the NUL-terminated string (but not the pointer) is
62// // printed.
63// std::string ::testing::PrintToString(const T& value);
64//
65// // Prints a value tersely: for a reference type, the referenced
66// // value (but not the address) is printed; for a (const or not) char
67// // pointer, the NUL-terminated string (but not the pointer) is
68// // printed.
69// void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
70//
71// // Prints value using the type inferred by the compiler. The difference
72// // from UniversalTersePrint() is that this function prints both the
73// // pointer and the NUL-terminated string for a (const or not) char pointer.
74// void ::testing::internal::UniversalPrint(const T& value, ostream*);
75//
76// // Prints the fields of a tuple tersely to a string vector, one
77// // element for each field. Tuple support must be enabled in
78// // gtest-port.h.
79// std::vector<string> UniversalTersePrintTupleFieldsToStrings(
80// const Tuple& value);
81//
82// Known limitation:
83//
84// The print primitives print the elements of an STL-style container
85// using the compiler-inferred type of *iter where iter is a
86// const_iterator of the container. When const_iterator is an input
87// iterator but not a forward iterator, this inferred type may not
88// match value_type, and the print output may be incorrect. In
89// practice, this is rarely a problem as for most containers
90// const_iterator is a forward iterator. We'll fix this if there's an
91// actual need for it. Note that this fix cannot rely on value_type
92// being defined as many user-defined container types don't have
93// value_type.
94
95#ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96#define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
97
98#include <ostream> // NOLINT
99#include <sstream>
100#include <string>
101#include <utility>
102#include <vector>
103#include "gtest/internal/gtest-port.h"
104#include "gtest/internal/gtest-internal.h"
Sam McCall2d8242d2018-02-12 10:20:09 +0000105#include "gtest/internal/custom/raw-ostream.h"
Jay Foad22a83d62011-07-27 09:25:14 +0000106
Chandler Carrutha9775822017-01-04 23:06:03 +0000107#if GTEST_HAS_STD_TUPLE_
108# include <tuple>
109#endif
110
Jay Foad22a83d62011-07-27 09:25:14 +0000111namespace testing {
112
113// Definitions in the 'internal' and 'internal2' name spaces are
114// subject to change without notice. DO NOT USE THEM IN USER CODE!
115namespace internal2 {
116
117// Prints the given number of bytes in the given object to the given
118// ostream.
119GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
120 size_t count,
121 ::std::ostream* os);
122
123// For selecting which printer to use when a given type has neither <<
124// nor PrintTo().
125enum TypeKind {
126 kProtobuf, // a protobuf type
127 kConvertibleToInteger, // a type implicitly convertible to BiggestInt
128 // (e.g. a named or unnamed enum type)
129 kOtherType // anything else
130};
131
132// TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
133// by the universal printer to print a value of type T when neither
134// operator<< nor PrintTo() is defined for T, where kTypeKind is the
135// "kind" of T as defined by enum TypeKind.
136template <typename T, TypeKind kTypeKind>
137class TypeWithoutFormatter {
138 public:
139 // This default version is called when kTypeKind is kOtherType.
140 static void PrintValue(const T& value, ::std::ostream* os) {
141 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
142 sizeof(value), os);
143 }
144};
145
146// We print a protobuf using its ShortDebugString() when the string
147// doesn't exceed this many characters; otherwise we print it using
148// DebugString() for better readability.
149const size_t kProtobufOneLinerMaxLength = 50;
150
151template <typename T>
152class TypeWithoutFormatter<T, kProtobuf> {
153 public:
154 static void PrintValue(const T& value, ::std::ostream* os) {
155 const ::testing::internal::string short_str = value.ShortDebugString();
156 const ::testing::internal::string pretty_str =
157 short_str.length() <= kProtobufOneLinerMaxLength ?
158 short_str : ("\n" + value.DebugString());
159 *os << ("<" + pretty_str + ">");
160 }
161};
162
163template <typename T>
164class TypeWithoutFormatter<T, kConvertibleToInteger> {
165 public:
166 // Since T has no << operator or PrintTo() but can be implicitly
167 // converted to BiggestInt, we print it as a BiggestInt.
168 //
169 // Most likely T is an enum type (either named or unnamed), in which
170 // case printing it as an integer is the desired behavior. In case
171 // T is not an enum, printing it as an integer is the best we can do
172 // given that it has no user-defined printer.
173 static void PrintValue(const T& value, ::std::ostream* os) {
174 const internal::BiggestInt kBigInt = value;
175 *os << kBigInt;
176 }
177};
178
179// Prints the given value to the given ostream. If the value is a
180// protocol message, its debug string is printed; if it's an enum or
181// of a type implicitly convertible to BiggestInt, it's printed as an
182// integer; otherwise the bytes in the value are printed. This is
183// what UniversalPrinter<T>::Print() does when it knows nothing about
184// type T and T has neither << operator nor PrintTo().
185//
186// A user can override this behavior for a class type Foo by defining
187// a << operator in the namespace where Foo is defined.
188//
189// We put this operator in namespace 'internal2' instead of 'internal'
190// to simplify the implementation, as much code in 'internal' needs to
191// use << in STL, which would conflict with our own << were it defined
192// in 'internal'.
193//
194// Note that this operator<< takes a generic std::basic_ostream<Char,
195// CharTraits> type instead of the more restricted std::ostream. If
196// we define it to take an std::ostream instead, we'll get an
197// "ambiguous overloads" compiler error when trying to print a type
198// Foo that supports streaming to std::basic_ostream<Char,
199// CharTraits>, as the compiler cannot tell whether
200// operator<<(std::ostream&, const T&) or
201// operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
202// specific.
203template <typename Char, typename CharTraits, typename T>
204::std::basic_ostream<Char, CharTraits>& operator<<(
205 ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
206 TypeWithoutFormatter<T,
207 (internal::IsAProtocolMessage<T>::value ? kProtobuf :
208 internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
209 kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
210 return os;
211}
212
213} // namespace internal2
214} // namespace testing
215
216// This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
217// magic needed for implementing UniversalPrinter won't work.
218namespace testing_internal {
219
220// Used to print a value that is not an STL-style container when the
221// user doesn't define PrintTo() for it.
222template <typename T>
223void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
224 // With the following statement, during unqualified name lookup,
225 // testing::internal2::operator<< appears as if it was declared in
226 // the nearest enclosing namespace that contains both
227 // ::testing_internal and ::testing::internal2, i.e. the global
228 // namespace. For more details, refer to the C++ Standard section
229 // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
230 // testing::internal2::operator<< in case T doesn't come with a <<
231 // operator.
232 //
233 // We cannot write 'using ::testing::internal2::operator<<;', which
234 // gcc 3.3 fails to compile due to a compiler bug.
235 using namespace ::testing::internal2; // NOLINT
236
237 // Assuming T is defined in namespace foo, in the next statement,
238 // the compiler will consider all of:
239 //
240 // 1. foo::operator<< (thanks to Koenig look-up),
241 // 2. ::operator<< (as the current namespace is enclosed in ::),
242 // 3. testing::internal2::operator<< (thanks to the using statement above).
243 //
244 // The operator<< whose type matches T best will be picked.
245 //
246 // We deliberately allow #2 to be a candidate, as sometimes it's
247 // impossible to define #1 (e.g. when foo is ::std, defining
248 // anything in it is undefined behavior unless you are a compiler
249 // vendor.).
Sam McCall2d8242d2018-02-12 10:20:09 +0000250 *os << ::llvm_gtest::printable(value);
Jay Foad22a83d62011-07-27 09:25:14 +0000251}
252
253} // namespace testing_internal
254
255namespace testing {
256namespace internal {
257
Chandler Carrutha9775822017-01-04 23:06:03 +0000258// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
259// value of type ToPrint that is an operand of a comparison assertion
260// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
261// the comparison, and is used to help determine the best way to
262// format the value. In particular, when the value is a C string
263// (char pointer) and the other operand is an STL string object, we
264// want to format the C string as a string, since we know it is
265// compared by value with the string object. If the value is a char
266// pointer but the other operand is not an STL string object, we don't
267// know whether the pointer is supposed to point to a NUL-terminated
268// string, and thus want to print it as a pointer to be safe.
269//
270// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
271
272// The default case.
273template <typename ToPrint, typename OtherOperand>
274class FormatForComparison {
275 public:
276 static ::std::string Format(const ToPrint& value) {
277 return ::testing::PrintToString(value);
278 }
279};
280
281// Array.
282template <typename ToPrint, size_t N, typename OtherOperand>
283class FormatForComparison<ToPrint[N], OtherOperand> {
284 public:
285 static ::std::string Format(const ToPrint* value) {
286 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
287 }
288};
289
290// By default, print C string as pointers to be safe, as we don't know
291// whether they actually point to a NUL-terminated string.
292
293#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
294 template <typename OtherOperand> \
295 class FormatForComparison<CharType*, OtherOperand> { \
296 public: \
297 static ::std::string Format(CharType* value) { \
298 return ::testing::PrintToString(static_cast<const void*>(value)); \
299 } \
300 }
301
302GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
303GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
304GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
305GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
306
307#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
308
309// If a C string is compared with an STL string object, we know it's meant
310// to point to a NUL-terminated string, and thus can print it as a string.
311
312#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
313 template <> \
314 class FormatForComparison<CharType*, OtherStringType> { \
315 public: \
316 static ::std::string Format(CharType* value) { \
317 return ::testing::PrintToString(value); \
318 } \
319 }
320
321GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
322GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
323
324#if GTEST_HAS_GLOBAL_STRING
325GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string);
326GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string);
327#endif
328
329#if GTEST_HAS_GLOBAL_WSTRING
330GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring);
331GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring);
332#endif
333
334#if GTEST_HAS_STD_WSTRING
335GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
336GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
337#endif
338
339#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
340
341// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
342// operand to be used in a failure message. The type (but not value)
343// of the other operand may affect the format. This allows us to
344// print a char* as a raw pointer when it is compared against another
345// char* or void*, and print it as a C string when it is compared
346// against an std::string object, for example.
347//
348// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
349template <typename T1, typename T2>
350std::string FormatForComparisonFailureMessage(
351 const T1& value, const T2& /* other_operand */) {
352 return FormatForComparison<T1, T2>::Format(value);
353}
354
Jay Foad22a83d62011-07-27 09:25:14 +0000355// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
356// value to the given ostream. The caller must ensure that
357// 'ostream_ptr' is not NULL, or the behavior is undefined.
358//
359// We define UniversalPrinter as a class template (as opposed to a
360// function template), as we need to partially specialize it for
361// reference types, which cannot be done with function templates.
362template <typename T>
363class UniversalPrinter;
364
365template <typename T>
366void UniversalPrint(const T& value, ::std::ostream* os);
367
368// Used to print an STL-style container when the user doesn't define
369// a PrintTo() for it.
370template <typename C>
371void DefaultPrintTo(IsContainer /* dummy */,
372 false_type /* is not a pointer */,
373 const C& container, ::std::ostream* os) {
374 const size_t kMaxCount = 32; // The maximum number of elements to print.
375 *os << '{';
376 size_t count = 0;
377 for (typename C::const_iterator it = container.begin();
378 it != container.end(); ++it, ++count) {
379 if (count > 0) {
380 *os << ',';
381 if (count == kMaxCount) { // Enough has been printed.
382 *os << " ...";
383 break;
384 }
385 }
386 *os << ' ';
387 // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
388 // handle *it being a native array.
389 internal::UniversalPrint(*it, os);
390 }
391
392 if (count > 0) {
393 *os << ' ';
394 }
395 *os << '}';
396}
397
398// Used to print a pointer that is neither a char pointer nor a member
399// pointer, when the user doesn't define PrintTo() for it. (A member
400// variable pointer or member function pointer doesn't really point to
401// a location in the address space. Their representation is
402// implementation-defined. Therefore they will be printed as raw
403// bytes.)
404template <typename T>
405void DefaultPrintTo(IsNotContainer /* dummy */,
406 true_type /* is a pointer */,
407 T* p, ::std::ostream* os) {
408 if (p == NULL) {
409 *os << "NULL";
410 } else {
411 // C++ doesn't allow casting from a function pointer to any object
412 // pointer.
413 //
414 // IsTrue() silences warnings: "Condition is always true",
415 // "unreachable code".
416 if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
417 // T is not a function type. We just call << to print p,
418 // relying on ADL to pick up user-defined << for their pointer
419 // types, if any.
420 *os << p;
421 } else {
422 // T is a function type, so '*os << p' doesn't do what we want
423 // (it just prints p as bool). We want to print p as a const
424 // void*. However, we cannot cast it to const void* directly,
425 // even using reinterpret_cast, as earlier versions of gcc
426 // (e.g. 3.4.5) cannot compile the cast when p is a function
427 // pointer. Casting to UInt64 first solves the problem.
428 *os << reinterpret_cast<const void*>(
429 reinterpret_cast<internal::UInt64>(p));
430 }
431 }
432}
433
434// Used to print a non-container, non-pointer value when the user
435// doesn't define PrintTo() for it.
436template <typename T>
437void DefaultPrintTo(IsNotContainer /* dummy */,
438 false_type /* is not a pointer */,
439 const T& value, ::std::ostream* os) {
440 ::testing_internal::DefaultPrintNonContainerTo(value, os);
441}
442
443// Prints the given value using the << operator if it has one;
444// otherwise prints the bytes in it. This is what
445// UniversalPrinter<T>::Print() does when PrintTo() is not specialized
446// or overloaded for type T.
447//
448// A user can override this behavior for a class type Foo by defining
449// an overload of PrintTo() in the namespace where Foo is defined. We
450// give the user this option as sometimes defining a << operator for
451// Foo is not desirable (e.g. the coding style may prevent doing it,
452// or there is already a << operator but it doesn't do what the user
453// wants).
454template <typename T>
455void PrintTo(const T& value, ::std::ostream* os) {
456 // DefaultPrintTo() is overloaded. The type of its first two
457 // arguments determine which version will be picked. If T is an
458 // STL-style container, the version for container will be called; if
459 // T is a pointer, the pointer version will be called; otherwise the
460 // generic version will be called.
461 //
462 // Note that we check for container types here, prior to we check
463 // for protocol message types in our operator<<. The rationale is:
464 //
465 // For protocol messages, we want to give people a chance to
466 // override Google Mock's format by defining a PrintTo() or
467 // operator<<. For STL containers, other formats can be
468 // incompatible with Google Mock's format for the container
469 // elements; therefore we check for container types here to ensure
470 // that our format is used.
471 //
472 // The second argument of DefaultPrintTo() is needed to bypass a bug
473 // in Symbian's C++ compiler that prevents it from picking the right
474 // overload between:
475 //
476 // PrintTo(const T& x, ...);
477 // PrintTo(T* x, ...);
478 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
479}
480
481// The following list of PrintTo() overloads tells
482// UniversalPrinter<T>::Print() how to print standard types (built-in
483// types, strings, plain arrays, and pointers).
484
485// Overloads for various char types.
486GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
487GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
488inline void PrintTo(char c, ::std::ostream* os) {
489 // When printing a plain char, we always treat it as unsigned. This
490 // way, the output won't be affected by whether the compiler thinks
491 // char is signed or not.
492 PrintTo(static_cast<unsigned char>(c), os);
493}
494
495// Overloads for other simple built-in types.
496inline void PrintTo(bool x, ::std::ostream* os) {
497 *os << (x ? "true" : "false");
498}
499
500// Overload for wchar_t type.
501// Prints a wchar_t as a symbol if it is printable or as its internal
502// code otherwise and also as its decimal code (except for L'\0').
503// The L'\0' char is printed as "L'\\0'". The decimal code is printed
504// as signed integer when wchar_t is implemented by the compiler
505// as a signed type and is printed as an unsigned integer when wchar_t
506// is implemented as an unsigned type.
507GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
508
509// Overloads for C strings.
510GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
511inline void PrintTo(char* s, ::std::ostream* os) {
512 PrintTo(ImplicitCast_<const char*>(s), os);
513}
514
515// signed/unsigned char is often used for representing binary data, so
516// we print pointers to it as void* to be safe.
517inline void PrintTo(const signed char* s, ::std::ostream* os) {
518 PrintTo(ImplicitCast_<const void*>(s), os);
519}
520inline void PrintTo(signed char* s, ::std::ostream* os) {
521 PrintTo(ImplicitCast_<const void*>(s), os);
522}
523inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
524 PrintTo(ImplicitCast_<const void*>(s), os);
525}
526inline void PrintTo(unsigned char* s, ::std::ostream* os) {
527 PrintTo(ImplicitCast_<const void*>(s), os);
528}
529
530// MSVC can be configured to define wchar_t as a typedef of unsigned
531// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
532// type. When wchar_t is a typedef, defining an overload for const
533// wchar_t* would cause unsigned short* be printed as a wide string,
534// possibly causing invalid memory accesses.
535#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
536// Overloads for wide C strings
537GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
538inline void PrintTo(wchar_t* s, ::std::ostream* os) {
539 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
540}
541#endif
542
543// Overload for C arrays. Multi-dimensional arrays are printed
544// properly.
545
546// Prints the given number of elements in an array, without printing
547// the curly braces.
548template <typename T>
549void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
550 UniversalPrint(a[0], os);
551 for (size_t i = 1; i != count; i++) {
552 *os << ", ";
553 UniversalPrint(a[i], os);
554 }
555}
556
557// Overloads for ::string and ::std::string.
558#if GTEST_HAS_GLOBAL_STRING
559GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
560inline void PrintTo(const ::string& s, ::std::ostream* os) {
561 PrintStringTo(s, os);
562}
563#endif // GTEST_HAS_GLOBAL_STRING
564
565GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
566inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
567 PrintStringTo(s, os);
568}
569
570// Overloads for ::wstring and ::std::wstring.
571#if GTEST_HAS_GLOBAL_WSTRING
572GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
573inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
574 PrintWideStringTo(s, os);
575}
576#endif // GTEST_HAS_GLOBAL_WSTRING
577
578#if GTEST_HAS_STD_WSTRING
579GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
580inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
581 PrintWideStringTo(s, os);
582}
583#endif // GTEST_HAS_STD_WSTRING
584
Chandler Carrutha9775822017-01-04 23:06:03 +0000585#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
Jay Foad22a83d62011-07-27 09:25:14 +0000586// Helper function for printing a tuple. T must be instantiated with
587// a tuple type.
588template <typename T>
589void PrintTupleTo(const T& t, ::std::ostream* os);
Chandler Carrutha9775822017-01-04 23:06:03 +0000590#endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
591
592#if GTEST_HAS_TR1_TUPLE
593// Overload for ::std::tr1::tuple. Needed for printing function arguments,
594// which are packed as tuples.
Jay Foad22a83d62011-07-27 09:25:14 +0000595
596// Overloaded PrintTo() for tuples of various arities. We support
597// tuples of up-to 10 fields. The following implementation works
598// regardless of whether tr1::tuple is implemented using the
599// non-standard variadic template feature or not.
600
601inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
602 PrintTupleTo(t, os);
603}
604
605template <typename T1>
606void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
607 PrintTupleTo(t, os);
608}
609
610template <typename T1, typename T2>
611void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
612 PrintTupleTo(t, os);
613}
614
615template <typename T1, typename T2, typename T3>
616void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
617 PrintTupleTo(t, os);
618}
619
620template <typename T1, typename T2, typename T3, typename T4>
621void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
622 PrintTupleTo(t, os);
623}
624
625template <typename T1, typename T2, typename T3, typename T4, typename T5>
626void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
627 ::std::ostream* os) {
628 PrintTupleTo(t, os);
629}
630
631template <typename T1, typename T2, typename T3, typename T4, typename T5,
632 typename T6>
633void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
634 ::std::ostream* os) {
635 PrintTupleTo(t, os);
636}
637
638template <typename T1, typename T2, typename T3, typename T4, typename T5,
639 typename T6, typename T7>
640void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
641 ::std::ostream* os) {
642 PrintTupleTo(t, os);
643}
644
645template <typename T1, typename T2, typename T3, typename T4, typename T5,
646 typename T6, typename T7, typename T8>
647void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
648 ::std::ostream* os) {
649 PrintTupleTo(t, os);
650}
651
652template <typename T1, typename T2, typename T3, typename T4, typename T5,
653 typename T6, typename T7, typename T8, typename T9>
654void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
655 ::std::ostream* os) {
656 PrintTupleTo(t, os);
657}
658
659template <typename T1, typename T2, typename T3, typename T4, typename T5,
660 typename T6, typename T7, typename T8, typename T9, typename T10>
661void PrintTo(
662 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
663 ::std::ostream* os) {
664 PrintTupleTo(t, os);
665}
666#endif // GTEST_HAS_TR1_TUPLE
667
Chandler Carrutha9775822017-01-04 23:06:03 +0000668#if GTEST_HAS_STD_TUPLE_
669template <typename... Types>
670void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
671 PrintTupleTo(t, os);
672}
673#endif // GTEST_HAS_STD_TUPLE_
674
Jay Foad22a83d62011-07-27 09:25:14 +0000675// Overload for std::pair.
676template <typename T1, typename T2>
677void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
678 *os << '(';
679 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
680 // a reference type. The same for printing value.second.
681 UniversalPrinter<T1>::Print(value.first, os);
682 *os << ", ";
683 UniversalPrinter<T2>::Print(value.second, os);
684 *os << ')';
685}
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>
690class UniversalPrinter {
691 public:
692 // MSVC warns about adding const to a function type, so we want to
693 // disable the warning.
Chandler Carrutha9775822017-01-04 23:06:03 +0000694 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
Jay Foad22a83d62011-07-27 09:25:14 +0000695
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
Chandler Carrutha9775822017-01-04 23:06:03 +0000711 GTEST_DISABLE_MSC_WARNINGS_POP_()
Jay Foad22a83d62011-07-27 09:25:14 +0000712};
713
714// UniversalPrintArray(begin, len, os) prints an array of 'len'
715// elements, starting at address 'begin'.
716template <typename T>
717void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
718 if (len == 0) {
719 *os << "{}";
720 } else {
721 *os << "{ ";
722 const size_t kThreshold = 18;
723 const size_t kChunkSize = 8;
724 // If the array has more than kThreshold elements, we'll have to
725 // omit some details by printing only the first and the last
726 // kChunkSize elements.
727 // TODO(wan@google.com): let the user control the threshold using a flag.
728 if (len <= kThreshold) {
729 PrintRawArrayTo(begin, len, os);
730 } else {
731 PrintRawArrayTo(begin, kChunkSize, os);
732 *os << ", ..., ";
733 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
734 }
735 *os << " }";
736 }
737}
738// This overload prints a (const) char array compactly.
Chandler Carrutha9775822017-01-04 23:06:03 +0000739GTEST_API_ void UniversalPrintArray(
740 const char* begin, size_t len, ::std::ostream* os);
741
742// This overload prints a (const) wchar_t array compactly.
743GTEST_API_ void UniversalPrintArray(
744 const wchar_t* begin, size_t len, ::std::ostream* os);
Jay Foad22a83d62011-07-27 09:25:14 +0000745
746// Implements printing an array type T[N].
747template <typename T, size_t N>
748class UniversalPrinter<T[N]> {
749 public:
750 // Prints the given array, omitting some elements when there are too
751 // many.
752 static void Print(const T (&a)[N], ::std::ostream* os) {
753 UniversalPrintArray(a, N, os);
754 }
755};
756
757// Implements printing a reference type T&.
758template <typename T>
759class UniversalPrinter<T&> {
760 public:
761 // MSVC warns about adding const to a function type, so we want to
762 // disable the warning.
Chandler Carrutha9775822017-01-04 23:06:03 +0000763 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
Jay Foad22a83d62011-07-27 09:25:14 +0000764
765 static void Print(const T& value, ::std::ostream* os) {
766 // Prints the address of the value. We use reinterpret_cast here
767 // as static_cast doesn't compile when T is a function type.
768 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
769
770 // Then prints the value itself.
771 UniversalPrint(value, os);
772 }
773
Chandler Carrutha9775822017-01-04 23:06:03 +0000774 GTEST_DISABLE_MSC_WARNINGS_POP_()
Jay Foad22a83d62011-07-27 09:25:14 +0000775};
776
777// Prints a value tersely: for a reference type, the referenced value
778// (but not the address) is printed; for a (const) char pointer, the
779// NUL-terminated string (but not the pointer) is printed.
Chandler Carrutha9775822017-01-04 23:06:03 +0000780
781template <typename T>
782class UniversalTersePrinter {
783 public:
784 static void Print(const T& value, ::std::ostream* os) {
785 UniversalPrint(value, os);
786 }
787};
788template <typename T>
789class UniversalTersePrinter<T&> {
790 public:
791 static void Print(const T& value, ::std::ostream* os) {
792 UniversalPrint(value, os);
793 }
794};
795template <typename T, size_t N>
796class UniversalTersePrinter<T[N]> {
797 public:
798 static void Print(const T (&value)[N], ::std::ostream* os) {
799 UniversalPrinter<T[N]>::Print(value, os);
800 }
801};
802template <>
803class UniversalTersePrinter<const char*> {
804 public:
805 static void Print(const char* str, ::std::ostream* os) {
806 if (str == NULL) {
807 *os << "NULL";
808 } else {
809 UniversalPrint(string(str), os);
810 }
811 }
812};
813template <>
814class UniversalTersePrinter<char*> {
815 public:
816 static void Print(char* str, ::std::ostream* os) {
817 UniversalTersePrinter<const char*>::Print(str, os);
818 }
819};
820
821#if GTEST_HAS_STD_WSTRING
822template <>
823class UniversalTersePrinter<const wchar_t*> {
824 public:
825 static void Print(const wchar_t* str, ::std::ostream* os) {
826 if (str == NULL) {
827 *os << "NULL";
828 } else {
829 UniversalPrint(::std::wstring(str), os);
830 }
831 }
832};
833#endif
834
835template <>
836class UniversalTersePrinter<wchar_t*> {
837 public:
838 static void Print(wchar_t* str, ::std::ostream* os) {
839 UniversalTersePrinter<const wchar_t*>::Print(str, os);
840 }
841};
842
Jay Foad22a83d62011-07-27 09:25:14 +0000843template <typename T>
844void UniversalTersePrint(const T& value, ::std::ostream* os) {
Chandler Carrutha9775822017-01-04 23:06:03 +0000845 UniversalTersePrinter<T>::Print(value, os);
Jay Foad22a83d62011-07-27 09:25:14 +0000846}
847
848// Prints a value using the type inferred by the compiler. The
849// difference between this and UniversalTersePrint() is that for a
850// (const) char pointer, this prints both the pointer and the
851// NUL-terminated string.
852template <typename T>
853void UniversalPrint(const T& value, ::std::ostream* os) {
Chandler Carrutha9775822017-01-04 23:06:03 +0000854 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
855 // UniversalPrinter with T directly.
856 typedef T T1;
857 UniversalPrinter<T1>::Print(value, os);
Jay Foad22a83d62011-07-27 09:25:14 +0000858}
859
Jay Foad22a83d62011-07-27 09:25:14 +0000860typedef ::std::vector<string> Strings;
861
Chandler Carrutha9775822017-01-04 23:06:03 +0000862// TuplePolicy<TupleT> must provide:
863// - tuple_size
864// size of tuple TupleT.
865// - get<size_t I>(const TupleT& t)
866// static function extracting element I of tuple TupleT.
867// - tuple_element<size_t I>::type
868// type of element I of tuple TupleT.
869template <typename TupleT>
870struct TuplePolicy;
871
872#if GTEST_HAS_TR1_TUPLE
873template <typename TupleT>
874struct TuplePolicy {
875 typedef TupleT Tuple;
876 static const size_t tuple_size = ::std::tr1::tuple_size<Tuple>::value;
877
878 template <size_t I>
879 struct tuple_element : ::std::tr1::tuple_element<I, Tuple> {};
880
881 template <size_t I>
882 static typename AddReference<
883 const typename ::std::tr1::tuple_element<I, Tuple>::type>::type get(
884 const Tuple& tuple) {
885 return ::std::tr1::get<I>(tuple);
886 }
887};
888template <typename TupleT>
889const size_t TuplePolicy<TupleT>::tuple_size;
890#endif // GTEST_HAS_TR1_TUPLE
891
892#if GTEST_HAS_STD_TUPLE_
893template <typename... Types>
894struct TuplePolicy< ::std::tuple<Types...> > {
895 typedef ::std::tuple<Types...> Tuple;
896 static const size_t tuple_size = ::std::tuple_size<Tuple>::value;
897
898 template <size_t I>
899 struct tuple_element : ::std::tuple_element<I, Tuple> {};
900
901 template <size_t I>
902 static const typename ::std::tuple_element<I, Tuple>::type& get(
903 const Tuple& tuple) {
904 return ::std::get<I>(tuple);
905 }
906};
907template <typename... Types>
908const size_t TuplePolicy< ::std::tuple<Types...> >::tuple_size;
909#endif // GTEST_HAS_STD_TUPLE_
910
911#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
Jay Foad22a83d62011-07-27 09:25:14 +0000912// This helper template allows PrintTo() for tuples and
913// UniversalTersePrintTupleFieldsToStrings() to be defined by
914// induction on the number of tuple fields. The idea is that
915// TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
916// fields in tuple t, and can be defined in terms of
917// TuplePrefixPrinter<N - 1>.
Chandler Carrutha9775822017-01-04 23:06:03 +0000918//
Jay Foad22a83d62011-07-27 09:25:14 +0000919// The inductive case.
920template <size_t N>
921struct TuplePrefixPrinter {
922 // Prints the first N fields of a tuple.
923 template <typename Tuple>
924 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
925 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
Chandler Carrutha9775822017-01-04 23:06:03 +0000926 GTEST_INTENTIONAL_CONST_COND_PUSH_()
927 if (N > 1) {
928 GTEST_INTENTIONAL_CONST_COND_POP_()
929 *os << ", ";
930 }
931 UniversalPrinter<
932 typename TuplePolicy<Tuple>::template tuple_element<N - 1>::type>
933 ::Print(TuplePolicy<Tuple>::template get<N - 1>(t), os);
Jay Foad22a83d62011-07-27 09:25:14 +0000934 }
935
936 // Tersely prints the first N fields of a tuple to a string vector,
937 // one element for each field.
938 template <typename Tuple>
939 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
940 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
941 ::std::stringstream ss;
Chandler Carrutha9775822017-01-04 23:06:03 +0000942 UniversalTersePrint(TuplePolicy<Tuple>::template get<N - 1>(t), &ss);
Jay Foad22a83d62011-07-27 09:25:14 +0000943 strings->push_back(ss.str());
944 }
945};
946
Chandler Carrutha9775822017-01-04 23:06:03 +0000947// Base case.
Jay Foad22a83d62011-07-27 09:25:14 +0000948template <>
949struct TuplePrefixPrinter<0> {
950 template <typename Tuple>
951 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
952
953 template <typename Tuple>
954 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
955};
Jay Foad22a83d62011-07-27 09:25:14 +0000956
Chandler Carrutha9775822017-01-04 23:06:03 +0000957// Helper function for printing a tuple.
958// Tuple must be either std::tr1::tuple or std::tuple type.
959template <typename Tuple>
960void PrintTupleTo(const Tuple& t, ::std::ostream* os) {
Jay Foad22a83d62011-07-27 09:25:14 +0000961 *os << "(";
Chandler Carrutha9775822017-01-04 23:06:03 +0000962 TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::PrintPrefixTo(t, os);
Jay Foad22a83d62011-07-27 09:25:14 +0000963 *os << ")";
964}
965
966// Prints the fields of a tuple tersely to a string vector, one
967// element for each field. See the comment before
968// UniversalTersePrint() for how we define "tersely".
969template <typename Tuple>
970Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
971 Strings result;
Chandler Carrutha9775822017-01-04 23:06:03 +0000972 TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::
Jay Foad22a83d62011-07-27 09:25:14 +0000973 TersePrintPrefixToStrings(value, &result);
974 return result;
975}
Chandler Carrutha9775822017-01-04 23:06:03 +0000976#endif // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
Jay Foad22a83d62011-07-27 09:25:14 +0000977
978} // namespace internal
979
980template <typename T>
981::std::string PrintToString(const T& value) {
982 ::std::stringstream ss;
Chandler Carrutha9775822017-01-04 23:06:03 +0000983 internal::UniversalTersePrinter<T>::Print(value, &ss);
Jay Foad22a83d62011-07-27 09:25:14 +0000984 return ss.str();
985}
986
987} // namespace testing
988
Chandler Carrutha9775822017-01-04 23:06:03 +0000989// Include any custom printer added by the local installation.
990// We must include this header at the end to make sure it can use the
991// declarations from this file.
992#include "gtest/internal/custom/gtest-printers.h"
993
Jay Foad22a83d62011-07-27 09:25:14 +0000994#endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_