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
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
#include <stdint.h>
|
2018-11-27 10:52:00 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cfloat>
|
|
|
|
#include <climits>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2019-07-25 16:01:21 +00:00
|
|
|
// Check if 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
|
|
|
|
|
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 "mock-allocator.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#undef ERROR
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
|
|
|
using testing::Return;
|
|
|
|
using testing::StrictMock;
|
|
|
|
|
|
|
|
// 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) {
|
2019-09-01 21:57:36 +00:00
|
|
|
typedef fmt::internal::compiled_format_base<decltype(format)> provider;
|
|
|
|
typedef fmt::internal::format_part<char>
|
|
|
|
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) {
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto prepared = fmt::compile<int>("test {}");
|
2019-08-03 13:28:31 +00:00
|
|
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto wprepared = fmt::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
|
|
|
}
|
|
|
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, PassCompileString) {
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
|
2019-08-03 13:28:31 +00:00
|
|
|
EXPECT_EQ("test 42", fmt::format(prepared, 42));
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto wprepared = fmt::compile<int>(FMT_STRING(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
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-25 13:22:13 +00:00
|
|
|
TEST(CompileTest, FormatToArrayOfChars) {
|
2019-05-21 19:03:57 +00:00
|
|
|
char buffer[32] = {0};
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
2019-08-11 16:27:59 +00:00
|
|
|
fmt::format_to(fmt::internal::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};
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
2019-08-11 16:27:59 +00:00
|
|
|
fmt::format_to(fmt::internal::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, ' ');
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto prepared = fmt::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' ');
|
2019-07-25 16:01:21 +00:00
|
|
|
const auto wprepared = fmt::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];
|
|
|
|
auto f = fmt::compile<int>("{:10}");
|
|
|
|
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) {
|
|
|
|
auto f = fmt::compile<int>("{:10}");
|
|
|
|
EXPECT_EQ(fmt::formatted_size(f, 42), 10);
|
2019-05-21 19:03:57 +00:00
|
|
|
}
|
2019-09-01 18:48:01 +00:00
|
|
|
|
|
|
|
struct formattable {};
|
|
|
|
|
2019-09-01 19:12:19 +00:00
|
|
|
FMT_BEGIN_NAMESPACE
|
2019-09-01 21:57:36 +00:00
|
|
|
template <> struct formatter<formattable> : formatter<const char*> {
|
2019-09-01 18:48:01 +00:00
|
|
|
auto format(formattable, format_context& ctx) -> decltype(ctx.out()) {
|
|
|
|
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) {
|
|
|
|
auto f = fmt::compile<formattable>("{}");
|
|
|
|
EXPECT_EQ(fmt::format(f, formattable()), "foo");
|
|
|
|
}
|
2019-09-01 21:57:36 +00:00
|
|
|
|
|
|
|
TEST(CompileTest, EmptyFormatString) {
|
|
|
|
auto f = fmt::compile<>("");
|
|
|
|
EXPECT_EQ(fmt::format(f), "");
|
|
|
|
}
|