Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
eb3953f8
"git@developer.sourcefind.cn:yangql/googletest.git" did not exist on "6386897feb0a3f4fbe104fe1fa4570ec8158d9e5"
Commit
eb3953f8
authored
Mar 07, 2020
by
Krystian Kuzniarek
Browse files
make UniversalPrinter<std::any> support RTTI
parent
843267f0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
23 deletions
+46
-23
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+9
-2
googletest/include/gtest/internal/gtest-type-util.h
googletest/include/gtest/internal/gtest-type-util.h
+21
-17
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+16
-4
No files found.
googletest/include/gtest/gtest-printers.h
View file @
eb3953f8
...
@@ -688,13 +688,20 @@ class UniversalPrinter<Any> {
...
@@ -688,13 +688,20 @@ class UniversalPrinter<Any> {
public:
public:
static
void
Print
(
const
Any
&
value
,
::
std
::
ostream
*
os
)
{
static
void
Print
(
const
Any
&
value
,
::
std
::
ostream
*
os
)
{
if
(
value
.
has_value
())
if
(
value
.
has_value
())
*
os
<<
"'any' type with value of type "
<<
GetTypeName
();
*
os
<<
"'any' type with value of type "
<<
GetTypeName
(
value
);
else
else
*
os
<<
"'any' type with no value"
;
*
os
<<
"'any' type with no value"
;
}
}
private:
private:
static
std
::
string
GetTypeName
()
{
return
"the element type"
;
}
static
std
::
string
GetTypeName
(
const
Any
&
value
)
{
#if GTEST_HAS_RTTI
return
internal
::
GetTypeName
(
value
.
type
());
#else
static_cast
<
void
>
(
value
);
// possibly unused
return
"the element type"
;
#endif // GTEST_HAS_RTTI
}
};
};
#endif // GTEST_INTERNAL_HAS_ANY
#endif // GTEST_INTERNAL_HAS_ANY
...
...
googletest/include/gtest/internal/gtest-type-util.h
View file @
eb3953f8
...
@@ -64,34 +64,38 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
...
@@ -64,34 +64,38 @@ inline std::string CanonicalizeForStdLibVersioning(std::string s) {
return
s
;
return
s
;
}
}
// GetTypeName<T>() returns a human-readable name of type T.
#if GTEST_HAS_RTTI
// NB: This function is also used in Google Mock, so don't move it inside of
// GetTypeName(const std::type_info&) returns a human-readable name of type T.
// the typed-test-only section below.
inline
std
::
string
GetTypeName
(
const
std
::
type_info
&
type
)
{
template
<
typename
T
>
const
char
*
const
name
=
type
.
name
();
std
::
string
GetTypeName
()
{
#if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
# if GTEST_HAS_RTTI
const
char
*
const
name
=
typeid
(
T
).
name
();
# if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)
int
status
=
0
;
int
status
=
0
;
// gcc's implementation of typeid(T).name() mangles the type name,
// gcc's implementation of typeid(T).name() mangles the type name,
// so we have to demangle it.
// so we have to demangle it.
#
if GTEST_HAS_CXXABI_H_
#if GTEST_HAS_CXXABI_H_
using
abi
::
__cxa_demangle
;
using
abi
::
__cxa_demangle
;
#
endif // GTEST_HAS_CXXABI_H_
#endif // GTEST_HAS_CXXABI_H_
char
*
const
readable_name
=
__cxa_demangle
(
name
,
nullptr
,
nullptr
,
&
status
);
char
*
const
readable_name
=
__cxa_demangle
(
name
,
nullptr
,
nullptr
,
&
status
);
const
std
::
string
name_str
(
status
==
0
?
readable_name
:
name
);
const
std
::
string
name_str
(
status
==
0
?
readable_name
:
name
);
free
(
readable_name
);
free
(
readable_name
);
return
CanonicalizeForStdLibVersioning
(
name_str
);
return
CanonicalizeForStdLibVersioning
(
name_str
);
#
else
#else
return
name
;
return
name
;
#
endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC
}
#
else
#
endif // GTEST_HAS_RTTI
// GetTypeName<T>() returns a human-readable name of type T if and only if
// RTTI is enabled, otherwise it returns a dummy type name.
// NB: This function is also used in Google Mock, so don't move it inside of
// the typed-test-only section below.
template
<
typename
T
>
std
::
string
GetTypeName
()
{
#if GTEST_HAS_RTTI
return
GetTypeName
(
typeid
(
T
));
#else
return
"<type>"
;
return
"<type>"
;
#endif // GTEST_HAS_RTTI
# endif // GTEST_HAS_RTTI
}
}
#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
...
...
googletest/test/googletest-printers-test.cc
View file @
eb3953f8
...
@@ -1532,22 +1532,34 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
...
@@ -1532,22 +1532,34 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
}
}
#if GTEST_INTERNAL_HAS_ANY
#if GTEST_INTERNAL_HAS_ANY
TEST
(
PrintAnyTest
,
Empty
)
{
class
PrintAnyTest
:
public
::
testing
::
Test
{
protected:
template
<
typename
T
>
static
std
::
string
ExpectedTypeName
()
{
#if GTEST_HAS_RTTI
return
internal
::
GetTypeName
<
T
>
();
#else
return
"the element type"
;
#endif // GTEST_HAS_RTTI
}
};
TEST_F
(
PrintAnyTest
,
Empty
)
{
internal
::
Any
any
;
internal
::
Any
any
;
EXPECT_EQ
(
"'any' type with no value"
,
PrintToString
(
any
));
EXPECT_EQ
(
"'any' type with no value"
,
PrintToString
(
any
));
}
}
TEST
(
PrintAnyTest
,
NonEmpty
)
{
TEST
_F
(
PrintAnyTest
,
NonEmpty
)
{
internal
::
Any
any
;
internal
::
Any
any
;
constexpr
int
val1
=
10
;
constexpr
int
val1
=
10
;
const
std
::
string
val2
=
"content"
;
const
std
::
string
val2
=
"content"
;
any
=
val1
;
any
=
val1
;
EXPECT_EQ
(
"'any' type with value of type
the element type"
,
EXPECT_EQ
(
"'any' type with value of type
"
+
ExpectedTypeName
<
int
>
()
,
PrintToString
(
any
));
PrintToString
(
any
));
any
=
val2
;
any
=
val2
;
EXPECT_EQ
(
"'any' type with value of type
the element type"
,
EXPECT_EQ
(
"'any' type with value of type
"
+
ExpectedTypeName
<
std
::
string
>
()
,
PrintToString
(
any
));
PrintToString
(
any
));
}
}
#endif // GTEST_INTERNAL_HAS_ANY
#endif // GTEST_INTERNAL_HAS_ANY
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment