Improve gtest printing of Result class. am: 174fef50ba

Original change: https://android-review.googlesource.com/c/platform/system/libbase/+/1727415

Change-Id: Ib2c0d10d84f63a32d7f0d546efe4fa69114a1384
diff --git a/include/android-base/result-gmock.h b/include/android-base/result-gmock.h
index 52e1ccb..4694e7e 100644
--- a/include/android-base/result-gmock.h
+++ b/include/android-base/result-gmock.h
@@ -16,6 +16,7 @@
 
 #include <android-base/result.h>
 #include <gmock/gmock.h>
+#include <gtest/gtest.h>
 
 /*
  * Matchers for android::base::Result<T> that produces human-readable test results.
@@ -38,7 +39,27 @@
  * EXPECT_THAT(result, HasError(WithCode(AnyOf(EBADF, EACCES))) << "Unexpected code from library";
  */
 
-namespace android::base::testing {
+namespace android::base {
+
+template <typename T>
+void PrintTo(const Result<T>& result, std::ostream* os) {
+  if (result.ok()) {
+    *os << "OK: " << ::testing::PrintToString(result.value());
+  } else {
+    *os << "Error: " << result.error();
+  }
+}
+
+template <>
+void PrintTo(const Result<void>& result, std::ostream* os) {
+  if (result.ok()) {
+    *os << "OK";
+  } else {
+    *os << "Error: " << result.error();
+  }
+}
+
+namespace testing {
 
 MATCHER(Ok, "") {
   if (arg.ok()) {
@@ -75,4 +96,5 @@
   return ::testing::ExplainMatchResult(messgae_matcher, arg.message(), result_listener);
 }
 
-}  // namespace android::base::testing
+}  // namespace testing
+}  // namespace android::base