diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-05-20 21:19:02 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-05-20 21:19:02 +0100 |
commit | 2f36f18734c030a1d4937dc6f471917e06120dd4 (patch) | |
tree | 169e77018f6cfc758bd689c7357f8f6c45f11b8f | |
parent | Extend TestPresenter to be a full Presenter implementation (diff) | |
download | project2-2f36f18734c030a1d4937dc6f471917e06120dd4.tar.bz2 project2-2f36f18734c030a1d4937dc6f471917e06120dd4.tar.xz project2-2f36f18734c030a1d4937dc6f471917e06120dd4.zip |
Fix XML raw view to handle namespaces better, covering unit test
-rw-r--r-- | project2/Jamfile.jam | 1 | ||||
-rw-r--r-- | project2/ut/testScriptHost.cpp | 1 | ||||
-rw-r--r-- | project2/xml/Jamfile.jam | 3 | ||||
-rw-r--r-- | project2/xml/rawView.cpp | 21 | ||||
-rw-r--r-- | project2/xml/unittests/Jamfile.jam | 18 | ||||
-rw-r--r-- | project2/xml/unittests/expected/rawview.xml | 12 | ||||
-rw-r--r-- | project2/xml/unittests/rawview.xml | 15 | ||||
-rw-r--r-- | project2/xml/unittests/testxml.cpp | 34 |
8 files changed, 97 insertions, 8 deletions
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<PresenterLoader>(&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 ] : <include>. <include>../libmisc @@ -27,6 +27,7 @@ lib p2xml : <library>boost_date_time : : <include>. + <library>../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<const Presenter *>(mp)) { - for (const auto * node : getCopyRoot(ec)->get_children()) { - const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(node); - if (e) { - copyNode(p, e); + if (auto copyRoot = getCopyRoot(ec)) { + for (const auto * node : copyRoot->get_children()) { + const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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 + : + <define>ROOT=\"$(me)\" + <library>..//p2xml + <library>../../ut//p2ut + <library>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 @@ +<?xml version="1.0" encoding="utf-8"?> +<test xmlns:project2="http://project2.randomdan.homeip.net"> + <nonamespace with="attr"> + <nonamespace with="attr"/> + </nonamespace> + <unusednamespace with="attr"> + <nonamespace with="attr"/> + </unusednamespace> + <namespaced xmlns:nsattr="http://unused/" nsattr:with="attr"> + <nsattr:that with="attr"/> + </namespaced> +</test> 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 @@ +<?xml version="1.0"?> +<view xmlns:project2="http://project2.randomdan.homeip.net" name="index"> + <project2:rawview> + <nonamespace with="attr"> + <nonamespace with="attr"/> + </nonamespace> + <unusednamespace xmlns:unsattr="http://unused/" with="attr"> + <nonamespace with="attr" /> + </unusednamespace> + <namespaced xmlns:nsattr="http://unused/" nsattr:with="attr"> + <nsattr:that with="attr" /> + </namespaced> + </project2:rawview> + <project2:xml root="test"/> +</view> 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 <boost/test/unit_test.hpp> + +#include <definedDirs.h> +#include "xmlPresenter.h" +#include "xmlScriptParser.h" +#include <testScriptHost.h> +#include <testOptionsSource.h> +#include <fstream> +#include <logger.h> + +template <typename ... T> +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<TestScriptHost> h = new TestScriptHost(s); + h->executeViews(NULL); + auto p = boost::dynamic_pointer_cast<XmlPresenter>(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")); +} + |