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
95b0ea2c
Commit
95b0ea2c
authored
Mar 07, 2020
by
Krystian Kuzniarek
Browse files
specialize UniversalPrinter<> for std::optional
parent
33b44c4b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
13 deletions
+42
-13
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+5
-6
googletest/include/gtest/internal/gtest-port.h
googletest/include/gtest/internal/gtest-port.h
+32
-0
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+5
-7
No files found.
googletest/include/gtest/gtest-printers.h
View file @
95b0ea2c
...
@@ -113,7 +113,6 @@
...
@@ -113,7 +113,6 @@
#if GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
#include "absl/strings/string_view.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#endif // GTEST_HAS_ABSL
#endif // GTEST_HAS_ABSL
namespace
testing
{
namespace
testing
{
...
@@ -680,14 +679,14 @@ class UniversalPrinter {
...
@@ -680,14 +679,14 @@ class UniversalPrinter {
GTEST_DISABLE_MSC_WARNINGS_POP_
()
GTEST_DISABLE_MSC_WARNINGS_POP_
()
};
};
#if GTEST_
HAS_ABS
L
#if GTEST_
INTERNAL_HAS_OPTIONA
L
// Printer for absl::optional
// Printer for
std::optional /
absl::optional
template
<
typename
T
>
template
<
typename
T
>
class
UniversalPrinter
<
::
absl
::
o
ptional
<
T
>>
{
class
UniversalPrinter
<
O
ptional
<
T
>>
{
public:
public:
static
void
Print
(
const
::
absl
::
o
ptional
<
T
>&
value
,
::
std
::
ostream
*
os
)
{
static
void
Print
(
const
O
ptional
<
T
>&
value
,
::
std
::
ostream
*
os
)
{
*
os
<<
'('
;
*
os
<<
'('
;
if
(
!
value
)
{
if
(
!
value
)
{
*
os
<<
"nullopt"
;
*
os
<<
"nullopt"
;
...
@@ -698,7 +697,7 @@ class UniversalPrinter<::absl::optional<T>> {
...
@@ -698,7 +697,7 @@ class UniversalPrinter<::absl::optional<T>> {
}
}
};
};
#endif // GTEST_
HAS_ABS
L
#endif // GTEST_
INTERNAL_HAS_OPTIONA
L
#if GTEST_INTERNAL_HAS_VARIANT
#if GTEST_INTERNAL_HAS_VARIANT
...
...
googletest/include/gtest/internal/gtest-port.h
View file @
95b0ea2c
...
@@ -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_OPTIONAL - for enabling UniversalPrinter<std::optional> or
// 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
// Matcher<absl::string_view>
// Matcher<absl::string_view>
// specializations.
// specializations.
...
@@ -2225,6 +2227,36 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
...
@@ -2225,6 +2227,36 @@ 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::optional for UniversalPrinter<> specializations if googletest
// is built with absl support.
# define GTEST_INTERNAL_HAS_OPTIONAL 1
#include "absl/types/optional.h"
namespace
testing
{
namespace
internal
{
template
<
typename
T
>
using
Optional
=
::
absl
::
optional
<
T
>
;
}
// namespace internal
}
// namespace testing
#else
# ifdef __has_include
# if __has_include(<optional>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::optional for UniversalPrinter<>
// specializations.
# define GTEST_INTERNAL_HAS_OPTIONAL 1
#include <optional>
namespace
testing
{
namespace
internal
{
template
<
typename
T
>
using
Optional
=
::
std
::
optional
<
T
>
;
}
// namespace internal
}
// namespace testing
// The case where absl is configured NOT to alias std::optional is not
// supported.
# endif // __has_include(<optional>) && __cplusplus >= 201703L
# endif // __has_include
#endif // GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
// Always use absl::string_view for Matcher<> specializations if googletest
// Always use absl::string_view for Matcher<> specializations if googletest
// is built with absl support.
// is built with absl support.
...
...
googletest/test/googletest-printers-test.cc
View file @
95b0ea2c
...
@@ -1531,18 +1531,16 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
...
@@ -1531,18 +1531,16 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
EXPECT_EQ
(
"
\"
a
\"
"
,
result
[
1
]);
EXPECT_EQ
(
"
\"
a
\"
"
,
result
[
1
]);
}
}
#if GTEST_HAS_ABSL
#if GTEST_INTERNAL_HAS_OPTIONAL
TEST
(
PrintOptionalTest
,
Basic
)
{
TEST
(
PrintOptionalTest
,
Basic
)
{
abs
l
::
o
ptional
<
int
>
value
;
interna
l
::
O
ptional
<
int
>
value
;
EXPECT_EQ
(
"(nullopt)"
,
PrintToString
(
value
));
EXPECT_EQ
(
"(nullopt)"
,
PrintToString
(
value
));
value
=
{
7
};
value
=
{
7
};
EXPECT_EQ
(
"(7)"
,
PrintToString
(
value
));
EXPECT_EQ
(
"(7)"
,
PrintToString
(
value
));
EXPECT_EQ
(
"(1.1)"
,
PrintToString
(
abs
l
::
o
ptional
<
double
>
{
1.1
}));
EXPECT_EQ
(
"(1.1)"
,
PrintToString
(
interna
l
::
O
ptional
<
double
>
{
1.1
}));
EXPECT_EQ
(
"(
\"
A
\"
)"
,
PrintToString
(
abs
l
::
o
ptional
<
std
::
string
>
{
"A"
}));
EXPECT_EQ
(
"(
\"
A
\"
)"
,
PrintToString
(
interna
l
::
O
ptional
<
std
::
string
>
{
"A"
}));
}
}
#endif // GTEST_INTERNAL_HAS_OPTIONAL
#endif // GTEST_HAS_ABSL
#if GTEST_INTERNAL_HAS_VARIANT
#if GTEST_INTERNAL_HAS_VARIANT
struct
NonPrintable
{
struct
NonPrintable
{
...
...
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