Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/google/googletest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Dekker <N.Dekker@lumc.nl>2020-12-24 15:42:39 +0300
committerNiels Dekker <N.Dekker@lumc.nl>2021-02-25 15:23:52 +0300
commitac3c2a8d0496893787015014a5abd397b766cce2 (patch)
treeb3502f10ab4a4745fb764f162a9501f39bdcadc0
parent1de637fbdd4ab0051229707f855eee76f5a3d5da (diff)
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<AbstractProduct> 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 <D0-65 54-8C F6-7F 00-00> ("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.
-rw-r--r--googletest/include/gtest/gtest-printers.h23
-rw-r--r--googletest/test/googletest-printers-test.cc34
2 files changed, 57 insertions, 0 deletions
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 <typeindex>
+#include <typeinfo>
+#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<T1, T2>& value, ::std::ostream* os) {
*os << ')';
}
+template <typename T>
+void PrintWithNameTo(const T& value, ::std::ostream* os) {
+ internal::PrintTo<T>(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 <typename T>
diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc
index 8247d4e1..1d35c549 100644
--- a/googletest/test/googletest-printers-test.cc
+++ b/googletest/test/googletest-printers-test.cc
@@ -1589,6 +1589,40 @@ TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
"\n As Text: \"From ä — ẑ\"");
}
+#if GTEST_HAS_RTTI
+TEST(PrintToStringTest, IncludesNameWithTypeInfoAndTypeIndex) {
+ // The following lambda tests that both the printed string for the specified
+ // `typeid`, and the one for its `std::type_index` contain the string returned
+ // by its `name()` member function.
+ const auto TestTypeId = [](const ::std::type_info& id) {
+ const auto name = id.name();
+ const auto contains_name = [name](const ::std::string& str) {
+ return str.find(name) != ::std::string::npos;
+ };
+ EXPECT_TRUE(contains_name(PrintToString(id)));
+ EXPECT_TRUE(contains_name(PrintToString(::std::type_index{id})));
+ };
+
+ TestTypeId(typeid(void));
+ TestTypeId(typeid(int));
+ TestTypeId(typeid(const volatile int*));
+
+ struct Base {
+ virtual ~Base() = default;
+ };
+ struct Derived : Base {};
+
+ TestTypeId(typeid(Base));
+ TestTypeId(typeid(Derived));
+
+ Derived derived;
+ Base& base = derived;
+
+ TestTypeId(typeid(base));
+ TestTypeId(typeid(derived));
+}
+#endif // GTEST_HAS_RTTI
+
TEST(IsValidUTF8Test, IllFormedUTF8) {
// The following test strings are ill-formed UTF-8 and are printed
// as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is