Skip to content

Commit

Permalink
Add cct::format
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 25, 2024
1 parent 53d4250 commit f0b6d74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/objects/test/exchangename_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <gtest/gtest.h>

#include "cct_format.hpp"
#include "cct_invalid_argument_exception.hpp"
#include "cct_log.hpp"

namespace cct {
TEST(ExchangeName, NoKeyName) {
Expand Down Expand Up @@ -67,20 +67,20 @@ TEST(ExchangeName, Equality) {

TEST(ExchangeName, FormatWithoutKey) {
ExchangeName en("kraken");
EXPECT_EQ(fmt::format("{}", en), "kraken");
EXPECT_EQ(fmt::format("{:e}", en), "kraken");
EXPECT_EQ(fmt::format("{:n}", en), "kraken");
EXPECT_EQ(fmt::format("{:k}", en), "");
EXPECT_EQ(fmt::format("{:ek}", en), "kraken");
EXPECT_EQ(format("{}", en), "kraken");
EXPECT_EQ(format("{:e}", en), "kraken");
EXPECT_EQ(format("{:n}", en), "kraken");
EXPECT_EQ(format("{:k}", en), "");
EXPECT_EQ(format("{:ek}", en), "kraken");
}

TEST(ExchangeName, FormatWithKey) {
ExchangeName en("binance_key");
EXPECT_EQ(fmt::format("{}", en), "binance_key");
EXPECT_EQ(fmt::format("{:e}", en), "binance");
EXPECT_EQ(fmt::format("{:n}", en), "binance");
EXPECT_EQ(fmt::format("{:k}", en), "key");
EXPECT_EQ(fmt::format("{:ek}", en), "binance_key");
EXPECT_EQ(format("{}", en), "binance_key");
EXPECT_EQ(format("{:e}", en), "binance");
EXPECT_EQ(format("{:n}", en), "binance");
EXPECT_EQ(format("{:k}", en), "key");
EXPECT_EQ(format("{:ek}", en), "binance_key");
}

} // namespace cct
10 changes: 9 additions & 1 deletion src/tech/include/cct_format.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#ifdef CCT_DISABLE_SPDLOG
#include <string>
#include <string_view>
#else
#include <spdlog/spdlog.h>
Expand All @@ -17,10 +18,17 @@ template <typename OutputIt, typename... T>
auto format_to(OutputIt out, format_string<T...>, T&&...) -> OutputIt {
return out;
}

template <typename... T>
auto format(format_string<T...> fmt, [[maybe_unused]] T&&... args) -> std::string {
return std::string{fmt};
}

#else
template <typename... Args>
using format_string = spdlog::format_string_t<Args...>;
using fmt::format;
using fmt::format_to;
#endif

} // namespace cct
} // namespace cct

0 comments on commit f0b6d74

Please sign in to comment.