summaryrefslogtreecommitdiffstats
path: root/googletest/test
diff options
context:
space:
mode:
authorDino Radaković <dinor@google.com>2021-03-09 10:59:10 -0800
committerDino Radaković <dinor@google.com>2021-03-09 10:59:10 -0800
commitbcfcf75ef62af29f15fbb98be25fbfb87f45036e (patch)
treee7aab07ac6de00c4d07e4358409730c593d72ac9 /googletest/test
parent79b556eff66383002e7186c30a93a0098625f498 (diff)
parentac3c2a8d0496893787015014a5abd397b766cce2 (diff)
downloadgoogletest-bcfcf75ef62af29f15fbb98be25fbfb87f45036e.tar.gz
googletest-bcfcf75ef62af29f15fbb98be25fbfb87f45036e.zip
Merge pull request #3184 from N-Dekker:PrintTo-type_index-overload
PiperOrigin-RevId: 361175466
Diffstat (limited to 'googletest/test')
-rw-r--r--googletest/test/googletest-printers-test.cc55
1 files changed, 55 insertions, 0 deletions
diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc
index 8247d4e1..f037480b 100644
--- a/googletest/test/googletest-printers-test.cc
+++ b/googletest/test/googletest-printers-test.cc
@@ -1589,6 +1589,61 @@ TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
"\n As Text: \"From ä — ẑ\"");
}
+#if GTEST_HAS_RTTI
+template <typename T>
+class PrintToStringTest : public testing::Test {
+ public:
+ using TestType = T;
+};
+
+struct PrintBase {
+ virtual ~PrintBase() = default;
+};
+struct PrintDerived : PrintBase {};
+
+using PrintToStringTestTypes =
+ testing::Types<void, int, const volatile int*, PrintBase, PrintDerived>;
+TYPED_TEST_SUITE(PrintToStringTest, PrintToStringTestTypes);
+
+// Returns `true` if `haystack` contains `needle`.
+//
+// FIXME: Replace with `EXPECT_THAT(haystack, HasSubstr(needle))` once
+// GoogleTest starts depending on GoogleMock.
+bool ContainsSubstr(const std::string& haystack, const std::string& needle) {
+ return haystack.find(needle) != std::string::npos;
+}
+
+TYPED_TEST(PrintToStringTest, IncludesNameWithTypeInfoAndTypeIndex) {
+ const ::std::type_info& info = typeid(typename TestFixture::TestType);
+ SCOPED_TRACE(info.name());
+ EXPECT_TRUE(ContainsSubstr(PrintToString(info), info.name()));
+ EXPECT_TRUE(
+ ContainsSubstr(PrintToString(::std::type_index{info}), info.name()));
+}
+
+TEST(PrintToStringTest, IncludesNameWithTypeInfoAndTypeIndexViaBaseRef) {
+ PrintDerived derived;
+ PrintBase& base = derived;
+
+ {
+ const ::std::type_info& derived_info = typeid(derived);
+ SCOPED_TRACE(derived_info.name());
+ EXPECT_TRUE(
+ ContainsSubstr(PrintToString(derived_info), derived_info.name()));
+ EXPECT_TRUE(ContainsSubstr(PrintToString(::std::type_index{derived_info}),
+ derived_info.name()));
+ }
+ {
+ const ::std::type_info& base_ref_info = typeid(base);
+ SCOPED_TRACE(base_ref_info.name());
+ EXPECT_TRUE(
+ ContainsSubstr(PrintToString(base_ref_info), base_ref_info.name()));
+ EXPECT_TRUE(ContainsSubstr(PrintToString(::std::type_index{base_ref_info}),
+ base_ref_info.name()));
+ }
+}
+#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
OpenPOWER on IntegriCloud