Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_xml_parser.h
10278 views
1
/**************************************************************************/
2
/* test_xml_parser.h */
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
#pragma once
32
33
#include "core/io/xml_parser.h"
34
35
#include "tests/test_macros.h"
36
37
namespace TestXMLParser {
38
TEST_CASE("[XMLParser] End-to-end") {
39
String source = "<?xml version = \"1.0\" encoding=\"UTF-8\" ?>\
40
<top attr=\"attr value\">\
41
Text&lt;&#65;&#x42;&gt;\
42
</top>";
43
Vector<uint8_t> buff = source.to_utf8_buffer();
44
45
XMLParser parser;
46
parser.open_buffer(buff);
47
48
// <?xml ...?> gets parsed as NODE_UNKNOWN
49
CHECK(parser.read() == OK);
50
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_UNKNOWN);
51
52
CHECK(parser.read() == OK);
53
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT);
54
CHECK(parser.get_node_name() == "top");
55
CHECK(parser.has_attribute("attr"));
56
CHECK(parser.get_named_attribute_value("attr") == "attr value");
57
58
CHECK(parser.read() == OK);
59
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_TEXT);
60
CHECK(parser.get_node_data().lstrip(" \t") == "Text<AB>");
61
62
CHECK(parser.read() == OK);
63
CHECK(parser.get_node_type() == XMLParser::NodeType::NODE_ELEMENT_END);
64
CHECK(parser.get_node_name() == "top");
65
66
parser.close();
67
}
68
69
TEST_CASE("[XMLParser] Comments") {
70
XMLParser parser;
71
72
SUBCASE("Missing end of comment") {
73
const String input = "<first></first><!-- foo";
74
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
75
REQUIRE_EQ(parser.read(), OK);
76
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
77
REQUIRE_EQ(parser.read(), OK);
78
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
79
REQUIRE_EQ(parser.read(), OK);
80
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
81
CHECK_EQ(parser.get_node_name(), " foo");
82
}
83
SUBCASE("Bad start of comment") {
84
const String input = "<first></first><!-";
85
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
86
REQUIRE_EQ(parser.read(), OK);
87
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
88
REQUIRE_EQ(parser.read(), OK);
89
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
90
REQUIRE_EQ(parser.read(), OK);
91
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
92
CHECK_EQ(parser.get_node_name(), "-");
93
}
94
SUBCASE("Unblanced angle brackets in comment") {
95
const String input = "<!-- example << --><next-tag></next-tag>";
96
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
97
REQUIRE_EQ(parser.read(), OK);
98
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
99
CHECK_EQ(parser.get_node_name(), " example << ");
100
}
101
SUBCASE("Doctype") {
102
const String input = "<!DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]>";
103
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
104
REQUIRE_EQ(parser.read(), OK);
105
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
106
CHECK_EQ(parser.get_node_name(), "DOCTYPE greeting [<!ELEMENT greeting (#PCDATA)>]");
107
}
108
}
109
110
TEST_CASE("[XMLParser] Premature endings") {
111
SUBCASE("Simple cases") {
112
String input;
113
String expected_name;
114
XMLParser::NodeType expected_type;
115
116
SUBCASE("Incomplete Unknown") {
117
input = "<first></first><?xml";
118
expected_type = XMLParser::NodeType::NODE_UNKNOWN;
119
expected_name = "?xml";
120
}
121
SUBCASE("Incomplete CDStart") {
122
input = "<first></first><![CD";
123
expected_type = XMLParser::NodeType::NODE_CDATA;
124
expected_name = "";
125
}
126
SUBCASE("Incomplete CData") {
127
input = "<first></first><![CDATA[example";
128
expected_type = XMLParser::NodeType::NODE_CDATA;
129
expected_name = "example";
130
}
131
SUBCASE("Incomplete CDEnd") {
132
input = "<first></first><![CDATA[example]]";
133
expected_type = XMLParser::NodeType::NODE_CDATA;
134
expected_name = "example]]";
135
}
136
SUBCASE("Incomplete start-tag name") {
137
input = "<first></first><second";
138
expected_type = XMLParser::NodeType::NODE_ELEMENT;
139
expected_name = "second";
140
}
141
142
XMLParser parser;
143
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
144
REQUIRE_EQ(parser.read(), OK);
145
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
146
REQUIRE_EQ(parser.read(), OK);
147
REQUIRE_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
148
REQUIRE_EQ(parser.read(), OK);
149
CHECK_EQ(parser.get_node_type(), expected_type);
150
CHECK_EQ(parser.get_node_name(), expected_name);
151
}
152
153
SUBCASE("With attributes and texts") {
154
XMLParser parser;
155
156
SUBCASE("Incomplete start-tag attribute name") {
157
const String input = "<first></first><second attr1=\"foo\" attr2";
158
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
159
REQUIRE_EQ(parser.read(), OK);
160
REQUIRE_EQ(parser.read(), OK);
161
REQUIRE_EQ(parser.read(), OK);
162
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
163
CHECK_EQ(parser.get_node_name(), "second");
164
CHECK_EQ(parser.get_attribute_count(), 1);
165
CHECK_EQ(parser.get_attribute_name(0), "attr1");
166
CHECK_EQ(parser.get_attribute_value(0), "foo");
167
}
168
169
SUBCASE("Incomplete start-tag attribute unquoted value") {
170
const String input = "<first></first><second attr1=\"foo\" attr2=bar";
171
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
172
REQUIRE_EQ(parser.read(), OK);
173
REQUIRE_EQ(parser.read(), OK);
174
REQUIRE_EQ(parser.read(), OK);
175
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
176
CHECK_EQ(parser.get_node_name(), "second");
177
CHECK_EQ(parser.get_attribute_count(), 1);
178
CHECK_EQ(parser.get_attribute_name(0), "attr1");
179
CHECK_EQ(parser.get_attribute_value(0), "foo");
180
}
181
182
SUBCASE("Incomplete start-tag attribute quoted value") {
183
const String input = "<first></first><second attr1=\"foo\" attr2=\"bar";
184
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
185
REQUIRE_EQ(parser.read(), OK);
186
REQUIRE_EQ(parser.read(), OK);
187
REQUIRE_EQ(parser.read(), OK);
188
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
189
CHECK_EQ(parser.get_node_name(), "second");
190
CHECK_EQ(parser.get_attribute_count(), 2);
191
CHECK_EQ(parser.get_attribute_name(0), "attr1");
192
CHECK_EQ(parser.get_attribute_value(0), "foo");
193
CHECK_EQ(parser.get_attribute_name(1), "attr2");
194
CHECK_EQ(parser.get_attribute_value(1), "bar");
195
}
196
197
SUBCASE("Incomplete end-tag name") {
198
const String input = "<first></fir";
199
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
200
REQUIRE_EQ(parser.read(), OK);
201
REQUIRE_EQ(parser.read(), OK);
202
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
203
CHECK_EQ(parser.get_node_name(), "fir");
204
}
205
206
SUBCASE("Trailing text") {
207
const String input = "<first></first>example";
208
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
209
REQUIRE_EQ(parser.read(), OK);
210
REQUIRE_EQ(parser.read(), OK);
211
REQUIRE_EQ(parser.read(), OK);
212
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_TEXT);
213
CHECK_EQ(parser.get_node_data(), "example");
214
}
215
}
216
}
217
218
TEST_CASE("[XMLParser] CDATA") {
219
const String input = "<a><![CDATA[my cdata content goes here]]></a>";
220
XMLParser parser;
221
REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
222
REQUIRE_EQ(parser.read(), OK);
223
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT);
224
CHECK_EQ(parser.get_node_name(), "a");
225
REQUIRE_EQ(parser.read(), OK);
226
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_CDATA);
227
CHECK_EQ(parser.get_node_name(), "my cdata content goes here");
228
REQUIRE_EQ(parser.read(), OK);
229
CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_ELEMENT_END);
230
CHECK_EQ(parser.get_node_name(), "a");
231
}
232
} // namespace TestXMLParser
233
234