From 4f96062c5a66a61b417caf116c0154f22e388247 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 12 Jan 2013 10:08:51 -0800 Subject: [PATCH] Add tests. --- format_test.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/format_test.cc b/format_test.cc index 9f9dae41..40e575c8 100644 --- a/format_test.cc +++ b/format_test.cc @@ -854,10 +854,18 @@ class Date { public: Date(int year, int month, int day) : year_(year), month_(month), day_(day) {} + int year() const { return year_; } + int month() const { return month_; } + int day() const { return day_; } + friend std::ostream &operator<<(std::ostream &os, const Date &d) { os << d.year_ << '-' << d.month_ << '-' << d.day_; return os; } + + friend BasicFormatter &operator<<(BasicFormatter &f, const Date &d) { + return f << d.year_ << '-' << d.month_ << '-' << d.day_; + } }; TEST(FormatterTest, FormatUsingIOStreams) { @@ -910,6 +918,9 @@ TEST(FormatterTest, FormatterAppend) { } TEST(FormatterTest, FormatterExamples) { + std::string message = str(Format("The answer is {}") << 42); + EXPECT_EQ("The answer is 42", message); + EXPECT_EQ("42", str(Format("{}") << 42)); EXPECT_EQ("42", str(Format(std::string("{}")) << 42)); EXPECT_EQ("42", str(Format(Format("{{}}")) << 42)); @@ -1071,6 +1082,21 @@ TEST(StrTest, hexu) { EXPECT_EQ("BABE", f.str()); } +class ISO8601DateFormatter { + const Date *date_; + +public: + ISO8601DateFormatter(const Date &d) : date_(&d) {} + + friend BasicFormatter &operator<<( + BasicFormatter &f, const ISO8601DateFormatter &d) { + return f << pad(d.date_->year(), 4, '0') << '-' + << pad(d.date_->month(), 2, '0') << '-' << pad(d.date_->day(), 2, '0'); + } +}; + +ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); } + TEST(StrTest, pad) { BasicFormatter f; f << pad(hex(0xbeef), 8); @@ -1078,6 +1104,12 @@ TEST(StrTest, pad) { f.Clear(); f << pad(42, 5, '0'); EXPECT_EQ("00042", f.str()); + f.Clear(); + f << Date(2012, 12, 9); + EXPECT_EQ("2012-12-9", f.str()); + f.Clear(); + f << iso8601(Date(2012, 1, 9)); + EXPECT_EQ("2012-01-09", f.str()); } template