 Chromium Code Reviews
 Chromium Code Reviews Issue 2704483002:
  Add a PrintTo function for rtc::Optional to aid with testing.  (Closed)
    
  
    Issue 2704483002:
  Add a PrintTo function for rtc::Optional to aid with testing.  (Closed) 
  | Index: webrtc/base/optional_unittest.cc | 
| diff --git a/webrtc/base/optional_unittest.cc b/webrtc/base/optional_unittest.cc | 
| index 65070fabb03f2ed0383d64a3aa70f279188e5c14..18bd901d0aa0136b074b6a30ab4f14b91e339e22 100644 | 
| --- a/webrtc/base/optional_unittest.cc | 
| +++ b/webrtc/base/optional_unittest.cc | 
| @@ -21,6 +21,26 @@ namespace rtc { | 
| namespace { | 
| +struct MyUnprintableType { | 
| + MyUnprintableType() = default; | 
| + MyUnprintableType(int value) : value(value) {} | 
| + | 
| + int value; | 
| 
kwiberg-webrtc
2017/02/16 14:07:37
You can make these classes several lines shorter b
 
ossu
2017/02/16 14:23:48
That is true. I could also just use brace initiali
 | 
| +}; | 
| + | 
| +struct MyPrintableType { | 
| + MyPrintableType() = default; | 
| + MyPrintableType(int value) : value(value) {} | 
| + | 
| + int value; | 
| + | 
| + friend std::ostream& operator<<(std::ostream& os, | 
| + const MyPrintableType& 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 +760,18 @@ TEST(OptionalTest, TestMoveValue) { | 
| *log); | 
| } | 
| +TEST(OptionalTest, TestPrintTo) { | 
| + constexpr char kEmptyOptionalMessage[] = "<empty optional>"; | 
| + const Optional<MyUnprintableType> empty_unprintable; | 
| + const Optional<MyPrintableType> empty_printable; | 
| + static_assert(std::is_same<void, decltype(PrintTo(empty_printable, | 
| 
ossu
2017/02/16 13:20:05
Not really the greatest place to do a static_asser
 
kwiberg-webrtc
2017/02/16 14:07:37
Hmm. When would the return value not be void?
 
ossu
2017/02/16 14:23:48
I put this here only to ensure that my cast in dec
 
kwiberg-webrtc
2017/02/16 14:33:33
Oh, OK, that explains why you were talking about w
 | 
| + static_cast<std::ostream*>( | 
| + nullptr)))>::value != 0, | 
| 
kwiberg-webrtc
2017/02/16 14:07:37
std::declval<std::ostream*>() ?
 
ossu
2017/02/16 14:23:48
Ah, right! The last time I did this, I had a C++11
 | 
| + "PrintTo should return void"); | 
| + EXPECT_NE(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable)); | 
| + EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable)); | 
| + EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>(1))); | 
| + EXPECT_EQ("1", ::testing::PrintToString(Optional<MyPrintableType>(1))); | 
| +} | 
| + | 
| } // namespace rtc |