2018-03-04 20:16:51 +03: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 17:37:20 +03:00
|
|
|
|
2019-04-20 03:02:31 +03:00
|
|
|
#include "fmt/format.h"
|
|
|
|
|
|
|
|
struct test {};
|
|
|
|
|
|
|
|
// Test that there is no issues with specializations when fmt/ostream.h is
|
|
|
|
// included after fmt/format.h.
|
|
|
|
namespace fmt {
|
|
|
|
template <> struct formatter<test> : formatter<int> {
|
|
|
|
template <typename FormatContext>
|
|
|
|
typename FormatContext::iterator format(const test&, FormatContext& ctx) {
|
|
|
|
return formatter<int>::format(42, ctx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace fmt
|
|
|
|
|
2016-05-06 17:37:20 +03:00
|
|
|
#include <sstream>
|
2020-05-07 03:15:46 +03:00
|
|
|
|
|
|
|
#include "fmt/ostream.h"
|
2020-11-22 03:31:22 +03:00
|
|
|
#include "fmt/ranges.h"
|
2018-06-06 16:57:59 +03:00
|
|
|
#include "gmock.h"
|
2016-05-06 17:37:20 +03:00
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using fmt::format;
|
2016-08-25 18:38:07 +03:00
|
|
|
using fmt::format_error;
|
2016-05-06 17:37:20 +03:00
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
static std::ostream& operator<<(std::ostream& os, const Date& d) {
|
2016-05-06 17:37:20 +03:00
|
|
|
os << d.year() << '-' << d.month() << '-' << d.day();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
static std::wostream& operator<<(std::wostream& os, const Date& d) {
|
2016-05-06 17:37:20 +03:00
|
|
|
os << d.year() << L'-' << d.month() << L'-' << d.day();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:45:14 +03:00
|
|
|
// Make sure that overloaded comma operators do no harm to is_streamable.
|
|
|
|
struct type_with_comma_op {};
|
|
|
|
template <typename T> void operator,(type_with_comma_op, const T&);
|
|
|
|
template <typename T> type_with_comma_op operator<<(T&, const Date&);
|
|
|
|
|
2019-06-07 17:08:04 +03:00
|
|
|
enum streamable_enum {};
|
|
|
|
static std::ostream& operator<<(std::ostream& os, streamable_enum) {
|
|
|
|
return os << "streamable_enum";
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 17:08:04 +03:00
|
|
|
static std::wostream& operator<<(std::wostream& os, streamable_enum) {
|
|
|
|
return os << L"streamable_enum";
|
2018-04-26 21:32:14 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 17:08:04 +03:00
|
|
|
enum unstreamable_enum {};
|
2016-05-06 17:37:20 +03:00
|
|
|
|
|
|
|
TEST(OStreamTest, Enum) {
|
2019-06-07 17:08:04 +03:00
|
|
|
EXPECT_EQ("streamable_enum", fmt::format("{}", streamable_enum()));
|
|
|
|
EXPECT_EQ("0", fmt::format("{}", unstreamable_enum()));
|
|
|
|
EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));
|
|
|
|
EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2020-05-30 02:51:45 +03:00
|
|
|
struct test_arg_formatter
|
2020-07-04 16:41:24 +03:00
|
|
|
: fmt::detail::arg_formatter<fmt::format_context::iterator, char> {
|
2019-02-10 06:34:42 +03:00
|
|
|
fmt::format_parse_context parse_ctx;
|
2019-01-13 05:27:38 +03:00
|
|
|
test_arg_formatter(fmt::format_context& ctx, fmt::format_specs& s)
|
2020-07-04 16:41:24 +03:00
|
|
|
: fmt::detail::arg_formatter<fmt::format_context::iterator, char>(
|
2020-05-30 02:51:45 +03:00
|
|
|
ctx, &parse_ctx, &s),
|
|
|
|
parse_ctx("") {}
|
2016-05-06 17:37:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(OStreamTest, CustomArg) {
|
2017-02-18 20:13:12 +03:00
|
|
|
fmt::memory_buffer buffer;
|
2020-07-09 19:04:46 +03:00
|
|
|
fmt::format_context ctx(fmt::detail::buffer_appender<char>{buffer},
|
|
|
|
fmt::format_args());
|
2017-01-28 15:51:35 +03:00
|
|
|
fmt::format_specs spec;
|
2018-01-15 19:22:31 +03:00
|
|
|
test_arg_formatter af(ctx, spec);
|
2019-02-01 07:47:57 +03:00
|
|
|
fmt::visit_format_arg(
|
2020-05-10 17:25:42 +03:00
|
|
|
af, fmt::detail::make_arg<fmt::format_context>(streamable_enum()));
|
2019-06-07 17:08:04 +03:00
|
|
|
EXPECT_EQ("streamable_enum", std::string(buffer.data(), buffer.size()));
|
2016-05-06 17:37:20 +03: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")));
|
2020-07-03 18:30:05 +03:00
|
|
|
#if FMT_DEPRECATED_NUMERIC_ALIGN
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0:=5}", TestString("def")), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2019-09-06 17:12:35 +03:00
|
|
|
#endif
|
2016-05-06 17:37:20 +03:00
|
|
|
EXPECT_EQ(" def ", format("{0:^5}", TestString("def")));
|
|
|
|
EXPECT_EQ("def**", format("{0:*<5}", TestString("def")));
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0:+}", TestString()), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0:-}", TestString()), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0: }", TestString()), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0:#}", TestString()), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2020-11-15 20:03:20 +03:00
|
|
|
EXPECT_THROW_MSG(format(+"{0:05}", TestString()), format_error,
|
2019-01-13 05:27:38 +03:00
|
|
|
"format specifier requires numeric argument");
|
2016-05-06 17:37:20 +03: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 05:27:38 +03:00
|
|
|
static std::ostream& operator<<(std::ostream& os, EmptyTest) {
|
2016-05-06 17:37:20 +03: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 21:32:14 +03:00
|
|
|
std::wostringstream wos;
|
|
|
|
fmt::print(wos, L"Don't {}!", L"panic");
|
|
|
|
EXPECT_EQ(L"Don't panic!", wos.str());
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, WriteToOStream) {
|
|
|
|
std::ostringstream os;
|
2017-02-18 20:13:12 +03:00
|
|
|
fmt::memory_buffer buffer;
|
2019-01-13 05:27:38 +03:00
|
|
|
const char* foo = "foo";
|
2017-02-15 00:29:47 +03:00
|
|
|
buffer.append(foo, foo + std::strlen(foo));
|
2020-06-08 17:23:18 +03:00
|
|
|
fmt::detail::write_buffer(os, buffer);
|
2016-05-06 17:37:20 +03:00
|
|
|
EXPECT_EQ("foo", os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, WriteToOStreamMaxSize) {
|
2020-05-10 17:25:42 +03:00
|
|
|
size_t max_size = fmt::detail::max_value<size_t>();
|
|
|
|
std::streamsize max_streamsize = fmt::detail::max_value<std::streamsize>();
|
|
|
|
if (max_size <= fmt::detail::to_unsigned(max_streamsize)) return;
|
2016-05-06 17:37:20 +03:00
|
|
|
|
2020-10-16 03:41:56 +03:00
|
|
|
struct test_buffer final : fmt::detail::buffer<char> {
|
2020-07-10 17:50:37 +03:00
|
|
|
explicit test_buffer(size_t size)
|
2020-10-28 06:55:44 +03:00
|
|
|
: fmt::detail::buffer<char>(nullptr, size, size) {}
|
2020-05-08 01:59:46 +03:00
|
|
|
void grow(size_t) {}
|
2017-02-15 00:29:47 +03:00
|
|
|
} buffer(max_size);
|
2016-05-06 17:37:20 +03:00
|
|
|
|
2018-01-14 23:25:03 +03:00
|
|
|
struct mock_streambuf : std::streambuf {
|
2019-01-13 05:27:38 +03: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 17:37:20 +03:00
|
|
|
return xsputn(v, n);
|
|
|
|
}
|
2017-02-15 00:29:47 +03:00
|
|
|
} streambuf;
|
2016-05-06 17:37:20 +03:00
|
|
|
|
2018-01-14 23:25:03 +03:00
|
|
|
struct test_ostream : std::ostream {
|
2020-10-28 06:55:44 +03:00
|
|
|
explicit test_ostream(mock_streambuf& output_buffer)
|
|
|
|
: std::ostream(&output_buffer) {}
|
2017-02-15 00:29:47 +03:00
|
|
|
} os(streambuf);
|
2016-05-06 17:37:20 +03:00
|
|
|
|
|
|
|
testing::InSequence sequence;
|
2019-05-30 17:01:31 +03:00
|
|
|
const char* data = nullptr;
|
2018-12-05 18:11:06 +03:00
|
|
|
typedef std::make_unsigned<std::streamsize>::type ustreamsize;
|
|
|
|
ustreamsize size = max_size;
|
2016-05-06 17:37:20 +03:00
|
|
|
do {
|
2020-05-10 17:25:42 +03:00
|
|
|
auto n = std::min(size, fmt::detail::to_unsigned(max_streamsize));
|
2017-02-15 00:29:47 +03:00
|
|
|
EXPECT_CALL(streambuf, xsputn(data, static_cast<std::streamsize>(n)))
|
2016-05-06 17:37:20 +03:00
|
|
|
.WillOnce(testing::Return(max_streamsize));
|
|
|
|
data += n;
|
2019-02-03 04:24:07 +03:00
|
|
|
size -= n;
|
2016-05-06 17:37:20 +03:00
|
|
|
} while (size != 0);
|
2020-06-08 17:23:18 +03:00
|
|
|
fmt::detail::write_buffer(os, buffer);
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
2018-05-22 06:21:06 +03: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 20:05:27 +03:00
|
|
|
|
2018-07-04 23:17:03 +03:00
|
|
|
#if FMT_USE_CONSTEXPR
|
2018-06-10 20:05:27 +03:00
|
|
|
TEST(OStreamTest, ConstexprString) {
|
2019-11-19 21:20:31 +03:00
|
|
|
EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));
|
|
|
|
EXPECT_EQ("a string", format(FMT_STRING("{0}"), TestString("a string")));
|
2018-06-10 20:05:27 +03:00
|
|
|
}
|
2018-07-04 23:17:03 +03:00
|
|
|
#endif
|
2018-07-09 16:49:44 +03:00
|
|
|
|
|
|
|
namespace fmt_test {
|
|
|
|
struct ABC {};
|
|
|
|
|
2019-01-13 05:27:38 +03:00
|
|
|
template <typename Output> Output& operator<<(Output& out, ABC) {
|
2018-07-09 16:49:44 +03:00
|
|
|
out << "ABC";
|
|
|
|
return out;
|
|
|
|
}
|
2019-01-13 05:27:38 +03:00
|
|
|
} // namespace fmt_test
|
2018-07-09 16:49:44 +03:00
|
|
|
|
2019-02-01 07:47:57 +03:00
|
|
|
template <typename T> struct TestTemplate {};
|
2019-01-21 18:11:49 +03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
|
|
|
|
return os << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt {
|
2019-02-01 07:47:57 +03:00
|
|
|
template <typename T> struct formatter<TestTemplate<T>> : formatter<int> {
|
2019-01-21 18:11:49 +03:00
|
|
|
template <typename FormatContext>
|
|
|
|
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
|
|
|
|
return formatter<int>::format(2, ctx);
|
|
|
|
}
|
|
|
|
};
|
2019-02-01 07:47:57 +03:00
|
|
|
} // namespace fmt
|
2019-01-21 18:11:49 +03:00
|
|
|
|
|
|
|
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 407
|
|
|
|
TEST(OStreamTest, Template) {
|
|
|
|
EXPECT_EQ("2", fmt::format("{}", TestTemplate<int>()));
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:49:44 +03: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 18:11:49 +03:00
|
|
|
#endif
|
2018-08-11 19:13:54 +03:00
|
|
|
|
|
|
|
#if FMT_USE_USER_DEFINED_LITERALS
|
|
|
|
TEST(FormatTest, UDL) {
|
|
|
|
using namespace fmt::literals;
|
|
|
|
EXPECT_EQ("{}"_format("test"), "test");
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-23 21:45:07 +03:00
|
|
|
|
2019-11-26 03:46:33 +03:00
|
|
|
template <typename T> struct convertible {
|
2019-09-23 21:45:07 +03:00
|
|
|
T value;
|
|
|
|
explicit convertible(const T& val) : value(val) {}
|
|
|
|
operator T() const { return value; }
|
|
|
|
};
|
|
|
|
|
2019-09-24 06:34:08 +03:00
|
|
|
TEST(OStreamTest, DisableBuiltinOStreamOperators) {
|
|
|
|
EXPECT_EQ("42", fmt::format("{:d}", convertible<unsigned short>(42)));
|
|
|
|
EXPECT_EQ(L"42", fmt::format(L"{:d}", convertible<unsigned short>(42)));
|
2019-09-23 21:45:07 +03:00
|
|
|
EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
|
|
|
|
}
|
2019-11-26 03:46:33 +03:00
|
|
|
|
|
|
|
struct explicitly_convertible_to_string_like {
|
|
|
|
template <typename String,
|
|
|
|
typename = typename std::enable_if<std::is_constructible<
|
2020-05-08 01:59:46 +03:00
|
|
|
String, const char*, size_t>::value>::type>
|
2019-11-26 03:46:33 +03:00
|
|
|
explicit operator String() const {
|
|
|
|
return String("foo", 3u);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os,
|
|
|
|
explicitly_convertible_to_string_like) {
|
|
|
|
return os << "bar";
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:15:46 +03:00
|
|
|
TEST(OStreamTest, FormatExplicitlyConvertibleToStringLike) {
|
2020-02-08 05:47:48 +03:00
|
|
|
EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
|
2019-11-27 22:06:40 +03:00
|
|
|
}
|
2019-12-10 00:25:08 +03:00
|
|
|
|
|
|
|
#ifdef FMT_USE_STRING_VIEW
|
|
|
|
struct explicitly_convertible_to_std_string_view {
|
2020-05-10 17:25:42 +03:00
|
|
|
explicit operator fmt::detail::std_string_view<char>() const {
|
2019-12-10 00:25:08 +03:00
|
|
|
return {"foo", 3u};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os,
|
|
|
|
explicitly_convertible_to_std_string_view) {
|
|
|
|
return os << "bar";
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:15:46 +03:00
|
|
|
TEST(OStreamTest, FormatExplicitlyConvertibleToStdStringView) {
|
2020-02-08 05:47:48 +03:00
|
|
|
EXPECT_EQ("bar", fmt::format("{}", explicitly_convertible_to_string_like()));
|
2019-12-10 00:25:08 +03:00
|
|
|
}
|
|
|
|
#endif // FMT_USE_STRING_VIEW
|
2020-05-07 03:15:46 +03:00
|
|
|
|
2020-07-12 18:26:35 +03:00
|
|
|
struct streamable_and_convertible_to_bool {
|
|
|
|
operator bool() const { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, streamable_and_convertible_to_bool) {
|
|
|
|
return os << "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, FormatConvertibleToBool) {
|
|
|
|
EXPECT_EQ("foo", fmt::format("{}", streamable_and_convertible_to_bool()));
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:15:46 +03:00
|
|
|
struct copyfmt_test {};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, copyfmt_test) {
|
|
|
|
std::ios ios(nullptr);
|
|
|
|
ios.copyfmt(os);
|
|
|
|
return os << "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OStreamTest, CopyFmt) {
|
|
|
|
EXPECT_EQ("foo", fmt::format("{}", copyfmt_test()));
|
|
|
|
}
|
2020-05-21 00:59:57 +03:00
|
|
|
|
|
|
|
TEST(OStreamTest, CompileTimeString) {
|
|
|
|
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
|
|
|
|
}
|
2020-08-20 16:41:09 +03:00
|
|
|
|
|
|
|
TEST(OStreamTest, ToString) {
|
|
|
|
EXPECT_EQ("ABC", fmt::to_string(fmt_test::ABC()));
|
|
|
|
}
|
2020-11-22 03:31:22 +03:00
|
|
|
|
|
|
|
TEST(OStreamTest, Range) {
|
|
|
|
auto strs = std::vector<TestString>{TestString("foo"), TestString("bar")};
|
|
|
|
EXPECT_EQ("{foo, bar}", format("{}", strs));
|
|
|
|
}
|