gtest-printers.h (29699B)
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> 111 #include "gtest/internal/gtest-internal.h" 112 #include "gtest/internal/gtest-port.h" 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* namespaces are subject to change without notice. 123 // DO NOT USE THEM IN USER CODE! 124 namespace internal { 125 126 template <typename T> 127 void UniversalPrint(const T& value, ::std::ostream* os); 128 129 // Used to print an STL-style container when the user doesn't define 130 // a PrintTo() for it. 131 struct ContainerPrinter { 132 template <typename T, 133 typename = typename std::enable_if< 134 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) && 135 !IsRecursiveContainer<T>::value>::type> 136 static void PrintValue(const T& container, std::ostream* os) { 137 const size_t kMaxCount = 32; // The maximum number of elements to print. 138 *os << '{'; 139 size_t count = 0; 140 for (auto&& elem : container) { 141 if (count > 0) { 142 *os << ','; 143 if (count == kMaxCount) { // Enough has been printed. 144 *os << " ..."; 145 break; 146 } 147 } 148 *os << ' '; 149 // We cannot call PrintTo(elem, os) here as PrintTo() doesn't 150 // handle `elem` being a native array. 151 internal::UniversalPrint(elem, os); 152 ++count; 153 } 154 155 if (count > 0) { 156 *os << ' '; 157 } 158 *os << '}'; 159 } 160 }; 161 162 // Used to print a pointer that is neither a char pointer nor a member 163 // pointer, when the user doesn't define PrintTo() for it. (A member 164 // variable pointer or member function pointer doesn't really point to 165 // a location in the address space. Their representation is 166 // implementation-defined. Therefore they will be printed as raw 167 // bytes.) 168 struct FunctionPointerPrinter { 169 template <typename T, typename = typename std::enable_if< 170 std::is_function<T>::value>::type> 171 static void PrintValue(T* p, ::std::ostream* os) { 172 if (p == nullptr) { 173 *os << "NULL"; 174 } else { 175 // T is a function type, so '*os << p' doesn't do what we want 176 // (it just prints p as bool). We want to print p as a const 177 // void*. 178 *os << reinterpret_cast<const void*>(p); 179 } 180 } 181 }; 182 183 struct PointerPrinter { 184 template <typename T> 185 static void PrintValue(T* p, ::std::ostream* os) { 186 if (p == nullptr) { 187 *os << "NULL"; 188 } else { 189 // T is not a function type. We just call << to print p, 190 // relying on ADL to pick up user-defined << for their pointer 191 // types, if any. 192 *os << p; 193 } 194 } 195 }; 196 197 namespace internal_stream { 198 199 struct Sentinel; 200 template <typename Char, typename CharTraits, typename T> 201 Sentinel* operator<<(::std::basic_ostream<Char, CharTraits>& os, const T& x); 202 203 // Check if the user has a user-defined operator<< for their type. 204 // 205 // We put this in its own namespace to inject a custom operator<< that allows us 206 // to probe the type's operator. 207 // 208 // Note that this operator<< takes a generic std::basic_ostream<Char, 209 // CharTraits> type instead of the more restricted std::ostream. If 210 // we define it to take an std::ostream instead, we'll get an 211 // "ambiguous overloads" compiler error when trying to print a type 212 // Foo that supports streaming to std::basic_ostream<Char, 213 // CharTraits>, as the compiler cannot tell whether 214 // operator<<(std::ostream&, const T&) or 215 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more 216 // specific. 217 template <typename T> 218 constexpr bool UseStreamOperator() { 219 return !std::is_same<decltype(std::declval<std::ostream&>() 220 << std::declval<const T&>()), 221 Sentinel*>::value; 222 } 223 224 } // namespace internal_stream 225 226 struct StreamPrinter { 227 template <typename T, typename = typename std::enable_if< 228 internal_stream::UseStreamOperator<T>()>::type> 229 static void PrintValue(const T& value, ::std::ostream* os) { 230 *os << value; 231 } 232 }; 233 234 struct ProtobufPrinter { 235 // We print a protobuf using its ShortDebugString() when the string 236 // doesn't exceed this many characters; otherwise we print it using 237 // DebugString() for better readability. 238 static const size_t kProtobufOneLinerMaxLength = 50; 239 240 template <typename T, typename = typename std::enable_if< 241 internal::IsAProtocolMessage<T>::value>::type> 242 static void PrintValue(const T& value, ::std::ostream* os) { 243 std::string pretty_str = value.ShortDebugString(); 244 if (pretty_str.length() > kProtobufOneLinerMaxLength) { 245 pretty_str = "\n" + value.DebugString(); 246 } 247 *os << ("<" + pretty_str + ">"); 248 } 249 }; 250 251 struct ConvertibleToIntegerPrinter { 252 // Since T has no << operator or PrintTo() but can be implicitly 253 // converted to BiggestInt, we print it as a BiggestInt. 254 // 255 // Most likely T is an enum type (either named or unnamed), in which 256 // case printing it as an integer is the desired behavior. In case 257 // T is not an enum, printing it as an integer is the best we can do 258 // given that it has no user-defined printer. 259 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) { 260 *os << value; 261 } 262 }; 263 264 struct ConvertibleToStringViewPrinter { 265 #if GTEST_INTERNAL_HAS_STRING_VIEW 266 static void PrintValue(internal::StringView value, ::std::ostream* os) { 267 internal::UniversalPrint(value, os); 268 } 269 #endif 270 }; 271 272 273 // Prints the given number of bytes in the given object to the given 274 // ostream. 275 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, 276 size_t count, 277 ::std::ostream* os); 278 struct FallbackPrinter { 279 template <typename T> 280 static void PrintValue(const T& value, ::std::ostream* os) { 281 PrintBytesInObjectTo( 282 static_cast<const unsigned char*>( 283 reinterpret_cast<const void*>(std::addressof(value))), 284 sizeof(value), os); 285 } 286 }; 287 288 // Try every printer in order and return the first one that works. 289 template <typename T, typename E, typename Printer, typename... Printers> 290 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {}; 291 292 template <typename T, typename Printer, typename... Printers> 293 struct FindFirstPrinter< 294 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)), 295 Printer, Printers...> { 296 using type = Printer; 297 }; 298 299 // Select the best printer in the following order: 300 // - Print containers (they have begin/end/etc). 301 // - Print function pointers. 302 // - Print object pointers. 303 // - Use the stream operator, if available. 304 // - Print protocol buffers. 305 // - Print types convertible to BiggestInt. 306 // - Print types convertible to StringView, if available. 307 // - Fallback to printing the raw bytes of the object. 308 template <typename T> 309 void PrintWithFallback(const T& value, ::std::ostream* os) { 310 using Printer = typename FindFirstPrinter< 311 T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, 312 StreamPrinter, ProtobufPrinter, ConvertibleToIntegerPrinter, 313 ConvertibleToStringViewPrinter, FallbackPrinter>::type; 314 Printer::PrintValue(value, os); 315 } 316 317 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a 318 // value of type ToPrint that is an operand of a comparison assertion 319 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in 320 // the comparison, and is used to help determine the best way to 321 // format the value. In particular, when the value is a C string 322 // (char pointer) and the other operand is an STL string object, we 323 // want to format the C string as a string, since we know it is 324 // compared by value with the string object. If the value is a char 325 // pointer but the other operand is not an STL string object, we don't 326 // know whether the pointer is supposed to point to a NUL-terminated 327 // string, and thus want to print it as a pointer to be safe. 328 // 329 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 330 331 // The default case. 332 template <typename ToPrint, typename OtherOperand> 333 class FormatForComparison { 334 public: 335 static ::std::string Format(const ToPrint& value) { 336 return ::testing::PrintToString(value); 337 } 338 }; 339 340 // Array. 341 template <typename ToPrint, size_t N, typename OtherOperand> 342 class FormatForComparison<ToPrint[N], OtherOperand> { 343 public: 344 static ::std::string Format(const ToPrint* value) { 345 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value); 346 } 347 }; 348 349 // By default, print C string as pointers to be safe, as we don't know 350 // whether they actually point to a NUL-terminated string. 351 352 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ 353 template <typename OtherOperand> \ 354 class FormatForComparison<CharType*, OtherOperand> { \ 355 public: \ 356 static ::std::string Format(CharType* value) { \ 357 return ::testing::PrintToString(static_cast<const void*>(value)); \ 358 } \ 359 } 360 361 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); 362 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); 363 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); 364 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); 365 366 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ 367 368 // If a C string is compared with an STL string object, we know it's meant 369 // to point to a NUL-terminated string, and thus can print it as a string. 370 371 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ 372 template <> \ 373 class FormatForComparison<CharType*, OtherStringType> { \ 374 public: \ 375 static ::std::string Format(CharType* value) { \ 376 return ::testing::PrintToString(value); \ 377 } \ 378 } 379 380 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); 381 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); 382 383 #if GTEST_HAS_STD_WSTRING 384 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); 385 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); 386 #endif 387 388 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ 389 390 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) 391 // operand to be used in a failure message. The type (but not value) 392 // of the other operand may affect the format. This allows us to 393 // print a char* as a raw pointer when it is compared against another 394 // char* or void*, and print it as a C string when it is compared 395 // against an std::string object, for example. 396 // 397 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 398 template <typename T1, typename T2> 399 std::string FormatForComparisonFailureMessage( 400 const T1& value, const T2& /* other_operand */) { 401 return FormatForComparison<T1, T2>::Format(value); 402 } 403 404 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given 405 // value to the given ostream. The caller must ensure that 406 // 'ostream_ptr' is not NULL, or the behavior is undefined. 407 // 408 // We define UniversalPrinter as a class template (as opposed to a 409 // function template), as we need to partially specialize it for 410 // reference types, which cannot be done with function templates. 411 template <typename T> 412 class UniversalPrinter; 413 414 // Prints the given value using the << operator if it has one; 415 // otherwise prints the bytes in it. This is what 416 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized 417 // or overloaded for type T. 418 // 419 // A user can override this behavior for a class type Foo by defining 420 // an overload of PrintTo() in the namespace where Foo is defined. We 421 // give the user this option as sometimes defining a << operator for 422 // Foo is not desirable (e.g. the coding style may prevent doing it, 423 // or there is already a << operator but it doesn't do what the user 424 // wants). 425 template <typename T> 426 void PrintTo(const T& value, ::std::ostream* os) { 427 internal::PrintWithFallback(value, os); 428 } 429 430 // The following list of PrintTo() overloads tells 431 // UniversalPrinter<T>::Print() how to print standard types (built-in 432 // types, strings, plain arrays, and pointers). 433 434 // Overloads for various char types. 435 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os); 436 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os); 437 inline void PrintTo(char c, ::std::ostream* os) { 438 // When printing a plain char, we always treat it as unsigned. This 439 // way, the output won't be affected by whether the compiler thinks 440 // char is signed or not. 441 PrintTo(static_cast<unsigned char>(c), os); 442 } 443 444 // Overloads for other simple built-in types. 445 inline void PrintTo(bool x, ::std::ostream* os) { 446 *os << (x ? "true" : "false"); 447 } 448 449 // Overload for wchar_t type. 450 // Prints a wchar_t as a symbol if it is printable or as its internal 451 // code otherwise and also as its decimal code (except for L'\0'). 452 // The L'\0' char is printed as "L'\\0'". The decimal code is printed 453 // as signed integer when wchar_t is implemented by the compiler 454 // as a signed type and is printed as an unsigned integer when wchar_t 455 // is implemented as an unsigned type. 456 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os); 457 458 // Overloads for C strings. 459 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os); 460 inline void PrintTo(char* s, ::std::ostream* os) { 461 PrintTo(ImplicitCast_<const char*>(s), os); 462 } 463 464 // signed/unsigned char is often used for representing binary data, so 465 // we print pointers to it as void* to be safe. 466 inline void PrintTo(const signed char* s, ::std::ostream* os) { 467 PrintTo(ImplicitCast_<const void*>(s), os); 468 } 469 inline void PrintTo(signed char* s, ::std::ostream* os) { 470 PrintTo(ImplicitCast_<const void*>(s), os); 471 } 472 inline void PrintTo(const unsigned char* s, ::std::ostream* os) { 473 PrintTo(ImplicitCast_<const void*>(s), os); 474 } 475 inline void PrintTo(unsigned char* s, ::std::ostream* os) { 476 PrintTo(ImplicitCast_<const void*>(s), os); 477 } 478 479 // MSVC can be configured to define wchar_t as a typedef of unsigned 480 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native 481 // type. When wchar_t is a typedef, defining an overload for const 482 // wchar_t* would cause unsigned short* be printed as a wide string, 483 // possibly causing invalid memory accesses. 484 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 485 // Overloads for wide C strings 486 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os); 487 inline void PrintTo(wchar_t* s, ::std::ostream* os) { 488 PrintTo(ImplicitCast_<const wchar_t*>(s), os); 489 } 490 #endif 491 492 // Overload for C arrays. Multi-dimensional arrays are printed 493 // properly. 494 495 // Prints the given number of elements in an array, without printing 496 // the curly braces. 497 template <typename T> 498 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) { 499 UniversalPrint(a[0], os); 500 for (size_t i = 1; i != count; i++) { 501 *os << ", "; 502 UniversalPrint(a[i], os); 503 } 504 } 505 506 // Overloads for ::std::string. 507 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os); 508 inline void PrintTo(const ::std::string& s, ::std::ostream* os) { 509 PrintStringTo(s, os); 510 } 511 512 // Overloads for ::std::wstring. 513 #if GTEST_HAS_STD_WSTRING 514 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os); 515 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { 516 PrintWideStringTo(s, os); 517 } 518 #endif // GTEST_HAS_STD_WSTRING 519 520 #if GTEST_INTERNAL_HAS_STRING_VIEW 521 // Overload for internal::StringView. 522 inline void PrintTo(internal::StringView sp, ::std::ostream* os) { 523 PrintTo(::std::string(sp), os); 524 } 525 #endif // GTEST_INTERNAL_HAS_STRING_VIEW 526 527 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } 528 529 template <typename T> 530 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) { 531 UniversalPrinter<T&>::Print(ref.get(), os); 532 } 533 534 // Helper function for printing a tuple. T must be instantiated with 535 // a tuple type. 536 template <typename T> 537 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>, 538 ::std::ostream*) {} 539 540 template <typename T, size_t I> 541 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>, 542 ::std::ostream* os) { 543 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os); 544 GTEST_INTENTIONAL_CONST_COND_PUSH_() 545 if (I > 1) { 546 GTEST_INTENTIONAL_CONST_COND_POP_() 547 *os << ", "; 548 } 549 UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print( 550 std::get<I - 1>(t), os); 551 } 552 553 template <typename... Types> 554 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) { 555 *os << "("; 556 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os); 557 *os << ")"; 558 } 559 560 // Overload for std::pair. 561 template <typename T1, typename T2> 562 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) { 563 *os << '('; 564 // We cannot use UniversalPrint(value.first, os) here, as T1 may be 565 // a reference type. The same for printing value.second. 566 UniversalPrinter<T1>::Print(value.first, os); 567 *os << ", "; 568 UniversalPrinter<T2>::Print(value.second, os); 569 *os << ')'; 570 } 571 572 // Implements printing a non-reference type T by letting the compiler 573 // pick the right overload of PrintTo() for T. 574 template <typename T> 575 class UniversalPrinter { 576 public: 577 // MSVC warns about adding const to a function type, so we want to 578 // disable the warning. 579 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) 580 581 // Note: we deliberately don't call this PrintTo(), as that name 582 // conflicts with ::testing::internal::PrintTo in the body of the 583 // function. 584 static void Print(const T& value, ::std::ostream* os) { 585 // By default, ::testing::internal::PrintTo() is used for printing 586 // the value. 587 // 588 // Thanks to Koenig look-up, if T is a class and has its own 589 // PrintTo() function defined in its namespace, that function will 590 // be visible here. Since it is more specific than the generic ones 591 // in ::testing::internal, it will be picked by the compiler in the 592 // following statement - exactly what we want. 593 PrintTo(value, os); 594 } 595 596 GTEST_DISABLE_MSC_WARNINGS_POP_() 597 }; 598 599 #if GTEST_HAS_ABSL 600 601 // Printer for absl::optional 602 603 template <typename T> 604 class UniversalPrinter<::absl::optional<T>> { 605 public: 606 static void Print(const ::absl::optional<T>& value, ::std::ostream* os) { 607 *os << '('; 608 if (!value) { 609 *os << "nullopt"; 610 } else { 611 UniversalPrint(*value, os); 612 } 613 *os << ')'; 614 } 615 }; 616 617 // Printer for absl::variant 618 619 template <typename... T> 620 class UniversalPrinter<::absl::variant<T...>> { 621 public: 622 static void Print(const ::absl::variant<T...>& value, ::std::ostream* os) { 623 *os << '('; 624 absl::visit(Visitor{os}, value); 625 *os << ')'; 626 } 627 628 private: 629 struct Visitor { 630 template <typename U> 631 void operator()(const U& u) const { 632 *os << "'" << GetTypeName<U>() << "' with value "; 633 UniversalPrint(u, os); 634 } 635 ::std::ostream* os; 636 }; 637 }; 638 639 #endif // GTEST_HAS_ABSL 640 641 // UniversalPrintArray(begin, len, os) prints an array of 'len' 642 // elements, starting at address 'begin'. 643 template <typename T> 644 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { 645 if (len == 0) { 646 *os << "{}"; 647 } else { 648 *os << "{ "; 649 const size_t kThreshold = 18; 650 const size_t kChunkSize = 8; 651 // If the array has more than kThreshold elements, we'll have to 652 // omit some details by printing only the first and the last 653 // kChunkSize elements. 654 if (len <= kThreshold) { 655 PrintRawArrayTo(begin, len, os); 656 } else { 657 PrintRawArrayTo(begin, kChunkSize, os); 658 *os << ", ..., "; 659 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); 660 } 661 *os << " }"; 662 } 663 } 664 // This overload prints a (const) char array compactly. 665 GTEST_API_ void UniversalPrintArray( 666 const char* begin, size_t len, ::std::ostream* os); 667 668 // This overload prints a (const) wchar_t array compactly. 669 GTEST_API_ void UniversalPrintArray( 670 const wchar_t* begin, size_t len, ::std::ostream* os); 671 672 // Implements printing an array type T[N]. 673 template <typename T, size_t N> 674 class UniversalPrinter<T[N]> { 675 public: 676 // Prints the given array, omitting some elements when there are too 677 // many. 678 static void Print(const T (&a)[N], ::std::ostream* os) { 679 UniversalPrintArray(a, N, os); 680 } 681 }; 682 683 // Implements printing a reference type T&. 684 template <typename T> 685 class UniversalPrinter<T&> { 686 public: 687 // MSVC warns about adding const to a function type, so we want to 688 // disable the warning. 689 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) 690 691 static void Print(const T& value, ::std::ostream* os) { 692 // Prints the address of the value. We use reinterpret_cast here 693 // as static_cast doesn't compile when T is a function type. 694 *os << "@" << reinterpret_cast<const void*>(&value) << " "; 695 696 // Then prints the value itself. 697 UniversalPrint(value, os); 698 } 699 700 GTEST_DISABLE_MSC_WARNINGS_POP_() 701 }; 702 703 // Prints a value tersely: for a reference type, the referenced value 704 // (but not the address) is printed; for a (const) char pointer, the 705 // NUL-terminated string (but not the pointer) is printed. 706 707 template <typename T> 708 class UniversalTersePrinter { 709 public: 710 static void Print(const T& value, ::std::ostream* os) { 711 UniversalPrint(value, os); 712 } 713 }; 714 template <typename T> 715 class UniversalTersePrinter<T&> { 716 public: 717 static void Print(const T& value, ::std::ostream* os) { 718 UniversalPrint(value, os); 719 } 720 }; 721 template <typename T, size_t N> 722 class UniversalTersePrinter<T[N]> { 723 public: 724 static void Print(const T (&value)[N], ::std::ostream* os) { 725 UniversalPrinter<T[N]>::Print(value, os); 726 } 727 }; 728 template <> 729 class UniversalTersePrinter<const char*> { 730 public: 731 static void Print(const char* str, ::std::ostream* os) { 732 if (str == nullptr) { 733 *os << "NULL"; 734 } else { 735 UniversalPrint(std::string(str), os); 736 } 737 } 738 }; 739 template <> 740 class UniversalTersePrinter<char*> { 741 public: 742 static void Print(char* str, ::std::ostream* os) { 743 UniversalTersePrinter<const char*>::Print(str, os); 744 } 745 }; 746 747 #if GTEST_HAS_STD_WSTRING 748 template <> 749 class UniversalTersePrinter<const wchar_t*> { 750 public: 751 static void Print(const wchar_t* str, ::std::ostream* os) { 752 if (str == nullptr) { 753 *os << "NULL"; 754 } else { 755 UniversalPrint(::std::wstring(str), os); 756 } 757 } 758 }; 759 #endif 760 761 template <> 762 class UniversalTersePrinter<wchar_t*> { 763 public: 764 static void Print(wchar_t* str, ::std::ostream* os) { 765 UniversalTersePrinter<const wchar_t*>::Print(str, os); 766 } 767 }; 768 769 template <typename T> 770 void UniversalTersePrint(const T& value, ::std::ostream* os) { 771 UniversalTersePrinter<T>::Print(value, os); 772 } 773 774 // Prints a value using the type inferred by the compiler. The 775 // difference between this and UniversalTersePrint() is that for a 776 // (const) char pointer, this prints both the pointer and the 777 // NUL-terminated string. 778 template <typename T> 779 void UniversalPrint(const T& value, ::std::ostream* os) { 780 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating 781 // UniversalPrinter with T directly. 782 typedef T T1; 783 UniversalPrinter<T1>::Print(value, os); 784 } 785 786 typedef ::std::vector< ::std::string> Strings; 787 788 // Tersely prints the first N fields of a tuple to a string vector, 789 // one element for each field. 790 template <typename Tuple> 791 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>, 792 Strings*) {} 793 template <typename Tuple, size_t I> 794 void TersePrintPrefixToStrings(const Tuple& t, 795 std::integral_constant<size_t, I>, 796 Strings* strings) { 797 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(), 798 strings); 799 ::std::stringstream ss; 800 UniversalTersePrint(std::get<I - 1>(t), &ss); 801 strings->push_back(ss.str()); 802 } 803 804 // Prints the fields of a tuple tersely to a string vector, one 805 // element for each field. See the comment before 806 // UniversalTersePrint() for how we define "tersely". 807 template <typename Tuple> 808 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { 809 Strings result; 810 TersePrintPrefixToStrings( 811 value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(), 812 &result); 813 return result; 814 } 815 816 } // namespace internal 817 818 template <typename T> 819 ::std::string PrintToString(const T& value) { 820 ::std::stringstream ss; 821 internal::UniversalTersePrinter<T>::Print(value, &ss); 822 return ss.str(); 823 } 824 825 } // namespace testing 826 827 // Include any custom printer added by the local installation. 828 // We must include this header at the end to make sure it can use the 829 // declarations from this file. 830 #include "gtest/internal/custom/gtest-printers.h" 831 832 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_