Add macros to check for successful Result

These macros are meant to be used in tests:

  Result<File> maybe_a_file = OpenFile(...);
  EXPECT_OK(maybe_a_file);

On failure, the error is printed.

There's no equivalent EXPECT_NOT_OK() because it is a testing anti-pattern
which causes tests to pass even when the error changed as a result of a
regression. Use EPECT_EQ(result, Error(...)) instead.

Test: cd system/core && atest
Test: m
Change-Id: Ie26f90d3c62620e7b1f10013829ba43ef5364fe1
diff --git a/base/include/android-base/result.h b/base/include/android-base/result.h
index b6d26e7..52fb6e7 100644
--- a/base/include/android-base/result.h
+++ b/base/include/android-base/result.h
@@ -204,5 +204,28 @@
 template <typename T>
 using Result = android::base::expected<T, ResultError>;
 
+// Macros for testing the results of functions that return android::base::Result.
+// These also work with base::android::expected.
+
+#define CHECK_RESULT_OK(stmt)       \
+  do {                              \
+    const auto& tmp = (stmt);       \
+    CHECK(tmp.ok()) << tmp.error(); \
+  } while (0)
+
+#define ASSERT_RESULT_OK(stmt)            \
+  do {                                    \
+    const auto& tmp = (stmt);             \
+    ASSERT_TRUE(tmp.ok()) << tmp.error(); \
+  } while (0)
+
+#define EXPECT_RESULT_OK(stmt)            \
+  do {                                    \
+    auto tmp = (stmt);                    \
+    EXPECT_TRUE(tmp.ok()) << tmp.error(); \
+  } while (0)
+
+// TODO: Maybe add RETURN_IF_ERROR() and ASSIGN_OR_RETURN()
+
 }  // namespace base
 }  // namespace android