Skip to content

Commit

Permalink
Adds AbslStringify to absl::Status for completeness.
Browse files Browse the repository at this point in the history
This also adds a test for `operator<<`.

PiperOrigin-RevId: 568590367
Change-Id: Ia0ad39cb582e7d24e6c4131827818d8c4b10dfd9
  • Loading branch information
Abseil Team authored and copybara-github committed Sep 26, 2023
1 parent f4c6246 commit e3114cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
1 change: 1 addition & 0 deletions absl/status/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ cc_test(
":status",
"//absl/strings",
"//absl/strings:cord",
"//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
1 change: 1 addition & 0 deletions absl/status/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::status
absl::str_format
absl::strings
GTest::gmock_main
)
Expand Down
7 changes: 6 additions & 1 deletion absl/status/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
#include "absl/functional/function_ref.h"
#include "absl/status/internal/status_internal.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"

Expand Down Expand Up @@ -523,6 +522,12 @@ class Status final {
std::string ToString(
StatusToStringMode mode = StatusToStringMode::kDefault) const;

// Support `absl::StrCat`, `absl::StrFormat`, etc.
template <typename Sink>
friend void AbslStringify(Sink& sink, const Status& status) {
sink.Append(status.ToString(StatusToStringMode::kWithEverything));
}

// Status::IgnoreError()
//
// Ignores any errors. This method does nothing except potentially suppress
Expand Down
67 changes: 54 additions & 13 deletions absl/status/status_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "gtest/gtest.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"

namespace {

Expand Down Expand Up @@ -305,37 +306,77 @@ TEST(Status, TestForEachPayload) {
}

TEST(Status, ToString) {
absl::Status s(absl::StatusCode::kInternal, "fail");
EXPECT_EQ("INTERNAL: fail", s.ToString());
s.SetPayload("foo", absl::Cord("bar"));
EXPECT_EQ("INTERNAL: fail [foo='bar']", s.ToString());
s.SetPayload("bar", absl::Cord("\377"));
EXPECT_THAT(s.ToString(),
absl::Status status(absl::StatusCode::kInternal, "fail");
EXPECT_EQ("INTERNAL: fail", status.ToString());
status.SetPayload("foo", absl::Cord("bar"));
EXPECT_EQ("INTERNAL: fail [foo='bar']", status.ToString());
status.SetPayload("bar", absl::Cord("\377"));
EXPECT_THAT(status.ToString(),
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
HasSubstr("[bar='\\xff']")));
}

TEST(Status, ToStringMode) {
absl::Status s(absl::StatusCode::kInternal, "fail");
s.SetPayload("foo", absl::Cord("bar"));
s.SetPayload("bar", absl::Cord("\377"));
absl::Status status(absl::StatusCode::kInternal, "fail");
status.SetPayload("foo", absl::Cord("bar"));
status.SetPayload("bar", absl::Cord("\377"));

EXPECT_EQ("INTERNAL: fail",
s.ToString(absl::StatusToStringMode::kWithNoExtraData));
status.ToString(absl::StatusToStringMode::kWithNoExtraData));

EXPECT_THAT(s.ToString(absl::StatusToStringMode::kWithPayload),
EXPECT_THAT(status.ToString(absl::StatusToStringMode::kWithPayload),
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
HasSubstr("[bar='\\xff']")));

EXPECT_THAT(s.ToString(absl::StatusToStringMode::kWithEverything),
EXPECT_THAT(status.ToString(absl::StatusToStringMode::kWithEverything),
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
HasSubstr("[bar='\\xff']")));

EXPECT_THAT(s.ToString(~absl::StatusToStringMode::kWithPayload),
EXPECT_THAT(status.ToString(~absl::StatusToStringMode::kWithPayload),
AllOf(HasSubstr("INTERNAL: fail"), Not(HasSubstr("[foo='bar']")),
Not(HasSubstr("[bar='\\xff']"))));
}

TEST(Status, OstreamOperator) {
absl::Status status(absl::StatusCode::kInternal, "fail");
{ std::stringstream stream;
stream << status;
EXPECT_EQ("INTERNAL: fail", stream.str());
}
status.SetPayload("foo", absl::Cord("bar"));
{ std::stringstream stream;
stream << status;
EXPECT_EQ("INTERNAL: fail [foo='bar']", stream.str());
}
status.SetPayload("bar", absl::Cord("\377"));
{ std::stringstream stream;
stream << status;
EXPECT_THAT(stream.str(),
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
HasSubstr("[bar='\\xff']")));
}
}

TEST(Status, AbslStringify) {
absl::Status status(absl::StatusCode::kInternal, "fail");
EXPECT_EQ("INTERNAL: fail", absl::StrCat(status));
EXPECT_EQ("INTERNAL: fail", absl::StrFormat("%v", status));
status.SetPayload("foo", absl::Cord("bar"));
EXPECT_EQ("INTERNAL: fail [foo='bar']", absl::StrCat(status));
status.SetPayload("bar", absl::Cord("\377"));
EXPECT_THAT(absl::StrCat(status),
AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
HasSubstr("[bar='\\xff']")));
}

TEST(Status, OstreamEqStringify) {
absl::Status status(absl::StatusCode::kUnknown, "fail");
status.SetPayload("foo", absl::Cord("bar"));
std::stringstream stream;
stream << status;
EXPECT_EQ(stream.str(), absl::StrCat(status));
}

absl::Status EraseAndReturn(const absl::Status& base) {
absl::Status copy = base;
EXPECT_TRUE(copy.ErasePayload(kUrl1));
Expand Down

0 comments on commit e3114cc

Please sign in to comment.