Просмотр исходного кода

Implemented XMLDocument class

- completed XMLDocument class
- added tests for XMLDocument class
Patrick-Laptop 4 лет назад
Родитель
Сommit
2bb5b3def5

+ 4 - 2
CMakeLists.txt

@@ -119,7 +119,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/base/Version.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/base/Version.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDocument.cpp)
 
 set(LIBRARY_SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/lib/tiny_xml_2/include/tinyxml2.h
@@ -165,7 +166,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLAttributeTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLNodeTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/base/VersionTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDeclarationTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDeclarationTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDocumentTest.cpp)
 
 ##########################################################
 # Build

+ 44 - 0
source/io/xml/XMLDocument.cpp

@@ -0,0 +1,44 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-30
+ * Changed:         2020-09-30
+ *
+ * */
+
+#include "XMLDocument.hpp"
+
+ls_std::XMLDocument::XMLDocument() : Class("XMLDocument")
+{}
+
+std::shared_ptr<ls_std::XMLDeclaration> ls_std::XMLDocument::getDeclaration()
+{
+  return this->declaration;
+}
+
+std::shared_ptr<ls_std::XMLNode> ls_std::XMLDocument::getRootElement()
+{
+  return this->rootElement;
+}
+
+void ls_std::XMLDocument::setDeclaration(const std::shared_ptr<ls_std::XMLDeclaration> &_declaration)
+{
+  this->declaration = _declaration;
+}
+
+void ls_std::XMLDocument::setRootElement(const std::shared_ptr<ls_std::XMLNode> &_root)
+{
+  this->rootElement = _root;
+}
+
+std::string ls_std::XMLDocument::toXML()
+{
+  std::string xmlString {};
+
+  if(this->declaration != nullptr) {
+    xmlString = this->declaration->toXML();
+  }
+
+  return xmlString + this->rootElement->toXML();
+}

+ 5 - 6
source/io/xml/XMLDocument.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-27
- * Changed:         2020-09-27
+ * Changed:         2020-09-29
  *
  * */
 
@@ -13,6 +13,7 @@
 #include <memory>
 #include "../../base/Class.hpp"
 #include "XMLNode.hpp"
+#include "XMLDeclaration.hpp"
 
 namespace ls_std {
   class XMLDocument : public Class {
@@ -21,18 +22,16 @@ namespace ls_std {
       XMLDocument();
       ~XMLDocument() = default;
 
-      std::string getHeader();
+      std::shared_ptr<ls_std::XMLDeclaration> getDeclaration();
       std::shared_ptr<ls_std::XMLNode> getRootElement();
-      void parse(const std::string& _xmlStream);
+      void setDeclaration(const std::shared_ptr<ls_std::XMLDeclaration>& _declaration);
       void setRootElement(const std::shared_ptr<ls_std::XMLNode>& _root);
       std::string toXML();
 
     private:
 
-      std::string header {};
+      std::shared_ptr<ls_std::XMLDeclaration> declaration {};
       std::shared_ptr<ls_std::XMLNode> rootElement {};
-
-      std::string _getHeader();
   };
 }
 

+ 75 - 0
test/cases/io/xml/XMLDocumentTest.cpp

@@ -0,0 +1,75 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-30
+ * Changed:         2020-09-30
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../source/io/xml/XMLDocument.hpp"
+#include "../../../TestDataFactory.hpp"
+
+namespace {
+  class XMLDocumentTest : public ::testing::Test {
+    protected:
+
+      XMLDocumentTest() = default;
+      ~XMLDocumentTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(XMLDocumentTest, getDeclaration)
+  {
+    ls_std::XMLDocument document {};
+    ASSERT_TRUE(document.getDeclaration() == nullptr);
+  }
+
+  TEST_F(XMLDocumentTest, getRootElement)
+  {
+    ls_std::XMLDocument document {};
+    ASSERT_TRUE(document.getRootElement() == nullptr);
+  }
+
+  TEST_F(XMLDocumentTest, setDeclaration)
+  {
+    ls_std::XMLDocument document {};
+    ASSERT_TRUE(document.getDeclaration() == nullptr);
+
+    ls_std::XMLDeclaration declaration {"1.0"};
+    document.setDeclaration(std::make_shared<ls_std::XMLDeclaration>(declaration));
+    ASSERT_TRUE(document.getDeclaration() != nullptr);
+    ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
+  }
+
+  TEST_F(XMLDocumentTest, setRootElement)
+  {
+    ls_std::XMLDocument document {};
+    ASSERT_TRUE(document.getRootElement() == nullptr);
+
+    ls_std::XMLDeclaration declaration {"1.0"};
+    document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
+    ASSERT_TRUE(document.getRootElement() != nullptr);
+    ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
+  }
+
+  TEST_F(XMLDocumentTest, toXML)
+  {
+    ls_std::XMLDocument document {};
+
+    ls_std::XMLDeclaration declaration {"1.0"};
+    declaration.setEncoding("UTF-8");
+    declaration.setStandalone("yes");
+    document.setDeclaration(std::make_shared<ls_std::XMLDeclaration>(declaration));
+
+    document.setRootElement(ls_std_test::TestDataFactory::createXMLContent());
+    std::string xmlStream = document.toXML();
+
+    ASSERT_TRUE(!xmlStream.empty());
+    std::string expectedXMLContent = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><dialog name="dungeon_001"><dialogUnit id="001"><text>Hello!</text></dialogUnit><dialogUnit id="002"><text>Hello again!</text></dialogUnit></dialog>)";
+    ASSERT_STREQ(expectedXMLContent.c_str(), xmlStream.c_str());
+  }
+}