sled/tests/pugixml_test.cc
2024-05-15 19:51:27 +08:00

49 lines
1.2 KiB
C++

#include <sled/sled.h>
static constexpr char kXmlA[] =
R"(<?xml version="1.0"?>
<root>
<str>hello</str>
<node id="1">text1</node>
<node id="2">text2</node>
<node id="3">text3</node>
</root>
)";
static constexpr char kXmlB[] =
R"(<?xml version="1.0"?>
<root>
<level1>
<level2>
<node id="1">text1</node>
<node id="2">text2</node>
<node id="3">text3</node>
</level2>
</level1>
</root>)";
TEST_SUITE("pugixml")
{
TEST_CASE("xpath query")
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(kXmlA);
CHECK(result);
pugi::xpath_node_set nodes = doc.select_nodes("//node");
CHECK_EQ(nodes.size(), 3);
CHECK_EQ(nodes[0].node().attribute("id").as_int(), 1);
CHECK_EQ(nodes[1].node().attribute("id").as_int(), 2);
CHECK_EQ(nodes[2].node().attribute("id").as_int(), 3);
}
TEST_CASE("xpath *")
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(kXmlA);
CHECK(result);
pugi::xpath_node_set nodes = doc.select_nodes("/*/str");
CHECK(nodes.size() == 1);
CHECK_EQ(std::string("hello"), nodes[0].node().text().as_string());
}
}