From 4b2987d0f2937c1d7c2e63ad573b7e8d4abf6614 Mon Sep 17 00:00:00 2001 From: Tim Mayberry Date: Mon, 26 Sep 2016 12:16:01 +1000 Subject: Add XMLNode::operator==/!=() for comparing XMLNode instances 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 | 3 +++ libs/pbd/xml++.cc | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/libs/pbd/pbd/xml++.h b/libs/pbd/pbd/xml++.h index f0956edd25..fa55f11c35 100644 --- a/libs/pbd/pbd/xml++.h +++ b/libs/pbd/pbd/xml++.h @@ -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; } diff --git a/libs/pbd/xml++.cc b/libs/pbd/xml++.cc index 80fc88242d..b12eb69252 100644 --- a/libs/pbd/xml++.cc +++ b/libs/pbd/xml++.cc @@ -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) { -- cgit v1.2.3