summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-10 21:58:44 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-10-10 21:58:44 +0000
commit6ff7e795752b0cfc3d85e447bab063667e786150 (patch)
treef9d875c492ce0618b0c6e6b29f3c6e4dd387562e /tests
parent6db04f4320cd5d24ae625dbc1df5a8a71b93e51d (diff)
tests: Added more node/attribute tests
git-svn-id: http://pugixml.googlecode.com/svn/trunk@141 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests')
-rw-r--r--tests/test.hpp8
-rw-r--r--tests/test_dom_traverse.cpp150
2 files changed, 128 insertions, 30 deletions
diff --git a/tests/test.hpp b/tests/test.hpp
index 09d7024..6b8878e 100644
--- a/tests/test.hpp
+++ b/tests/test.hpp
@@ -1,6 +1,13 @@
#ifndef HEADER_TEST_HPP
#define HEADER_TEST_HPP
+#include <string.h>
+
+inline bool test_string_equal(const char* lhs, const char* rhs)
+{
+ return (!lhs || !rhs) ? lhs == rhs : strcmp(lhs, rhs) == 0;
+}
+
struct test_runner
{
test_runner(const char* name)
@@ -55,5 +62,6 @@ struct dummy_fixture {};
TEST_FIXTURE(name, test_fixture_##name)
#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
#endif
diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp
index bbee076..d48cf74 100644
--- a/tests/test_dom_traverse.cpp
+++ b/tests/test_dom_traverse.cpp
@@ -1,51 +1,141 @@
#include "common.hpp"
-TEST_XML(dom_attr_bool_ops, "<node attr='1'/>")
+#include <utility>
+
+template <typename T> void generic_bool_ops_test(const T& obj)
{
- xml_attribute attr1;
- xml_attribute attr2 = doc.child("node").attribute("attr");
+ T null;
- CHECK(!attr1);
- CHECK(attr2);
- CHECK(!!attr2);
+ CHECK(!null);
+ CHECK(obj);
+ CHECK(!!obj);
- bool attr1b = attr1;
- bool attr2b = attr2;
+ bool b1 = null, b2 = obj;
- CHECK(!attr1b);
- CHECK(attr2b);
+ CHECK(!b1);
+ CHECK(b2);
}
-TEST_XML(dom_attr_empty, "<node attr='1'/>")
+template <typename T> void generic_rel_ops_test(T obj1, T obj2)
{
- xml_attribute attr1;
- xml_attribute attr2 = doc.child("node").attribute("attr");
+ T null = T();
+
+ // obj1 < obj2 (we use operator<, but there is no other choice
+ if (obj1 > obj2) std::swap(obj1, obj2);
+
+ // operator==
+ CHECK(null == null);
+ CHECK(obj1 == obj1);
+ CHECK(!(null == obj1));
+ CHECK(!(null == obj2));
+ CHECK(T(null) == null);
+ CHECK(T(obj1) == obj1);
- CHECK(attr1.empty());
- CHECK(!attr2.empty());
+ // operator!=
+ CHECK(!(null != null));
+ CHECK(!(obj1 != obj1));
+ CHECK(null != obj1);
+ CHECK(null != obj2);
+ CHECK(!(T(null) != null));
+ CHECK(!(T(obj1) != obj1));
+
+ // operator<
+ CHECK(null < obj1);
+ CHECK(null < obj2);
+ CHECK(obj1 < obj2);
+ CHECK(!(null < null));
+ CHECK(!(obj1 < obj1));
+ CHECK(!(obj1 < null));
+ CHECK(!(obj2 < obj1));
+
+ // operator<=
+ CHECK(null <= obj1);
+ CHECK(null <= obj2);
+ CHECK(obj1 <= obj2);
+ CHECK(null <= null);
+ CHECK(obj1 <= obj1);
+ CHECK(!(obj1 <= null));
+ CHECK(!(obj2 <= obj1));
+
+ // operator>
+ CHECK(obj1 > null);
+ CHECK(obj2 > null);
+ CHECK(obj2 > obj1);
+ CHECK(!(null > null));
+ CHECK(!(obj1 > obj1));
+ CHECK(!(null > obj1));
+ CHECK(!(obj1 > obj2));
+
+ // operator>=
+ CHECK(obj1 >= null);
+ CHECK(obj2 >= null);
+ CHECK(obj2 >= obj1);
+ CHECK(null >= null);
+ CHECK(obj1 >= obj1);
+ CHECK(!(null >= obj1));
+ CHECK(!(obj1 >= obj2));
}
-TEST_XML(dom_node_bool_ops, "<node/>")
+template <typename T> void generic_empty_test(const T& obj)
+{
+ T null;
+
+ CHECK(null.empty());
+ CHECK(!obj.empty());
+}
+
+TEST_XML(dom_attr_bool_ops, "<node attr='1'/>")
+{
+ generic_bool_ops_test(doc.child("node").attribute("attr"));
+}
+
+TEST_XML(dom_attr_rel_ops, "<node attr1='1' attr2='2'/>")
+{
+ generic_rel_ops_test(doc.child("node").attribute("attr1"), doc.child("node").attribute("attr2"));
+}
+
+TEST_XML(dom_attr_empty, "<node attr='1'/>")
+{
+ generic_empty_test(doc.child("node").attribute("attr"));
+}
+
+TEST_XML(dom_attr_next_previous_attribute, "<node attr1='1' attr2='2' />")
{
- xml_node node1;
- xml_node node2 = doc.child("node");
+ xml_attribute attr1 = doc.child("node").attribute("attr1");
+ xml_attribute attr2 = doc.child("node").attribute("attr2");
- CHECK(!node1);
- CHECK(node2);
- CHECK(!!node2);
+ CHECK(attr1.next_attribute() == attr2);
+ CHECK(attr2.next_attribute() == xml_attribute());
+
+ CHECK(attr1.previous_attribute() == xml_attribute());
+ CHECK(attr2.previous_attribute() == attr1);
- bool node1b = node1;
- bool node2b = node2;
+ CHECK(xml_attribute().next_attribute() == xml_attribute());
+ CHECK(xml_attribute().previous_attribute() == xml_attribute());
+}
+
+TEST_XML(dom_attr_name_value, "<node attr='1'/>")
+{
+ xml_attribute attr = doc.child("node").attribute("attr");
- CHECK(!node1b);
- CHECK(node2b);
+ CHECK_STRING(attr.name(), "attr");
+ CHECK_STRING(attr.value(), "1");
+
+ CHECK_STRING(xml_attribute().name(), "");
+ CHECK_STRING(xml_attribute().value(), "");
}
-TEST_XML(dom_node_empty, "<node/>")
+TEST_XML(dom_node_bool_ops, "<node/>")
{
- xml_node node1;
- xml_node node2 = doc.child("node");
+ generic_bool_ops_test(doc.child("node"));
+}
- CHECK(node1.empty());
- CHECK(!node2.empty());
+TEST_XML(dom_node_rel_ops, "<node><node1/><node2/></node>")
+{
+ generic_rel_ops_test(doc.child("node").child("node1"), doc.child("node").child("node2"));
+}
+
+TEST_XML(dom_node_empty, "<node/>")
+{
+ generic_empty_test(doc.child("node"));
}