-
Notifications
You must be signed in to change notification settings - Fork 89
/
iterators.cpp
131 lines (124 loc) · 3.96 KB
/
iterators.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Created by dave on 10/07/2024.
//
#include <gtest/gtest.h>
#include <list>
#include <algorithm>
#include <ranges>
#include "rapidxml.hpp"
TEST(Iterators, Nodes) {
std::string xml = "<children><one/><two/><three/></children>";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
int i = 0;
for (auto & child : doc.first_node()->children()) {
++i;
switch(i) {
case 1:
EXPECT_EQ(child.name(), "one");
break;
case 2:
EXPECT_EQ(child.name(), "two");
break;
case 3:
EXPECT_EQ(child.name(), "three");
break;
}
}
EXPECT_EQ(i, 3);
}
TEST(Iterators, Attributes) {
std::string xml = R"(<children one="1" two="2" three="3"/>)";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
int i = 0;
for (auto & child : doc.first_node()->attributes()) {
++i;
switch(i) {
case 1:
EXPECT_EQ(child.name(), "one");
break;
case 2:
EXPECT_EQ(child.name(), "two");
break;
case 3:
EXPECT_EQ(child.name(), "three");
break;
}
}
EXPECT_EQ(i, 3);
}
TEST(Predicates, Nodes) {
std::string xml = "<children><one/><two/><three/></children>";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
auto r = doc.first_node()->children();
for (auto const & child : r | std::ranges::views::filter([](auto const & n) { return n.name() == "two"; })) {
EXPECT_EQ(child.name(), "two");
}
auto c = std::ranges::count_if(r, [](auto const & n) { return n.name() == "two"; });
EXPECT_EQ(c, 1);
auto match = std::ranges::find_if(r, [](auto const & n) { return n.name() == "two"; });
EXPECT_EQ(match->name(), "two");
}
TEST(Predicates, AllNodes) {
std::string xml = "<children><one><two/></one><three><four><five/></four><six/></three></children>";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
auto it = rapidxml::descendant_iterator<>(doc.first_node());
EXPECT_EQ(it->name(), "one");
++it;
EXPECT_EQ(it->name(), "two");
++it;
EXPECT_EQ(it->name(), "three");
++it;
EXPECT_EQ(it->name(), "four");
++it;
EXPECT_EQ(it->name(), "five");
++it;
EXPECT_EQ(it->name(), "six");
++it;
EXPECT_FALSE(it.valid());
}
TEST(Predicates, AllNodesRev) {
std::string xml = "<children><one><two/></one><three><four><five/></four><six/></three></children>";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
auto it = rapidxml::descendant_iterator<>(doc.first_node());
EXPECT_EQ(it->name(), "one");
++it;
EXPECT_EQ(it->name(), "two");
++it;
EXPECT_EQ(it->name(), "three");
++it;
EXPECT_EQ(it->name(), "four");
++it;
EXPECT_EQ(it->name(), "five");
++it;
EXPECT_EQ(it->name(), "six");
--it;
EXPECT_EQ(it->name(), "five");
--it;
EXPECT_EQ(it->name(), "four");
--it;
EXPECT_EQ(it->name(), "three");
--it;
EXPECT_EQ(it->name(), "two");
--it;
EXPECT_EQ(it->name(), "one");
}
TEST(Predicates, Attributes) {
std::string xml = R"(<children one="1" two="2" three="3"/>)";
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_full>(xml);
auto r = doc.first_node()->attributes();
for (auto const & child : r | std::ranges::views::filter([](auto const & n) { return n.name() == "two"; })) {
EXPECT_EQ(child.name(), "two");
}
auto c = std::ranges::count_if(r, [](auto const & n) { return n.name() == "two"; });
EXPECT_EQ(c, 1);
auto match = std::ranges::find_if(r, [](auto const & n) { return n.name() == "two"; });
EXPECT_EQ(match->name(), "two");
auto match2 = std::ranges::find_if(doc.first_node()->attributes(), [](auto const & n) { return n.name() == "two"; });
EXPECT_EQ(match2->name(), "two");
}