Skip to content

Commit

Permalink
Add test for hinted attribute, and add simple test coverage for all o…
Browse files Browse the repository at this point in the history
…ther added functions
  • Loading branch information
Dan Ignatoff committed Oct 24, 2024
1 parent 1916949 commit 1f967eb
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 10 deletions.
115 changes: 106 additions & 9 deletions tests/test_dom_modify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string.h>
#include <limits.h>

#include "writer_string.hpp"

using namespace pugi;

TEST_XML(dom_attr_assign, "<node/>")
Expand All @@ -33,7 +35,13 @@ TEST_XML(dom_attr_assign, "<node/>")
node.append_attribute(STR("attr8")) = true;
xml_attribute() = true;

CHECK_NODE(node, STR("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\"/>"));
#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("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\" attr9=\"v2\"/>"));
}

TEST_XML(dom_attr_set_name, "<node attr='value' />")
Expand Down Expand Up @@ -103,7 +111,14 @@ TEST_XML(dom_attr_set_value, "<node/>")
CHECK(node.append_attribute(STR("attr10")).set_value(STR("v3foobar"), 2));
CHECK(!xml_attribute().set_value(STR("v3")));

CHECK_NODE(node, STR("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\" attr9=\"v2\" attr10=\"v3\"/>"));
#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("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"0.25\" attr8=\"true\" attr9=\"v2\" attr10=\"v3\" attr11=\"v4\"/>"));
}

#if LONG_MAX > 2147483647
Expand Down Expand Up @@ -335,7 +350,15 @@ TEST_XML(dom_node_prepend_attribute, "<node><child/></node>")
CHECK(a3 && a1 != a3 && a2 != a3);
a3 = STR("v3");

CHECK_NODE(doc, STR("<node a2=\"v2\" a1=\"v1\"><child a3=\"v3\"/></node>"));
#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("<node a2=\"v2\" a1=\"v1\"><child a4=\"v4\" a3=\"v3\"/></node>"));
}

TEST_XML(dom_node_append_attribute, "<node><child/></node>")
Expand Down Expand Up @@ -385,7 +408,17 @@ TEST_XML(dom_node_insert_attribute_after, "<node a1='v1'><child a2='v2'/></node>

CHECK(child.insert_attribute_after(STR("a"), a4) == xml_attribute());

CHECK_NODE(doc, STR("<node a1=\"v1\" a4=\"v4\" a3=\"v3\" a5=\"v5\"><child a2=\"v2\"/></node>"));
#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("<node a1=\"v1\" a4=\"v4\" a3=\"v3\" a5=\"v5\" a6=\"v6\"><child a2=\"v2\"/></node>"));
}

TEST_XML(dom_node_insert_attribute_before, "<node a1='v1'><child a2='v2'/></node>")
Expand Down Expand Up @@ -415,7 +448,17 @@ TEST_XML(dom_node_insert_attribute_before, "<node a1='v1'><child a2='v2'/></node

CHECK(child.insert_attribute_before(STR("a"), a4) == xml_attribute());

CHECK_NODE(doc, STR("<node a5=\"v5\" a3=\"v3\" a4=\"v4\" a1=\"v1\"><child a2=\"v2\"/></node>"));
#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("<node a5=\"v5\" a3=\"v3\" a4=\"v4\" a6=\"v6\" a1=\"v1\"><child a2=\"v2\"/></node>"));
}

TEST_XML(dom_node_prepend_copy_attribute, "<node a1='v1'><child a2='v2'/><child/></node>")
Expand Down Expand Up @@ -589,6 +632,14 @@ TEST_XML(dom_node_remove_attribute, "<node a1='v1' a2='v2' a3='v3'><child a4='v4
CHECK(child.remove_attribute(STR("a4")));

CHECK_NODE(doc, STR("<node a2=\"v2\"><child/></node>"));

#ifdef PUGIXML_HAS_STRING_VIEW
CHECK(!node.remove_attribute(string_view_t()));
CHECK(!node.remove_attribute(string_view_t(STR("a2"), 1)));
CHECK(node.remove_attribute(string_view_t(STR("a2extra"), 2)));

CHECK_NODE(doc, STR("<node><child/></node>"));
#endif
}

TEST_XML(dom_node_remove_attributes, "<node a1='v1' a2='v2' a3='v3'><child a4='v4'/></node>")
Expand Down Expand Up @@ -747,14 +798,25 @@ TEST_XML(dom_node_prepend_child_name, "<node>foo<child/></node>")
{
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);

xml_node n2 = doc.child(STR("node")).prepend_child(STR("n2"));
CHECK(n2 && n1 != n2);

CHECK_NODE(doc, STR("<node><n2/><n1/>foo<child/></node>"));
#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("<n3/><node><n2/><n1/>foo<child/></node>"));
}

TEST_XML(dom_node_append_child_name, "<node>foo<child/></node>")
Expand All @@ -768,7 +830,14 @@ TEST_XML(dom_node_append_child_name, "<node>foo<child/></node>")
xml_node n2 = doc.child(STR("node")).append_child(STR("n2"));
CHECK(n2 && n1 != n2);

CHECK_NODE(doc, STR("<node>foo<child/><n1/><n2/></node>"));
#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("<node>foo<child/><n1/><n2/></node><n3/>"));
}

TEST_XML(dom_node_insert_child_after_name, "<node>foo<child/></node>")
Expand All @@ -790,7 +859,14 @@ TEST_XML(dom_node_insert_child_after_name, "<node>foo<child/></node>")

CHECK(child.insert_child_after(STR(""), n2) == xml_node());

CHECK_NODE(doc, STR("<node>foo<child/><n2/><n1/></node>"));
#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("<node>foo<child/><n2/><n1/><n3/></node>"));
}

TEST_XML(dom_node_insert_child_before_name, "<node>foo<child/></node>")
Expand All @@ -812,7 +888,14 @@ TEST_XML(dom_node_insert_child_before_name, "<node>foo<child/></node>")

CHECK(child.insert_child_before(STR(""), n2) == xml_node());

CHECK_NODE(doc, STR("<node>foo<n1/><n2/><child/></node>"));
#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("<node>foo<n1/><n2/><n3/><child/></node>"));
}

TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node>")
Expand All @@ -834,6 +917,20 @@ TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node
CHECK(child.remove_child(STR("n4")));

CHECK_NODE(doc, STR("<node><n2/><child/></node>"));

#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("<node><n2/><child/></node>"));

CHECK(node.remove_child(string_view_t(STR("child"))));
CHECK_NODE(doc, STR("<node><n2/></node>"));

CHECK(node.remove_child(string_view_t(STR("n2_notinview"), 2)));
CHECK_NODE(doc, STR("<node/>"));
#endif
}

TEST_XML(dom_node_remove_children, "<node><n1/><n2/><n3/><child><n4/></child></node>")
Expand Down
9 changes: 8 additions & 1 deletion tests/test_dom_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,14 @@ TEST_XML(dom_text_assign, "<node/>")
node.append_child(STR("text8")).text() = true;
xml_text() = true;

CHECK_NODE(node, STR("<node><text1>v1</text1><text2>-2147483647</text2><text3>-2147483648</text3><text4>4294967295</text4><text5>4294967294</text5><text6>0.5</text6><text7>0.25</text7><text8>true</text8></node>"));
#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("<node><text1>v1</text1><text2>-2147483647</text2><text3>-2147483648</text3><text4>4294967295</text4><text5>4294967294</text5><text6>0.5</text6><text7>0.25</text7><text8>true</text8><text9>v2</text9></node>"));
}

TEST_XML(dom_text_set_value, "<node/>")
Expand Down
58 changes: 58 additions & 0 deletions tests/test_dom_traverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,64 @@ TEST_XML(dom_node_attribute_hinted, "<node attr1='1' attr2='2' attr3='3' />")
CHECK(!node.attribute(STR("attr"), hint) && hint == attr2);
}

#ifdef PUGIXML_HAS_STRING_VIEW
TEST_XML(dom_node_attribute_hinted_stringview, "<node attr1='1' attr2='2' attr3='3' />")
{
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, "<node attr1='1' attr2='2' attr3='3' />")
{
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, "<node attr1='-2147483649' attr2='2147483648' attr3='-4294967296' />")
{
xml_node node = doc.child(STR("node"));
Expand Down

0 comments on commit 1f967eb

Please sign in to comment.