 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.h | 
| diff --git a/webrtc/base/optional.h b/webrtc/base/optional.h | 
| index f5354ee0f25fdb6eabcf6fa367e3db64152681ee..104ba6684900d9ba30645db9aaf3cd0cc0c395e5 100644 | 
| --- a/webrtc/base/optional.h | 
| +++ b/webrtc/base/optional.h | 
| @@ -13,6 +13,7 @@ | 
| #include <algorithm> | 
| #include <memory> | 
| +#include <ostream> | 
| #include <utility> | 
| #include "webrtc/base/array_view.h" | 
| @@ -296,6 +297,19 @@ class Optional final { | 
| }; | 
| }; | 
| +// Add a PrintTo function for Optionals for use with gtest, only if | 
| +// there is already a valid operator<< available for the enclosed type. | 
| +// The cast in decltype will ensure PrintTo has a void return type. | 
| +template <typename T> | 
| +auto PrintTo(const Optional<T>& opt, std::ostream* os) | 
| 
ossu
2017/02/16 14:23:48
Turns out the compiler does clever things that lin
 
tommi
2017/02/17 13:46:23
Can we call this PrintToForTest?
There's a risk t
 
ossu
2017/02/17 13:50:06
Alas, no, it needs to be called PrintTo for gtest
 | 
| + -> decltype(static_cast<void>(*os << *opt)) { | 
| 
ossu
2017/02/16 13:20:05
The SFINAE here is maybe not super-obvious if one'
 
kwiberg-webrtc
2017/02/16 14:07:37
Yes, more explanation is probably called for. How
 
ossu
2017/02/16 14:23:48
Will do!
 | 
| + if (opt) { | 
| + *os << *opt; | 
| 
kwiberg-webrtc
2017/02/16 14:07:37
This is test code, so maybe indicate that the valu
 
ossu
2017/02/16 14:23:48
I considered that but was unsure if it would be mo
 | 
| + } else { | 
| + *os << "<empty optional>"; | 
| + } | 
| +} | 
| + | 
| } // namespace rtc | 
| #endif // WEBRTC_BASE_OPTIONAL_H_ |