Skip to content

Commit 5ac7646

Browse files
committed
make UniversalPrinter<std::any> support RTTI
1 parent c7858b7 commit 5ac7646

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

googletest/include/gtest/gtest-printers.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,13 +688,20 @@ class UniversalPrinter<Any> {
688688
public:
689689
static void Print(const Any& value, ::std::ostream* os) {
690690
if (value.has_value())
691-
*os << "'any' type with value of type " << GetTypeName();
691+
*os << "'any' type with value of type " << GetTypeName(value);
692692
else
693693
*os << "'any' type with no value";
694694
}
695695

696696
private:
697-
static std::string GetTypeName() { return "the element type"; }
697+
static std::string GetTypeName(const Any& value) {
698+
#if GTEST_HAS_RTTI
699+
return internal::GetTypeName(value.type());
700+
#else
701+
static_cast<void>(value); // possibly unused
702+
return "the element type";
703+
#endif // GTEST_HAS_RTTI
704+
}
698705
};
699706

700707
#endif // GTEST_INTERNAL_HAS_ANY

googletest/include/gtest/internal/gtest-type-util.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,40 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
6464
return s;
6565
}
6666

67-
// GetTypeName<T>() returns a human-readable name of type T.
67+
#if GTEST_HAS_RTTI
68+
// GetTypeName(const std::type_info&) returns a human-readable name of type T.
6869
// NB: This function is also used in Google Mock, so don't move it inside of
6970
// the typed-test-only section below.
70-
template <typename T>
71-
std::string GetTypeName() {
72-
# if GTEST_HAS_RTTI
73-
74-
const char* const name = typeid(T).name();
75-
# if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
71+
inline std::string GetTypeName(const std::type_info& type) {
72+
const char* const name = type.name();
73+
#if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
7674
int status = 0;
7775
// gcc's implementation of typeid(T).name() mangles the type name,
7876
// so we have to demangle it.
79-
# if GTEST_HAS_CXXABI_H_
77+
#if GTEST_HAS_CXXABI_H_
8078
using abi::__cxa_demangle;
81-
# endif // GTEST_HAS_CXXABI_H_
79+
#endif // GTEST_HAS_CXXABI_H_
8280
char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status);
8381
const std::string name_str(status == 0 ? readable_name : name);
8482
free(readable_name);
8583
return CanonicalizeForStdLibVersioning(name_str);
86-
# else
84+
#else
8785
return name;
88-
# endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
89-
90-
# else
86+
#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
87+
}
88+
#endif // GTEST_HAS_RTTI
9189

90+
// GetTypeName<T>() returns a human-readable name of type T if and only if
91+
// RTTI is enabled, otherwise it returns a dummy type name.
92+
// NB: This function is also used in Google Mock, so don't move it inside of
93+
// the typed-test-only section below.
94+
template <typename T>
95+
std::string GetTypeName() {
96+
#if GTEST_HAS_RTTI
97+
return GetTypeName(typeid(T));
98+
#else
9299
return "<type>";
93-
94-
# endif // GTEST_HAS_RTTI
100+
#endif // GTEST_HAS_RTTI
95101
}
96102

97103
#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P

googletest/test/googletest-printers-test.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,22 +1532,34 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
15321532
}
15331533

15341534
#if GTEST_INTERNAL_HAS_ANY
1535-
TEST(PrintAnyTest, Empty) {
1535+
class PrintAnyTest : public ::testing::Test {
1536+
protected:
1537+
template <typename T>
1538+
static std::string ExpectedTypeName() {
1539+
#if GTEST_HAS_RTTI
1540+
return internal::GetTypeName<T>();
1541+
#else
1542+
return "the element type";
1543+
#endif // GTEST_HAS_RTTI
1544+
}
1545+
};
1546+
1547+
TEST_F(PrintAnyTest, Empty) {
15361548
internal::Any any;
15371549
EXPECT_EQ("'any' type with no value", PrintToString(any));
15381550
}
15391551

1540-
TEST(PrintAnyTest, NonEmpty) {
1552+
TEST_F(PrintAnyTest, NonEmpty) {
15411553
internal::Any any;
15421554
constexpr int val1 = 10;
15431555
const std::string val2 = "content";
15441556

15451557
any = val1;
1546-
EXPECT_EQ("'any' type with value of type the element type",
1558+
EXPECT_EQ("'any' type with value of type " + ExpectedTypeName<int>(),
15471559
PrintToString(any));
15481560

15491561
any = val2;
1550-
EXPECT_EQ("'any' type with value of type the element type",
1562+
EXPECT_EQ("'any' type with value of type " + ExpectedTypeName<std::string>(),
15511563
PrintToString(any));
15521564
}
15531565
#endif // GTEST_INTERNAL_HAS_ANY

0 commit comments

Comments
 (0)