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) { ...@@ -1479,32 +1479,43 @@ TEST(StringRefTest, ConvertToString) {
EXPECT_EQ("abc", s); EXPECT_EQ("abc", s);
} }
struct CountCalls { TEST(FormatterTest, Ctor) {
int &num_calls; 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 { void operator()(const Writer &) const {
++num_calls; ++num_writes;
} }
}; };
TEST(FormatterTest, Action) { TEST(FormatterTest, Sink) {
int num_calls = 0; int num_writes = 0;
{ {
fmt::Formatter<CountCalls> af("test", CountCalls(num_calls)); fmt::Formatter<CountingSink> f("test", CountingSink(num_writes));
EXPECT_EQ(0, num_calls); EXPECT_EQ(0, num_writes);
} }
EXPECT_EQ(1, num_calls); EXPECT_EQ(1, num_writes);
} }
TEST(FormatterTest, ActionNotCalledOnError) { TEST(FormatterTest, OutputNotWrittenOnError) {
int num_calls = 0; int num_writes = 0;
{ {
typedef fmt::Formatter<CountCalls> TestFormatter; typedef fmt::Formatter<CountingSink> TestFormatter;
EXPECT_THROW(TestFormatter af("{0", CountCalls(num_calls)), FormatError); 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 // The test doesn't compile on older compilers which follow C++03 and
...@@ -1641,7 +1652,7 @@ class File { ...@@ -1641,7 +1652,7 @@ class File {
int fd() const { return fd_; } int fd() const { return fd_; }
}; };
TEST(ColorTest, PrintColored) { TEST(FormatTest, PrintColored) {
std::fflush(stdout); std::fflush(stdout);
File saved_stdio(dup(1)); File saved_stdio(dup(1));
EXPECT_NE(-1, saved_stdio.fd()); EXPECT_NE(-1, saved_stdio.fd());
...@@ -1661,6 +1672,13 @@ TEST(ColorTest, PrintColored) { ...@@ -1661,6 +1672,13 @@ TEST(ColorTest, PrintColored) {
#endif #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> template <typename T>
std::string str(const T &value) { std::string str(const T &value) {
return fmt::str(fmt::Format("{0}") << value); return fmt::str(fmt::Format("{0}") << value);
...@@ -1672,13 +1690,6 @@ TEST(StrTest, Convert) { ...@@ -1672,13 +1690,6 @@ TEST(StrTest, Convert) {
EXPECT_EQ("2012-12-9", s); 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) { int main(int argc, char **argv) {
#ifdef _WIN32 #ifdef _WIN32
// Disable message boxes on assertion failures. // Disable message boxes on assertion failures.
......
...@@ -688,12 +688,13 @@ void fmt::BasicWriter<Char>::FormatParser::Format( ...@@ -688,12 +688,13 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
writer.buffer_.append(start, s); 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"; char escape[] = "\x1b[30m";
escape[3] = '0' + static_cast<char>(color_); escape[3] = '0' + static_cast<char>(color_);
std::fputs(escape, stdout); std::fputs(escape, file_);
std::fwrite(w.data(), 1, w.size(), stdout); std::fwrite(w.data(), 1, w.size(), file_);
std::fputs(RESET_COLOR, stdout); std::fputs(RESET_COLOR, file_);
} }
// Explicit instantiations for char. // 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