From 6bde78b5743d6b323b9af14d434927fd175175c3 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 30 May 2014 03:51:54 +0200 Subject: =?UTF-8?q?XmlNode=E2=80=99s=20copy=20constructor=20now=20recursiv?= =?UTF-8?q?ely=20copies=20the=20children=20nodes=20as=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test.cpp | 27 +++++++++++++++++---------- src/xmpp/xmpp_stanza.hpp | 10 +++++++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/test.cpp b/src/test.cpp index e71ea35..8fe739c 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -151,18 +151,25 @@ int main() std::cout << color << "Testing XML parsing…" << reset << std::endl; XmppParser xml; const std::string doc = "innertail"; - xml.add_stanza_callback([](const Stanza& stanza) + auto check_stanza = [](const Stanza& stanza) + { + assert(stanza.get_name() == "stanza"); + assert(stanza.get_tag("xmlns") == "stream_ns"); + assert(stanza.get_tag("b") == "c"); + assert(stanza.get_inner() == "inner"); + assert(stanza.get_tail() == ""); + assert(stanza.get_child("child1", "stream_ns") != nullptr); + assert(stanza.get_child("child2", "stream_ns") == nullptr); + assert(stanza.get_child("child2", "child2_ns") != nullptr); + assert(stanza.get_child("child2", "child2_ns")->get_tail() == "tail"); + }; + xml.add_stanza_callback([check_stanza](const Stanza& stanza) { std::cout << stanza.to_string() << std::endl; - assert(stanza.get_name() == "stanza"); - assert(stanza.get_tag("xmlns") == "stream_ns"); - assert(stanza.get_tag("b") == "c"); - assert(stanza.get_inner() == "inner"); - assert(stanza.get_tail() == ""); - assert(stanza.get_child("child1", "stream_ns") != nullptr); - assert(stanza.get_child("child2", "stream_ns") == nullptr); - assert(stanza.get_child("child2", "child2_ns") != nullptr); - assert(stanza.get_child("child2", "child2_ns")->get_tail() == "tail"); + check_stanza(stanza); + // Do the same checks on a copy of that stanza. + Stanza copy(stanza); + check_stanza(copy); }); xml.feed(doc.data(), doc.size(), true); diff --git a/src/xmpp/xmpp_stanza.hpp b/src/xmpp/xmpp_stanza.hpp index e55d555..53e0139 100644 --- a/src/xmpp/xmpp_stanza.hpp +++ b/src/xmpp/xmpp_stanza.hpp @@ -35,9 +35,8 @@ public: node.parent = nullptr; } /** - * The copy constructor do not copy the children or parent attributes. The - * copied node is identical to the original except that it is not attached - * to any other node. + * The copy constructor do not copy the parent attribute. The children + * nodes are all copied recursively. */ XmlNode(const XmlNode& node): name(node.name), @@ -48,6 +47,11 @@ public: inner(node.inner), tail(node.tail) { + for (XmlNode* child: node.children) + { + XmlNode* child_copy = new XmlNode(*child); + this->add_child(child_copy); + } } ~XmlNode(); -- cgit v1.2.3