From 4c0f1e8256d68924cc398bee5544744e0ed0b02f Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Sat, 14 Nov 2020 20:03:11 +0300 Subject: [PATCH] add test for compile-time formatting --- test/CMakeLists.txt | 9 ++++ test/compile-time-formatting-test.cc | 66 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 test/compile-time-formatting-test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7ae5659d..25012959 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,6 +146,15 @@ else () target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1) endif () +if (${CMAKE_CXX_STANDARD} GREATER_EQUAL "20") + 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}") if (FMT_PEDANTIC) diff --git a/test/compile-time-formatting-test.cc b/test/compile-time-formatting-test.cc new file mode 100644 index 00000000..aeac94e0 --- /dev/null +++ b/test/compile-time-formatting-test.cc @@ -0,0 +1,66 @@ +#include +#include + +#include "fmt/compile.h" +#include "fmt/core.h" +#include "gmock.h" +#include "gtest-extra.h" + +template struct constexpr_buffer_helper { + template + constexpr constexpr_buffer_helper& modify(Func func) { + func(buffer); + return *this; + } + + template constexpr bool operator==(const T& rhs) const noexcept { + return (std::string_view(rhs).compare(buffer.data()) == 0); + } + + std::array buffer{}; +}; + +TEST(CompileTimeFormattingTest, OneInteger) { + constexpr auto result42 = + constexpr_buffer_helper<3>{}.modify([](auto& buffer) { + fmt::format_to(buffer.data(), FMT_COMPILE("{}"), 42); + }); + EXPECT_EQ(result42, "42"); + constexpr auto result420 = + constexpr_buffer_helper<3>{}.modify([](auto& buffer) { + fmt::format_to(buffer.data(), FMT_COMPILE("{}"), 420); + }); + EXPECT_EQ(result420, "420"); +} + +TEST(CompileTimeFormattingTest, TwoIntegers) { + constexpr auto result = constexpr_buffer_helper<6>{}.modify([](auto& buffer) { + fmt::format_to(buffer.data(), FMT_COMPILE("{} {}"), 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(), FMT_COMPILE("{}"), "42"); + }); + EXPECT_EQ(result, "42"); +} + +TEST(CompileTimeFormattingTest, TwoStrings) { + constexpr auto result = + constexpr_buffer_helper<17>{}.modify([](auto& buffer) { + fmt::format_to(buffer.data(), FMT_COMPILE("{} 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(), FMT_COMPILE("{} is {}"), "The answer", + 42); + }); + EXPECT_EQ(result, "The answer is 42"); +}