From 2f36f18734c030a1d4937dc6f471917e06120dd4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 20 May 2015 21:19:02 +0100 Subject: Fix XML raw view to handle namespaces better, covering unit test --- project2/Jamfile.jam | 1 + project2/ut/testScriptHost.cpp | 1 + project2/xml/Jamfile.jam | 3 ++- project2/xml/rawView.cpp | 21 ++++++++++++------ project2/xml/unittests/Jamfile.jam | 18 +++++++++++++++ project2/xml/unittests/expected/rawview.xml | 12 ++++++++++ project2/xml/unittests/rawview.xml | 15 +++++++++++++ project2/xml/unittests/testxml.cpp | 34 +++++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 project2/xml/unittests/Jamfile.jam create mode 100644 project2/xml/unittests/expected/rawview.xml create mode 100644 project2/xml/unittests/rawview.xml create mode 100644 project2/xml/unittests/testxml.cpp diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 0261170..e7e33b9 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -39,6 +39,7 @@ build-project common//unittests ; build-project basics//unittests ; build-project ice//unittests ; build-project sql//unittests ; +build-project xml//unittests ; explicit install installp2con installp2cgi installp2fcgi ; package.install install : : console//p2console cgi//p2cgi cgi//p2fcgi daemon//p2daemon ; diff --git a/project2/ut/testScriptHost.cpp b/project2/ut/testScriptHost.cpp index 69ee39b..c3090ff 100644 --- a/project2/ut/testScriptHost.cpp +++ b/project2/ut/testScriptHost.cpp @@ -11,6 +11,7 @@ TestScriptHost::TestScriptHost(ScriptReaderPtr script) : ViewHost(script->root()) { Logger()->message(LOG_DEBUG, __PRETTY_FUNCTION__); + script->loader.addLoadTarget(script->root(), Storer::into(&presenter, Scripted, (ExecContext*)NULL)); } TestScriptHost::~TestScriptHost() diff --git a/project2/xml/Jamfile.jam b/project2/xml/Jamfile.jam index 2665444..4a69457 100644 --- a/project2/xml/Jamfile.jam +++ b/project2/xml/Jamfile.jam @@ -14,7 +14,7 @@ cpp-pch pch : pch.hpp : ; lib p2xml : pch - [ glob-tree *.cpp ] + [ glob-tree *.cpp : unittests ] : . ../libmisc @@ -27,6 +27,7 @@ lib p2xml : boost_date_time : : . + ../common//p2common ; diff --git a/project2/xml/rawView.cpp b/project2/xml/rawView.cpp index c583e00..4f013f9 100644 --- a/project2/xml/rawView.cpp +++ b/project2/xml/rawView.cpp @@ -15,10 +15,12 @@ class RawViewBase : public View { void execute(const MultiRowSetPresenter * mp, ExecContext * ec) const { if (const Presenter * p = dynamic_cast(mp)) { - for (const auto * node : getCopyRoot(ec)->get_children()) { - const xmlpp::Element * e = dynamic_cast(node); - if (e) { - copyNode(p, e); + if (auto copyRoot = getCopyRoot(ec)) { + for (const auto * node : copyRoot->get_children()) { + const xmlpp::Element * e = dynamic_cast(node); + if (e) { + copyNode(p, e); + } } } } @@ -30,11 +32,16 @@ class RawViewBase : public View { private: void copyNode(const Presenter * p, const xmlpp::Element * n) const { - p->pushSub(n->get_name()); - p->setNamespace(n->get_namespace_prefix(), n->get_namespace_uri()); + if (!n->get_namespace_uri().empty()) { + p->setNamespace(n->get_namespace_prefix(), n->get_namespace_uri()); + } + p->pushSub(n->get_name(), n->get_namespace_prefix()); xmlpp::Element::AttributeList al = n->get_attributes(); for (const xmlpp::Attribute * a : al) { - p->addAttribute(a->get_name(), a->get_value()); + if (!a->get_namespace_uri().empty()) { + p->setNamespace(a->get_namespace_prefix(), a->get_namespace_uri()); + } + p->addAttribute(a->get_name(), a->get_namespace_prefix(), a->get_value()); } const xmlpp::Node::NodeList ch = n->get_children(); for (const xmlpp::Node * c : ch) { diff --git a/project2/xml/unittests/Jamfile.jam b/project2/xml/unittests/Jamfile.jam new file mode 100644 index 0000000..f520c04 --- /dev/null +++ b/project2/xml/unittests/Jamfile.jam @@ -0,0 +1,18 @@ +import testing ; + +lib boost_system ; +lib boost_filesystem ; + +path-constant me : . ; + +run + testxml.cpp + : : + expected/rawview.xml rawview.xml + : + ROOT=\"$(me)\" + ..//p2xml + ../../ut//p2ut + boost_filesystem + : testxml ; + diff --git a/project2/xml/unittests/expected/rawview.xml b/project2/xml/unittests/expected/rawview.xml new file mode 100644 index 0000000..5a11eb5 --- /dev/null +++ b/project2/xml/unittests/expected/rawview.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/project2/xml/unittests/rawview.xml b/project2/xml/unittests/rawview.xml new file mode 100644 index 0000000..396ba50 --- /dev/null +++ b/project2/xml/unittests/rawview.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/project2/xml/unittests/testxml.cpp b/project2/xml/unittests/testxml.cpp new file mode 100644 index 0000000..539c0a0 --- /dev/null +++ b/project2/xml/unittests/testxml.cpp @@ -0,0 +1,34 @@ +#define BOOST_TEST_MODULE TestXML +#include + +#include +#include "xmlPresenter.h" +#include "xmlScriptParser.h" +#include +#include +#include +#include + +template +int +systembf(const char * fmt, const T & ... params) +{ + return system(stringbf(fmt, params...).c_str()); +} + +BOOST_AUTO_TEST_CASE( rawview ) +{ + TestOptionsSource::LoadTestOptions({ }); + ScriptReaderPtr s = new XmlScriptParser(RootDir / "rawview.xml"); + boost::intrusive_ptr h = new TestScriptHost(s); + h->executeViews(NULL); + auto p = boost::dynamic_pointer_cast(h->getPresenter(NULL)); + BOOST_REQUIRE(p); + std::fstream strm("/tmp/out.xml", std::ios::out); + BOOST_REQUIRE(strm.is_open()); + p->writeTo(strm, "utf-8", NULL); + BOOST_REQUIRE_EQUAL(0, systembf("diff -w --unified %s %s", + "/tmp/out.xml", + RootDir / "expected" / "rawview.xml")); +} + -- cgit v1.2.3