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

Unified Diff: webrtc/base/optional.h

Issue 2704483002: Add a PrintTo function for rtc::Optional to aid with testing. (Closed)
Patch Set: 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 | « no previous file | webrtc/base/optional_unittest.cc » ('j') | webrtc/base/optional_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_
« no previous file with comments | « no previous file | webrtc/base/optional_unittest.cc » ('j') | webrtc/base/optional_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698