Index: webrtc/base/optional_unittest.cc |
diff --git a/webrtc/base/optional_unittest.cc b/webrtc/base/optional_unittest.cc |
index 65070fabb03f2ed0383d64a3aa70f279188e5c14..78eb6b4fc1858ccdcb095ce3920ae1dbc3344b8c 100644 |
--- a/webrtc/base/optional_unittest.cc |
+++ b/webrtc/base/optional_unittest.cc |
@@ -21,6 +21,43 @@ namespace rtc { |
namespace { |
+struct MyUnprintableType { |
+ int value; |
+}; |
+ |
+struct MyPrintableType { |
+ int value; |
+}; |
+ |
+struct MyOstreamPrintableType { |
+ int value; |
+}; |
+ |
+// Since only one of these two (PrintTo and operator<<) will be picked by gtest |
+// for printing, we need to ensure it's fine that the other one is unused. The |
+// test itself will ensure the correct one is called. |
+void PrintTo(const MyPrintableType& mpt, std::ostream* os) |
+ __attribute__((unused)); |
+ |
+void PrintTo(const MyPrintableType& mpt, std::ostream* os) { |
+ *os << "The value is " << mpt.value; |
+} |
+ |
+std::ostream& operator<<(std::ostream& os, |
+ const MyPrintableType& mpt) __attribute__((unused)); |
+ |
+std::ostream& operator<<(std::ostream& os, |
+ const MyPrintableType& mpt) { |
+ os << mpt.value; |
+ return os; |
+} |
+ |
+std::ostream& operator<<(std::ostream& os, |
+ const MyOstreamPrintableType& mpt) { |
+ os << mpt.value; |
+ return os; |
+} |
+ |
// Class whose instances logs various method calls (constructor, destructor, |
// etc.). Each instance has a unique ID (a simple global sequence number) and |
// an origin ID. When a copy is made, the new object gets a fresh ID but copies |
@@ -740,4 +777,21 @@ TEST(OptionalTest, TestMoveValue) { |
*log); |
} |
+TEST(OptionalTest, TestPrintTo) { |
+ constexpr char kEmptyOptionalMessage[] = "<empty optional>"; |
+ const Optional<MyUnprintableType> empty_unprintable; |
+ const Optional<MyPrintableType> empty_printable; |
+ const Optional<MyOstreamPrintableType> empty_ostream_printable; |
+ EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable)); |
+ EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable)); |
+ EXPECT_EQ(kEmptyOptionalMessage, |
+ ::testing::PrintToString(empty_ostream_printable)); |
+ EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>({1}))); |
+ EXPECT_NE("1", ::testing::PrintToString(Optional<MyPrintableType>({1}))); |
+ EXPECT_EQ("The value is 1", |
+ ::testing::PrintToString(Optional<MyPrintableType>({1}))); |
+ EXPECT_EQ("1", |
+ ::testing::PrintToString(Optional<MyOstreamPrintableType>({1}))); |
+} |
+ |
} // namespace rtc |