add test for compile-time formatting

This commit is contained in:
Alexey Ochapov 2020-11-14 20:03:11 +03:00
parent 6d14f78115
commit 4c0f1e8256
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
2 changed files with 75 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,66 @@
#include <array>
#include <string>
#include "fmt/compile.h"
#include "fmt/core.h"
#include "gmock.h"
#include "gtest-extra.h"
template <size_t max_string_length> struct constexpr_buffer_helper {
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 {
return (std::string_view(rhs).compare(buffer.data()) == 0);
}
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(), 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");
}