Parcourir la source

[trac901] The arg method without replacing

Michal 'vorner' Vaner il y a 14 ans
Parent
commit
db0ca2fbc6
2 fichiers modifiés avec 69 ajouts et 3 suppressions
  1. 13 3
      src/lib/log/log_formatter.h
  2. 56 0
      src/lib/log/tests/log_formatter_unittest.cc

+ 13 - 3
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>());
+        }
     }
 };
 

+ 56 - 0
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);
+}
+
 }