|
|
|
|
@@ -3,7 +3,7 @@
|
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
|
// All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// For the license information refer to prepare.h.
|
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
@@ -16,12 +16,12 @@
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
// Check if fmt/prepare.h compiles with windows.h included before it.
|
|
|
|
|
// Check if fmt/compile.h compiles with windows.h included before it.
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "fmt/prepare.h"
|
|
|
|
|
#include "fmt/compile.h"
|
|
|
|
|
#include "gmock.h"
|
|
|
|
|
#include "gtest-extra.h"
|
|
|
|
|
#include "mock-allocator.h"
|
|
|
|
|
@@ -434,11 +434,11 @@ TEST(PrepareTest, CompileTimePreparedPartsTypeProvider) {
|
|
|
|
|
// Use the struct instead of a function to workaround GCC 4.4's 'sorry,
|
|
|
|
|
// unimplemented: mangling template_id_expr' issue.
|
|
|
|
|
template <typename... Args> struct copied_prepared_format_creator {
|
|
|
|
|
static decltype(fmt::prepare<Args...>(std::declval<std::string>())) make(
|
|
|
|
|
static decltype(fmt::compile<Args...>(std::declval<std::string>())) make(
|
|
|
|
|
std::string format_str) {
|
|
|
|
|
auto prepared_format = fmt::prepare<Args...>(std::move(format_str));
|
|
|
|
|
auto prepared_format = fmt::compile<Args...>(std::move(format_str));
|
|
|
|
|
auto copied_prepared_format = prepared_format;
|
|
|
|
|
prepared_format = fmt::prepare<Args...>("");
|
|
|
|
|
prepared_format = fmt::compile<Args...>("");
|
|
|
|
|
|
|
|
|
|
return copied_prepared_format;
|
|
|
|
|
}
|
|
|
|
|
@@ -478,9 +478,9 @@ TEST(PrepareTest, CopyPreparedFormat_InternalStringViewsAreNotInvalidated) {
|
|
|
|
|
TEST(PrepareTest, ReusedPreparedFormatType) {
|
|
|
|
|
typedef fmt::prepared_format<std::string, int>::type prepared_format;
|
|
|
|
|
|
|
|
|
|
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}.");
|
|
|
|
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
|
|
|
|
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
|
|
|
|
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -491,9 +491,9 @@ TEST(PrepareTest, UserProvidedPartsContainerUnderlyingContainer) {
|
|
|
|
|
typedef fmt::basic_prepared_format<std::string, parts_container, std::string,
|
|
|
|
|
int>::type prepared_format;
|
|
|
|
|
|
|
|
|
|
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}.");
|
|
|
|
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
|
|
|
|
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
|
|
|
|
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -535,67 +535,67 @@ TEST(PrepareTest, UserProvidedPartsContainer) {
|
|
|
|
|
typedef fmt::basic_prepared_format<std::string, custom_parts_container,
|
|
|
|
|
std::string, int>::type prepared_format;
|
|
|
|
|
|
|
|
|
|
prepared_format prepared = fmt::prepare<prepared_format>("The {} is {}.");
|
|
|
|
|
prepared_format prepared = fmt::compile<prepared_format>("The {} is {}.");
|
|
|
|
|
EXPECT_EQ("The answer is 42.", prepared.format("answer", 42));
|
|
|
|
|
prepared = fmt::prepare<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
prepared = fmt::compile<prepared_format>("40 {} 2 = {}");
|
|
|
|
|
EXPECT_EQ("40 + 2 = 42", prepared.format("+", 42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassConstCharPointerFormat) {
|
|
|
|
|
const char* c_format = "test {}";
|
|
|
|
|
const auto prepared = fmt::prepare<int>(c_format);
|
|
|
|
|
const auto prepared = fmt::compile<int>(c_format);
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const wchar_t* wc_format = L"test {}";
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(wc_format);
|
|
|
|
|
const auto wprepared = fmt::compile<int>(wc_format);
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassCharArrayFormat) {
|
|
|
|
|
char c_format[] = "test {}";
|
|
|
|
|
const auto prepared = fmt::prepare<int>(c_format);
|
|
|
|
|
const auto prepared = fmt::compile<int>(c_format);
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
wchar_t wc_format[] = L"test {}";
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(wc_format);
|
|
|
|
|
const auto wprepared = fmt::compile<int>(wc_format);
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassConstCharArrayFormat) {
|
|
|
|
|
const char c_format[] = "test {}";
|
|
|
|
|
const auto prepared = fmt::prepare<int>(c_format);
|
|
|
|
|
const auto prepared = fmt::compile<int>(c_format);
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const wchar_t wc_format[] = L"test {}";
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(wc_format);
|
|
|
|
|
const auto wprepared = fmt::compile<int>(wc_format);
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassStringLiteralFormat) {
|
|
|
|
|
const auto prepared = fmt::prepare<int>("test {}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("test {}");
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"test {}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"test {}");
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassStringViewFormat) {
|
|
|
|
|
const auto prepared =
|
|
|
|
|
fmt::prepare<int>(fmt::basic_string_view<char>("test {}"));
|
|
|
|
|
fmt::compile<int>(fmt::basic_string_view<char>("test {}"));
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const auto wprepared =
|
|
|
|
|
fmt::prepare<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
|
|
|
|
|
fmt::compile<int>(fmt::basic_string_view<wchar_t>(L"test {}"));
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, PassBasicStringFormat) {
|
|
|
|
|
const auto prepared = fmt::prepare<int>(std::string("test {}"));
|
|
|
|
|
const auto prepared = fmt::compile<int>(std::string("test {}"));
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(std::wstring(L"test {}"));
|
|
|
|
|
const auto wprepared = fmt::compile<int>(std::wstring(L"test {}"));
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_CONSTEXPR
|
|
|
|
|
TEST(PrepareTest, PassCompileString) {
|
|
|
|
|
const auto prepared = fmt::prepare<int>(FMT_STRING("test {}"));
|
|
|
|
|
const auto prepared = fmt::compile<int>(FMT_STRING("test {}"));
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(FMT_STRING(L"test {}"));
|
|
|
|
|
const auto wprepared = fmt::compile<int>(FMT_STRING(L"test {}"));
|
|
|
|
|
EXPECT_EQ(L"test 42", wprepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
@@ -634,61 +634,61 @@ template <typename T> struct user_allocator {
|
|
|
|
|
TEST(PrepareTest, PassUserTypeFormat) {
|
|
|
|
|
typedef std::basic_string<char, std::char_traits<char>, user_allocator<char>>
|
|
|
|
|
user_format;
|
|
|
|
|
const auto prepared = fmt::prepare<int>(user_format("test {}"));
|
|
|
|
|
const auto prepared = fmt::compile<int>(user_format("test {}"));
|
|
|
|
|
EXPECT_EQ("test 42", prepared.format(42));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, FormatToArrayOfChars) {
|
|
|
|
|
char buffer[32] = {0};
|
|
|
|
|
const auto prepared = fmt::prepare<int>("4{}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
|
|
|
|
prepared.format_to(buffer, 2);
|
|
|
|
|
EXPECT_EQ(std::string("42"), buffer);
|
|
|
|
|
wchar_t wbuffer[32] = {0};
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"4{}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
|
|
|
|
wprepared.format_to(wbuffer, 2);
|
|
|
|
|
EXPECT_EQ(std::wstring(L"42"), wbuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, FormatToIterator) {
|
|
|
|
|
std::string s(2, ' ');
|
|
|
|
|
const auto prepared = fmt::prepare<int>("4{}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
|
|
|
|
prepared.format_to(s.begin(), 2);
|
|
|
|
|
EXPECT_EQ("42", s);
|
|
|
|
|
std::wstring ws(2, L' ');
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"4{}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
|
|
|
|
wprepared.format_to(ws.begin(), 2);
|
|
|
|
|
EXPECT_EQ(L"42", ws);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, FormatToBackInserter) {
|
|
|
|
|
std::string s;
|
|
|
|
|
const auto prepared = fmt::prepare<int>("4{}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
|
|
|
|
prepared.format_to(std::back_inserter(s), 2);
|
|
|
|
|
EXPECT_EQ("42", s);
|
|
|
|
|
std::wstring ws;
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"4{}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
|
|
|
|
wprepared.format_to(std::back_inserter(ws), 2);
|
|
|
|
|
EXPECT_EQ(L"42", ws);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, FormatToBasicMemoryBuffer) {
|
|
|
|
|
fmt::basic_memory_buffer<char, 100> buffer;
|
|
|
|
|
const auto prepared = fmt::prepare<int>("4{}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
|
|
|
|
prepared.format_to(buffer, 2);
|
|
|
|
|
EXPECT_EQ("42", to_string(buffer));
|
|
|
|
|
fmt::basic_memory_buffer<wchar_t, 100> wbuffer;
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"4{}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
|
|
|
|
wprepared.format_to(wbuffer, 2);
|
|
|
|
|
EXPECT_EQ(L"42", to_string(wbuffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PrepareTest, FormatToMemoryBuffer) {
|
|
|
|
|
fmt::memory_buffer buffer;
|
|
|
|
|
const auto prepared = fmt::prepare<int>("4{}");
|
|
|
|
|
const auto prepared = fmt::compile<int>("4{}");
|
|
|
|
|
prepared.format_to(buffer, 2);
|
|
|
|
|
EXPECT_EQ("42", to_string(buffer));
|
|
|
|
|
fmt::wmemory_buffer wbuffer;
|
|
|
|
|
const auto wprepared = fmt::prepare<int>(L"4{}");
|
|
|
|
|
const auto wprepared = fmt::compile<int>(L"4{}");
|
|
|
|
|
wprepared.format_to(wbuffer, 2);
|
|
|
|
|
EXPECT_EQ(L"42", to_string(wbuffer));
|
|
|
|
|
}
|