feat update
All checks were successful
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m23s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m25s

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

@ -129,6 +129,7 @@ if(SLED_BUILD_TESTS)
src/strings/base64_test.cc src/strings/base64_test.cc
src/synchronization/sequence_checker_test.cc src/synchronization/sequence_checker_test.cc
src/cleanup_test.cc src/cleanup_test.cc
src/status_test.cc
src/status_or_test.cc src/status_or_test.cc
src/system/fiber/fiber_test.cc src/system/fiber/fiber_test.cc
src/system/thread_pool_test.cc src/system/thread_pool_test.cc

View File

@ -10,22 +10,52 @@
#include "sled/system/location.h" #include "sled/system/location.h"
#include <assert.h> #include <assert.h>
#include <fmt/chrono.h> // clang-format off
#include <fmt/color.h>
#include <fmt/compile.h>
#include <fmt/format.h>
#include <fmt/ostream.h>// support fmt base ostream #include <fmt/ostream.h>// support fmt base ostream
#include <fmt/printf.h>
#include <fmt/ranges.h>
#include <fmt/std.h>
#include <fmt/xchar.h>
template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0> // clang-format on
auto // #include <fmt/chrono.h>
format_as(const T &value) -> int // #include <fmt/color.h>
{ // #include <fmt/compile.h>
return static_cast<int>(value); // #include <fmt/format.h>
} // #include <fmt/printf.h>
// #include <fmt/ranges.h>
// #include <fmt/std.h>
// #include <fmt/xchar.h>
// #include <sstream>
namespace {
template<typename S, typename T>
class is_streamable {
template<typename SS, typename TT>
static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
template<typename, typename>
static auto test(...) -> std::false_type;
public:
static const bool value = decltype(test<S, T>(0))::value;
};
struct OtherType {};
}// namespace
// template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type *>
// auto
// format_as(T const &value) -> decltype(fmt::underlying(value))
// {
// return fmt::underlying(value);
// }
// template<typename T, typename std::enable_if<is_streamable<std::ostream, T>::value, int>::type *>
// auto
// format_as(T const &value) -> std::string
// {
// std::stringstream ss;
// ss << value;
// return ss.str();
// }
namespace sled { namespace sled {
enum class LogLevel { enum class LogLevel {

View File

@ -98,7 +98,7 @@ private:
std::unique_ptr<Impl> impl_; std::unique_ptr<Impl> impl_;
}; };
std::ostream &operator<<(std::ostream &os, Status const &status); std::ostream &operator<<(std::ostream &os, sled::Status const &status);
}// namespace sled }// namespace sled

View File

@ -9,6 +9,10 @@ TEST(format, enum)
kThree = 3, kThree = 3,
}; };
std::stringstream ss;
ss << kOne;
EXPECT_EQ(ss.str(), "1");
EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "123"); 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"); 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_); return (a.ok() && b.ok()) || (a.impl_ && b.impl_ && *a.impl_ == *b.impl_);
} }
std::ostream & namespace internal {
operator<<(std::ostream &os, const Status &s) 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(); os << s.code() << ": " << s.message();
auto const &e = s.error_info(); auto const &e = s.error_info();
if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { return os; } 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 << "}"; 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 }// namespace sled

View File

@ -1,4 +1,5 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/status_or.h> #include <sled/status_or.h>
TEST(StatusOr, TestStatusOr) 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), "");
}