From b6433db22642307330311c42fffb6950287415df Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 11 Oct 2009 10:24:12 +0000 Subject: tests: Initial tree modification tests git-svn-id: http://pugixml.googlecode.com/svn/trunk@148 99668b35-9821-0410-8761-19e4c4f06640 --- tests/common.hpp | 2 -- tests/test.hpp | 12 +++++++++ tests/test_dom_modify.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/test_dom_modify.cpp diff --git a/tests/common.hpp b/tests/common.hpp index 0091e1c..306fb18 100644 --- a/tests/common.hpp +++ b/tests/common.hpp @@ -1,5 +1,3 @@ #include "test.hpp" -#include "../src/pugixml.hpp" - using namespace pugi; diff --git a/tests/test.hpp b/tests/test.hpp index fa4ef1d..10e32c7 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -3,6 +3,9 @@ #include #include +#include + +#include "../src/pugixml.hpp" inline bool test_string_equal(const char* lhs, const char* rhs) { @@ -14,6 +17,14 @@ template inline bool test_node_name_value(const Node& node, cons return test_string_equal(node.name(), name) && test_string_equal(node.value(), value); } +inline bool test_node(const pugi::xml_node& node, const char* contents) +{ + std::ostringstream oss; + node.print(oss, "", pugi::format_raw); + + return oss.str() == contents; +} + struct test_runner { test_runner(const char* name) @@ -73,5 +84,6 @@ struct dummy_fixture {}; #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 +#define CHECK_NODE(node, expected) if (test_node(node, expected)) ; else throw #node " contents does not match " #expected #endif diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp new file mode 100644 index 0000000..b775d2d --- /dev/null +++ b/tests/test_dom_modify.cpp @@ -0,0 +1,63 @@ +#include "common.hpp" + +TEST_XML(dom_attr_assign, "") +{ + xml_node node = doc.child("node"); + + node.attribute("attr1") = "v1"; + xml_attribute() = "v1"; + + node.attribute("attr2") = -2147483647 - 1; + xml_attribute() = -2147483647 - 1; + + node.attribute("attr3") = 2147483647u; + xml_attribute() = 2147483647; + + node.attribute("attr4") = 0.5; + xml_attribute() = 0.5; + + node.attribute("attr5") = true; + xml_attribute() = true; + + CHECK_NODE(node, ""); +} + +TEST_XML(dom_attr_set_value, "") +{ + xml_node node = doc.child("node"); + + CHECK(node.attribute("attr1").set_value("v1")); + CHECK(!xml_attribute().set_value("v1")); + + CHECK(node.attribute("attr2").set_value(-2147483647 - 1)); + CHECK(!xml_attribute().set_value(-2147483647 - 1)); + + CHECK(node.attribute("attr3").set_value(2147483647u)); + CHECK(!xml_attribute().set_value(2147483647)); + + CHECK(node.attribute("attr4").set_value(0.5)); + CHECK(!xml_attribute().set_value(0.5)); + + CHECK(node.attribute("attr5").set_value(true)); + CHECK(!xml_attribute().set_value(true)); + + CHECK_NODE(node, ""); +} + +TEST_XML(dom_node_set_name, "text") +{ + CHECK(doc.child("node").set_name("n")); + CHECK(!doc.child("node").first_child().set_name("n")); + CHECK(!xml_node().set_name("n")); + + CHECK_NODE(doc, "text"); +} + +TEST_XML(dom_node_set_value, "text") +{ + CHECK(doc.child("node").first_child().set_value("no text")); + CHECK(!doc.child("node").set_value("no text")); + CHECK(!xml_node().set_value("no text")); + + CHECK_NODE(doc, "no text"); +} -- cgit v1.2.3