瀏覽代碼

Address SonarLint findings in XmlNode class

Patrick-Christopher Mattulat 1 年之前
父節點
當前提交
63c8feb14e
共有 3 個文件被更改,包括 38 次插入38 次删除
  1. 9 9
      include/ls-std/io/xml/XmlNode.hpp
  2. 12 12
      source/ls-std/io/xml/XmlNode.cpp
  3. 17 17
      test/cases/io/xml/XmlNodeTest.cpp

+ 9 - 9
include/ls-std/io/xml/XmlNode.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2023-02-22
+ * Changed:         2023-05-16
  *
  * */
 
@@ -34,11 +34,11 @@ namespace ls::std::io
       bool addChildToBeginning(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);                                                    // nodiscard is optional here
       bool addChildToEnd(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);                                                          // nodiscard is optional here
       void clearValue();
-      [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlAttribute>> getAttributes();
-      [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren();
+      [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlAttribute>> getAttributes() const;
+      [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren() const;
       [[nodiscard]] ::std::list<::std::shared_ptr<ls::std::io::XmlNode>> getChildren(const ::std::string &_name);
-      [[nodiscard]] ::std::string getName();
-      [[nodiscard]] ::std::string getValue();
+      [[nodiscard]] ::std::string getName() const;
+      [[nodiscard]] ::std::string getValue() const;
       [[nodiscard]] bool hasAttribute(const ::std::string &_name);
       [[nodiscard]] bool hasChild(const ::std::string &_name);
       [[nodiscard]] bool hasChild(const ::std::shared_ptr<ls::std::io::XmlNode> &_child);
@@ -70,10 +70,10 @@ namespace ls::std::io
       [[nodiscard]] bool _hasChild(const ::std::string &_name);
       [[nodiscard]] ::std::string _toXmlAttributes();
       [[nodiscard]] ::std::string _toXmlChildren(uint8_t _tabSize);
-      [[nodiscard]] ::std::string _toXmlCloseTag();
-      [[nodiscard]] ::std::string _toXmlOpenTag();
-      [[nodiscard]] ::std::string _toXmlOpenTagClose();
-      [[nodiscard]] ::std::string _toXmlValue();
+      [[nodiscard]] ::std::string _toXmlCloseTag() const;
+      [[nodiscard]] ::std::string _toXmlOpenTag() const;
+      [[nodiscard]] ::std::string _toXmlOpenTagClose() const;
+      [[nodiscard]] ::std::string _toXmlValue() const;
   };
 }
 

+ 12 - 12
source/ls-std/io/xml/XmlNode.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-24
- * Changed:         2023-02-24
+ * Changed:         2023-05-16
  *
  * */
 
@@ -197,12 +197,12 @@ void XmlNode::clearValue()
   this->value.clear();
 }
 
-list<shared_ptr<XmlAttribute>> XmlNode::getAttributes()
+list<shared_ptr<XmlAttribute>> XmlNode::getAttributes() const
 {
   return this->attributes;
 }
 
-list<shared_ptr<XmlNode>> XmlNode::getChildren()
+list<shared_ptr<XmlNode>> XmlNode::getChildren() const
 {
   return this->children;
 }
@@ -210,17 +210,17 @@ list<shared_ptr<XmlNode>> XmlNode::getChildren()
 list<shared_ptr<XmlNode>> XmlNode::getChildren(const string &_name)
 {
   list<shared_ptr<XmlNode>> childrenWithName{};
-  copy_if(this->children.begin(), this->children.end(), back_inserter(childrenWithName), [_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
+  copy_if(this->children.begin(), this->children.end(), back_inserter(childrenWithName), [&_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
 
   return childrenWithName;
 }
 
-string XmlNode::getName()
+string XmlNode::getName() const
 {
   return this->name;
 }
 
-string XmlNode::getValue()
+string XmlNode::getValue() const
 {
   return this->value;
 }
@@ -346,7 +346,7 @@ string XmlNode::_getTab(uint8_t _tabSize)
 bool XmlNode::_hasAttribute(const string &_name)
 {
   EmptyStringArgumentEvaluator{_name, "xml attribute name is empty!"}.evaluate();
-  return any_of(this->attributes.begin(), this->attributes.end(), [_name](const shared_ptr<XmlAttribute> &_attribute) { return _attribute->getName() == _name; });
+  return any_of(this->attributes.begin(), this->attributes.end(), [&_name](const shared_ptr<XmlAttribute> &_attribute) { return _attribute->getName() == _name; });
 }
 
 bool XmlNode::_hasChild(const shared_ptr<XmlNode> &_child)
@@ -358,7 +358,7 @@ bool XmlNode::_hasChild(const shared_ptr<XmlNode> &_child)
 bool XmlNode::_hasChild(const string &_name)
 {
   EmptyStringArgumentEvaluator{_name, "xml child node name is empty!"}.evaluate();
-  return any_of(this->children.begin(), this->children.end(), [_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
+  return any_of(this->children.begin(), this->children.end(), [&_name](const shared_ptr<XmlNode> &_node) { return _node->getName() == _name; });
 }
 
 string XmlNode::_toXmlAttributes()
@@ -371,7 +371,7 @@ string XmlNode::_toXmlChildren(uint8_t _tabSize)
   return accumulate(this->children.begin(), this->children.end(), string{}, [_tabSize](string stream, const shared_ptr<XmlNode> &_child) { return ::move(stream) + _child->_toXml_(_tabSize); });
 }
 
-string XmlNode::_toXmlCloseTag()
+string XmlNode::_toXmlCloseTag() const
 {
   string stream{};
 
@@ -383,12 +383,12 @@ string XmlNode::_toXmlCloseTag()
   return stream;
 }
 
-string XmlNode::_toXmlOpenTag()
+string XmlNode::_toXmlOpenTag() const
 {
   return "<" + this->name;
 }
 
-string XmlNode::_toXmlOpenTagClose()
+string XmlNode::_toXmlOpenTagClose() const
 {
   string stream{};
 
@@ -404,7 +404,7 @@ string XmlNode::_toXmlOpenTagClose()
   return stream;
 }
 
-string XmlNode::_toXmlValue()
+string XmlNode::_toXmlValue() const
 {
   return this->value.empty() ? "\n" : this->value;
 }

+ 17 - 17
test/cases/io/xml/XmlNodeTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-25
- * Changed:         2023-03-25
+ * Changed:         2023-05-16
  *
  * */
 
@@ -279,10 +279,10 @@ namespace
   {
     XmlNode dialogsNode{"dialogs"};
     shared_ptr<XmlNode> currentNode{};
-    shared_ptr<XmlNode> dialogNodeA = make_shared<XmlNode>("dialogNodeA");
-    shared_ptr<XmlNode> dialogNodeB = make_shared<XmlNode>("dialogNodeB");
-    shared_ptr<XmlNode> dialogNodeC = make_shared<XmlNode>("dialogNodeC");
-    shared_ptr<XmlNode> dialogNodeD = make_shared<XmlNode>("dialogNodeD");
+    auto dialogNodeA = make_shared<XmlNode>("dialogNodeA");
+    auto dialogNodeB = make_shared<XmlNode>("dialogNodeB");
+    auto dialogNodeC = make_shared<XmlNode>("dialogNodeC");
+    auto dialogNodeD = make_shared<XmlNode>("dialogNodeD");
 
     ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
     ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
@@ -320,8 +320,8 @@ namespace
   TEST_F(XmlNodeTest, addChildAfter_no_search_node_available)
   {
     XmlNode dialogsNode{"dialogs"};
-    shared_ptr<XmlNode> newChild = make_shared<XmlNode>("newChild");
-    shared_ptr<XmlNode> searchNode = make_shared<XmlNode>("searchNode");
+    auto newChild = make_shared<XmlNode>("newChild");
+    auto searchNode = make_shared<XmlNode>("searchNode");
 
     ASSERT_FALSE(dialogsNode.addChildAfter(newChild, searchNode));
   }
@@ -366,10 +366,10 @@ namespace
   {
     XmlNode dialogsNode{"dialogs"};
     shared_ptr<XmlNode> currentNode{};
-    shared_ptr<XmlNode> dialogNodeA = make_shared<XmlNode>("dialogNodeA");
-    shared_ptr<XmlNode> dialogNodeB = make_shared<XmlNode>("dialogNodeB");
-    shared_ptr<XmlNode> dialogNodeC = make_shared<XmlNode>("dialogNodeC");
-    shared_ptr<XmlNode> dialogNodeD = make_shared<XmlNode>("dialogNodeD");
+    auto dialogNodeA = make_shared<XmlNode>("dialogNodeA");
+    auto dialogNodeB = make_shared<XmlNode>("dialogNodeB");
+    auto dialogNodeC = make_shared<XmlNode>("dialogNodeC");
+    auto dialogNodeD = make_shared<XmlNode>("dialogNodeD");
 
     ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeB));
     ASSERT_TRUE(dialogsNode.addChildToEnd(dialogNodeC));
@@ -407,8 +407,8 @@ namespace
   TEST_F(XmlNodeTest, addChildBefore_no_search_node_available)
   {
     XmlNode dialogsNode{"dialogs"};
-    shared_ptr<XmlNode> newChild = make_shared<XmlNode>("newChild");
-    shared_ptr<XmlNode> searchNode = make_shared<XmlNode>("searchNode");
+    auto newChild = make_shared<XmlNode>("newChild");
+    auto searchNode = make_shared<XmlNode>("searchNode");
 
     ASSERT_FALSE(dialogsNode.addChildBefore(newChild, searchNode));
   }
@@ -701,7 +701,7 @@ namespace
   TEST_F(XmlNodeTest, hasChildV2)
   {
     XmlNode dialogsNode{"dialogsNode"};
-    shared_ptr<XmlNode> searchNode = make_shared<XmlNode>("dialogB");
+    auto searchNode = make_shared<XmlNode>("dialogB");
     dialogsNode.addChildToEnd(make_shared<XmlNode>("dialogA"));
     dialogsNode.addChildToEnd(searchNode);
     dialogsNode.addChildToEnd(make_shared<XmlNode>("dialogC"));
@@ -712,7 +712,7 @@ namespace
   TEST_F(XmlNodeTest, hasChildV2_child_not_available)
   {
     XmlNode dialogsNode{"dialogsNode"};
-    shared_ptr<XmlNode> searchNode = make_shared<XmlNode>("dialogB");
+    auto searchNode = make_shared<XmlNode>("dialogB");
 
     ASSERT_FALSE(dialogsNode.hasChild(searchNode));
   }
@@ -938,8 +938,8 @@ namespace
 
   TEST_F(XmlNodeTest, toXml_no_value)
   {
-    shared_ptr<XmlNode> singleLineElement = make_shared<XmlNode>("info");
-    shared_ptr<XmlAttribute> attribute = make_shared<XmlAttribute>("id");
+    auto singleLineElement = make_shared<XmlNode>("info");
+    auto attribute = make_shared<XmlAttribute>("id");
     attribute->setValue("important");
     singleLineElement->addAttributeToEnd(attribute);