Basics of formatting at compile-time based on compile-time API (#2019)
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#if __cplusplus >= 202002L
|
||||
# include <string_view>
|
||||
#endif
|
||||
|
||||
// Check that fmt/compile.h compiles with windows.h included before it.
|
||||
#ifdef _WIN32
|
||||
@@ -171,3 +174,69 @@ TEST(CompileTest, TextAndArg) {
|
||||
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if __cplusplus >= 202002L
|
||||
template <size_t max_string_length> struct test_string {
|
||||
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
|
||||
return (std::string_view(rhs).compare(buffer.data()) == 0);
|
||||
}
|
||||
|
||||
std::array<char, max_string_length> buffer{};
|
||||
};
|
||||
|
||||
template <size_t max_string_length, typename... Args>
|
||||
consteval auto test_format(auto format, const Args&... args) {
|
||||
test_string<max_string_length> string{};
|
||||
fmt::format_to(string.buffer.data(), format, args...);
|
||||
return string;
|
||||
}
|
||||
|
||||
TEST(CompileTimeFormattingTest, Bool) {
|
||||
{
|
||||
constexpr auto result = test_format<5>(FMT_COMPILE("{}"), true);
|
||||
EXPECT_EQ(result, "true");
|
||||
}
|
||||
{
|
||||
constexpr auto result = test_format<6>(FMT_COMPILE("{}"), false);
|
||||
EXPECT_EQ(result, "false");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CompileTimeFormattingTest, Integer) {
|
||||
{
|
||||
constexpr auto result = test_format<3>(FMT_COMPILE("{}"), 42);
|
||||
EXPECT_EQ(result, "42");
|
||||
}
|
||||
{
|
||||
constexpr auto result = test_format<4>(FMT_COMPILE("{}"), 420);
|
||||
EXPECT_EQ(result, "420");
|
||||
}
|
||||
{
|
||||
constexpr auto result = test_format<6>(FMT_COMPILE("{} {}"), 42, 42);
|
||||
EXPECT_EQ(result, "42 42");
|
||||
}
|
||||
{
|
||||
constexpr auto result =
|
||||
test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42});
|
||||
EXPECT_EQ(result, "42 42");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CompileTimeFormattingTest, String) {
|
||||
{
|
||||
constexpr auto result = test_format<3>(FMT_COMPILE("{}"), "42");
|
||||
EXPECT_EQ(result, "42");
|
||||
}
|
||||
{
|
||||
constexpr auto result =
|
||||
test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42");
|
||||
EXPECT_EQ(result, "The answer is 42");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CompileTimeFormattingTest, Combination) {
|
||||
constexpr auto result =
|
||||
test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer");
|
||||
EXPECT_EQ(result, "420, true, answer");
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user