Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_xml_parser.cpp
45997 views
1
/**************************************************************************/
2
/* test_xml_parser.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_xml_parser)
34
35
#include "core/io/xml_parser.h"
36
37
namespace TestXMLParser {
38
39
TEST_CASE("[XMLParser] End-to-end") {
40
String source = "<?xml version = \"1.0\" encoding=\"UTF-8\" ?>\
41
<top attr=\"attr value\">\
42
Text&lt;&#65;&#x42;&gt;\
43
</top>";
44
Vector<uint8_t> buff = source.to_utf8_buffer();
45
46
XMLParser parser;
47
parser.open_buffer(buff);
48
49
// <?xml ...?> gets parsed as NODE_UNKNOWN
50
CHECK(parser.read() == OK);
51
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_UNKNOWN);
52
53
CHECK(parser.read() == OK);
54
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT);
55
CHECK(parser.get_node_name() == "top");
56
CHECK(parser.has_attribute("attr"));
57
CHECK(parser.get_named_attribute_value("attr") == "attr value");
58
59
CHECK(parser.read() == OK);
60
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_TEXT);
61
CHECK(parser.get_node_data().lstrip(" \t") == "Text<AB>");
62
63
CHECK(parser.read() == OK);
64
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT_END);
65
CHECK(parser.get_node_name() == "top");
66
67
parser.close();
68
}
69
70
TEST_CASE("[XMLParser] Comments") {
71
XMLParser parser;
72
73
SUBCASE("Missing end of comment") {
74
const String input = "<first></first><!-- foo";
75
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
76
REQUIRE_EQ(parser.read(), OK);
77
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
78
REQUIRE_EQ(parser.read(), OK);
79
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
80
REQUIRE_EQ(parser.read(), OK);
81
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
82
CHECK_EQ(parser.get_node_name(), " foo");
83
}
84
SUBCASE("Bad start of comment") {
85
const String input = "<first></first><!-";
86
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
87
REQUIRE_EQ(parser.read(), OK);
88
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
89
REQUIRE_EQ(parser.read(), OK);
90
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
91
REQUIRE_EQ(parser.read(), OK);
92
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
93
CHECK_EQ(parser.get_node_name(), "-");
94
}
95
SUBCASE("Unblanced angle brackets in comment") {
96
const String input = "<!-- example << --><next-tag></next-tag>";
97
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
98
REQUIRE_EQ(parser.read(), OK);
99
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
100
CHECK_EQ(parser.get_node_name(), " example << ");
101
}
102
SUBCASE("Doctype") {
103
const String input = "<!DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]>";
104
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
105
REQUIRE_EQ(parser.read(), OK);
106
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
107
CHECK_EQ(parser.get_node_name(), "DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]");
108
}
109
}
110
111
TEST_CASE("[XMLParser] Premature endings") {
112
SUBCASE("Simple cases") {
113
String input;
114
String expected_name;
115
XMLParser::NodeType expected_type;
116
117
SUBCASE("Incomplete Unknown") {
118
input = "<first></first><?xml";
119
expected_type = XMLParser::NodeType::NODE_UNKNOWN;
120
expected_name = "?xml";
121
}
122
SUBCASE("Incomplete CDStart") {
123
input = "<first></first><![CD";
124
expected_type = XMLParser::NodeType::NODE_CDATA;
125
expected_name = "";
126
}
127
SUBCASE("Incomplete CData") {
128
input = "<first></first><![CDATA[example";
129
expected_type = XMLParser::NodeType::NODE_CDATA;
130
expected_name = "example";
131
}
132
SUBCASE("Incomplete CDEnd") {
133
input = "<first></first><![CDATA[example]]";
134
expected_type = XMLParser::NodeType::NODE_CDATA;
135
expected_name = "example]]";
136
}
137
SUBCASE("Incomplete start-tag name") {
138
input = "<first></first><second";
139
expected_type = XMLParser::NodeType::NODE_ELEMENT;
140
expected_name = "second";
141
}
142
143
XMLParser parser;
144
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
145
REQUIRE_EQ(parser.read(), OK);
146
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
147
REQUIRE_EQ(parser.read(), OK);
148
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
149
REQUIRE_EQ(parser.read(), OK);
150
CHECK_EQ(parser.get_node_type(), expected_type);
151
CHECK_EQ(parser.get_node_name(), expected_name);
152
}
153
154
SUBCASE("With attributes and texts") {
155
XMLParser parser;
156
157
SUBCASE("Incomplete start-tag attribute name") {
158
const String input = "<first></first><second attr1=\"foo\" attr2";
159
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
160
REQUIRE_EQ(parser.read(), OK);
161
REQUIRE_EQ(parser.read(), OK);
162
REQUIRE_EQ(parser.read(), OK);
163
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
164
CHECK_EQ(parser.get_node_name(), "second");
165
CHECK_EQ(parser.get_attribute_count(), 1);
166
CHECK_EQ(parser.get_attribute_name(0), "attr1");
167
CHECK_EQ(parser.get_attribute_value(0), "foo");
168
}
169
170
SUBCASE("Incomplete start-tag attribute unquoted value") {
171
const String input = "<first></first><second attr1=\"foo\" attr2=bar";
172
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
173
REQUIRE_EQ(parser.read(), OK);
174
REQUIRE_EQ(parser.read(), OK);
175
REQUIRE_EQ(parser.read(), OK);
176
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
177
CHECK_EQ(parser.get_node_name(), "second");
178
CHECK_EQ(parser.get_attribute_count(), 1);
179
CHECK_EQ(parser.get_attribute_name(0), "attr1");
180
CHECK_EQ(parser.get_attribute_value(0), "foo");
181
}
182
183
SUBCASE("Incomplete start-tag attribute quoted value") {
184
const String input = "<first></first><second attr1=\"foo\" attr2=\"bar";
185
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
186
REQUIRE_EQ(parser.read(), OK);
187
REQUIRE_EQ(parser.read(), OK);
188
REQUIRE_EQ(parser.read(), OK);
189
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
190
CHECK_EQ(parser.get_node_name(), "second");
191
CHECK_EQ(parser.get_attribute_count(), 2);
192
CHECK_EQ(parser.get_attribute_name(0), "attr1");
193
CHECK_EQ(parser.get_attribute_value(0), "foo");
194
CHECK_EQ(parser.get_attribute_name(1), "attr2");
195
CHECK_EQ(parser.get_attribute_value(1), "bar");
196
}
197
198
SUBCASE("Incomplete end-tag name") {
199
const String input = "<first></fir";
200
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
201
REQUIRE_EQ(parser.read(), OK);
202
REQUIRE_EQ(parser.read(), OK);
203
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
204
CHECK_EQ(parser.get_node_name(), "fir");
205
}
206
207
SUBCASE("Trailing text") {
208
const String input = "<first></first>example";
209
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
210
REQUIRE_EQ(parser.read(), OK);
211
REQUIRE_EQ(parser.read(), OK);
212
REQUIRE_EQ(parser.read(), OK);
213
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_TEXT);
214
CHECK_EQ(parser.get_node_data(), "example");
215
}
216
}
217
}
218
219
TEST_CASE("[XMLParser] CDATA") {
220
const String input = "<a><![CDATA[my cdata content goes here]]></a>";
221
XMLParser parser;
222
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
223
REQUIRE_EQ(parser.read(), OK);
224
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
225
CHECK_EQ(parser.get_node_name(), "a");
226
REQUIRE_EQ(parser.read(), OK);
227
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_CDATA);
228
CHECK_EQ(parser.get_node_name(), "my cdata content goes here");
229
REQUIRE_EQ(parser.read(), OK);
230
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
231
CHECK_EQ(parser.get_node_name(), "a");
232
}
233
234
} // namespace TestXMLParser
235
236