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