Browse Source

Implemented XMLDeclaration class

- completed implementation of XMLDeclaration class
- added tests for XMLDeclaration class
Patrick-Laptop 4 years ago
parent
commit
d90a7ea813

+ 4 - 2
CMakeLists.txt

@@ -118,7 +118,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDocument.hpp
         ${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/base/Version.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.cpp)
 
 set(LIBRARY_SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/lib/tiny_xml_2/include/tinyxml2.h
@@ -163,7 +164,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/xml/logic/SerializableXMLStateTest.cpp
         ${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/base/VersionTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XMLDeclarationTest.cpp)
 
 ##########################################################
 # Build

+ 68 - 0
source/io/xml/XMLDeclaration.cpp

@@ -0,0 +1,68 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-29
+ * Changed:         2020-09-29
+ *
+ * */
+
+#include "XMLDeclaration.hpp"
+
+ls_std::XMLDeclaration::XMLDeclaration(std::string _version) :
+Class("XMLDeclaration")
+{
+  this->version.setValue(std::move(_version));
+}
+
+std::string ls_std::XMLDeclaration::getEncoding()
+{
+  return this->encoding.getValue();
+}
+
+std::string ls_std::XMLDeclaration::getStandalone()
+{
+  return this->standalone.getValue();
+}
+
+std::string ls_std::XMLDeclaration::getVersion()
+{
+  return this->version.getValue();
+}
+
+void ls_std::XMLDeclaration::setEncoding(std::string _encoding)
+{
+  this->encoding.setValue(std::move(_encoding));
+}
+
+void ls_std::XMLDeclaration::setStandalone(std::string _standalone)
+{
+  this->standalone.setValue(std::move(_standalone));
+}
+
+void ls_std::XMLDeclaration::setVersion(std::string _version)
+{
+  this->version.setValue(std::move(_version));
+}
+
+std::string ls_std::XMLDeclaration::toXML()
+{
+  std::string declaration = "<?xml";
+
+  declaration += ls_std::XMLDeclaration::_toXMLAttribute(this->version);
+  declaration += ls_std::XMLDeclaration::_toXMLAttribute(this->encoding);
+  declaration += ls_std::XMLDeclaration::_toXMLAttribute(this->standalone);
+
+  return declaration + " ?>";
+}
+
+std::string ls_std::XMLDeclaration::_toXMLAttribute(ls_std::XMLAttribute _attribute)
+{
+  std::string xmlString {};
+
+  if(!_attribute.getValue().empty()) {
+    xmlString = " " + _attribute.toXML();
+  }
+
+  return xmlString;
+}

+ 16 - 3
source/io/xml/XMLDeclaration.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
  *
  * */
 
@@ -11,17 +11,30 @@
 #define LS_STD_XML_DECLARATION_HPP
 
 #include "../../base/Class.hpp"
+#include "XMLAttribute.hpp"
 
 namespace ls_std {
   class XMLDeclaration : public Class {
     public:
 
-      XMLDeclaration();
+      explicit XMLDeclaration(std::string _version);
       ~XMLDeclaration() = default;
 
+      std::string getEncoding();
+      std::string getStandalone();
+      std::string getVersion();
+      void setEncoding(std::string _encoding);
+      void setStandalone(std::string _standalone);
+      void setVersion(std::string _version);
+      std::string toXML();
+
     private:
 
-      std::string version {};
+      ls_std::XMLAttribute encoding {"encoding"};
+      ls_std::XMLAttribute standalone {"standalone"};
+      ls_std::XMLAttribute version {"version"};
+
+      static std::string _toXMLAttribute(XMLAttribute _attribute);
   };
 }
 

+ 85 - 0
test/cases/io/xml/XMLDeclarationTest.cpp

@@ -0,0 +1,85 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-29
+ * Changed:         2020-09-29
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../source/io/xml/XMLDeclaration.hpp"
+
+namespace {
+  class XMLDeclarationTest : public ::testing::Test {
+    protected:
+
+      XMLDeclarationTest() = default;
+      ~XMLDeclarationTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(XMLDeclarationTest, getEncoding)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+    ASSERT_TRUE(declaration.getEncoding().empty());
+  }
+
+  TEST_F(XMLDeclarationTest, getStandalone)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+    ASSERT_TRUE(declaration.getStandalone().empty());
+  }
+
+  TEST_F(XMLDeclarationTest, getVersion)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+    ASSERT_FALSE(declaration.getVersion().empty());
+    ASSERT_STREQ("1.0", declaration.getVersion().c_str());
+  }
+
+  TEST_F(XMLDeclarationTest, setEncoding)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+
+    ASSERT_TRUE(declaration.getEncoding().empty());
+
+    declaration.setEncoding("iso-8859-1");
+    ASSERT_STREQ("iso-8859-1", declaration.getEncoding().c_str());
+  }
+
+  TEST_F(XMLDeclarationTest, setStandalone)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+
+    ASSERT_TRUE(declaration.getStandalone().empty());
+
+    declaration.setStandalone("no");
+    ASSERT_STREQ("no", declaration.getStandalone().c_str());
+  }
+
+  TEST_F(XMLDeclarationTest, setVersion)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+
+    ASSERT_FALSE(declaration.getVersion().empty());
+    ASSERT_STREQ("1.0", declaration.getVersion().c_str());
+
+    declaration.setVersion("1.1");
+    ASSERT_STREQ("1.1", declaration.getVersion().c_str());
+  }
+
+  TEST_F(XMLDeclarationTest, toXML)
+  {
+    ls_std::XMLDeclaration declaration {"1.0"};
+    ASSERT_STREQ(R"(<?xml version="1.0" ?>)", declaration.toXML().c_str());
+
+    declaration.setStandalone("yes");
+    ASSERT_STREQ(R"(<?xml version="1.0" standalone="yes" ?>)", declaration.toXML().c_str());
+
+    declaration.setEncoding("UTF-8");
+    ASSERT_STREQ(R"(<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>)", declaration.toXML().c_str());
+  }
+}