2018-03-04 17:16:51 +00:00
|
|
|
// Formatting library for C++ - std::ostream support tests
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2018-09-08 16:55:41 +00:00
|
|
|
#define FMT_STRING_ALIAS 1
|
2016-08-03 15:52:05 +00:00
|
|
|
#include "fmt/ostream.h"
|
2016-05-06 14:37:20 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
2018-06-06 13:57:59 +00:00
|
|
|
#include "gmock.h"
|
2016-05-06 14:37:20 +00:00
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using fmt::format;
|
2016-08-25 15:38:07 +00:00
|
|
|
using fmt::format_error;
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
static std::ostream& operator<<(std::ostream& os, const Date& d) {
|
2016-05-06 14:37:20 +00:00
|
|
|
os << d.year() << '-' << d.month() << '-' << d.day();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
static std::wostream& operator<<(std::wostream& os, const Date& d) {
|
2016-05-06 14:37:20 +00:00
|
|
|
os << d.year() << L'-' << d.month() << L'-' << d.day();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TestEnum {};
|
2019-01-13 02:27:38 +00:00
|
|
|
static std::ostream& operator<<(std::ostream& os, TestEnum) {
|
2016-05-06 14:37:20 +00:00
|
|
|
return os << "TestEnum";
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
static std::wostream& operator<<(std::wostream& os, TestEnum) {
|
2018-04-26 18:32:14 +00:00
|
|
|
return os << L"TestEnum";
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
enum TestEnum2 { A };
|
2016-05-06 14:37:20 +00:00
|
|
|
|
|
|
|
TEST(OStreamTest, Enum) {
|
2018-08-22 14:40:06 +00:00
|
|
|
EXPECT_FALSE((fmt::convert_to_int<TestEnum, char>::value));
|
2016-05-06 14:37:20 +00:00
|
|
|
EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum()));
|
|
|
|
EXPECT_EQ("0", fmt::format("{}", A));
|
2018-08-22 14:40:06 +00:00
|
|
|
EXPECT_FALSE((fmt::convert_to_int<TestEnum, wchar_t>::value));
|
2018-04-26 18:32:14 +00:00
|
|
|
EXPECT_EQ(L"TestEnum", fmt::format(L"{}", TestEnum()));
|
|
|
|
EXPECT_EQ(L"0", fmt::format(L"{}", A));
|
2016-05-06 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 17:23:47 +00:00
|
|
|
typedef fmt::back_insert_range<fmt::internal::buffer> range;
|
2018-01-21 22:30:38 +00:00
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
struct test_arg_formatter : fmt::arg_formatter<range> {
|
|
|
|
test_arg_formatter(fmt::format_context& ctx, fmt::format_specs& s)
|
|
|
|
: fmt::arg_formatter<range>(ctx, &s) {}
|
2016-05-06 14:37:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(OStreamTest, CustomArg) {
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer buffer;
|
2019-01-13 02:27:38 +00:00
|
|
|
fmt::internal::buffer& base = buffer;
|
2018-04-08 14:03:44 +00:00
|
|
|
fmt::format_context ctx(std::back_inserter(base), "", fmt::format_args());
|
2017-01-28 12:51:35 +00:00
|
|
|
fmt::format_specs spec;
|
2018-01-15 16:22:31 +00:00
|
|
|
test_arg_formatter af(ctx, spec);
|
2018-04-08 14:03:44 +00:00
|
|
|
visit(af, fmt::internal::make_arg<fmt::format_context>(TestEnum()));
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_EQ("TestEnum", std::string(buffer.data(), buffer.size()));
|
2016-05-06 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, Format) {
|
|
|
|
EXPECT_EQ("a string", format("{0}", TestString("a string")));
|
|
|
|
std::string s = format("The date is {0}", Date(2012, 12, 9));
|
|
|
|
EXPECT_EQ("The date is 2012-12-9", s);
|
|
|
|
Date date(2012, 12, 9);
|
|
|
|
EXPECT_EQ(L"The date is 2012-12-9",
|
|
|
|
format(L"The date is {0}", Date(2012, 12, 9)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, FormatSpecs) {
|
|
|
|
EXPECT_EQ("def ", format("{0:<5}", TestString("def")));
|
|
|
|
EXPECT_EQ(" def", format("{0:>5}", TestString("def")));
|
2019-01-13 02:27:38 +00:00
|
|
|
EXPECT_THROW_MSG(format("{0:=5}", TestString("def")), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
2016-05-06 14:37:20 +00:00
|
|
|
EXPECT_EQ(" def ", format("{0:^5}", TestString("def")));
|
|
|
|
EXPECT_EQ("def**", format("{0:*<5}", TestString("def")));
|
2019-01-13 02:27:38 +00:00
|
|
|
EXPECT_THROW_MSG(format("{0:+}", TestString()), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
|
|
|
EXPECT_THROW_MSG(format("{0:-}", TestString()), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
|
|
|
EXPECT_THROW_MSG(format("{0: }", TestString()), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
|
|
|
EXPECT_THROW_MSG(format("{0:#}", TestString()), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
|
|
|
EXPECT_THROW_MSG(format("{0:05}", TestString()), format_error,
|
|
|
|
"format specifier requires numeric argument");
|
2016-05-06 14:37:20 +00:00
|
|
|
EXPECT_EQ("test ", format("{0:13}", TestString("test")));
|
|
|
|
EXPECT_EQ("test ", format("{0:{1}}", TestString("test"), 13));
|
|
|
|
EXPECT_EQ("te", format("{0:.2}", TestString("test")));
|
|
|
|
EXPECT_EQ("te", format("{0:.{1}}", TestString("test"), 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EmptyTest {};
|
2019-01-13 02:27:38 +00:00
|
|
|
static std::ostream& operator<<(std::ostream& os, EmptyTest) {
|
2016-05-06 14:37:20 +00:00
|
|
|
return os << "";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, EmptyCustomOutput) {
|
|
|
|
EXPECT_EQ("", fmt::format("{}", EmptyTest()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, Print) {
|
|
|
|
std::ostringstream os;
|
|
|
|
fmt::print(os, "Don't {}!", "panic");
|
|
|
|
EXPECT_EQ("Don't panic!", os.str());
|
2018-04-26 18:32:14 +00:00
|
|
|
std::wostringstream wos;
|
|
|
|
fmt::print(wos, L"Don't {}!", L"panic");
|
|
|
|
EXPECT_EQ(L"Don't panic!", wos.str());
|
2016-05-06 14:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, WriteToOStream) {
|
|
|
|
std::ostringstream os;
|
2017-02-18 17:13:12 +00:00
|
|
|
fmt::memory_buffer buffer;
|
2019-01-13 02:27:38 +00:00
|
|
|
const char* foo = "foo";
|
2017-02-14 21:29:47 +00:00
|
|
|
buffer.append(foo, foo + std::strlen(foo));
|
|
|
|
fmt::internal::write(os, buffer);
|
2016-05-06 14:37:20 +00:00
|
|
|
EXPECT_EQ("foo", os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, WriteToOStreamMaxSize) {
|
|
|
|
std::size_t max_size = std::numeric_limits<std::size_t>::max();
|
|
|
|
std::streamsize max_streamsize = std::numeric_limits<std::streamsize>::max();
|
2019-01-13 02:27:38 +00:00
|
|
|
if (max_size <= fmt::internal::to_unsigned(max_streamsize)) return;
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2018-01-14 22:15:59 +00:00
|
|
|
struct test_buffer : fmt::internal::buffer {
|
2018-01-14 20:25:03 +00:00
|
|
|
explicit test_buffer(std::size_t size) { resize(size); }
|
2017-02-14 21:29:47 +00:00
|
|
|
void grow(std::size_t) {}
|
|
|
|
} buffer(max_size);
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2018-01-14 20:25:03 +00:00
|
|
|
struct mock_streambuf : std::streambuf {
|
2019-01-13 02:27:38 +00:00
|
|
|
MOCK_METHOD2(xsputn, std::streamsize(const void* s, std::streamsize n));
|
|
|
|
std::streamsize xsputn(const char* s, std::streamsize n) {
|
|
|
|
const void* v = s;
|
2016-05-06 14:37:20 +00:00
|
|
|
return xsputn(v, n);
|
|
|
|
}
|
2017-02-14 21:29:47 +00:00
|
|
|
} streambuf;
|
2016-05-06 14:37:20 +00:00
|
|
|
|
2018-01-14 20:25:03 +00:00
|
|
|
struct test_ostream : std::ostream {
|
2019-01-13 02:27:38 +00:00
|
|
|
explicit test_ostream(mock_streambuf& buffer) : std::ostream(&buffer) {}
|
2017-02-14 21:29:47 +00:00
|
|
|
} os(streambuf);
|
2016-05-06 14:37:20 +00:00
|
|
|
|
|
|
|
testing::InSequence sequence;
|
2019-01-13 02:27:38 +00:00
|
|
|
const char* data = FMT_NULL;
|
2018-12-05 15:11:06 +00:00
|
|
|
typedef std::make_unsigned<std::streamsize>::type ustreamsize;
|
|
|
|
ustreamsize size = max_size;
|
2016-05-06 14:37:20 +00:00
|
|
|
do {
|
2018-12-05 15:11:06 +00:00
|
|
|
auto n = std::min(size, fmt::internal::to_unsigned(max_streamsize));
|
2017-02-14 21:29:47 +00:00
|
|
|
EXPECT_CALL(streambuf, xsputn(data, static_cast<std::streamsize>(n)))
|
2016-05-06 14:37:20 +00:00
|
|
|
.WillOnce(testing::Return(max_streamsize));
|
|
|
|
data += n;
|
2018-11-27 10:52:00 +00:00
|
|
|
size -= static_cast<std::size_t>(n);
|
2016-05-06 14:37:20 +00:00
|
|
|
} while (size != 0);
|
2017-02-14 21:29:47 +00:00
|
|
|
fmt::internal::write(os, buffer);
|
2016-05-06 14:37:20 +00:00
|
|
|
}
|
2018-05-22 03:21:06 +00:00
|
|
|
|
|
|
|
TEST(OStreamTest, Join) {
|
|
|
|
int v[3] = {1, 2, 3};
|
|
|
|
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, v + 3, ", ")));
|
|
|
|
}
|
2018-06-10 17:05:27 +00:00
|
|
|
|
2018-07-04 20:17:03 +00:00
|
|
|
#if FMT_USE_CONSTEXPR
|
2018-06-10 17:05:27 +00:00
|
|
|
TEST(OStreamTest, ConstexprString) {
|
|
|
|
EXPECT_EQ("42", format(fmt("{}"), std::string("42")));
|
|
|
|
}
|
2018-07-04 20:17:03 +00:00
|
|
|
#endif
|
2018-07-09 13:49:44 +00:00
|
|
|
|
|
|
|
namespace fmt_test {
|
|
|
|
struct ABC {};
|
|
|
|
|
2019-01-13 02:27:38 +00:00
|
|
|
template <typename Output> Output& operator<<(Output& out, ABC) {
|
2018-07-09 13:49:44 +00:00
|
|
|
out << "ABC";
|
|
|
|
return out;
|
|
|
|
}
|
2019-01-13 02:27:38 +00:00
|
|
|
} // namespace fmt_test
|
2018-07-09 13:49:44 +00:00
|
|
|
|
2019-01-21 15:11:49 +00:00
|
|
|
template <typename T>
|
|
|
|
struct TestTemplate {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
|
|
|
|
return os << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
template <typename T>
|
|
|
|
struct formatter<TestTemplate<T>> : formatter<int> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
|
|
|
|
return formatter<int>::format(2, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 407
|
|
|
|
TEST(OStreamTest, Template) {
|
|
|
|
EXPECT_EQ("2", fmt::format("{}", TestTemplate<int>()));
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:49:44 +00:00
|
|
|
TEST(FormatTest, FormatToN) {
|
|
|
|
char buffer[4];
|
|
|
|
buffer[3] = 'x';
|
|
|
|
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::ABC());
|
|
|
|
EXPECT_EQ(3u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 3, result.out);
|
|
|
|
EXPECT_EQ("ABCx", fmt::string_view(buffer, 4));
|
|
|
|
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::ABC());
|
|
|
|
EXPECT_EQ(5u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 3, result.out);
|
|
|
|
EXPECT_EQ("xABx", fmt::string_view(buffer, 4));
|
|
|
|
}
|
2019-01-21 15:11:49 +00:00
|
|
|
#endif
|
2018-08-11 16:13:54 +00:00
|
|
|
|
|
|
|
#if FMT_USE_USER_DEFINED_LITERALS
|
|
|
|
TEST(FormatTest, UDL) {
|
|
|
|
using namespace fmt::literals;
|
|
|
|
EXPECT_EQ("{}"_format("test"), "test");
|
|
|
|
}
|
|
|
|
#endif
|