Make absl_testing::StatusIs() print the status code as a string

PiperOrigin-RevId: 879816561
Change-Id: If257ed800753dc049dfb73e1948ba35ee7846b5d
This commit is contained in:
Abseil Team
2026-03-06 14:17:57 -08:00
committed by Copybara-Service
parent 6015232266
commit e72b94a2f2
2 changed files with 14 additions and 9 deletions

View File

@@ -126,18 +126,24 @@ class IsOkAndHoldsMatcher {
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..

View File

@@ -122,10 +122,9 @@ TEST(StatusMatcherTest, StatusIs) {
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(