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
|
|
|
|
2022-06-17 00:56:45 +03:00
|
|
|
#include <fstream>
|
|
|
|
|
2019-04-20 03:02:31 +03:00
|
|
|
#include "fmt/format.h"
|
|
|
|
|
2021-05-19 05:38:52 +03:00
|
|
|
using fmt::runtime;
|
|
|
|
|
2019-04-20 03:02:31 +03:00
|
|
|
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> {
|
2023-11-24 20:23:58 +03:00
|
|
|
auto format(const test&, format_context& ctx) const -> decltype(ctx.out()) {
|
2019-04-20 03:02:31 +03:00
|
|
|
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
|
|
|
|
2021-09-02 18:13:47 +03:00
|
|
|
#include "fmt/compile.h"
|
2020-05-07 03:15:46 +03:00
|
|
|
#include "fmt/ostream.h"
|
2020-11-22 03:31:22 +03:00
|
|
|
#include "fmt/ranges.h"
|
2021-04-29 01:59:43 +03:00
|
|
|
#include "gmock/gmock.h"
|
2016-05-06 17:37:20 +03:00
|
|
|
#include "gtest-extra.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2022-06-24 19:26:24 +03:00
|
|
|
auto operator<<(std::ostream& os, const date& d) -> std::ostream& {
|
2016-05-06 17:37:20 +03:00
|
|
|
os << d.year() << '-' << d.month() << '-' << d.day();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2022-06-24 19:26:24 +03:00
|
|
|
auto operator<<(std::wostream& os, const date& d) -> std::wostream& {
|
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&);
|
2021-05-06 03:43:06 +03:00
|
|
|
template <typename T> type_with_comma_op operator<<(T&, const date&);
|
2019-02-06 13:45:14 +03:00
|
|
|
|
2019-06-07 17:08:04 +03:00
|
|
|
enum streamable_enum {};
|
2021-06-06 06:19:17 +03:00
|
|
|
|
2022-06-24 19:26:24 +03:00
|
|
|
auto operator<<(std::ostream& os, streamable_enum) -> std::ostream& {
|
2019-06-07 17:08:04 +03:00
|
|
|
return os << "streamable_enum";
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 17:08:04 +03:00
|
|
|
enum unstreamable_enum {};
|
2022-07-03 18:05:47 +03:00
|
|
|
auto format_as(unstreamable_enum e) -> int { return e; }
|
2016-05-06 17:37:20 +03:00
|
|
|
|
2022-02-05 02:06:45 +03:00
|
|
|
struct empty_test {};
|
2022-06-24 19:26:24 +03:00
|
|
|
auto operator<<(std::ostream& os, empty_test) -> std::ostream& {
|
|
|
|
return os << "";
|
|
|
|
}
|
2022-02-05 02:06:45 +03:00
|
|
|
|
|
|
|
namespace fmt {
|
2022-02-05 05:33:55 +03:00
|
|
|
template <> struct formatter<test_string> : ostream_formatter {};
|
|
|
|
template <> struct formatter<date> : ostream_formatter {};
|
|
|
|
template <> struct formatter<streamable_enum> : ostream_formatter {};
|
|
|
|
template <> struct formatter<empty_test> : ostream_formatter {};
|
2022-02-05 02:06:45 +03:00
|
|
|
} // namespace fmt
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, enum) {
|
2019-06-07 17:08:04 +03:00
|
|
|
EXPECT_EQ("streamable_enum", fmt::format("{}", streamable_enum()));
|
|
|
|
EXPECT_EQ("0", fmt::format("{}", unstreamable_enum()));
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, format) {
|
2021-05-06 03:43:06 +03:00
|
|
|
EXPECT_EQ("a string", fmt::format("{0}", test_string("a string")));
|
|
|
|
EXPECT_EQ("The date is 2012-12-9",
|
|
|
|
fmt::format("The date is {0}", date(2012, 12, 9)));
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, format_specs) {
|
|
|
|
using fmt::format_error;
|
2021-05-06 03:43:06 +03:00
|
|
|
EXPECT_EQ("def ", fmt::format("{0:<5}", test_string("def")));
|
|
|
|
EXPECT_EQ(" def", fmt::format("{0:>5}", test_string("def")));
|
|
|
|
EXPECT_EQ(" def ", fmt::format("{0:^5}", test_string("def")));
|
|
|
|
EXPECT_EQ("def**", fmt::format("{0:*<5}", test_string("def")));
|
2021-11-25 19:52:21 +03:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), test_string()),
|
2022-12-30 21:20:07 +03:00
|
|
|
format_error, "invalid format specifier");
|
2021-11-25 19:52:21 +03:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), test_string()),
|
2022-12-30 21:20:07 +03:00
|
|
|
format_error, "invalid format specifier");
|
2021-11-25 19:52:21 +03:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), test_string()),
|
2022-12-30 21:20:07 +03:00
|
|
|
format_error, "invalid format specifier");
|
2021-11-25 19:52:21 +03:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), test_string()),
|
2022-12-30 21:58:22 +03:00
|
|
|
format_error, "invalid format specifier");
|
2021-11-25 19:52:21 +03:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), test_string()),
|
|
|
|
format_error, "format specifier requires numeric argument");
|
2021-05-06 03:43:06 +03:00
|
|
|
EXPECT_EQ("test ", fmt::format("{0:13}", test_string("test")));
|
|
|
|
EXPECT_EQ("test ", fmt::format("{0:{1}}", test_string("test"), 13));
|
|
|
|
EXPECT_EQ("te", fmt::format("{0:.2}", test_string("test")));
|
|
|
|
EXPECT_EQ("te", fmt::format("{0:.{1}}", test_string("test"), 2));
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, empty_custom_output) {
|
|
|
|
EXPECT_EQ("", fmt::format("{}", empty_test()));
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, print) {
|
2023-01-24 23:30:00 +03:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
fmt::print(os, "Don't {}!", "panic");
|
|
|
|
EXPECT_EQ("Don't panic!", os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
fmt::println(os, "Don't {}!", "panic");
|
|
|
|
EXPECT_EQ("Don't panic!\n", os.str());
|
|
|
|
}
|
2016-05-06 17:37:20 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, write_to_ostream) {
|
2016-05-06 17:37:20 +03:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, write_to_ostream_max_size) {
|
|
|
|
auto max_size = fmt::detail::max_value<size_t>();
|
|
|
|
auto max_streamsize = fmt::detail::max_value<std::streamsize>();
|
2020-05-10 17:25:42 +03:00
|
|
|
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)
|
2024-01-02 18:02:20 +03:00
|
|
|
: fmt::detail::buffer<char>([](buffer<char>&, size_t) {}, nullptr, size,
|
|
|
|
size) {}
|
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 {
|
2023-04-20 23:16:21 +03:00
|
|
|
MOCK_METHOD(std::streamsize, xsputn, (const void*, std::streamsize));
|
2022-06-24 19:26:24 +03:00
|
|
|
auto xsputn(const char* s, std::streamsize n) -> std::streamsize override {
|
2019-01-13 05:27:38 +03:00
|
|
|
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;
|
2021-05-05 02:54:20 +03:00
|
|
|
using ustreamsize = std::make_unsigned<std::streamsize>::type;
|
2018-12-05 18:11:06 +03:00
|
|
|
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
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, join) {
|
2018-05-22 06:21:06 +03:00
|
|
|
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
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, join_fallback_formatter) {
|
2021-05-06 03:43:06 +03:00
|
|
|
auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
|
2020-12-08 19:56:53 +03:00
|
|
|
EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", ")));
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:17:03 +03:00
|
|
|
#if FMT_USE_CONSTEXPR
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, constexpr_string) {
|
2022-05-30 16:41:33 +03:00
|
|
|
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), std::string("42")));
|
|
|
|
EXPECT_EQ("a string",
|
|
|
|
fmt::format(FMT_STRING("{0}"), test_string("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 {
|
2021-05-05 02:54:20 +03:00
|
|
|
struct abc {};
|
2018-07-09 16:49:44 +03:00
|
|
|
|
2022-06-24 19:26:24 +03:00
|
|
|
template <typename Output> auto operator<<(Output& out, abc) -> Output& {
|
2021-05-05 02:54:20 +03:00
|
|
|
return out << "abc";
|
2018-07-09 16:49:44 +03:00
|
|
|
}
|
2019-01-13 05:27:38 +03:00
|
|
|
} // namespace fmt_test
|
2018-07-09 16:49:44 +03:00
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
template <typename T> struct test_template {};
|
2019-01-21 18:11:49 +03:00
|
|
|
|
|
|
|
template <typename T>
|
2022-06-24 19:26:24 +03:00
|
|
|
auto operator<<(std::ostream& os, test_template<T>) -> std::ostream& {
|
2019-01-21 18:11:49 +03:00
|
|
|
return os << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt {
|
2021-05-05 02:54:20 +03:00
|
|
|
template <typename T> struct formatter<test_template<T>> : formatter<int> {
|
|
|
|
auto format(test_template<T>, format_context& ctx) -> decltype(ctx.out()) {
|
2019-01-21 18:11:49 +03:00
|
|
|
return formatter<int>::format(2, ctx);
|
|
|
|
}
|
|
|
|
};
|
2022-02-05 02:06:45 +03:00
|
|
|
|
2022-02-05 05:33:55 +03:00
|
|
|
template <> struct formatter<fmt_test::abc> : ostream_formatter {};
|
2019-02-01 07:47:57 +03:00
|
|
|
} // namespace fmt
|
2019-01-21 18:11:49 +03:00
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, template) {
|
|
|
|
EXPECT_EQ("2", fmt::format("{}", test_template<int>()));
|
2019-01-21 18:11:49 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, format_to_n) {
|
2018-07-09 16:49:44 +03:00
|
|
|
char buffer[4];
|
|
|
|
buffer[3] = 'x';
|
2021-05-05 02:54:20 +03:00
|
|
|
auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::abc());
|
2018-07-09 16:49:44 +03:00
|
|
|
EXPECT_EQ(3u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 3, result.out);
|
2021-05-05 02:54:20 +03:00
|
|
|
EXPECT_EQ("abcx", fmt::string_view(buffer, 4));
|
|
|
|
result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::abc());
|
2018-07-09 16:49:44 +03:00
|
|
|
EXPECT_EQ(5u, result.size);
|
|
|
|
EXPECT_EQ(buffer + 3, result.out);
|
2021-05-05 02:54:20 +03:00
|
|
|
EXPECT_EQ("xabx", fmt::string_view(buffer, 4));
|
2018-08-11 19:13:54 +03:00
|
|
|
}
|
2019-09-23 21:45:07 +03:00
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2022-02-05 02:06:45 +03:00
|
|
|
namespace fmt {
|
2022-02-05 05:33:55 +03:00
|
|
|
template <> struct formatter<copyfmt_test> : ostream_formatter {};
|
2022-02-05 02:06:45 +03:00
|
|
|
} // namespace fmt
|
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, copyfmt) {
|
2020-05-07 03:15:46 +03:00
|
|
|
EXPECT_EQ("foo", fmt::format("{}", copyfmt_test()));
|
|
|
|
}
|
2020-05-21 00:59:57 +03:00
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, to_string) {
|
|
|
|
EXPECT_EQ("abc", fmt::to_string(fmt_test::abc()));
|
2020-08-20 16:41:09 +03:00
|
|
|
}
|
2020-11-22 03:31:22 +03:00
|
|
|
|
2021-05-05 02:54:20 +03:00
|
|
|
TEST(ostream_test, range) {
|
2021-05-06 03:43:06 +03:00
|
|
|
auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
|
2021-05-05 07:04:21 +03:00
|
|
|
EXPECT_EQ("[foo, bar]", fmt::format("{}", strs));
|
2020-11-22 03:52:40 +03:00
|
|
|
}
|
2021-09-02 18:13:47 +03:00
|
|
|
|
|
|
|
struct abstract {
|
|
|
|
virtual ~abstract() = default;
|
|
|
|
virtual void f() = 0;
|
2022-06-24 19:26:24 +03:00
|
|
|
friend auto operator<<(std::ostream& os, const abstract&) -> std::ostream& {
|
2021-09-02 18:13:47 +03:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-05 02:06:45 +03:00
|
|
|
namespace fmt {
|
2022-02-05 05:33:55 +03:00
|
|
|
template <> struct formatter<abstract> : ostream_formatter {};
|
2022-02-05 02:06:45 +03:00
|
|
|
} // namespace fmt
|
|
|
|
|
2021-09-02 18:13:47 +03:00
|
|
|
void format_abstract_compiles(const abstract& a) {
|
|
|
|
fmt::format(FMT_COMPILE("{}"), a);
|
|
|
|
}
|
2021-12-09 20:48:55 +03:00
|
|
|
|
|
|
|
TEST(ostream_test, is_formattable) {
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<std::string>());
|
|
|
|
EXPECT_TRUE(fmt::is_formattable<fmt::detail::std_string_view<char>>());
|
|
|
|
}
|
2022-06-17 00:56:45 +03:00
|
|
|
|
2022-06-24 19:26:24 +03:00
|
|
|
struct streamable_and_unformattable {};
|
|
|
|
|
|
|
|
auto operator<<(std::ostream& os, streamable_and_unformattable)
|
|
|
|
-> std::ostream& {
|
|
|
|
return os << "foo";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ostream_test, streamed) {
|
|
|
|
EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
|
|
|
|
EXPECT_EQ(fmt::format("{}", fmt::streamed(streamable_and_unformattable())),
|
|
|
|
"foo");
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:56:45 +03:00
|
|
|
TEST(ostream_test, closed_ofstream) {
|
|
|
|
std::ofstream ofs;
|
|
|
|
fmt::print(ofs, "discard");
|
|
|
|
}
|
2023-11-24 20:23:58 +03:00
|
|
|
|
|
|
|
struct unlocalized {};
|
|
|
|
|
2024-01-02 18:02:20 +03:00
|
|
|
auto operator<<(std::ostream& os, unlocalized) -> std::ostream& {
|
2023-11-24 20:23:58 +03:00
|
|
|
return os << 12345;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
template <> struct formatter<unlocalized> : ostream_formatter {};
|
|
|
|
} // namespace fmt
|
|
|
|
|
|
|
|
TEST(ostream_test, unlocalized) {
|
|
|
|
auto loc = get_locale("en_US.UTF-8");
|
|
|
|
std::locale::global(loc);
|
|
|
|
EXPECT_EQ(fmt::format(loc, "{}", unlocalized()), "12345");
|
|
|
|
}
|