diff options
author | Gennadiy Civil <misterg@google.com> | 2018-01-30 16:42:03 (GMT) |
---|---|---|
committer | Gennadiy Civil <misterg@google.com> | 2018-01-30 16:42:03 (GMT) |
commit | 2a4683021ab3e969a63c5e9226c1db4522f7129d (patch) | |
tree | c9f6b555ded1537bcfcadd31a21ab76dea1fb1b0 /googletest | |
parent | 6c0c389601fc823f2e4c1ae27b39cb13d5d0a7d4 (diff) | |
download | googletest-2a4683021ab3e969a63c5e9226c1db4522f7129d.zip googletest-2a4683021ab3e969a63c5e9226c1db4522f7129d.tar.gz googletest-2a4683021ab3e969a63c5e9226c1db4522f7129d.tar.bz2 |
Ability to optionally depend on Abseil plus upstream of 183716547refs/pull/1434/head
Diffstat (limited to 'googletest')
-rw-r--r-- | googletest/include/gtest/gtest-printers.h | 28 | ||||
-rw-r--r-- | googletest/test/gtest-printers_test.cc | 12 |
2 files changed, 40 insertions, 0 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 38c63d2..8dcb256 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -46,6 +46,10 @@ // 2. operator<<(ostream&, const T&) defined in either foo or the // global namespace. // +// However if T is an STL-style container then it is printed element-wise +// unless foo::PrintTo(const T&, ostream*) is defined. Note that +// operator<<() is ignored for container types. +// // If none of the above is defined, it will print the debug string of // the value if it is a protocol buffer, or print the raw bytes in the // value otherwise. @@ -107,6 +111,10 @@ # include <tuple> #endif +#if GTEST_HAS_ABSL +#include "absl/types/optional.h" +#endif // GTEST_HAS_ABSL + namespace testing { // Definitions in the 'internal' and 'internal2' name spaces are @@ -722,6 +730,26 @@ class UniversalPrinter { GTEST_DISABLE_MSC_WARNINGS_POP_() }; +#if GTEST_HAS_ABSL + +// Printer for absl::optional + +template <typename T> +class UniversalPrinter<::absl::optional<T>> { + public: + static void Print(const ::absl::optional<T>& value, ::std::ostream* os) { + *os << '('; + if (!value) { + *os << "nullopt"; + } else { + UniversalPrint(*value, os); + } + *os << ')'; + } +}; + +#endif // GTEST_HAS_ABSL + // UniversalPrintArray(begin, len, os) prints an array of 'len' // elements, starting at address 'begin'. template <typename T> diff --git a/googletest/test/gtest-printers_test.cc b/googletest/test/gtest-printers_test.cc index e30ce7e..42e1965 100644 --- a/googletest/test/gtest-printers_test.cc +++ b/googletest/test/gtest-printers_test.cc @@ -1765,5 +1765,17 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) { #endif // GTEST_HAS_STD_TUPLE_ +#if GTEST_HAS_ABSL + +TEST(PrintOptionalTest, Basic) { + absl::optional<int> value; + EXPECT_EQ("(nullopt)", PrintToString(value)); + value = {7}; + EXPECT_EQ("(7)", PrintToString(value)); + EXPECT_EQ("(1.1)", PrintToString(absl::optional<double>{1.1})); + EXPECT_EQ("(\"A\")", PrintToString(absl::optional<std::string>{"A"})); +} +#endif // GTEST_HAS_ABSL + } // namespace gtest_printers_test } // namespace testing |