fmt/test/ostream-test.cc

151 lines
4.9 KiB
C++
Raw Normal View History

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
2016-08-03 15:52:05 +00:00
#include "fmt/ostream.h"
2016-05-06 14:37:20 +00:00
#include <sstream>
#include "gmock/gmock.h"
#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
std::ostream &operator<<(std::ostream &os, const Date &d) {
os << d.year() << '-' << d.month() << '-' << d.day();
return os;
}
std::wostream &operator<<(std::wostream &os, const Date &d) {
os << d.year() << L'-' << d.month() << L'-' << d.day();
return os;
}
enum TestEnum {};
std::ostream &operator<<(std::ostream &os, TestEnum) {
return os << "TestEnum";
}
enum TestEnum2 {A};
TEST(OStreamTest, Enum) {
EXPECT_FALSE((fmt::internal::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));
}
typedef fmt::back_insert_range<fmt::internal::buffer> range;
2018-01-21 22:30:38 +00:00
struct test_arg_formatter: fmt::arg_formatter<range> {
2018-01-15 16:22:31 +00:00
test_arg_formatter(fmt::context &ctx, fmt::format_specs &s)
2018-01-21 22:30:38 +00:00
: fmt::arg_formatter<range>(ctx, s) {}
2016-05-06 14:37:20 +00:00
};
TEST(OStreamTest, CustomArg) {
fmt::memory_buffer buffer;
2018-01-21 22:30:38 +00:00
fmt::internal::buffer &base = buffer;
fmt::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);
2017-02-05 14:41:39 +00:00
visit(af, fmt::internal::make_arg<fmt::context>(TestEnum()));
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")));
EXPECT_THROW_MSG(format("{0:=5}", TestString("def")),
2017-11-18 15:42:54 +00:00
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")));
EXPECT_THROW_MSG(format("{0:+}", TestString()),
2017-11-18 15:42:54 +00:00
format_error, "format specifier requires numeric argument");
2016-05-06 14:37:20 +00:00
EXPECT_THROW_MSG(format("{0:-}", TestString()),
2017-11-18 15:42:54 +00:00
format_error, "format specifier requires numeric argument");
2016-05-06 14:37:20 +00:00
EXPECT_THROW_MSG(format("{0: }", TestString()),
2017-11-18 15:42:54 +00:00
format_error, "format specifier requires numeric argument");
2016-05-06 14:37:20 +00:00
EXPECT_THROW_MSG(format("{0:#}", TestString()),
2017-11-18 15:42:54 +00:00
format_error, "format specifier requires numeric argument");
2016-05-06 14:37:20 +00:00
EXPECT_THROW_MSG(format("{0:05}", TestString()),
2017-11-18 15:42:54 +00:00
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 {};
std::ostream &operator<<(std::ostream &os, EmptyTest) {
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());
}
TEST(OStreamTest, WriteToOStream) {
std::ostringstream os;
fmt::memory_buffer buffer;
const char *foo = "foo";
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();
if (max_size <= fmt::internal::to_unsigned(max_streamsize))
return;
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); }
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 {
2016-05-06 14:37:20 +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;
return xsputn(v, n);
}
} streambuf;
2016-05-06 14:37:20 +00:00
2018-01-14 20:25:03 +00:00
struct test_ostream : std::ostream {
explicit test_ostream(mock_streambuf &buffer) : std::ostream(&buffer) {}
} os(streambuf);
2016-05-06 14:37:20 +00:00
testing::InSequence sequence;
const char *data = 0;
std::size_t size = max_size;
do {
2018-01-14 20:25:03 +00:00
typedef std::make_unsigned<std::streamsize>::type ustreamsize;
ustreamsize n = std::min<ustreamsize>(
2016-05-06 14:37:20 +00:00
size, fmt::internal::to_unsigned(max_streamsize));
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;
size -= static_cast<std::size_t>(n);
} while (size != 0);
fmt::internal::write(os, buffer);
2016-05-06 14:37:20 +00:00
}