Permalink
Browse files

tests for namespaces

  • Loading branch information...
1 parent 72adfe3 commit 7b8f6db9617a511e6f93883d73475b802f8d8007 @thriqon committed Aug 27, 2011
View
@@ -15,7 +15,8 @@ test_SOURCES = \
document/test_document.cxx \
event/test_event.cxx \
node/test_node.cxx \
- tree/test_tree.cxx
+ tree/test_tree.cxx \
+ namespaces/test_namespaces.cxx
if WITH_XSLT
LIBS += $(top_builddir)/src/libxsltwrapp.la
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<root xmlns:a="href_a"/>
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<root xmlns:a="href_a" xmlns:b="href_b"/>
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<root xmlns:prefix="href"/>
@@ -0,0 +1,2 @@
+<?xml version="1.0" ?>
+<root />
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<root xmlns:p1="href_1" xmlns:p2="href_2"><child /></root>
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<root xmlns:p1="href_1" xmlns:p2="href_2">
+ <p1:child/>
+</root>
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<root xmlns:p1="href_1" xmlns:p2="href_2">
+ <p2:child/>
+</root>
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2011 Jonas Weber <mail@jonasw.de>
+ * All Rights Reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name of the Author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "../test.h"
+
+#include <functional>
+
+
+BOOST_AUTO_TEST_SUITE( namespaces )
+
+BOOST_AUTO_TEST_CASE ( read_file_get_namespace_definitions )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/01.xml").c_str());
+
+ xml::document& doc = parser.get_document();
+ xml::node& root = doc.get_root_node();
+
+ xml::namespacedefinitions& nsdefs = root.get_namespace_definitions();
+ BOOST_CHECK( !nsdefs.empty() );
+
+ xml::namespacedefinitions::iterator it = nsdefs.begin();
+
+ BOOST_CHECK( (std::string(it->get_prefix()) == std::string("a")) && (std::string(it->get_href()) == std::string("href_a")) );
+}
+
+BOOST_AUTO_TEST_CASE ( read_file_get_namespace_definitions_iterate )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/02.xml").c_str());
+ xml::node& root = parser.get_document().get_root_node();
+
+ xml::namespacedefinitions& nsdefs = root.get_namespace_definitions();
+
+ xml::namespacedefinitions::iterator it = nsdefs.begin();
+
+ BOOST_CHECK( (std::string(it->get_prefix()) == std::string("a")) && (std::string(it->get_href()) == std::string("href_a")) );
+
+ xml::namespacedefinitions::iterator it2 = it;
+ BOOST_CHECK ( it2 == it++ );
+
+ BOOST_CHECK( (std::string(it->get_prefix()) == std::string("b")) && (std::string(it->get_href()) == std::string("href_b")) );
+
+ it2 = it;
+ BOOST_CHECK ( it2 != ++it );
+
+ BOOST_CHECK ( it == nsdefs.end() );
+}
+
+BOOST_AUTO_TEST_CASE ( nsdef_getters )
+{
+ xml::namespacedefinitions::nsdef mynsdef ("href", "prefix");
+
+ BOOST_CHECK ( std::string(mynsdef.get_href()) == "href" && std::string(mynsdef.get_prefix()) == "prefix" );
+}
+
+BOOST_AUTO_TEST_CASE ( read_file_add_namespace_definition )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/03.xml").c_str());
+ xml::node& root = parser.get_document().get_root_node();
+
+ xml::namespacedefinitions& nsdefs = root.get_namespace_definitions();
+
+ nsdefs.push_back (xml::namespacedefinitions::nsdef("href", "prefix"));
+
+ std::ostringstream ostr;
+ ostr << parser.get_document();
+
+ BOOST_CHECK( is_same_as_file (ostr, "namespaces/data/03.out") );
+}
+
+BOOST_AUTO_TEST_CASE ( find_namespaces )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/04.xml").c_str());
+ xml::node& root = parser.get_document().get_root_node();
+
+ xml::node::iterator child_it = root.find("child");
+ BOOST_CHECK (child_it != root.end());
+
+ xml::node& child = *child_it;
+
+ xml::namespacedefinitions& nsdefs = child.get_namespace_definitions();
+ xml::namespacedefinitions::iterator res = nsdefs.find("p1");
+ BOOST_CHECK ( std::string(res->get_href()) == std::string("href_1"));
+
+ res = nsdefs.findHref("href_2");
+ BOOST_CHECK ( std::string(res->get_prefix()) == std::string("p2"));
+
+ res = nsdefs.find("pXXX");
+ BOOST_CHECK ( res == nsdefs.end() );
+
+ res = nsdefs.findHref("href_XXX");
+ BOOST_CHECK ( res == nsdefs.end() );
+}
+
+BOOST_AUTO_TEST_CASE ( check_empty )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/04.xml").c_str());
+ xml::node& root = parser.get_document().get_root_node();
+
+ xml::node::iterator child_it = root.find("child");
+ xml::node& child = *child_it;
+
+ BOOST_CHECK( !root.get_namespace_definitions().empty() );
+ BOOST_CHECK ( child.get_namespace_definitions().empty() );
+
+}
+
+BOOST_AUTO_TEST_CASE ( set_namespaces )
+{
+ xml::tree_parser parser(test_file_path("namespaces/data/04.xml").c_str());
+ xml::node& root = parser.get_document().get_root_node();
+ xml::node::iterator child_it = root.find("child");
+ xml::node& child = *child_it;
+
+ child.set_namespace("p1");
+
+ std::ostringstream ostr1;
+ ostr1 << parser.get_document();
+
+ BOOST_CHECK( is_same_as_file (ostr1, "namespaces/data/04_01.out"));
+
+ child.set_namespace_href("href_2");
+
+ std::ostringstream ostr2;
+ ostr2 << parser.get_document();
+ BOOST_CHECK ( is_same_as_file (ostr2, "namespaces/data/04_02.out"));
+
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 7b8f6db

Please sign in to comment.