From ac3c2a8d0496893787015014a5abd397b766cce2 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 24 Dec 2020 13:42:39 +0100 Subject: overload PrintTo for std::type_info and std::type_index Included the string returned by their `name()` member function with the output of `PrintTo`. Typical use case: std::unique_ptr product = FactoryMethod(); // Assert that the product is of type X: ASSERT_EQ(std::type_index{typeid(*product)}, std::type_index{typeid(ProductX)}); Possible output in case of a test assert failure, now including the names of the compared type indices: > error: Expected equality of these values: > std::type_index(typeid(*product)) > Which is: 8-byte object ("class ProductY") > std::type_index(typeid(ProductX)) > Which is: 8-byte object <40-64 54-8C F6-7F 00-00> ("class ProductX") With help from Krystian Kuzniarek. --- googletest/include/gtest/gtest-printers.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'googletest/include/gtest/gtest-printers.h') diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index cd094949..aa9ca48c 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -113,6 +113,11 @@ #include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-port.h" +#if GTEST_HAS_RTTI +#include +#include +#endif // GTEST_HAS_RTTI + namespace testing { // Definitions in the internal* namespaces are subject to change without notice. @@ -650,6 +655,24 @@ void PrintTo(const ::std::pair& value, ::std::ostream* os) { *os << ')'; } +template +void PrintWithNameTo(const T& value, ::std::ostream* os) { + internal::PrintTo(value, os); + *os << " (\"" << value.name() << "\")"; +} + +#if GTEST_HAS_RTTI + +inline void PrintTo(const ::std::type_info& value, ::std::ostream* os) { + internal::PrintWithNameTo(value, os); +} + +inline void PrintTo(const ::std::type_index& value, ::std::ostream* os) { + internal::PrintWithNameTo(value, os); +} + +#endif // GTEST_HAS_RTTI + // Implements printing a non-reference type T by letting the compiler // pick the right overload of PrintTo() for T. template -- cgit v1.2.1