BIND 10 trac901, updated. db0ca2fbc64b390f261b5e36938c736321b171f9 [trac901] The arg method without replacing
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed May 4 14:51:51 UTC 2011
The branch, trac901 has been updated
via db0ca2fbc64b390f261b5e36938c736321b171f9 (commit)
from 2aca19e705f02400a6b213ef84fa81c86dff0375 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit db0ca2fbc64b390f261b5e36938c736321b171f9
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date: Wed May 4 16:51:27 2011 +0200
[trac901] The arg method without replacing
-----------------------------------------------------------------------
Summary of changes:
src/lib/log/log_formatter.h | 16 ++++++-
src/lib/log/tests/log_formatter_unittest.cc | 56 +++++++++++++++++++++++++++
2 files changed, 69 insertions(+), 3 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/log/log_formatter.h b/src/lib/log/log_formatter.h
index df42feb..6f3af2a 100644
--- a/src/lib/log/log_formatter.h
+++ b/src/lib/log/log_formatter.h
@@ -64,6 +64,8 @@ private:
const unsigned nextPlaceholder_;
/// \brief Should we do output?
bool active_;
+ Formatter& operator =(const Formatter& other);
+ Formatter(const Formatter& other);
public:
/// \brief Constructor of "active" formatter
///
@@ -77,7 +79,7 @@ public:
/// are used internally in the chain.
/// \param logger The logger where the final output will go.
Formatter(const char* prefix, const std::string& message,
- const unsigned& nextPlaceholder, Logger& logger) :
+ const unsigned nextPlaceholder, Logger& logger) :
logger_(&logger), prefix_(prefix), message_(message),
nextPlaceholder_(nextPlaceholder), active_(true)
{
@@ -103,8 +105,16 @@ public:
/// Replaces another placeholder and returns a new formatter with it.
/// Deactivates the current formatter. In case the formatter is not active,
/// only produces another inactive formatter.
- template<class Arg> Formatter arg(const Arg& arg) {
-
+ ///
+ /// \param arg The argument to place into the placeholder.
+ template<class Arg> Formatter arg(const Arg&) {
+ if (active_) {
+ active_ = false;
+ return (Formatter<Logger>(prefix_, message_, nextPlaceholder_ + 1,
+ *logger_));
+ } else {
+ return (Formatter<Logger>());
+ }
}
};
diff --git a/src/lib/log/tests/log_formatter_unittest.cc b/src/lib/log/tests/log_formatter_unittest.cc
index e3bf369..efc8bea 100644
--- a/src/lib/log/tests/log_formatter_unittest.cc
+++ b/src/lib/log/tests/log_formatter_unittest.cc
@@ -49,4 +49,60 @@ TEST_F(FormatterTest, active) {
EXPECT_EQ("Text of message", outputs[0].second);
}
+// No output even when we have an arg on the inactive formatter
+TEST_F(FormatterTest, inactiveArg) {
+ Formatter().arg("Hello");
+ EXPECT_EQ(0, outputs.size());
+}
+
+// Create an active formatter and replace a placeholder with string
+TEST_F(FormatterTest, stringArg) {
+ {
+ SCOPED_TRACE("C++ string");
+ Formatter("TEST", "Hello %1", 1, *this).arg(string("World"));
+ ASSERT_LE(1, outputs.size());
+ EXPECT_EQ(1, outputs.size());
+ EXPECT_STREQ("TEST", outputs[0].first);
+ EXPECT_EQ("Hello World", outputs[0].second);
+ }
+ {
+ SCOPED_TRACE("C++ string");
+ Formatter("TEST", "Hello %1", 1, *this).arg(string("Internet"));
+ ASSERT_LE(2, outputs.size());
+ EXPECT_EQ(2, outputs.size());
+ EXPECT_STREQ("TEST", outputs[1].first);
+ EXPECT_EQ("Hello Internet", outputs[1].second);
+ }
+}
+
+// Can convert to string
+TEST_F(FormatterTest, intArg) {
+ Formatter("TEST", "The answer is %1", 1, *this).arg(42);
+ ASSERT_LE(1, outputs.size());
+ EXPECT_EQ(1, outputs.size());
+ EXPECT_STREQ("TEST", outputs[0].first);
+ EXPECT_EQ("The answer is 42", outputs[0].second);
+}
+
+// Can use multiple arguments at different places
+TEST_F(FormatterTest, multiArg) {
+ Formatter("TEST", "The %2 are %1", 1, *this).arg("switched").
+ arg("arguments");
+ ASSERT_LE(1, outputs.size());
+ EXPECT_EQ(1, outputs.size());
+ EXPECT_STREQ("TEST", outputs[0].first);
+ EXPECT_EQ("The arguments are switched", outputs[0].second);
+}
+
+// Can survive and complains if placeholder is missing
+TEST_F(FormatterTest, missingPlace) {
+ Formatter("TEST", "Missing the first %2", 1, *this).arg("missing").
+ arg("argument");
+ ASSERT_LE(1, outputs.size());
+ EXPECT_EQ(1, outputs.size());
+ EXPECT_STREQ("TEST", outputs[0].first);
+ EXPECT_EQ("Missing the first argument "
+ "@@Missing placeholder %1 for 'missing'@@", outputs[0].second);
+}
+
}
More information about the bind10-changes
mailing list