From 6ff7e795752b0cfc3d85e447bab063667e786150 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sat, 10 Oct 2009 21:58:44 +0000 Subject: tests: Added more node/attribute tests git-svn-id: http://pugixml.googlecode.com/svn/trunk@141 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test.hpp | 8 +++ tests/test_dom_traverse.cpp | 150 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 128 insertions(+), 30 deletions(-) (limited to 'tests') 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 + +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, "") +#include + +template 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, "") +template 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, "") +template void generic_empty_test(const T& obj) +{ + T null; + + CHECK(null.empty()); + CHECK(!obj.empty()); +} + +TEST_XML(dom_attr_bool_ops, "") +{ + generic_bool_ops_test(doc.child("node").attribute("attr")); +} + +TEST_XML(dom_attr_rel_ops, "") +{ + generic_rel_ops_test(doc.child("node").attribute("attr1"), doc.child("node").attribute("attr2")); +} + +TEST_XML(dom_attr_empty, "") +{ + generic_empty_test(doc.child("node").attribute("attr")); +} + +TEST_XML(dom_attr_next_previous_attribute, "") { - 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, "") +{ + 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, "") +TEST_XML(dom_node_bool_ops, "") { - 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, "") +{ + generic_rel_ops_test(doc.child("node").child("node1"), doc.child("node").child("node2")); +} + +TEST_XML(dom_node_empty, "") +{ + generic_empty_test(doc.child("node")); } -- cgit v1.2.3