gtest-printers.h 40 KB

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