summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-11 06:49:30 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-11 06:49:30 +0000
commite11a81455afb36f59d2ec08ca7ccd4c56d0691a6 (patch)
tree71eed7bc143e2d9f6d0832e0347e02ca888df647 /tests
parent6ff7e795752b0cfc3d85e447bab063667e786150 (diff)
tests: Added more node/attribute tests
git-svn-id: http://pugixml.googlecode.com/svn/trunk@142 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests')
-rw-r--r--tests/test.hpp14
-rw-r--r--tests/test_dom_traverse.cpp195
-rw-r--r--tests/test_xpath.cpp19
3 files changed, 222 insertions, 6 deletions
diff --git a/tests/test.hpp b/tests/test.hpp
index 6b8878e..fa4ef1d 100644
--- a/tests/test.hpp
+++ b/tests/test.hpp
@@ -2,12 +2,18 @@
#define HEADER_TEST_HPP
#include <string.h>
+#include <math.h>
inline bool test_string_equal(const char* lhs, const char* rhs)
{
return (!lhs || !rhs) ? lhs == rhs : strcmp(lhs, rhs) == 0;
}
+template <typename Node> inline bool test_node_name_value(const Node& node, const char* name, const char* value)
+{
+ return test_string_equal(node.name(), name) && test_string_equal(node.value(), value);
+}
+
struct test_runner
{
test_runner(const char* name)
@@ -48,20 +54,24 @@ struct dummy_fixture {};
#define TEST(name) TEST_FIXTURE(name, dummy_fixture)
-#define TEST_XML(name, xml) \
+#define TEST_XML_FLAGS(name, xml, flags) \
struct test_fixture_##name \
{ \
pugi::xml_document doc; \
\
test_fixture_##name() \
{ \
- CHECK(doc.load(xml)); \
+ CHECK(doc.load(xml, flags)); \
} \
}; \
\
TEST_FIXTURE(name, test_fixture_##name)
+#define TEST_XML(name, xml) TEST_XML_FLAGS(name, xml, pugi::parse_default)
+
#define CHECK(condition) if (condition) ; else throw #condition " is false"
#define CHECK_STRING(value, expected) if (test_string_equal(value, expected)) ; else throw #value " is not equal to " #expected
+#define CHECK_DOUBLE(value, expected) if (fabs(value - expected) < 1e-6) ; else throw #value " is not equal to " #expected
+#define CHECK_NAME_VALUE(node, name, value) if (test_node_name_value(node, name, value)) ; else throw #node " name/value do not match " #name " and " #value
#endif
diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp
index d48cf74..8a7f6eb 100644
--- a/tests/test_dom_traverse.cpp
+++ b/tests/test_dom_traverse.cpp
@@ -2,6 +2,12 @@
#include <utility>
+template <typename I> I move_iter(I base, int n)
+{
+ std::advance(base, n);
+ return base;
+}
+
template <typename T> void generic_bool_ops_test(const T& obj)
{
T null;
@@ -118,11 +124,103 @@ TEST_XML(dom_attr_name_value, "<node attr='1'/>")
{
xml_attribute attr = doc.child("node").attribute("attr");
- CHECK_STRING(attr.name(), "attr");
- CHECK_STRING(attr.value(), "1");
+ CHECK_NAME_VALUE(attr, "attr", "1");
+ CHECK_NAME_VALUE(xml_attribute(), "", "");
+}
+
+TEST_XML(dom_attr_as_int, "<node attr1='1' attr2='-1' attr3='-2147483648' attr4='2147483647'/>")
+{
+ xml_node node = doc.child("node");
+
+ CHECK(node.attribute("attr1").as_int() == 1);
+ CHECK(node.attribute("attr2").as_int() == -1);
+ CHECK(node.attribute("attr3").as_int() == -2147483647 - 1);
+ CHECK(node.attribute("attr4").as_int() == 2147483647);
+}
+
+TEST_XML(dom_attr_as_uint, "<node attr1='0' attr2='1' attr3='2147483647'/>")
+{
+ xml_node node = doc.child("node");
+
+ CHECK(node.attribute("attr1").as_int() == 0);
+ CHECK(node.attribute("attr2").as_int() == 1);
+ CHECK(node.attribute("attr3").as_int() == 2147483647);
+}
+
+TEST_XML(dom_attr_as_float, "<node attr1='0' attr2='1' attr3='0.12' attr4='-5.1' attr5='3e-4' attr6='3.14159265358979323846'/>")
+{
+ xml_node node = doc.child("node");
+
+ CHECK_DOUBLE(node.attribute("attr1").as_float(), 0);
+ CHECK_DOUBLE(node.attribute("attr2").as_float(), 1);
+ CHECK_DOUBLE(node.attribute("attr3").as_float(), 0.12);
+ CHECK_DOUBLE(node.attribute("attr4").as_float(), -5.1);
+ CHECK_DOUBLE(node.attribute("attr5").as_float(), 3e-4);
+ CHECK_DOUBLE(node.attribute("attr6").as_float(), 3.14159265358979323846);
+}
+
+TEST_XML(dom_attr_as_double, "<node attr1='0' attr2='1' attr3='0.12' attr4='-5.1' attr5='3e-4' attr6='3.14159265358979323846'/>")
+{
+ xml_node node = doc.child("node");
+
+ CHECK_DOUBLE(node.attribute("attr1").as_double(), 0);
+ CHECK_DOUBLE(node.attribute("attr2").as_double(), 1);
+ CHECK_DOUBLE(node.attribute("attr3").as_double(), 0.12);
+ CHECK_DOUBLE(node.attribute("attr4").as_double(), -5.1);
+ CHECK_DOUBLE(node.attribute("attr5").as_double(), 3e-4);
+ CHECK_DOUBLE(node.attribute("attr6").as_double(), 3.14159265358979323846);
+}
+
+TEST_XML(dom_attr_as_bool, "<node attr1='0' attr2='1' attr3='true' attr4='True' attr5='Yes' attr6='yes' attr7='false'/>")
+{
+ xml_node node = doc.child("node");
+
+ CHECK(!node.attribute("attr1").as_bool());
+ CHECK(node.attribute("attr2").as_bool());
+ CHECK(node.attribute("attr3").as_bool());
+ CHECK(node.attribute("attr4").as_bool());
+ CHECK(node.attribute("attr5").as_bool());
+ CHECK(node.attribute("attr6").as_bool());
+ CHECK(!node.attribute("attr7").as_bool());
+}
+
+TEST_XML(dom_attr_iterator, "<node><node1 attr1='0'/><node2 attr1='0' attr2='1'/><node3/></node>")
+{
+ xml_node node1 = doc.child("node").child("node1");
+ xml_node node2 = doc.child("node").child("node2");
+ xml_node node3 = doc.child("node").child("node3");
+
+ CHECK(xml_node().attributes_begin() == xml_attribute_iterator());
+ CHECK(xml_node().attributes_end() == xml_attribute_iterator());
+
+ CHECK(node1.attributes_begin() == xml_attribute_iterator(node1.attribute("attr1")));
+ CHECK(move_iter(node1.attributes_begin(), 1) == node1.attributes_end());
+ CHECK(move_iter(node1.attributes_end(), -1) == node1.attributes_begin());
+ CHECK(*node1.attributes_begin() == node1.attribute("attr1"));
+ CHECK_STRING(node1.attributes_begin()->name(), "attr1");
+
+ CHECK(move_iter(node2.attributes_begin(), 2) == node2.attributes_end());
+ CHECK(move_iter(node2.attributes_end(), -2) == node2.attributes_begin());
+
+ CHECK(node3.attributes_begin() == xml_attribute_iterator());
+ CHECK(node3.attributes_begin() == node3.attributes_end());
+
+ xml_attribute_iterator it = node2.attribute("attr2");
+ xml_attribute_iterator itt = it;
+
+ CHECK(itt++ == it);
+ CHECK(itt == node2.attributes_end());
+
+ CHECK(itt-- == node2.attributes_end());
+ CHECK(itt == it);
+
+ CHECK(++itt == node2.attributes_end());
+ CHECK(itt == node2.attributes_end());
- CHECK_STRING(xml_attribute().name(), "");
- CHECK_STRING(xml_attribute().value(), "");
+ CHECK(--itt == it);
+ CHECK(itt == it);
+
+ CHECK(++itt != it);
}
TEST_XML(dom_node_bool_ops, "<node/>")
@@ -139,3 +237,92 @@ TEST_XML(dom_node_empty, "<node/>")
{
generic_empty_test(doc.child("node"));
}
+
+TEST_XML(dom_node_iterator, "<node><node1><child1/></node1><node2><child1/><child2/></node2><node3/></node>")
+{
+ xml_node node1 = doc.child("node").child("node1");
+ xml_node node2 = doc.child("node").child("node2");
+ xml_node node3 = doc.child("node").child("node3");
+
+ CHECK(xml_node().begin() == xml_node_iterator());
+ CHECK(xml_node().end() == xml_node_iterator());
+
+ CHECK(node1.begin() == xml_node_iterator(node1.child("child1")));
+ CHECK(move_iter(node1.begin(), 1) == node1.end());
+ CHECK(move_iter(node1.end(), -1) == node1.begin());
+ CHECK(*node1.begin() == node1.child("child1"));
+ CHECK_STRING(node1.begin()->name(), "child1");
+
+ CHECK(move_iter(node2.begin(), 2) == node2.end());
+ CHECK(move_iter(node2.end(), -2) == node2.begin());
+
+ CHECK(node3.begin() == xml_node_iterator());
+ CHECK(node3.begin() == node3.end());
+
+ xml_node_iterator it = node2.child("child2");
+ xml_node_iterator itt = it;
+
+ CHECK(itt++ == it);
+ CHECK(itt == node2.end());
+
+ CHECK(itt-- == node2.end());
+ CHECK(itt == it);
+
+ CHECK(++itt == node2.end());
+ CHECK(itt == node2.end());
+
+ CHECK(--itt == it);
+ CHECK(itt == it);
+
+ CHECK(++itt != it);
+}
+
+TEST_XML(dom_node_parent, "<node><child/></node>")
+{
+ CHECK(xml_node().parent() == xml_node());
+ CHECK(doc.child("node").child("child").parent() == doc.child("node"));
+ CHECK(doc.child("node").parent() == doc);
+}
+
+TEST_XML(dom_node_root, "<node><child/></node>")
+{
+ CHECK(xml_node().root() == xml_node());
+ CHECK(doc.child("node").child("child").root() == doc);
+ CHECK(doc.child("node").root() == doc);
+}
+
+TEST_XML_FLAGS(dom_node_type, "<?xml?><?pi?><!--comment--><node>pcdata<![CDATA[cdata]]></node>", parse_default | parse_pi | parse_comments | parse_declaration)
+{
+ CHECK(xml_node().type() == node_null);
+ CHECK(doc.type() == node_document);
+
+ xml_node_iterator it = doc.begin();
+
+ CHECK((it++)->type() == node_declaration);
+ CHECK((it++)->type() == node_pi);
+ CHECK((it++)->type() == node_comment);
+ CHECK((it++)->type() == node_element);
+
+ xml_node_iterator cit = doc.child("node").begin();
+
+ CHECK((cit++)->type() == node_pcdata);
+ CHECK((cit++)->type() == node_cdata);
+}
+
+TEST_XML_FLAGS(dom_node_name_value, "<?xml?><?pi?><!--comment--><node>pcdata<![CDATA[cdata]]></node>", parse_default | parse_pi | parse_comments | parse_declaration)
+{
+ CHECK_NAME_VALUE(xml_node(), "", "");
+ CHECK_NAME_VALUE(doc, "", "");
+
+ xml_node_iterator it = doc.begin();
+
+ CHECK_NAME_VALUE(*it++, "xml", "");
+ CHECK_NAME_VALUE(*it++, "pi", "");
+ CHECK_NAME_VALUE(*it++, "", "comment");
+ CHECK_NAME_VALUE(*it++, "node", "");
+
+ xml_node_iterator cit = doc.child("node").begin();
+
+ CHECK_NAME_VALUE(*cit++, "", "pcdata");
+ CHECK_NAME_VALUE(*cit++, "", "cdata");
+}
diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp
new file mode 100644
index 0000000..b3ace66
--- /dev/null
+++ b/tests/test_xpath.cpp
@@ -0,0 +1,19 @@
+#include "common.hpp"
+
+TEST_XML(xpath_document_order, "<node><child1 attr1='value1' attr2='value2'/><child2 attr1='value1'>test</child2></node>")
+{
+ CHECK(xml_node().document_order() == 0);
+ CHECK(doc.child("node").document_order() == 0);
+ CHECK(doc.document_order() == 0);
+
+ doc.precompute_document_order();
+
+ CHECK(doc.document_order() == 1);
+ CHECK(doc.child("node").document_order() == 2);
+ CHECK(doc.child("node").child("child1").document_order() == 3);
+ CHECK(doc.child("node").child("child1").attribute("attr1").document_order() == 4);
+ CHECK(doc.child("node").child("child1").attribute("attr2").document_order() == 5);
+ CHECK(doc.child("node").child("child2").document_order() == 6);
+ CHECK(doc.child("node").child("child2").attribute("attr1").document_order() == 7);
+ CHECK(doc.child("node").child("child2").first_child().document_order() == 8);
+}