فهرست منبع

Convert XmlParseParameter into a class

Patrick-Christopher Mattulat 2 سال پیش
والد
کامیت
c789ff5541

+ 2 - 0
CMakeLists.txt

@@ -188,6 +188,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlDeclaration.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlDocument.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlNode.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlParseParameter.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlParser.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/xml/XmlReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/File.cpp
@@ -265,6 +266,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlDeclarationTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlDocumentTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlNodeTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlParseParameterTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlParserTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlParserTestWrapperTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/xml/XmlReaderTest.cpp

+ 13 - 1
include/ls-std/io/xml/XmlParseParameter.hpp

@@ -16,12 +16,24 @@
 
 namespace ls::std::io
 {
-  struct XmlParseParameter // TODO: turn to class, rather than keeping it as struct
+  class XmlParseParameter
   {
     public:
 
+      XmlParseParameter();
+      ~XmlParseParameter();
+
+      [[nodiscard]] uint8_t getLevel() const;
+      [[nodiscard]] ::std::shared_ptr<ls::std::io::XmlNode> getNode();
+      void setLevel(const uint8_t &_level);
+      void setNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_node);
+
+    private:
+
       uint8_t level{};
       ::std::shared_ptr<ls::std::io::XmlNode> node{};
+
+      void _setNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_node);
   };
 }
 

+ 45 - 0
source/ls-std/io/xml/XmlParseParameter.cpp

@@ -0,0 +1,45 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-05
+* Changed:         2023-02-05
+*
+* */
+
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
+#include <ls-std/io/xml/XmlParseParameter.hpp>
+
+ls::std::io::XmlParseParameter::XmlParseParameter() = default;
+
+ls::std::io::XmlParseParameter::~XmlParseParameter() = default;
+
+uint8_t ls::std::io::XmlParseParameter::getLevel() const
+{
+  return this->level;
+}
+
+::std::shared_ptr<ls::std::io::XmlNode> ls::std::io::XmlParseParameter::getNode()
+{
+  return this->node;
+}
+
+void ls::std::io::XmlParseParameter::setLevel(const uint8_t &_level)
+{
+  this->level = _level;
+}
+
+void ls::std::io::XmlParseParameter::setNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_node)
+{
+  this->_setNode(_node);
+}
+
+void ls::std::io::XmlParseParameter::_setNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_node)
+{
+  if (_node == nullptr)
+  {
+    throw ls::std::core::IllegalArgumentException{};
+  }
+
+  this->node = _node;
+}

+ 10 - 10
source/ls-std/io/xml/XmlParser.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-11-26
- * Changed:         2023-02-04
+ * Changed:         2023-02-05
  *
  * */
 
@@ -218,7 +218,7 @@ void ls::std::io::XmlParser::_mergeNodes()
     this->maxLevel -= 1;
   }
 
-  this->document->setRootElement(this->parseParameters.front().node);
+  this->document->setRootElement(this->parseParameters.front().getNode());
 }
 
 void ls::std::io::XmlParser::_mergeChildrenToParentNode(const ::std::shared_ptr<ls::std::io::XmlNode> &_parent, ::std::list<ls::std::io::XmlParseParameter>::iterator &_iterator, uint8_t _parentLevel)
@@ -233,12 +233,12 @@ void ls::std::io::XmlParser::_mergeChildrenToParentNode(const ::std::shared_ptr<
     }
     else
     {
-      if (_iterator->level == this->maxLevel)
+      if (_iterator->getLevel() == this->maxLevel)
       {
-        _parent->addChildToEnd(_iterator->node);
+        _parent->addChildToEnd(_iterator->getNode());
       }
     }
-  } while (_iterator->level > _parentLevel);
+  } while (_iterator->getLevel() > _parentLevel);
 }
 
 void ls::std::io::XmlParser::_mergeNodesOnCurrentLevel()
@@ -248,9 +248,9 @@ void ls::std::io::XmlParser::_mergeNodesOnCurrentLevel()
 
   while (iterator != this->parseParameters.end())
   {
-    if (iterator->level == parentLevel)
+    if (iterator->getLevel() == parentLevel)
     {
-      this->_mergeChildrenToParentNode(iterator->node, iterator, parentLevel);
+      this->_mergeChildrenToParentNode(iterator->getNode(), iterator, parentLevel);
     }
     else
     {
@@ -369,8 +369,8 @@ size_t ls::std::io::XmlParser::_parseOpeningTag(const ls::std::core::type::byte_
   {
     ::std::shared_ptr<ls::std::io::XmlNode> node = ls::std::io::XmlParser::_createNode(ls::std::io::XmlParser::_parseAttributes(tagString), ls::std::io::XmlParser::_parseTagName(tagString));
 
-    singleParseParameter.level = this->currentLevel;
-    singleParseParameter.node = node;
+    singleParseParameter.setLevel(this->currentLevel);
+    singleParseParameter.setNode(node);
     this->parseParameters.push_back(singleParseParameter);
 
     if (!ls::std::io::XmlParser::_endsWith(tagString, "/>"))
@@ -398,7 +398,7 @@ ls::std::core::type::byte_field ls::std::io::XmlParser::_parseTagName(const ls::
 size_t ls::std::io::XmlParser::_parseValue(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
 {
   ls::std::core::type::byte_field value = _data.substr(_index, _data.substr(_index).find('<'));
-  this->parseParameters.back().node->setValue(value);
+  this->parseParameters.back().getNode()->setValue(value);
 
   return _index + (value.size() - 1);
 }

+ 79 - 0
test/cases/io/xml/XmlParseParameterTest.cpp

@@ -0,0 +1,79 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-05
+* Changed:         2023-02-05
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-core.hpp>
+#include <ls-std/ls-std-io.hpp>
+
+using namespace ls::std::core;
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class XmlParseParameterTest : public ::testing::Test
+  {
+    protected:
+
+      XmlParseParameterTest() = default;
+      ~XmlParseParameterTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(XmlParseParameterTest, getLevel)
+  {
+    XmlParseParameter parameter{};
+    ASSERT_EQ(0, parameter.getLevel());
+  }
+
+  TEST_F(XmlParseParameterTest, getNode)
+  {
+    XmlParseParameter parameter{};
+    ASSERT_TRUE(parameter.getNode() == nullptr);
+  }
+
+  TEST_F(XmlParseParameterTest, setLevel)
+  {
+    XmlParseParameter parameter{};
+    parameter.setLevel(3);
+
+    ASSERT_EQ(3, parameter.getLevel());
+  }
+
+  TEST_F(XmlParseParameterTest, setNode)
+  {
+    XmlParseParameter parameter{};
+    parameter.setNode(make_shared<XmlNode>("node"));
+
+    ASSERT_TRUE(parameter.getNode() != nullptr);
+  }
+
+  TEST_F(XmlParseParameterTest, setNode_no_reference)
+  {
+    XmlParseParameter parameter{};
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            parameter.setNode(nullptr);
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+}

+ 1 - 1
test/cases/io/xml/XmlParserTest.cpp

@@ -51,7 +51,7 @@ namespace
         {
           try
           {
-            XmlParser xmlParser{document};
+            XmlParser xmlParser = XmlParser{document};
           }
           catch (const IllegalArgumentException &_exception)
           {