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

Unified Diff: webrtc/base/optional_unittest.cc

Issue 2704483002: Add a PrintTo function for rtc::Optional to aid with testing. (Closed)
Patch Set: Reworked PrintTo so that it's always implemented, or gtest will print out uninitialized bytes. 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
« webrtc/base/optional.cc ('K') | « webrtc/base/optional.cc ('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..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
« webrtc/base/optional.cc ('K') | « webrtc/base/optional.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698