Make absl_testing::StatusIs() print the status code as a string PiperOrigin-RevId: 879816561 Change-Id: If257ed800753dc049dfb73e1948ba35ee7846b5d
diff --git a/absl/status/internal/status_matchers.h b/absl/status/internal/status_matchers.h index eb61d73..2eafd13 100644 --- a/absl/status/internal/status_matchers.h +++ b/absl/status/internal/status_matchers.h
@@ -126,18 +126,24 @@ class StatusCode { public: /*implicit*/ StatusCode(int code) // NOLINT - : code_(static_cast<::absl::StatusCode>(code)) {} - /*implicit*/ StatusCode(::absl::StatusCode code) : code_(code) {} // NOLINT + : code_(code) {} + /*implicit*/ StatusCode(::absl::StatusCode code) // NOLINT + : code_(static_cast<int>(code)) {} explicit operator int() const { return static_cast<int>(code_); } friend inline void PrintTo(const StatusCode& code, std::ostream* os) { - // TODO(b/321095377): Change this to print the status code as a string. - *os << static_cast<int>(code); + absl::string_view text = + absl::StatusCodeToStringView(static_cast<absl::StatusCode>(code.code_)); + if (!text.empty()) { + *os << text; + } else { + *os << code.code_; + } } private: - ::absl::StatusCode code_; + int code_; }; // Relational operators to handle matchers like Eq, Lt, etc..
diff --git a/absl/status/status_matchers_test.cc b/absl/status/status_matchers_test.cc index 8656b2d..54cc733 100644 --- a/absl/status/status_matchers_test.cc +++ b/absl/status/status_matchers_test.cc
@@ -122,10 +122,9 @@ StatusIs(absl::StatusCode::kInvalidArgument, "ungueltig")); auto m = StatusIs(absl::StatusCode::kInternal, "internal error"); - EXPECT_THAT( - ::testing::DescribeMatcher<absl::Status>(m), - MatchesRegex( - "has a status code that .*, and has an error message that .*")); + EXPECT_THAT(::testing::DescribeMatcher<absl::Status>(m), + MatchesRegex("has a status code that is equal to INTERNAL, and " + "has an error message that .*")); EXPECT_THAT( ::testing::DescribeMatcher<absl::Status>(m, /*negation=*/true), MatchesRegex(