From 8be5c0cdbe5e070da47fad4742cafe0f9ab04e29 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 14 Aug 2024 00:51:22 -0400 Subject: [PATCH] add support for testing absl::StatusOr --- src/test/core/testing.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/test/core/testing.h b/src/test/core/testing.h index b1577256..a8e68178 100644 --- a/src/test/core/testing.h +++ b/src/test/core/testing.h @@ -24,10 +24,24 @@ namespace test { // StatusIs is a matcher that matches a status that has the same code and // message as the expected status. MATCHER_P(StatusIs, status, "") { return arg.code() == status; } -MATCHER_P(StatusIs, status, "") { - return arg.code() == status; + +// Support for testing absl::StatusOr. +template +::testing::AssertionResult IsOkAndHolds(const absl::StatusOr& status_or, + const T& value) { + if (!status_or.ok()) { + return ::testing::AssertionFailure() + << "Expected status to be OK, but got: " << status_or.status(); + } + if (status_or.value() != value) { + return ::testing::AssertionFailure() << "Expected value to be " << value + << ", but got: " << status_or.value(); + } + return ::testing::AssertionSuccess(); } +MATCHER_P(IsOkAndHolds, value, "") { return IsOkAndHolds(arg, value); } + } // namespace test } // namespace yaze