Add XMLNode::operator==/!=() for comparing XMLNode instances
authorTim Mayberry <mojofunk@gmail.com>
Mon, 26 Sep 2016 02:16:01 +0000 (12:16 +1000)
committerTim Mayberry <mojofunk@gmail.com>
Sun, 9 Oct 2016 22:45:34 +0000 (08:45 +1000)
Implemented to be able to test that when writing an XML document via XMLTree
and then reading back into another XMLTree the structure is equivalent as a
general API test of pbd/xml++.h to check for breakage when changing
implementation.

libs/pbd/pbd/xml++.h
libs/pbd/xml++.cc

index f0956edd259617584de6dc692080aad4de3ee9e2..fa55f11c3575572fdbceb4915c1db21d0aee48e4 100644 (file)
@@ -101,6 +101,9 @@ public:
 
        XMLNode& operator= (const XMLNode& other);
 
+       bool operator== (const XMLNode& other) const;
+       bool operator!= (const XMLNode& other) const;
+
        const std::string& name() const { return _name; }
 
        bool          is_content() const { return _is_content; }
index 80fc88242dc9884dc79b5351a10bcd37830c981c..b12eb69252c06e69ea84e2683b2fbde97ac42e88 100644 (file)
@@ -289,6 +289,70 @@ XMLNode::operator= (const XMLNode& from)
        return *this;
 }
 
+bool
+XMLNode::operator== (const XMLNode& other) const
+{
+       if (is_content () != other.is_content ()) {
+               return false;
+       }
+
+       if (is_content ()) {
+               if (content () != other.content ()) {
+                       return false;
+               }
+       } else {
+               if (name () != other.name ()) {
+                       return false;
+               }
+       }
+
+       XMLPropertyList const& other_properties = other.properties ();
+
+       if (_proplist.size () != other_properties.size ()) {
+               return false;
+       }
+
+       XMLPropertyConstIterator our_prop_iter = _proplist.begin();
+       XMLPropertyConstIterator other_prop_iter = other_properties.begin();
+
+       while (our_prop_iter != _proplist.end ()) {
+               XMLProperty const* our_prop = *our_prop_iter;
+               XMLProperty const* other_prop = *other_prop_iter;
+               if (our_prop->name () != other_prop->name () || our_prop->value () != other_prop->value ()) {
+                       return false;
+               }
+               ++our_prop_iter;
+               ++other_prop_iter;
+       }
+
+       XMLNodeList const& other_children = other.children();
+
+       if (_children.size() != other_children.size()) {
+               return false;
+       }
+
+       XMLNodeConstIterator our_child_iter = _children.begin ();
+       XMLNodeConstIterator other_child_iter = other_children.begin ();
+
+       while (our_child_iter != _children.end()) {
+               XMLNode const* our_child = *our_child_iter;
+               XMLNode const* other_child = *other_child_iter;
+
+               if (*our_child != *other_child) {
+                       return false;
+               }
+               ++our_child_iter;
+               ++other_child_iter;
+       }
+       return true;
+}
+
+bool
+XMLNode::operator!= (const XMLNode& other) const
+{
+       return !(*this == other);
+}
+
 const string&
 XMLNode::set_content(const string& c)
 {