enable compile time formatting test based on special option

This commit is contained in:
Alexey Ochapov 2020-11-08 07:49:51 +03:00
parent 4b3dfc6e67
commit 3f74d368e8
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
2 changed files with 45 additions and 5 deletions

View File

@ -146,11 +146,14 @@ else ()
target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1) target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1)
endif () endif ()
add_fmt_executable(compile-time-formatting-test if (FMT_ENABLE_COMPILE_TIME_FORMATTING)
compile-time-formatting-test.cc test-main.cc) add_fmt_executable(compile-time-formatting-test
target_link_libraries(compile-time-formatting-test gmock) compile-time-formatting-test.cc test-main.cc)
target_include_directories(compile-time-formatting-test SYSTEM PUBLIC gtest gmock) target_link_libraries(compile-time-formatting-test gmock)
target_link_libraries(compile-time-formatting-test fmt::fmt-header-only) target_include_directories(compile-time-formatting-test SYSTEM PUBLIC gtest gmock)
target_link_libraries(compile-time-formatting-test fmt::fmt-header-only)
add_test(NAME compile-time-formatting-test COMMAND compile-time-formatting-test)
endif ()
message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}") message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}")

View File

@ -19,3 +19,40 @@ template <size_t max_string_length> struct constexpr_buffer_helper {
std::array<char, max_string_length> buffer{}; std::array<char, max_string_length> buffer{};
}; };
TEST(CompileTimeFormattingTest, OneInteger) {
constexpr auto result42 = constexpr_buffer_helper<3>{}.modify(
[](auto& buffer) { fmt::format_to(buffer.data(), "{}", 42); });
EXPECT_EQ(result42, "42");
constexpr auto result420 = constexpr_buffer_helper<3>{}.modify(
[](auto& buffer) { fmt::format_to(buffer.data(), "{}", 420); });
EXPECT_EQ(result420, "420");
}
TEST(CompileTimeFormattingTest, TwoIntegers) {
constexpr auto result = constexpr_buffer_helper<6>{}.modify(
[](auto& buffer) { fmt::format_to(buffer.data(), "{} {}", 41, 43); });
EXPECT_EQ(result, "41 43");
}
TEST(CompileTimeFormattingTest, OneString) {
constexpr auto result = constexpr_buffer_helper<3>{}.modify(
[](auto& buffer) { fmt::format_to(buffer.data(), "{}", "42"); });
EXPECT_EQ(result, "42");
}
TEST(CompileTimeFormattingTest, TwoStrings) {
constexpr auto result =
constexpr_buffer_helper<17>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), "{} is {}", "The answer", "42");
});
EXPECT_EQ(result, "The answer is 42");
}
TEST(CompileTimeFormattingTest, StringAndInteger) {
constexpr auto result =
constexpr_buffer_helper<17>{}.modify([](auto& buffer) {
fmt::format_to(buffer.data(), "{} is {}", "The answer", 42);
});
EXPECT_EQ(result, "The answer is 42");
}