[pp] Support 'MultiLineText' append (#598)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 13 Jul 2018 05:44:03 +0000 (14:44 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 13 Jul 2018 05:44:03 +0000 (14:44 +0900)
'LinearDocument' currently supports 'LinearDocument' append, but does not support
generic 'MultiLineText' append.

This commit revises 'LinearDocument' to support generic 'MultiLineText'
append.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/pp/include/pp/LinearDocument.h
contrib/pp/src/LinearDocument.test.cpp

index 3eb1d6a..42336c7 100644 (file)
@@ -8,6 +8,8 @@
 #include <vector>
 #include <string>
 
+#include <type_traits>
+
 namespace pp
 {
 
@@ -41,6 +43,17 @@ public:
 
 public:
   void append(const std::string &line);
+
+  template <typename Derived>
+  typename std::enable_if<std::is_base_of<MultiLineText, Derived>::value>::type
+  append(const Derived &txt)
+  {
+    for (uint32_t n = 0; n < txt.lines(); ++n)
+    {
+      append(txt.line(n));
+    }
+  }
+
   template <typename... Args> void append(const Args &... args) { append(fmt(args...)); }
 
 public:
index f424dd9..a025d69 100644 (file)
@@ -65,6 +65,38 @@ TEST(LINEAR_DOCUMENT, reverse_append)
   ASSERT_EQ(doc.line(2), "A");
 }
 
+struct TwoLineDocument final : public pp::MultiLineText
+{
+  uint32_t lines(void) const override { return 2; }
+
+  const std::string &line(uint32_t n) const override { return _lines[n]; }
+
+  std::string _lines[2];
+};
+
+TEST(LINEAR_DOCUMENT, append_multi_line_text)
+{
+  pp::LinearDocument doc;
+  TwoLineDocument sub;
+
+  sub._lines[0] = "B";
+  sub._lines[1] = "  C";
+
+  doc.append("A");
+  doc.indent();
+
+  doc.append(sub);
+  doc.unindent();
+  doc.append("D");
+
+  ASSERT_EQ(doc.lines(), 4);
+
+  ASSERT_EQ(doc.line(0), "A");
+  ASSERT_EQ(doc.line(1), "  B");
+  ASSERT_EQ(doc.line(2), "    C");
+  ASSERT_EQ(doc.line(3), "D");
+}
+
 TEST(LINEAR_DOCUMENT, document_append)
 {
   pp::LinearDocument doc{pp::LinearDocument::Direction::Forward};