Browse Source

Improve XmlAttribute class test coverage

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

+ 7 - 4
include/ls_std/io/xml/XmlAttribute.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-23
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
@@ -19,19 +19,22 @@ namespace ls_std
   {
     public:
 
-      explicit XmlAttribute(std::string _name);
+      explicit XmlAttribute(const std::string& _name);
       ~XmlAttribute() override = default;
 
       std::string getName();
       std::string getValue();
-      void setName(std::string _name);
-      void setValue(std::string _value);
+      void setName(const std::string& _name);
+      void setValue(const std::string& _value);
       std::string toXml();
 
     private:
 
       std::string name{};
       std::string value{};
+
+      void _assignName(const std::string& _name);
+      void _assignValue(const std::string& _value);
   };
 }
 

+ 30 - 9
source/ls_std/io/xml/XmlAttribute.cpp

@@ -3,16 +3,17 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-23
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
 #include <ls_std/io/xml/XmlAttribute.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
 
-ls_std::XmlAttribute::XmlAttribute(std::string _name)
-    : ls_std::Class("XmlAttribute"),
-      name(std::move(_name))
-{}
+ls_std::XmlAttribute::XmlAttribute(const std::string& _name) : ls_std::Class("XmlAttribute")
+{
+  this->_assignName(_name);
+}
 
 std::string ls_std::XmlAttribute::getName()
 {
@@ -24,17 +25,37 @@ std::string ls_std::XmlAttribute::getValue()
   return this->value;
 }
 
-void ls_std::XmlAttribute::setName(std::string _name)
+void ls_std::XmlAttribute::setName(const std::string& _name)
 {
-  this->name = std::move(_name);
+  this->_assignName(_name);
 }
 
-void ls_std::XmlAttribute::setValue(std::string _value)
+void ls_std::XmlAttribute::setValue(const std::string& _value)
 {
-  this->value = std::move(_value);
+  this->_assignValue(_value);
 }
 
 std::string ls_std::XmlAttribute::toXml()
 {
   return this->name + "=\"" + this->value + "\"";
 }
+
+void ls_std::XmlAttribute::_assignName(const std::string &_name)
+{
+  if (_name.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->name = _name;
+}
+
+void ls_std::XmlAttribute::_assignValue(const std::string &_value)
+{
+  if (_value.empty())
+  {
+    throw ls_std::IllegalArgumentException{};
+  }
+
+  this->value = _value;
+}

+ 45 - 1
test/cases/io/xml/XmlAttributeTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-23
- * Changed:         2021-05-02
+ * Changed:         2021-07-16
  *
  * */
 
@@ -26,6 +26,20 @@ namespace
       {}
   };
 
+  TEST_F(XmlAttributeTest, constructor_empty_name)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::XmlAttribute attribute{""};
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(XmlAttributeTest, getName)
   {
     ls_std::XmlAttribute attribute{"id"};
@@ -46,6 +60,21 @@ namespace
     ASSERT_STREQ("id2", attribute.getName().c_str());
   }
 
+  TEST_F(XmlAttributeTest, setName_empty_name)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::XmlAttribute attribute{"id"};
+                     attribute.setName("");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(XmlAttributeTest, setValue)
   {
     ls_std::XmlAttribute attribute{"id"};
@@ -54,6 +83,21 @@ namespace
     ASSERT_STREQ("some_content", attribute.getValue().c_str());
   }
 
+  TEST_F(XmlAttributeTest, setValue_empty_value)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::XmlAttribute attribute{"id"};
+                     attribute.setValue("");
+                   }
+                   catch (const ls_std::IllegalArgumentException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArgumentException);
+  }
+
   TEST_F(XmlAttributeTest, toXml)
   {
     ls_std::XmlAttribute attribute{"id"};