simplify tests

This commit is contained in:
Alexey Ochapov 2020-11-29 02:23:47 +03:00
parent 6b7665c329
commit af8bcb9fe5
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8

View File

@ -176,13 +176,7 @@ TEST(CompileTest, TextAndArg) {
#endif #endif
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
template <size_t max_string_length> struct constexpr_buffer_helper { template <size_t max_string_length> struct test_string {
template <typename Func>
constexpr constexpr_buffer_helper& modify(Func func) {
func(buffer);
return *this;
}
template <typename T> constexpr bool operator==(const T& rhs) const noexcept { template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
return (std::string_view(rhs).compare(buffer.data()) == 0); return (std::string_view(rhs).compare(buffer.data()) == 0);
} }
@ -190,69 +184,52 @@ template <size_t max_string_length> struct constexpr_buffer_helper {
std::array<char, max_string_length> buffer{}; 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) { TEST(CompileTimeFormattingTest, Bool) {
{ {
constexpr auto result = constexpr auto result = test_format<5>(FMT_COMPILE("{}"), true);
constexpr_buffer_helper<5>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{}"), true);
});
EXPECT_EQ(result, "true"); EXPECT_EQ(result, "true");
} }
{ {
constexpr auto result = constexpr auto result = test_format<6>(FMT_COMPILE("{}"), false);
constexpr_buffer_helper<6>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{}"), false);
});
EXPECT_EQ(result, "false"); EXPECT_EQ(result, "false");
} }
} }
TEST(CompileTimeFormattingTest, Integer) { TEST(CompileTimeFormattingTest, Integer) {
{ {
constexpr auto result = constexpr auto result = test_format<3>(FMT_COMPILE("{}"), 42);
constexpr_buffer_helper<3>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{}"), 42);
});
EXPECT_EQ(result, "42"); EXPECT_EQ(result, "42");
} }
{ {
constexpr auto result = constexpr auto result = test_format<4>(FMT_COMPILE("{}"), 420);
constexpr_buffer_helper<4>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{}"), 420);
});
EXPECT_EQ(result, "420"); EXPECT_EQ(result, "420");
} }
{ {
constexpr auto result = constexpr auto result = test_format<6>(FMT_COMPILE("{} {}"), 42, 42);
constexpr_buffer_helper<6>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{} {}"), 42, 42);
});
EXPECT_EQ(result, "42 42"); EXPECT_EQ(result, "42 42");
} }
{ {
constexpr auto result = constexpr auto result =
constexpr_buffer_helper<6>{}.modify([=](auto& buffer) { test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42});
fmt::format_to(buffer.data(), FMT_COMPILE("{} {}"), uint32_t{42},
uint64_t{42});
});
EXPECT_EQ(result, "42 42"); EXPECT_EQ(result, "42 42");
} }
} }
TEST(CompileTimeFormattingTest, String) { TEST(CompileTimeFormattingTest, String) {
{ {
constexpr auto result = constexpr auto result = test_format<3>(FMT_COMPILE("{}"), "42");
constexpr_buffer_helper<3>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), FMT_COMPILE("{}"), "42");
});
EXPECT_EQ(result, "42"); EXPECT_EQ(result, "42");
} }
{ {
constexpr auto result = constexpr auto result =
constexpr_buffer_helper<17>{}.modify([](auto& buffer) { test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42");
fmt::format_to(buffer.data(), FMT_COMPILE("{} is {}"), "The answer",
"42");
});
EXPECT_EQ(result, "The answer is 42"); EXPECT_EQ(result, "The answer is 42");
} }
} }
@ -260,10 +237,7 @@ TEST(CompileTimeFormattingTest, String) {
TEST(CompileTimeFormattingTest, Combination) { TEST(CompileTimeFormattingTest, Combination) {
{ {
constexpr auto result = constexpr auto result =
constexpr_buffer_helper<18>{}.modify([](auto& buffer) { test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer");
fmt::format_to(buffer.data(), FMT_COMPILE("{}, {}, {}"), 420, true,
"answer");
});
EXPECT_EQ(result, "420, true, answer"); EXPECT_EQ(result, "420, true, answer");
} }
} }