Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Unified Diff: webrtc/base/optional_unittest.cc

Issue 2704483002: Add a PrintTo function for rtc::Optional to aid with testing. (Closed)
Patch Set: Removed unnecessary stuff. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/optional_unittest.cc
diff --git a/webrtc/base/optional_unittest.cc b/webrtc/base/optional_unittest.cc
index 65070fabb03f2ed0383d64a3aa70f279188e5c14..cc9d2f91643a53aac1a2d5dde7835c7d30b6e7be 100644
--- a/webrtc/base/optional_unittest.cc
+++ b/webrtc/base/optional_unittest.cc
@@ -21,6 +21,34 @@ namespace rtc {
namespace {
+struct MyUnprintableType {
+ int value;
+};
+
+struct MyPrintableType {
+ int value;
+};
+
+struct MyOstreamPrintableType {
+ int value;
+};
+
+void PrintTo(const MyPrintableType& mpt, std::ostream* os) {
+ *os << "The value is " << mpt.value;
+}
+
+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 +768,32 @@ 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})));
+}
+
+void UnusedFunctionWorkaround() {
+ // These are here to ensure we don't get warnings about ostream and PrintTo
+ // for MyPrintableType never getting called.
+ const MyPrintableType dont_warn{17};
+ const MyOstreamPrintableType dont_warn2{18};
+ std::stringstream sstr;
+ sstr << dont_warn;
+ PrintTo(dont_warn, &sstr);
+ sstr << dont_warn2;
+}
+
} // namespace rtc
« no previous file with comments | « webrtc/base/optional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698