Browse Source

Completed XMLNode class

- added "toXML" method
- added value functionality
- extended tests for XMLNode class
Patrick 4 years ago
parent
commit
8a303335d6
3 changed files with 126 additions and 0 deletions
  1. 82 0
      source/io/xml/XMLNode.cpp
  2. 9 0
      source/io/xml/XMLNode.hpp
  3. 35 0
      test/cases/io/xml/XMLNodeTest.cpp

+ 82 - 0
source/io/xml/XMLNode.cpp

@@ -142,6 +142,11 @@ bool ls_std::XMLNode::addChildToEnd(const std::shared_ptr<XMLNode>& _child)
   return added;
 }
 
+void ls_std::XMLNode::clearValue()
+{
+  this->value.clear();
+}
+
 std::list<std::shared_ptr<ls_std::XMLAttribute>> ls_std::XMLNode::getAttributes()
 {
   return this->attributes;
@@ -170,6 +175,11 @@ std::string ls_std::XMLNode::getName()
   return this->name;
 }
 
+std::string ls_std::XMLNode::getValue()
+{
+  return this->value;
+}
+
 bool ls_std::XMLNode::hasAttribute(const std::string &_name)
 {
   return this->_hasAttribute(_name);
@@ -218,6 +228,25 @@ void ls_std::XMLNode::setName(std::string _name)
   this->name = std::move(_name);
 }
 
+void ls_std::XMLNode::setValue(std::string _value)
+{
+  this->value = std::move(_value);
+}
+
+std::string ls_std::XMLNode::toXML()
+{
+  std::string xmlStream {};
+
+  xmlStream += this->_toXMLOpenTag();
+  xmlStream += this->_toXMLAttributes();
+  xmlStream += this->_toXMLOpenTagClose();
+  xmlStream += this->value;
+  xmlStream += this->_toXMLChildren();
+  xmlStream += this->_toXMLCloseTag();
+
+  return xmlStream;
+}
+
 bool ls_std::XMLNode::_hasAttribute(const std::string &_name)
 {
   bool exists {};
@@ -250,3 +279,56 @@ bool ls_std::XMLNode::_hasChild(const std::string &_name)
 
   return exists;
 }
+
+std::string ls_std::XMLNode::_toXMLAttributes()
+{
+  std::string stream {};
+
+  for(const auto& _attribute : this->attributes) {
+    stream += " " + _attribute->toXML();
+  }
+
+  return stream;
+}
+
+std::string ls_std::XMLNode::_toXMLChildren()
+{
+  std::string stream {};
+
+  if(this->value.empty()) {
+    for(const auto& _child : this->children) {
+      stream += _child->toXML();
+    }
+  }
+
+  return stream;
+}
+
+std::string ls_std::XMLNode::_toXMLCloseTag()
+{
+  std::string stream {};
+
+  if(!this->children.empty() || !this->value.empty()) {
+    stream = "</" + this->name + ">";
+  }
+
+  return stream;
+}
+
+std::string ls_std::XMLNode::_toXMLOpenTag()
+{
+  return "<" + this->name;
+}
+
+std::string ls_std::XMLNode::_toXMLOpenTagClose()
+{
+  std::string stream {};
+
+  if(this->children.empty() && this->value.empty()) {
+    stream = "/>";
+  } else {
+    stream = ">";
+  }
+
+  return stream;
+}

+ 9 - 0
source/io/xml/XMLNode.hpp

@@ -30,10 +30,12 @@ namespace ls_std {
       bool addChildBefore(const std::shared_ptr<XMLNode>& _child, const std::shared_ptr<XMLNode>& _search);
       bool addChildToBeginning(const std::shared_ptr<XMLNode>& _child);
       bool addChildToEnd(const std::shared_ptr<XMLNode>& _child);
+      void clearValue();
       std::list<std::shared_ptr<XMLAttribute>> getAttributes();
       std::list<std::shared_ptr<XMLNode>> getChildren();
       std::list<std::shared_ptr<XMLNode>> getChildren(const std::string& _name);
       std::string getName();
+      std::string getValue();
       bool hasAttribute(const std::string& _name);
       bool hasChild(const std::string& _name);
       bool hasChild(const std::shared_ptr<XMLNode>& _child);
@@ -42,6 +44,7 @@ namespace ls_std {
       void removeFirstChild();
       void removeLastChild();
       void setName(std::string _name);
+      void setValue(std::string _value);
       std::string toXML();
 
     private:
@@ -49,10 +52,16 @@ namespace ls_std {
       std::list<std::shared_ptr<XMLAttribute>> attributes {};
       std::list<std::shared_ptr<XMLNode>> children {};
       std::string name {};
+      std::string value {};
 
       bool _hasAttribute(const std::string& _name);
       bool _hasChild(const std::shared_ptr<XMLNode>& _child);
       bool _hasChild(const std::string& _name);
+      std::string _toXMLAttributes();
+      std::string _toXMLChildren();
+      std::string _toXMLCloseTag();
+      std::string _toXMLOpenTag();
+      std::string _toXMLOpenTagClose();
   };
 }
 

+ 35 - 0
test/cases/io/xml/XMLNodeTest.cpp

@@ -9,6 +9,7 @@
 
 #include <gtest/gtest.h>
 #include "../../../../source/io/xml/XMLNode.hpp"
+#include "../../../TestDataFactory.hpp"
 
 namespace {
   class XMLNodeTest : public ::testing::Test {
@@ -338,6 +339,17 @@ namespace {
     ASSERT_STREQ("dialogNodeA", currentNode->getName().c_str());
   }
 
+  TEST_F(XMLNodeTest, clearValue)
+  {
+    ls_std::XMLNode dialogNode {"dialog"};
+    dialogNode.setValue("Something");
+    ASSERT_STREQ("Something", dialogNode.getValue().c_str());
+
+    dialogNode.clearValue();
+    ASSERT_TRUE(dialogNode.getValue().empty());
+    ASSERT_STREQ("", dialogNode.getValue().c_str());
+  }
+
   TEST_F(XMLNodeTest, getAttributes)
   {
     ls_std::XMLNode dialogNode {"dialog"};
@@ -378,6 +390,12 @@ namespace {
     ASSERT_STREQ("dialog", dialogNode.getName().c_str());
   }
 
+  TEST_F(XMLNodeTest, getValue)
+  {
+    ls_std::XMLNode dialogNode {"dialog"};
+    ASSERT_TRUE(dialogNode.getValue().empty());
+  }
+
   TEST_F(XMLNodeTest, hasAttribute)
   {
     ls_std::XMLNode dialogNode {"dialogNode"};
@@ -604,4 +622,21 @@ namespace {
 
     ASSERT_STREQ("dialog2", dialogNode.getName().c_str());
   }
+
+  TEST_F(XMLNodeTest, setValue)
+  {
+    ls_std::XMLNode dialogNode {"dialog"};
+    dialogNode.setValue("Something written");
+
+    ASSERT_STREQ("Something written", dialogNode.getValue().c_str());
+  }
+
+  TEST_F(XMLNodeTest, toXML)
+  {
+    auto root = ls_std_test::TestDataFactory::createXMLContent();
+    std::string xmlContent = root->toXML();
+    std::string expectedXMLContent = R"("<dialog name="dungeon_001"><dialogUnit id="001"><text>Hello!</text></dialogUnit><dialogUnit id="002"><text>Hello again!</text></dialogUnit></dialog>")";
+
+    ASSERT_TRUE(!xmlContent.empty());
+  }
 }