Browse Source

Improve XmlDocument class test coverage

- add missing test cases
Patrick-Christopher Mattulat 2 years ago
parent
commit
c2135c2e7d

+ 5 - 2
include/ls_std/io/xml/XmlDocument.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-27
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
@@ -27,13 +27,16 @@ namespace ls_std
       std::shared_ptr<ls_std::XmlDeclaration> getDeclaration();
       std::shared_ptr<ls_std::XmlNode> getRootElement();
       void setDeclaration(const std::shared_ptr<ls_std::XmlDeclaration> &_declaration);
-      void setRootElement(const std::shared_ptr<ls_std::XmlNode> &_root);
+      void setRootElement(const std::shared_ptr<ls_std::XmlNode> &_rootElement);
       std::string toXml();
 
     private:
 
       std::shared_ptr<ls_std::XmlDeclaration> declaration{};
       std::shared_ptr<ls_std::XmlNode> rootElement{};
+
+      void _assignDeclaration(const std::shared_ptr<ls_std::XmlDeclaration> &_declaration);
+      void _assignRootElement(const std::shared_ptr<ls_std::XmlNode> &_rootElement);
   };
 }
 

+ 25 - 4
source/ls_std/io/xml/XmlDocument.cpp

@@ -3,11 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-30
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
 #include <ls_std/io/xml/XmlDocument.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
 
 ls_std::XmlDocument::XmlDocument() : ls_std::Class("XmlDocument")
 {}
@@ -24,12 +25,12 @@ std::shared_ptr<ls_std::XmlNode> ls_std::XmlDocument::getRootElement()
 
 void ls_std::XmlDocument::setDeclaration(const std::shared_ptr<ls_std::XmlDeclaration> &_declaration)
 {
-  this->declaration = _declaration;
+  this->_assignDeclaration(_declaration);
 }
 
-void ls_std::XmlDocument::setRootElement(const std::shared_ptr<ls_std::XmlNode> &_root)
+void ls_std::XmlDocument::setRootElement(const std::shared_ptr<ls_std::XmlNode> &_rootElement)
 {
-  this->rootElement = _root;
+  this->_assignRootElement(_rootElement);
 }
 
 std::string ls_std::XmlDocument::toXml()
@@ -48,3 +49,23 @@ std::string ls_std::XmlDocument::toXml()
 
   return xmlString + this->rootElement->toXml();
 }
+
+void ls_std::XmlDocument::_assignDeclaration(const std::shared_ptr<ls_std::XmlDeclaration> &_declaration)
+{
+  if (_declaration == nullptr)
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->declaration = _declaration;
+}
+
+void ls_std::XmlDocument::_assignRootElement(const std::shared_ptr<ls_std::XmlNode> &_rootElement)
+{
+  if (_rootElement == nullptr)
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->rootElement = _rootElement;
+}

+ 33 - 1
test/cases/io/xml/XmlDocumentTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-30
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
@@ -50,6 +50,22 @@ namespace
     ASSERT_STREQ("1.0", document.getDeclaration()->getVersion().c_str());
   }
 
+  TEST_F(XmlDocumentTest, setDeclaration_no_reference)
+  {
+    ls_std::XmlDocument document{};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     document.setDeclaration(nullptr);
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(XmlDocumentTest, setRootElement)
   {
     ls_std::XmlDocument document{};
@@ -61,6 +77,22 @@ namespace
     ASSERT_STREQ("dialog", document.getRootElement()->getName().c_str());
   }
 
+  TEST_F(XmlDocumentTest, setRootElement_no_reference)
+  {
+    ls_std::XmlDocument document{};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     document.setRootElement(nullptr);
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(XmlDocumentTest, toXml)
   {
     ls_std::XmlDocument document{};