summaryrefslogtreecommitdiff
path: root/absl/status
diff options
context:
space:
mode:
authorGravatar Derek Mauro <dmauro@google.com>2024-05-03 09:01:15 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2024-05-03 09:02:24 -0700
commit7e149e40c7a2d8049ecd28d1f83f64cc197cc128 (patch)
treed8dd6fcf235e68f23f94cc4ec148544acff6f0d8 /absl/status
parentd9f501ef8482b3e6a41e3b544fde4569ad909c0b (diff)
Remove the forked absl::Status matchers implementation in statusor_test
PiperOrigin-RevId: 630404862 Change-Id: Icfe4bea2657d319cdd10902ee79af895c43602f1
Diffstat (limited to 'absl/status')
-rw-r--r--absl/status/BUILD.bazel1
-rw-r--r--absl/status/CMakeLists.txt1
-rw-r--r--absl/status/statusor_test.cc167
3 files changed, 26 insertions, 143 deletions
diff --git a/absl/status/BUILD.bazel b/absl/status/BUILD.bazel
index 810a3dda..8822e0f6 100644
--- a/absl/status/BUILD.bazel
+++ b/absl/status/BUILD.bazel
@@ -118,6 +118,7 @@ cc_test(
srcs = ["statusor_test.cc"],
deps = [
":status",
+ ":status_matchers",
":statusor",
"//absl/base",
"//absl/memory",
diff --git a/absl/status/CMakeLists.txt b/absl/status/CMakeLists.txt
index d9f04695..24c01e7a 100644
--- a/absl/status/CMakeLists.txt
+++ b/absl/status/CMakeLists.txt
@@ -98,6 +98,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::status
+ absl::status_matchers
absl::statusor
absl::strings
GTest::gmock_main
diff --git a/absl/status/statusor_test.cc b/absl/status/statusor_test.cc
index 09ffc658..83410404 100644
--- a/absl/status/statusor_test.cc
+++ b/absl/status/statusor_test.cc
@@ -31,6 +31,7 @@
#include "absl/base/casts.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
+#include "absl/status/status_matchers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/any.h"
@@ -39,6 +40,8 @@
namespace {
+using ::absl_testing::IsOk;
+using ::absl_testing::IsOkAndHolds;
using ::testing::AllOf;
using ::testing::AnyOf;
using ::testing::AnyWith;
@@ -52,128 +55,6 @@ using ::testing::Pointee;
using ::testing::StartsWith;
using ::testing::VariantWith;
-#ifdef GTEST_HAS_STATUS_MATCHERS
-using ::testing::status::IsOk;
-using ::testing::status::IsOkAndHolds;
-#else // GTEST_HAS_STATUS_MATCHERS
-inline const ::absl::Status& GetStatus(const ::absl::Status& status) {
- return status;
-}
-
-template <typename T>
-inline const ::absl::Status& GetStatus(const ::absl::StatusOr<T>& status) {
- return status.status();
-}
-
-// Monomorphic implementation of matcher IsOkAndHolds(m). StatusOrType is a
-// reference to StatusOr<T>.
-template <typename StatusOrType>
-class IsOkAndHoldsMatcherImpl
- : public ::testing::MatcherInterface<StatusOrType> {
- public:
- typedef
- typename std::remove_reference<StatusOrType>::type::value_type value_type;
-
- template <typename InnerMatcher>
- explicit IsOkAndHoldsMatcherImpl(InnerMatcher&& inner_matcher)
- : inner_matcher_(::testing::SafeMatcherCast<const value_type&>(
- std::forward<InnerMatcher>(inner_matcher))) {}
-
- void DescribeTo(std::ostream* os) const override {
- *os << "is OK and has a value that ";
- inner_matcher_.DescribeTo(os);
- }
-
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "isn't OK or has a value that ";
- inner_matcher_.DescribeNegationTo(os);
- }
-
- bool MatchAndExplain(
- StatusOrType actual_value,
- ::testing::MatchResultListener* result_listener) const override {
- if (!actual_value.ok()) {
- *result_listener << "which has status " << actual_value.status();
- return false;
- }
-
- ::testing::StringMatchResultListener inner_listener;
- const bool matches =
- inner_matcher_.MatchAndExplain(*actual_value, &inner_listener);
- const std::string inner_explanation = inner_listener.str();
- if (!inner_explanation.empty()) {
- *result_listener << "which contains value "
- << ::testing::PrintToString(*actual_value) << ", "
- << inner_explanation;
- }
- return matches;
- }
-
- private:
- const ::testing::Matcher<const value_type&> inner_matcher_;
-};
-
-// Implements IsOkAndHolds(m) as a polymorphic matcher.
-template <typename InnerMatcher>
-class IsOkAndHoldsMatcher {
- public:
- explicit IsOkAndHoldsMatcher(InnerMatcher inner_matcher)
- : inner_matcher_(std::move(inner_matcher)) {}
-
- // Converts this polymorphic matcher to a monomorphic matcher of the
- // given type. StatusOrType can be either StatusOr<T> or a
- // reference to StatusOr<T>.
- template <typename StatusOrType>
- operator ::testing::Matcher<StatusOrType>() const { // NOLINT
- return ::testing::Matcher<StatusOrType>(
- new IsOkAndHoldsMatcherImpl<const StatusOrType&>(inner_matcher_));
- }
-
- private:
- const InnerMatcher inner_matcher_;
-};
-
-// Monomorphic implementation of matcher IsOk() for a given type T.
-// T can be Status, StatusOr<>, or a reference to either of them.
-template <typename T>
-class MonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> {
- public:
- void DescribeTo(std::ostream* os) const override { *os << "is OK"; }
- void DescribeNegationTo(std::ostream* os) const override {
- *os << "is not OK";
- }
- bool MatchAndExplain(T actual_value,
- ::testing::MatchResultListener*) const override {
- return GetStatus(actual_value).ok();
- }
-};
-
-// Implements IsOk() as a polymorphic matcher.
-class IsOkMatcher {
- public:
- template <typename T>
- operator ::testing::Matcher<T>() const { // NOLINT
- return ::testing::Matcher<T>(new MonoIsOkMatcherImpl<T>());
- }
-};
-
-// Macros for testing the results of functions that return absl::Status or
-// absl::StatusOr<T> (for any type T).
-#define EXPECT_OK(expression) EXPECT_THAT(expression, IsOk())
-
-// Returns a gMock matcher that matches a StatusOr<> whose status is
-// OK and whose value matches the inner matcher.
-template <typename InnerMatcher>
-IsOkAndHoldsMatcher<typename std::decay<InnerMatcher>::type> IsOkAndHolds(
- InnerMatcher&& inner_matcher) {
- return IsOkAndHoldsMatcher<typename std::decay<InnerMatcher>::type>(
- std::forward<InnerMatcher>(inner_matcher));
-}
-
-// Returns a gMock matcher that matches a Status or StatusOr<> which is OK.
-inline IsOkMatcher IsOk() { return IsOkMatcher(); }
-#endif // GTEST_HAS_STATUS_MATCHERS
-
struct CopyDetector {
CopyDetector() = default;
explicit CopyDetector(int xx) : x(xx) {}
@@ -527,7 +408,7 @@ TEST(StatusOr, TestCopyCtorStatusOk) {
const int kI = 4;
const absl::StatusOr<int> original(kI);
const absl::StatusOr<int> copy(original);
- EXPECT_OK(copy.status());
+ EXPECT_THAT(copy.status(), IsOk());
EXPECT_EQ(*original, *copy);
}
@@ -542,7 +423,7 @@ TEST(StatusOr, TestCopyCtorNonAssignable) {
CopyNoAssign value(kI);
absl::StatusOr<CopyNoAssign> original(value);
absl::StatusOr<CopyNoAssign> copy(original);
- EXPECT_OK(copy.status());
+ EXPECT_THAT(copy.status(), IsOk());
EXPECT_EQ(original->foo, copy->foo);
}
@@ -550,7 +431,7 @@ TEST(StatusOr, TestCopyCtorStatusOKConverting) {
const int kI = 4;
absl::StatusOr<int> original(kI);
absl::StatusOr<double> copy(original);
- EXPECT_OK(copy.status());
+ EXPECT_THAT(copy.status(), IsOk());
EXPECT_DOUBLE_EQ(*original, *copy);
}
@@ -570,11 +451,11 @@ TEST(StatusOr, TestAssignmentStatusOk) {
target = source;
ASSERT_TRUE(target.ok());
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_EQ(p, *target);
ASSERT_TRUE(source.ok());
- EXPECT_OK(source.status());
+ EXPECT_THAT(source.status(), IsOk());
EXPECT_EQ(p, *source);
}
@@ -587,11 +468,11 @@ TEST(StatusOr, TestAssignmentStatusOk) {
target = std::move(source);
ASSERT_TRUE(target.ok());
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_EQ(p, *target);
ASSERT_TRUE(source.ok());
- EXPECT_OK(source.status());
+ EXPECT_THAT(source.status(), IsOk());
EXPECT_EQ(nullptr, *source);
}
}
@@ -638,11 +519,11 @@ TEST(StatusOr, TestAssignmentStatusOKConverting) {
target = source;
ASSERT_TRUE(target.ok());
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_DOUBLE_EQ(kI, *target);
ASSERT_TRUE(source.ok());
- EXPECT_OK(source.status());
+ EXPECT_THAT(source.status(), IsOk());
EXPECT_DOUBLE_EQ(kI, *source);
}
@@ -655,11 +536,11 @@ TEST(StatusOr, TestAssignmentStatusOKConverting) {
target = std::move(source);
ASSERT_TRUE(target.ok());
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_EQ(p, target->get());
ASSERT_TRUE(source.ok());
- EXPECT_OK(source.status());
+ EXPECT_THAT(source.status(), IsOk());
EXPECT_EQ(nullptr, source->get());
}
}
@@ -1078,7 +959,7 @@ TEST(StatusOr, SelfAssignment) {
so = *&so;
ASSERT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(long_str, *so);
}
@@ -1101,7 +982,7 @@ TEST(StatusOr, SelfAssignment) {
so = std::move(same);
ASSERT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(17, *so);
}
@@ -1128,7 +1009,7 @@ TEST(StatusOr, SelfAssignment) {
so = std::move(same);
ASSERT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(raw, so->get());
}
@@ -1361,7 +1242,7 @@ TEST(StatusOr, TestPointerValueCtor) {
{
absl::StatusOr<const int*> so(&kI);
EXPECT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(&kI, *so);
}
@@ -1369,7 +1250,7 @@ TEST(StatusOr, TestPointerValueCtor) {
{
absl::StatusOr<const int*> so(nullptr);
EXPECT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(nullptr, *so);
}
@@ -1379,7 +1260,7 @@ TEST(StatusOr, TestPointerValueCtor) {
absl::StatusOr<const int*> so(p);
EXPECT_TRUE(so.ok());
- EXPECT_OK(so.status());
+ EXPECT_THAT(so.status(), IsOk());
EXPECT_EQ(nullptr, *so);
}
}
@@ -1388,7 +1269,7 @@ TEST(StatusOr, TestPointerCopyCtorStatusOk) {
const int kI = 0;
absl::StatusOr<const int*> original(&kI);
absl::StatusOr<const int*> copy(original);
- EXPECT_OK(copy.status());
+ EXPECT_THAT(copy.status(), IsOk());
EXPECT_EQ(*original, *copy);
}
@@ -1402,7 +1283,7 @@ TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) {
Derived derived;
absl::StatusOr<Derived*> original(&derived);
absl::StatusOr<Base2*> copy(original);
- EXPECT_OK(copy.status());
+ EXPECT_THAT(copy.status(), IsOk());
EXPECT_EQ(static_cast<const Base2*>(*original), *copy);
}
@@ -1417,7 +1298,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOk) {
absl::StatusOr<const int*> source(&kI);
absl::StatusOr<const int*> target;
target = source;
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_EQ(*source, *target);
}
@@ -1433,7 +1314,7 @@ TEST(StatusOr, TestPointerAssignmentStatusOKConverting) {
absl::StatusOr<Derived*> source(&derived);
absl::StatusOr<Base2*> target;
target = source;
- EXPECT_OK(target.status());
+ EXPECT_THAT(target.status(), IsOk());
EXPECT_EQ(static_cast<const Base2*>(*source), *target);
}