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
843267f0
Commit
843267f0
authored
Mar 07, 2020
by
Krystian Kuzniarek
Browse files
specialize UniversalPrinter<> for std::any (without support for RTTI)
parent
95b0ea2c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
0 deletions
+72
-0
BUILD.bazel
BUILD.bazel
+1
-0
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+20
-0
googletest/include/gtest/internal/gtest-port.h
googletest/include/gtest/internal/gtest-port.h
+30
-0
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+21
-0
No files found.
BUILD.bazel
View file @
843267f0
...
@@ -103,6 +103,7 @@ cc_library(
...
@@ -103,6 +103,7 @@ cc_library(
"@com_google_absl//absl/debugging:stacktrace"
,
"@com_google_absl//absl/debugging:stacktrace"
,
"@com_google_absl//absl/debugging:symbolize"
,
"@com_google_absl//absl/debugging:symbolize"
,
"@com_google_absl//absl/strings"
,
"@com_google_absl//absl/strings"
,
"@com_google_absl//absl/types:any"
,
"@com_google_absl//absl/types:optional"
,
"@com_google_absl//absl/types:optional"
,
"@com_google_absl//absl/types:variant"
,
"@com_google_absl//absl/types:variant"
,
],
],
...
...
googletest/include/gtest/gtest-printers.h
View file @
843267f0
...
@@ -679,6 +679,26 @@ class UniversalPrinter {
...
@@ -679,6 +679,26 @@ class UniversalPrinter {
GTEST_DISABLE_MSC_WARNINGS_POP_
()
GTEST_DISABLE_MSC_WARNINGS_POP_
()
};
};
#if GTEST_INTERNAL_HAS_ANY
// Printer for std::any / absl::any
template
<>
class
UniversalPrinter
<
Any
>
{
public:
static
void
Print
(
const
Any
&
value
,
::
std
::
ostream
*
os
)
{
if
(
value
.
has_value
())
*
os
<<
"'any' type with value of type "
<<
GetTypeName
();
else
*
os
<<
"'any' type with no value"
;
}
private:
static
std
::
string
GetTypeName
()
{
return
"the element type"
;
}
};
#endif // GTEST_INTERNAL_HAS_ANY
#if GTEST_INTERNAL_HAS_OPTIONAL
#if GTEST_INTERNAL_HAS_OPTIONAL
// Printer for std::optional / absl::optional
// Printer for std::optional / absl::optional
...
...
googletest/include/gtest/internal/gtest-port.h
View file @
843267f0
...
@@ -199,6 +199,8 @@
...
@@ -199,6 +199,8 @@
// suppressed (constant conditional).
// suppressed (constant conditional).
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
// is suppressed.
// is suppressed.
// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or
// UniversalPrinter<absl::any> specializations.
// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional> or
// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional> or
// UniversalPrinter<absl::optional> specializations.
// UniversalPrinter<absl::optional> specializations.
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
...
@@ -2227,6 +2229,34 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
...
@@ -2227,6 +2229,34 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
#endif // !defined(GTEST_INTERNAL_DEPRECATED)
#endif // !defined(GTEST_INTERNAL_DEPRECATED)
#if GTEST_HAS_ABSL
// Always use absl::any for UniversalPrinter<> specializations if googletest
// is built with absl support.
# define GTEST_INTERNAL_HAS_ANY 1
#include "absl/types/any.h"
namespace
testing
{
namespace
internal
{
using
Any
=
::
absl
::
any
;
}
// namespace internal
}
// namespace testing
#else
# ifdef __has_include
# if __has_include(<any>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::any for UniversalPrinter<>
// specializations.
# define GTEST_INTERNAL_HAS_ANY 1
#include <any>
namespace
testing
{
namespace
internal
{
using
Any
=
::
std
::
any
;
}
// namespace internal
}
// namespace testing
// The case where absl is configured NOT to alias std::any is not
// supported.
# endif // __has_include(<any>) && __cplusplus >= 201703L
# endif // __has_include
#endif // GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
// Always use absl::optional for UniversalPrinter<> specializations if googletest
// Always use absl::optional for UniversalPrinter<> specializations if googletest
// is built with absl support.
// is built with absl support.
...
...
googletest/test/googletest-printers-test.cc
View file @
843267f0
...
@@ -1531,6 +1531,27 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
...
@@ -1531,6 +1531,27 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
EXPECT_EQ
(
"
\"
a
\"
"
,
result
[
1
]);
EXPECT_EQ
(
"
\"
a
\"
"
,
result
[
1
]);
}
}
#if GTEST_INTERNAL_HAS_ANY
TEST
(
PrintAnyTest
,
Empty
)
{
internal
::
Any
any
;
EXPECT_EQ
(
"'any' type with no value"
,
PrintToString
(
any
));
}
TEST
(
PrintAnyTest
,
NonEmpty
)
{
internal
::
Any
any
;
constexpr
int
val1
=
10
;
const
std
::
string
val2
=
"content"
;
any
=
val1
;
EXPECT_EQ
(
"'any' type with value of type the element type"
,
PrintToString
(
any
));
any
=
val2
;
EXPECT_EQ
(
"'any' type with value of type the element type"
,
PrintToString
(
any
));
}
#endif // GTEST_INTERNAL_HAS_ANY
#if GTEST_INTERNAL_HAS_OPTIONAL
#if GTEST_INTERNAL_HAS_OPTIONAL
TEST
(
PrintOptionalTest
,
Basic
)
{
TEST
(
PrintOptionalTest
,
Basic
)
{
internal
::
Optional
<
int
>
value
;
internal
::
Optional
<
int
>
value
;
...
...
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