2018-11-27 10:52:00 +00:00
|
|
|
// Formatting library for C++ - formatting library tests
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
2019-07-25 16:01:21 +00:00
|
|
|
// For the license information refer to format.h.
|
2018-11-27 10:52:00 +00:00
|
|
|
|
|
|
|
#include <string>
|
2020-10-11 15:30:14 +00:00
|
|
|
#include <type_traits>
|
2018-11-27 10:52:00 +00:00
|
|
|
|
2020-10-11 15:30:14 +00:00
|
|
|
// Check that fmt/compile.h compiles with windows.h included before it.
|
2018-11-27 10:52:00 +00:00
|
|
|
#ifdef _WIN32
|
2019-01-13 02:27:38 +00:00
|
|
|
# include <windows.h>
|
2018-11-27 10:52:00 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-16 05:07:36 +00:00
|
|
|
#include "fmt/chrono.h"
|
2019-07-25 16:01:21 +00:00
|
|
|
#include "fmt/compile.h"
|
2018-11-27 10:52:00 +00:00
|
|
|
#include "gmock.h"
|
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2021-03-06 16:35:36 +00:00
|
|
|
TEST(IteratorTest, TruncatingIterator) {
|
|
|
|
char* p = nullptr;
|
|
|
|
fmt::detail::truncating_iterator<char*> it(p, 3);
|
|
|
|
auto prev = it++;
|
|
|
|
EXPECT_EQ(prev.base(), p);
|
|
|
|
EXPECT_EQ(it.base(), p + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IteratorTest, TruncatingIteratorDefaultConstruct) {
|
2021-03-10 20:08:02 +00:00
|
|
|
static_assert(std::is_default_constructible<
|
|
|
|
fmt::detail::truncating_iterator<char*>>::value,
|
|
|
|
"");
|
|
|
|
|
2021-03-06 16:35:36 +00:00
|
|
|
fmt::detail::truncating_iterator<char*> it;
|
|
|
|
EXPECT_EQ(nullptr, it.base());
|
|
|
|
EXPECT_EQ(std::size_t{0}, it.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cpp_lib_ranges
|
|
|
|
TEST(IteratorTest, TruncatingIteratorOutputIterator) {
|
2021-03-10 20:08:02 +00:00
|
|
|
static_assert(
|
|
|
|
std::output_iterator<fmt::detail::truncating_iterator<char*>, char>);
|
2021-03-06 16:35:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(IteratorTest, TruncatingBackInserter) {
|
|
|
|
std::string buffer;
|
|
|
|
auto bi = std::back_inserter(buffer);
|
|
|
|
fmt::detail::truncating_iterator<decltype(bi)> it(bi, 2);
|
|
|
|
*it++ = '4';
|
|
|
|
*it++ = '2';
|
|
|
|
*it++ = '1';
|
|
|
|
EXPECT_EQ(buffer.size(), 2);
|
|
|
|
EXPECT_EQ(buffer, "42");
|
|
|
|
}
|
|
|
|
|
2018-11-27 10:52:00 +00:00
|
|
|
// compiletime_prepared_parts_type_provider is useful only with relaxed
|
|
|
|
// constexpr.
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
template <unsigned EXPECTED_PARTS_COUNT, typename Format>
|
|
|
|
void check_prepared_parts_type(Format format) {
|
2020-05-10 14:25:42 +00:00
|
|
|
typedef fmt::detail::compiled_format_base<decltype(format)> provider;
|
|
|
|
typedef fmt::detail::format_part<char>
|
2019-09-01 21:57:36 +00:00
|
|
|
expected_parts_type[EXPECTED_PARTS_COUNT];
|
|
|
|
static_assert(std::is_same<typename provider::parts_container,
|
|
|
|
expected_parts_type>::value,
|
|
|
|
"CompileTimePreparedPartsTypeProvider test failed");
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, CompileTimePreparedPartsTypeProvider) {
|
2018-11-27 10:52:00 +00:00
|
|
|
check_prepared_parts_type<1u>(FMT_STRING("text"));
|
|
|
|
check_prepared_parts_type<1u>(FMT_STRING("{}"));
|
|
|
|
check_prepared_parts_type<2u>(FMT_STRING("text{}"));
|
|
|
|
check_prepared_parts_type<2u>(FMT_STRING("{}text"));
|
|
|
|
check_prepared_parts_type<3u>(FMT_STRING("text{}text"));
|
|
|
|
check_prepared_parts_type<3u>(FMT_STRING("{:{}.{}} {:{}}"));
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
check_prepared_parts_type<3u>(FMT_STRING("{{{}}}")); // '{', 'argument', '}'
|
|
|
|
check_prepared_parts_type<2u>(FMT_STRING("text{{")); // 'text', '{'
|
|
|
|
check_prepared_parts_type<3u>(FMT_STRING("text{{ ")); // 'text', '{', ' '
|
|
|
|
check_prepared_parts_type<2u>(FMT_STRING("}}text")); // '}', text
|
|
|
|
check_prepared_parts_type<2u>(FMT_STRING("text}}text")); // 'text}', 'text'
|
2018-11-27 10:52:00 +00:00
|
|
|
check_prepared_parts_type<4u>(
|
2019-01-13 02:27:38 +00:00
|
|
|
FMT_STRING("text{{}}text")); // 'text', '{', '}', 'text'
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, PassStringLiteralFormat) {
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto prepared = fmt::detail::compile<int>("test {}");
|
2019-08-03 13:28:31 +00:00
|
|
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto wprepared = fmt::detail::compile<int>(L"test {}");
|
2019-08-03 13:28:31 +00:00
|
|
|
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
|
2018-11-27 10:52:00 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, FormatToArrayOfChars) {
|
2019-05-21 19:03:57 +00:00
|
|
|
char buffer[32] = {0};
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto prepared = fmt::detail::compile<int>("4{}");
|
2020-05-10 14:25:42 +00:00
|
|
|
fmt::format_to(fmt::detail::make_checked(buffer, 32), prepared, 2);
|
2019-05-21 19:03:57 +00:00
|
|
|
EXPECT_EQ(std::string("42"), buffer);
|
|
|
|
wchar_t wbuffer[32] = {0};
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto wprepared = fmt::detail::compile<int>(L"4{}");
|
2020-05-10 14:25:42 +00:00
|
|
|
fmt::format_to(fmt::detail::make_checked(wbuffer, 32), wprepared, 2);
|
2019-05-21 19:03:57 +00:00
|
|
|
EXPECT_EQ(std::wstring(L"42"), wbuffer);
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, FormatToIterator) {
|
2019-05-21 19:03:57 +00:00
|
|
|
std::string s(2, ' ');
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto prepared = fmt::detail::compile<int>("4{}");
|
2019-08-03 15:35:02 +00:00
|
|
|
fmt::format_to(s.begin(), prepared, 2);
|
2019-05-21 19:03:57 +00:00
|
|
|
EXPECT_EQ("42", s);
|
|
|
|
std::wstring ws(2, L' ');
|
2020-06-23 21:03:37 +00:00
|
|
|
const auto wprepared = fmt::detail::compile<int>(L"4{}");
|
2019-08-03 15:35:02 +00:00
|
|
|
fmt::format_to(ws.begin(), wprepared, 2);
|
2019-05-21 19:03:57 +00:00
|
|
|
EXPECT_EQ(L"42", ws);
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:38:41 +00:00
|
|
|
TEST(CompileTest, FormatToN) {
|
|
|
|
char buf[5];
|
2020-06-23 21:03:37 +00:00
|
|
|
auto f = fmt::detail::compile<int>("{:10}");
|
2019-08-25 13:38:41 +00:00
|
|
|
auto result = fmt::format_to_n(buf, 5, f, 42);
|
|
|
|
EXPECT_EQ(result.size, 10);
|
|
|
|
EXPECT_EQ(result.out, buf + 5);
|
|
|
|
EXPECT_EQ(fmt::string_view(buf, 5), " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTest, FormattedSize) {
|
2020-06-23 21:03:37 +00:00
|
|
|
auto f = fmt::detail::compile<int>("{:10}");
|
2019-08-25 13:38:41 +00:00
|
|
|
EXPECT_EQ(fmt::formatted_size(f, 42), 10);
|
2019-05-21 19:03:57 +00:00
|
|
|
}
|
2019-09-01 18:48:01 +00:00
|
|
|
|
2019-09-24 09:54:49 +00:00
|
|
|
TEST(CompileTest, MultipleTypes) {
|
2020-06-23 21:03:37 +00:00
|
|
|
auto f = fmt::detail::compile<int, int>("{} {}");
|
2019-09-24 09:54:49 +00:00
|
|
|
EXPECT_EQ(fmt::format(f, 42, 42), "42 42");
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:13:21 +00:00
|
|
|
struct test_formattable {};
|
2019-09-01 18:48:01 +00:00
|
|
|
|
2019-09-01 19:12:19 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2020-07-14 18:13:21 +00:00
|
|
|
template <> struct formatter<test_formattable> : formatter<const char*> {
|
2020-06-23 19:05:37 +00:00
|
|
|
template <typename FormatContext>
|
2020-07-14 18:13:21 +00:00
|
|
|
auto format(test_formattable, FormatContext& ctx) -> decltype(ctx.out()) {
|
2019-09-01 18:48:01 +00:00
|
|
|
return formatter<const char*>::format("foo", ctx);
|
|
|
|
}
|
|
|
|
};
|
2019-09-01 19:12:19 +00:00
|
|
|
FMT_END_NAMESPACE
|
2019-09-01 18:48:01 +00:00
|
|
|
|
|
|
|
TEST(CompileTest, FormatUserDefinedType) {
|
2020-07-14 18:13:21 +00:00
|
|
|
auto f = fmt::detail::compile<test_formattable>("{}");
|
|
|
|
EXPECT_EQ(fmt::format(f, test_formattable()), "foo");
|
2019-09-01 18:48:01 +00:00
|
|
|
}
|
2019-09-01 21:57:36 +00:00
|
|
|
|
|
|
|
TEST(CompileTest, EmptyFormatString) {
|
2020-06-23 21:03:37 +00:00
|
|
|
auto f = fmt::detail::compile<>("");
|
2019-09-01 21:57:36 +00:00
|
|
|
EXPECT_EQ(fmt::format(f), "");
|
|
|
|
}
|
2020-06-12 20:24:49 +00:00
|
|
|
|
|
|
|
#ifdef __cpp_if_constexpr
|
2020-06-14 14:16:50 +00:00
|
|
|
TEST(CompileTest, FormatDefault) {
|
2020-06-12 20:24:49 +00:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42));
|
2020-06-14 14:16:50 +00:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42u));
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ll));
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), 42ull));
|
|
|
|
EXPECT_EQ("true", fmt::format(FMT_COMPILE("{}"), true));
|
|
|
|
EXPECT_EQ("x", fmt::format(FMT_COMPILE("{}"), 'x'));
|
|
|
|
EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2));
|
2020-06-12 20:24:49 +00:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
|
2020-06-14 14:16:50 +00:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
2020-07-14 18:13:21 +00:00
|
|
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
|
2020-12-27 15:23:28 +00:00
|
|
|
# ifdef __cpp_lib_byte
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42}));
|
|
|
|
# endif
|
2020-06-23 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:35:33 +00:00
|
|
|
TEST(CompileTest, FormatWideString) {
|
|
|
|
EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{}"), 42));
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:37 +00:00
|
|
|
TEST(CompileTest, FormatSpecs) {
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{:x}"), 0x42));
|
2021-02-16 05:07:36 +00:00
|
|
|
EXPECT_EQ("1.2 ms ",
|
|
|
|
fmt::format(FMT_COMPILE("{:7.1%Q %q}"),
|
|
|
|
std::chrono::duration<double, std::milli>(1.234)));
|
2020-06-12 20:24:49 +00:00
|
|
|
}
|
2020-06-14 18:04:41 +00:00
|
|
|
|
2021-02-07 20:43:52 +00:00
|
|
|
TEST(CompileTest, DynamicFormatSpecs) {
|
2021-02-16 05:07:36 +00:00
|
|
|
EXPECT_EQ("foo ", fmt::format(FMT_COMPILE("{:{}}"), "foo", 5));
|
|
|
|
EXPECT_EQ(" 3.14", fmt::format(FMT_COMPILE("{:{}.{}f}"), 3.141592, 6, 2));
|
|
|
|
EXPECT_EQ(
|
|
|
|
"=1.234ms=",
|
|
|
|
fmt::format(FMT_COMPILE("{:=^{}.{}}"),
|
|
|
|
std::chrono::duration<double, std::milli>(1.234), 9, 3));
|
2021-02-07 20:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTest, ManualOrdering) {
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{0}"), 42));
|
|
|
|
EXPECT_EQ(" -42", fmt::format(FMT_COMPILE("{0:4}"), -42));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {1}"), 41, 43));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{1} {0}"), 43, 41));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{0} {2}"), 41, 42, 43));
|
|
|
|
EXPECT_EQ(" 41 43", fmt::format(FMT_COMPILE("{1:{2}} {0:4}"), 43, 41, 4));
|
2021-02-16 05:07:36 +00:00
|
|
|
EXPECT_EQ("42 1.2 ms ",
|
|
|
|
fmt::format(FMT_COMPILE("{0} {1:7.1%Q %q}"), 42,
|
|
|
|
std::chrono::duration<double, std::milli>(1.234)));
|
2021-02-07 20:43:52 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
"true 42 42 foo 0x1234 foo",
|
|
|
|
fmt::format(FMT_COMPILE("{0} {1} {2} {3} {4} {5}"), true, 42, 42.0f,
|
|
|
|
"foo", reinterpret_cast<void*>(0x1234), test_formattable()));
|
|
|
|
EXPECT_EQ(L"42", fmt::format(FMT_COMPILE(L"{0}"), 42));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTest, Named) {
|
2021-02-16 04:04:52 +00:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), fmt::arg("arg", 42)));
|
|
|
|
EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{} {}"), fmt::arg("arg", 41),
|
|
|
|
fmt::arg("arg", 43)));
|
|
|
|
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
2021-02-16 04:16:57 +00:00
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foofoo", fmt::format(FMT_COMPILE("{a0}{}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{0}{a1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar", fmt::format(FMT_COMPILE("{a0}{1}"), fmt::arg("a0", "foo"),
|
|
|
|
fmt::arg("a1", "bar")));
|
2021-02-16 04:04:52 +00:00
|
|
|
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{}{a1}"), "foo", fmt::arg("a1", "bar")));
|
|
|
|
EXPECT_EQ("foobar",
|
|
|
|
fmt::format(FMT_COMPILE("{a0}{a1}"), fmt::arg("a1", "bar"),
|
|
|
|
fmt::arg("a2", "baz"), fmt::arg("a0", "foo")));
|
|
|
|
EXPECT_EQ(" bar foo ",
|
|
|
|
fmt::format(FMT_COMPILE(" {foo} {bar} "), fmt::arg("foo", "bar"),
|
|
|
|
fmt::arg("bar", "foo")));
|
2021-02-07 20:43:52 +00:00
|
|
|
|
|
|
|
EXPECT_THROW(fmt::format(FMT_COMPILE("{invalid}"), fmt::arg("valid", 42)),
|
|
|
|
fmt::format_error);
|
2020-08-10 16:13:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:50:02 +00:00
|
|
|
TEST(CompileTest, FormatTo) {
|
2020-09-20 13:59:01 +00:00
|
|
|
char buf[8];
|
|
|
|
auto end = fmt::format_to(buf, FMT_COMPILE("{}"), 42);
|
|
|
|
*end = '\0';
|
|
|
|
EXPECT_STREQ("42", buf);
|
|
|
|
end = fmt::format_to(buf, FMT_COMPILE("{:x}"), 42);
|
|
|
|
*end = '\0';
|
|
|
|
EXPECT_STREQ("2a", buf);
|
2020-06-20 15:50:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:28:06 +00:00
|
|
|
TEST(CompileTest, FormatToNWithCompileMacro) {
|
2020-09-20 13:59:01 +00:00
|
|
|
constexpr auto buffer_size = 8;
|
|
|
|
char buffer[buffer_size];
|
|
|
|
auto res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{}"), 42);
|
|
|
|
*res.out = '\0';
|
|
|
|
EXPECT_STREQ("42", buffer);
|
|
|
|
res = fmt::format_to_n(buffer, buffer_size, FMT_COMPILE("{:x}"), 42);
|
|
|
|
*res.out = '\0';
|
|
|
|
EXPECT_STREQ("2a", buffer);
|
2020-09-15 14:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 14:44:36 +00:00
|
|
|
TEST(CompileTest, FormattedSizeWithCompileMacro) {
|
|
|
|
EXPECT_EQ(2, fmt::formatted_size(FMT_COMPILE("{0}"), 42));
|
|
|
|
EXPECT_EQ(5, fmt::formatted_size(FMT_COMPILE("{0:<4.2f}"), 42.0));
|
|
|
|
}
|
|
|
|
|
2020-06-14 18:04:41 +00:00
|
|
|
TEST(CompileTest, TextAndArg) {
|
|
|
|
EXPECT_EQ(">>>42<<<", fmt::format(FMT_COMPILE(">>>{}<<<"), 42));
|
2020-07-11 15:35:26 +00:00
|
|
|
EXPECT_EQ("42!", fmt::format(FMT_COMPILE("{}!"), 42));
|
2020-06-14 18:04:41 +00:00
|
|
|
}
|
2020-12-02 15:14:57 +00:00
|
|
|
|
2021-03-04 15:20:57 +00:00
|
|
|
TEST(CompileTest, UnknownFormatFallback) {
|
|
|
|
EXPECT_EQ(" 42 ",
|
|
|
|
fmt::format(FMT_COMPILE("{name:^4}"), fmt::arg("name", 42)));
|
|
|
|
|
|
|
|
std::vector<char> v;
|
|
|
|
fmt::format_to(std::back_inserter(v), FMT_COMPILE("{name:^4}"),
|
|
|
|
fmt::arg("name", 42));
|
|
|
|
EXPECT_EQ(" 42 ", fmt::string_view(v.data(), v.size()));
|
|
|
|
|
|
|
|
char buffer[4];
|
|
|
|
auto result = fmt::format_to_n(buffer, 4, FMT_COMPILE("{name:^5}"),
|
|
|
|
fmt::arg("name", 42));
|
|
|
|
EXPECT_EQ(5u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 4, result.out);
|
|
|
|
EXPECT_EQ(" 42 ", fmt::string_view(buffer, 4));
|
|
|
|
}
|
|
|
|
|
2021-02-07 20:43:52 +00:00
|
|
|
TEST(CompileTest, Empty) { EXPECT_EQ("", fmt::format(FMT_COMPILE(""))); }
|
2021-03-19 13:43:38 +00:00
|
|
|
|
|
|
|
struct to_stringable {
|
|
|
|
friend fmt::string_view to_string_view(to_stringable) { return {}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
FMT_BEGIN_NAMESPACE
|
|
|
|
template <> struct formatter<to_stringable> {
|
|
|
|
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
|
|
|
|
return ctx.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const to_stringable&, FormatContext& ctx) -> decltype(ctx.out()) {
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FMT_END_NAMESPACE
|
|
|
|
|
|
|
|
TEST(CompileTest, ToStringAndFormatter) {
|
|
|
|
fmt::format(FMT_COMPILE("{}"), to_stringable());
|
|
|
|
}
|
2020-06-12 20:24:49 +00:00
|
|
|
#endif
|
2020-11-29 16:59:11 +00:00
|
|
|
|
2020-12-07 23:53:11 +00:00
|
|
|
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
|
|
|
|
TEST(CompileTest, CompileFormatStringLiteral) {
|
|
|
|
using namespace fmt::literals;
|
|
|
|
EXPECT_EQ("", fmt::format(""_cf));
|
|
|
|
EXPECT_EQ("42", fmt::format("{}"_cf, 42));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-25 14:40:03 +00:00
|
|
|
#if __cplusplus >= 202002L || \
|
|
|
|
(__cplusplus >= 201709L && FMT_GCC_VERSION >= 1002)
|
|
|
|
template <size_t max_string_length, typename Char = char> struct test_string {
|
2020-11-29 16:59:11 +00:00
|
|
|
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
|
2020-12-25 14:40:03 +00:00
|
|
|
return fmt::basic_string_view<Char>(rhs).compare(buffer.data()) == 0;
|
2020-11-29 16:59:11 +00:00
|
|
|
}
|
2020-12-25 14:40:03 +00:00
|
|
|
std::array<Char, max_string_length> buffer{};
|
2020-11-29 16:59:11 +00:00
|
|
|
};
|
|
|
|
|
2020-12-25 14:40:03 +00:00
|
|
|
template <size_t max_string_length, typename Char = char, typename... Args>
|
2020-11-29 16:59:11 +00:00
|
|
|
consteval auto test_format(auto format, const Args&... args) {
|
2020-12-25 14:40:03 +00:00
|
|
|
test_string<max_string_length, Char> string{};
|
2020-11-29 16:59:11 +00:00
|
|
|
fmt::format_to(string.buffer.data(), format, args...);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, Bool) {
|
2020-12-03 22:21:23 +00:00
|
|
|
EXPECT_EQ("true", test_format<5>(FMT_COMPILE("{}"), true));
|
|
|
|
EXPECT_EQ("false", test_format<6>(FMT_COMPILE("{}"), false));
|
2020-12-25 14:40:03 +00:00
|
|
|
EXPECT_EQ("true ", test_format<6>(FMT_COMPILE("{:5}"), true));
|
|
|
|
EXPECT_EQ("1", test_format<2>(FMT_COMPILE("{:d}"), true));
|
2020-11-29 16:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, Integer) {
|
2020-12-03 22:21:23 +00:00
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), 42));
|
|
|
|
EXPECT_EQ("420", test_format<4>(FMT_COMPILE("{}"), 420));
|
|
|
|
EXPECT_EQ("42 42", test_format<6>(FMT_COMPILE("{} {}"), 42, 42));
|
|
|
|
EXPECT_EQ("42 42",
|
|
|
|
test_format<6>(FMT_COMPILE("{} {}"), uint32_t{42}, uint64_t{42}));
|
2020-12-25 14:40:03 +00:00
|
|
|
|
|
|
|
EXPECT_EQ("+42", test_format<4>(FMT_COMPILE("{:+}"), 42));
|
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{:-}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<4>(FMT_COMPILE("{: }"), 42));
|
|
|
|
|
|
|
|
EXPECT_EQ("-0042", test_format<6>(FMT_COMPILE("{:05}"), -42));
|
|
|
|
|
|
|
|
EXPECT_EQ("101010", test_format<7>(FMT_COMPILE("{:b}"), 42));
|
|
|
|
EXPECT_EQ("0b101010", test_format<9>(FMT_COMPILE("{:#b}"), 42));
|
|
|
|
EXPECT_EQ("0B101010", test_format<9>(FMT_COMPILE("{:#B}"), 42));
|
|
|
|
EXPECT_EQ("042", test_format<4>(FMT_COMPILE("{:#o}"), 042));
|
|
|
|
EXPECT_EQ("0x4a", test_format<5>(FMT_COMPILE("{:#x}"), 0x4a));
|
|
|
|
EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a));
|
|
|
|
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll));
|
|
|
|
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull));
|
|
|
|
|
|
|
|
EXPECT_EQ("42 ", test_format<5>(FMT_COMPILE("{:<4}"), 42));
|
|
|
|
EXPECT_EQ(" 42", test_format<5>(FMT_COMPILE("{:>4}"), 42));
|
|
|
|
EXPECT_EQ(" 42 ", test_format<5>(FMT_COMPILE("{:^4}"), 42));
|
|
|
|
EXPECT_EQ("**-42", test_format<6>(FMT_COMPILE("{:*>5}"), -42));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, Char) {
|
|
|
|
EXPECT_EQ("c", test_format<2>(FMT_COMPILE("{}"), 'c'));
|
|
|
|
|
|
|
|
EXPECT_EQ("c ", test_format<4>(FMT_COMPILE("{:3}"), 'c'));
|
|
|
|
EXPECT_EQ("99", test_format<3>(FMT_COMPILE("{:d}"), 'c'));
|
2020-11-29 16:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, String) {
|
2020-12-03 22:21:23 +00:00
|
|
|
EXPECT_EQ("42", test_format<3>(FMT_COMPILE("{}"), "42"));
|
|
|
|
EXPECT_EQ("The answer is 42",
|
|
|
|
test_format<17>(FMT_COMPILE("{} is {}"), "The answer", "42"));
|
2020-12-25 14:40:03 +00:00
|
|
|
|
|
|
|
EXPECT_EQ("abc**", test_format<6>(FMT_COMPILE("{:*<5}"), "abc"));
|
2021-02-13 17:24:39 +00:00
|
|
|
EXPECT_EQ("**🤡**", test_format<9>(FMT_COMPILE("{:*^6}"), "🤡"));
|
2020-11-29 16:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, Combination) {
|
2020-12-03 22:21:23 +00:00
|
|
|
EXPECT_EQ("420, true, answer",
|
|
|
|
test_format<18>(FMT_COMPILE("{}, {}, {}"), 420, true, "answer"));
|
2020-12-25 14:40:03 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(" -42", test_format<5>(FMT_COMPILE("{:{}}"), -42, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CompileTimeFormattingTest, MultiByteFill) {
|
|
|
|
EXPECT_EQ("жж42", test_format<8>(FMT_COMPILE("{:ж>4}"), 42));
|
2020-11-29 16:59:11 +00:00
|
|
|
}
|
|
|
|
#endif
|