From 1f967ebe4085fa5225f930d6918351980bd8a351 Mon Sep 17 00:00:00 2001 From: Dan Ignatoff Date: Thu, 24 Oct 2024 11:56:51 -0700 Subject: [PATCH] Add test for hinted attribute, and add simple test coverage for all other added functions --- tests/test_dom_modify.cpp | 115 +++++++++++++++++++++++++++++++++--- tests/test_dom_text.cpp | 9 ++- tests/test_dom_traverse.cpp | 58 ++++++++++++++++++ 3 files changed, 172 insertions(+), 10 deletions(-) diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp index 71dc5540..65ef3a3a 100644 --- a/tests/test_dom_modify.cpp +++ b/tests/test_dom_modify.cpp @@ -7,6 +7,8 @@ #include #include +#include "writer_string.hpp" + using namespace pugi; TEST_XML(dom_attr_assign, "") @@ -33,7 +35,13 @@ TEST_XML(dom_attr_assign, "") node.append_attribute(STR("attr8")) = true; xml_attribute() = true; - CHECK_NODE(node, STR("")); +#ifdef PUGIXML_HAS_STRING_VIEW + node.append_attribute(string_view_t(STR("attr9"))) = string_view_t(STR("v2")); +#else + node.append_attribute(STR("attr9")) = STR("v2"); +#endif + + CHECK_NODE(node, STR("")); } TEST_XML(dom_attr_set_name, "") @@ -103,7 +111,14 @@ TEST_XML(dom_attr_set_value, "") CHECK(node.append_attribute(STR("attr10")).set_value(STR("v3foobar"), 2)); CHECK(!xml_attribute().set_value(STR("v3"))); - CHECK_NODE(node, STR("")); +#ifdef PUGIXML_HAS_STRING_VIEW + CHECK(node.append_attribute(string_view_t(STR("attr11"))).set_value(string_view_t(STR("v4")))); + CHECK(!xml_attribute().set_value(string_view_t(STR("v4")))); +#else + CHECK(node.append_attribute(STR("attr11")).set_value(STR("v4"))); +#endif + + CHECK_NODE(node, STR("")); } #if LONG_MAX > 2147483647 @@ -335,7 +350,15 @@ TEST_XML(dom_node_prepend_attribute, "") CHECK(a3 && a1 != a3 && a2 != a3); a3 = STR("v3"); - CHECK_NODE(doc, STR("")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_attribute a4 = doc.child(STR("node")).child(STR("child")).prepend_attribute(string_view_t(STR("a4"))); +#else + xml_attribute a4 = doc.child(STR("node")).child(STR("child")).prepend_attribute(STR("a4")); +#endif + CHECK(a4 && a1 != a4 && a2 != a4 && a3 != a4); + a4 = STR("v4"); + + CHECK_NODE(doc, STR("")); } TEST_XML(dom_node_append_attribute, "") @@ -385,7 +408,17 @@ TEST_XML(dom_node_insert_attribute_after, " CHECK(child.insert_attribute_after(STR("a"), a4) == xml_attribute()); - CHECK_NODE(doc, STR("")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_attribute a6 = node.insert_attribute_after(string_view_t(STR("a6")), a5); + CHECK(child.insert_attribute_after(string_view_t(STR("a")), a5) == xml_attribute()); +#else + xml_attribute a6 = node.insert_attribute_after(STR("a6"), a5); + CHECK(child.insert_attribute_after(STR("a"), a5) == xml_attribute()); +#endif + CHECK(a6 && a6 != a5 && a6 != a4 && a6 != a3 && a6 != a2 && a6 != a1); + a6 = STR("v6"); + + CHECK_NODE(doc, STR("")); } TEST_XML(dom_node_insert_attribute_before, "") @@ -415,7 +448,17 @@ TEST_XML(dom_node_insert_attribute_before, "")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_attribute a6 = node.insert_attribute_before(string_view_t(STR("a6")), a1); + CHECK(child.insert_attribute_before(string_view_t(STR("a")), a4) == xml_attribute()); +#else + xml_attribute a6 = node.insert_attribute_before(STR("a6"), a1); + CHECK(child.insert_attribute_before(STR("a"), a4) == xml_attribute()); +#endif + CHECK(a6 && a6 != a5 && a6 != a4 && a6 != a3 && a6 != a2 && a6 != a1); + a6 = STR("v6"); + + CHECK_NODE(doc, STR("")); } TEST_XML(dom_node_prepend_copy_attribute, "") @@ -589,6 +632,14 @@ TEST_XML(dom_node_remove_attribute, "") @@ -747,6 +798,10 @@ TEST_XML(dom_node_prepend_child_name, "foo") { CHECK(xml_node().prepend_child(STR("")) == xml_node()); CHECK(doc.child(STR("node")).first_child().prepend_child(STR("")) == xml_node()); +#ifdef PUGIXML_HAS_STRING_VIEW + CHECK(xml_node().prepend_child(string_view_t()) == xml_node()); + CHECK(doc.child(STR("node")).first_child().prepend_child(string_view_t()) == xml_node()); +#endif xml_node n1 = doc.child(STR("node")).prepend_child(STR("n1")); CHECK(n1); @@ -754,7 +809,14 @@ TEST_XML(dom_node_prepend_child_name, "foo") xml_node n2 = doc.child(STR("node")).prepend_child(STR("n2")); CHECK(n2 && n1 != n2); - CHECK_NODE(doc, STR("foo")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_node n3 = doc.prepend_child(string_view_t(STR("n3"))); +#else + xml_node n3 = doc.prepend_child(STR("n3")); +#endif + CHECK(n3 && n1 != n3 && n2 != n3); + + CHECK_NODE(doc, STR("foo")); } TEST_XML(dom_node_append_child_name, "foo") @@ -768,7 +830,14 @@ TEST_XML(dom_node_append_child_name, "foo") xml_node n2 = doc.child(STR("node")).append_child(STR("n2")); CHECK(n2 && n1 != n2); - CHECK_NODE(doc, STR("foo")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_node n3 = doc.append_child(string_view_t(STR("n3"))); +#else + xml_node n3 = doc.append_child(STR("n3")); +#endif + CHECK(n3 && n3 != n2 && n3 != n1); + + CHECK_NODE(doc, STR("foo")); } TEST_XML(dom_node_insert_child_after_name, "foo") @@ -790,7 +859,14 @@ TEST_XML(dom_node_insert_child_after_name, "foo") CHECK(child.insert_child_after(STR(""), n2) == xml_node()); - CHECK_NODE(doc, STR("foo")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_node n3 = node.insert_child_after(string_view_t(STR("n3")), n1); +#else + xml_node n3 = node.insert_child_after(STR("n3"), n1); +#endif + CHECK(n3 && n3 != node && n3 != child && n3 != n2 && n3 != n1); + + CHECK_NODE(doc, STR("foo")); } TEST_XML(dom_node_insert_child_before_name, "foo") @@ -812,7 +888,14 @@ TEST_XML(dom_node_insert_child_before_name, "foo") CHECK(child.insert_child_before(STR(""), n2) == xml_node()); - CHECK_NODE(doc, STR("foo")); +#ifdef PUGIXML_HAS_STRING_VIEW + xml_node n3 = node.insert_child_before(string_view_t(STR("n3")), child); +#else + xml_node n3 = node.insert_child_before(STR("n3"), child); +#endif + CHECK(n3 && n3 != node && n3 != child && n3 != n2 && n3 != n1); + + CHECK_NODE(doc, STR("foo")); } TEST_XML(dom_node_remove_child, "") @@ -834,6 +917,20 @@ TEST_XML(dom_node_remove_child, "")); + +#ifdef PUGIXML_HAS_STRING_VIEW + CHECK(!node.remove_child(string_view_t())); + CHECK(!node.remove_child(string_view_t(STR("child"), 3))); + CHECK(!node.remove_child(string_view_t(STR("n2"), 1))); + + CHECK_NODE(doc, STR("")); + + CHECK(node.remove_child(string_view_t(STR("child")))); + CHECK_NODE(doc, STR("")); + + CHECK(node.remove_child(string_view_t(STR("n2_notinview"), 2))); + CHECK_NODE(doc, STR("")); +#endif } TEST_XML(dom_node_remove_children, "") diff --git a/tests/test_dom_text.cpp b/tests/test_dom_text.cpp index d14284d2..71edc70f 100644 --- a/tests/test_dom_text.cpp +++ b/tests/test_dom_text.cpp @@ -388,7 +388,14 @@ TEST_XML(dom_text_assign, "") node.append_child(STR("text8")).text() = true; xml_text() = true; - CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25true")); +#ifdef PUGIXML_HAS_STRING_VIEW + node.append_child(string_view_t(STR("text9"))).text() = string_view_t(STR("v2")); + xml_text() = string_view_t(STR("text9")); +#else + node.append_child(STR("text9")).text() = STR("v2"); +#endif + + CHECK_NODE(node, STR("v1-2147483647-2147483648429496729542949672940.50.25truev2")); } TEST_XML(dom_text_set_value, "") diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp index ba0da35c..18b93efe 100644 --- a/tests/test_dom_traverse.cpp +++ b/tests/test_dom_traverse.cpp @@ -1250,6 +1250,64 @@ TEST_XML(dom_node_attribute_hinted, "") CHECK(!node.attribute(STR("attr"), hint) && hint == attr2); } +#ifdef PUGIXML_HAS_STRING_VIEW +TEST_XML(dom_node_attribute_hinted_stringview, "") +{ + xml_node node = doc.first_child(); + string_view_t a1name = string_view_t(STR("attr1")); + string_view_t a2name = string_view_t(STR("attr2")); + string_view_t a3name = string_view_t(STR("attr3")); + xml_attribute attr1 = node.attribute(a1name); + xml_attribute attr2 = node.attribute(a2name); + xml_attribute attr3 = node.attribute(a3name); + + xml_attribute hint; + CHECK(!xml_node().attribute(string_view_t(string_view_t(STR("test"))), hint) && !hint); + + CHECK(node.attribute(a2name, hint) == attr2 && hint == attr3); + CHECK(node.attribute(a3name, hint) == attr3 && !hint); + + CHECK(node.attribute(a1name, hint) == attr1 && hint == attr2); + CHECK(node.attribute(a2name, hint) == attr2 && hint == attr3); + CHECK(node.attribute(a1name, hint) == attr1 && hint == attr2); + CHECK(node.attribute(a1name, hint) == attr1 && hint == attr2); + + CHECK(!node.attribute(string_view_t(), hint) && hint == attr2); + CHECK(!node.attribute(string_view_t(STR("attr1"), 4), hint) && hint == attr2); // "attr" + CHECK(node.attribute(string_view_t(STR("attr3_extra"), 5), hint) == attr3 && !hint); // "attr3" +} + +TEST_XML(dom_node_attribute_hint_interior_null, "") +{ + xml_node node = doc.first_child(); + + xml_attribute attr1 = node.attribute(STR("attr1")); + xml_attribute attr2 = node.attribute(STR("attr2")); + xml_attribute attr3 = node.attribute(STR("attr3")); + + CHECK(node && attr1 && attr2 && attr3); + + const char_t name[] = STR("attr2\0extra"); + size_t len = (sizeof(name) / sizeof(char_t)) - 1; + CHECK(len == 11); + + xml_attribute hint; + CHECK(node.attribute(string_view_t(name, 5), hint) == attr2 && hint == attr3); // "attr2" + CHECK(node.attribute(string_view_t(name, 5), hint) == attr2 && hint == attr3); // "attr2" + + CHECK(!node.attribute(string_view_t(name, 6), hint) && hint == attr3); // "attr2\0" + CHECK(!node.attribute(string_view_t(name, len), hint) && hint == attr3); // "attr2\0extra" + + attr2.set_name(string_view_t(name, len)); // attr2\0extra + + CHECK(node.attribute(string_view_t(name, 5), hint) == attr2 && hint == attr3); // "attr2" + CHECK(node.attribute(string_view_t(name, 5), hint) == attr2 && hint == attr3); // "attr2" + + CHECK(!node.attribute(string_view_t(name, 6), hint) && hint == attr3); // "attr2\0" + CHECK(!node.attribute(string_view_t(name, len), hint) && hint == attr3); // "attr2\0extra" +} +#endif + TEST_XML(dom_as_int_overflow, "") { xml_node node = doc.child(STR("node"));