diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 35ca8d2d..9a3a3ada 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,11 +146,14 @@ else () target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1) endif () -add_fmt_executable(compile-time-formatting-test - compile-time-formatting-test.cc test-main.cc) -target_link_libraries(compile-time-formatting-test gmock) -target_include_directories(compile-time-formatting-test SYSTEM PUBLIC gtest gmock) -target_link_libraries(compile-time-formatting-test fmt::fmt-header-only) +if (FMT_ENABLE_COMPILE_TIME_FORMATTING) + add_fmt_executable(compile-time-formatting-test + compile-time-formatting-test.cc test-main.cc) + target_link_libraries(compile-time-formatting-test gmock) + 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}") diff --git a/test/compile-time-formatting-test.cc b/test/compile-time-formatting-test.cc index ebcc182a..1e648342 100644 --- a/test/compile-time-formatting-test.cc +++ b/test/compile-time-formatting-test.cc @@ -19,3 +19,40 @@ template struct constexpr_buffer_helper { std::array 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"); +}