feat update

This commit is contained in:
tqcq
2024-03-23 15:40:27 +08:00
parent 56d57d5783
commit 74b59961ed
7 changed files with 94 additions and 33 deletions

View File

@ -9,6 +9,10 @@ TEST(format, enum)
kThree = 3,
};
std::stringstream ss;
ss << kOne;
EXPECT_EQ(ss.str(), "1");
EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "123");
}
@ -22,3 +26,19 @@ TEST(format, neg_enum)
EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "-1-2-3");
}
struct Streamable {
int value;
};
std::ostream &
operator<<(std::ostream &os, const Streamable &s)
{
return os << s.value;
}
TEST(format, streamable)
{
Streamable s{42};
EXPECT_EQ(fmt::format("{}", s), "42");
}

View File

@ -150,10 +150,25 @@ Status::Equals(Status const &a, Status const &b)
return (a.ok() && b.ok()) || (a.impl_ && b.impl_ && *a.impl_ == *b.impl_);
}
std::ostream &
operator<<(std::ostream &os, const Status &s)
namespace internal {
void
AddMetadata(ErrorInfo &error_info, std::string const &key, std::string value)
{
if (s.ok()) return os << StatusCode::kOk;
error_info.metadata_[key] = std::move(value);
}
void
SetPayload(Status &s, std::string key, std::string payload)
{
if (s.impl_) s.impl_->payload()[std::move(key)] = std::move(payload);
}
}// namespace internal
std::ostream &
operator<<(std::ostream &os, const sled::Status &s)
{
if (s.ok()) return os << sled::StatusCode::kOk;
os << s.code() << ": " << s.message();
auto const &e = s.error_info();
if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { return os; }
@ -182,19 +197,4 @@ operator<<(std::ostream &os, const Status &s)
return os << "}";
}
namespace internal {
void
AddMetadata(ErrorInfo &error_info, std::string const &key, std::string value)
{
error_info.metadata_[key] = std::move(value);
}
void
SetPayload(Status &s, std::string key, std::string payload)
{
if (s.impl_) s.impl_->payload()[std::move(key)] = std::move(payload);
}
}// namespace internal
}// namespace sled

View File

@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/status_or.h>
TEST(StatusOr, TestStatusOr)

9
src/status_test.cc Normal file
View File

@ -0,0 +1,9 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/status.h>
TEST(Status, format)
{
auto status = sled::Status(sled::StatusCode::kOk, "");
EXPECT_EQ(fmt::format("{}", status), "");
}