Commit d9db8981 authored by Victor Zverovich's avatar Victor Zverovich

Refactor action classes, Action -> Sink, add comments.

parent d2bf0733
......@@ -1479,32 +1479,43 @@ TEST(StringRefTest, ConvertToString) {
EXPECT_EQ("abc", s);
}
struct CountCalls {
int &num_calls;
TEST(FormatterTest, Ctor) {
fmt::Formatter<> f1("test");
fmt::Formatter<> f1copy(f1);
fmt::Formatter<> f2("test", fmt::NullSink());
fmt::Formatter<fmt::NullSink> f3("test");
fmt::Formatter<fmt::NullSink, wchar_t> f4(L"test");
fmt::Formatter<fmt::NullSink, wchar_t> f4copy(f4);
fmt::Formatter<fmt::NullSink, wchar_t> f5(L"test", fmt::NullSink());
}
// A sink that counts the number of times the output is written to it.
struct CountingSink {
int &num_writes;
CountCalls(int &num_calls) : num_calls(num_calls) {}
explicit CountingSink(int &num_calls) : num_writes(num_writes) {}
void operator()(const Writer &) const {
++num_calls;
++num_writes;
}
};
TEST(FormatterTest, Action) {
int num_calls = 0;
TEST(FormatterTest, Sink) {
int num_writes = 0;
{
fmt::Formatter<CountCalls> af("test", CountCalls(num_calls));
EXPECT_EQ(0, num_calls);
fmt::Formatter<CountingSink> f("test", CountingSink(num_writes));
EXPECT_EQ(0, num_writes);
}
EXPECT_EQ(1, num_calls);
EXPECT_EQ(1, num_writes);
}
TEST(FormatterTest, ActionNotCalledOnError) {
int num_calls = 0;
TEST(FormatterTest, OutputNotWrittenOnError) {
int num_writes = 0;
{
typedef fmt::Formatter<CountCalls> TestFormatter;
EXPECT_THROW(TestFormatter af("{0", CountCalls(num_calls)), FormatError);
typedef fmt::Formatter<CountingSink> TestFormatter;
EXPECT_THROW(TestFormatter f("{0", CountingSink(num_writes)), FormatError);
}
EXPECT_EQ(0, num_calls);
EXPECT_EQ(0, num_writes);
}
// The test doesn't compile on older compilers which follow C++03 and
......@@ -1641,7 +1652,7 @@ class File {
int fd() const { return fd_; }
};
TEST(ColorTest, PrintColored) {
TEST(FormatTest, PrintColored) {
std::fflush(stdout);
File saved_stdio(dup(1));
EXPECT_NE(-1, saved_stdio.fd());
......@@ -1661,6 +1672,13 @@ TEST(ColorTest, PrintColored) {
#endif
#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
TEST(FormatTest, Variadic) {
EXPECT_EQ("Hello, world!1", str(Format("Hello, {}!{}", "world", 1)));
EXPECT_EQ(L"Hello, world!1", str(Format(L"Hello, {}!{}", L"world", 1)));
}
#endif // FMT_USE_VARIADIC_TEMPLATES
template <typename T>
std::string str(const T &value) {
return fmt::str(fmt::Format("{0}") << value);
......@@ -1672,13 +1690,6 @@ TEST(StrTest, Convert) {
EXPECT_EQ("2012-12-9", s);
}
#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
TEST(FormatTest, Variadic) {
EXPECT_EQ("Hello, world!1", str(Format("Hello, {}!{}", "world", 1)));
EXPECT_EQ(L"Hello, world!1", str(Format(L"Hello, {}!{}", L"world", 1)));
}
#endif // FMT_USE_VARIADIC_TEMPLATES
int main(int argc, char **argv) {
#ifdef _WIN32
// Disable message boxes on assertion failures.
......
......@@ -688,12 +688,13 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
writer.buffer_.append(start, s);
}
void fmt::ColorWriter::operator()(const fmt::BasicWriter<char> &w) const {
void fmt::ANSITerminalSink::operator()(
const fmt::BasicWriter<char> &w) const {
char escape[] = "\x1b[30m";
escape[3] = '0' + static_cast<char>(color_);
std::fputs(escape, stdout);
std::fwrite(w.data(), 1, w.size(), stdout);
std::fputs(RESET_COLOR, stdout);
std::fputs(escape, file_);
std::fwrite(w.data(), 1, w.size(), file_);
std::fputs(RESET_COLOR, file_);
}
// Explicit instantiations for char.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment