/
xml_spec.cr
362 lines (300 loc) Β· 8.52 KB
/
xml_spec.cr
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require "spec"
require "xml"
describe XML do
it "parses" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<people>
<person id="1" id2="2">
<name>John</name>
</person>
</people>
XML
)
doc.document.should eq(doc)
doc.name.should eq("document")
doc.attributes.empty?.should be_true
doc.namespace.should be_nil
people = doc.root.not_nil!
people.name.should eq("people")
people.type.should eq(XML::Type::ELEMENT_NODE)
people.attributes.empty?.should be_true
children = doc.children
children.size.should eq(1)
children.empty?.should be_false
people = children[0]
people.name.should eq("people")
people.document.should eq(doc)
children = people.children
children.size.should eq(3)
text = children[0]
text.name.should eq("text")
text.content.should eq("\n ")
person = children[1]
person.name.should eq("person")
text = children[2]
text.content.should eq("\n")
attrs = person.attributes
attrs.empty?.should be_false
attrs.size.should eq(2)
attr = attrs[0]
attr.name.should eq("id")
attr.content.should eq("1")
attr.text.should eq("1")
attr.inner_text.should eq("1")
attr = attrs[1]
attr.name.should eq("id2")
attr.content.should eq("2")
attrs["id"].content.should eq("1")
attrs["id2"].content.should eq("2")
attrs["id3"]?.should be_nil
expect_raises(KeyError) { attrs["id3"] }
person["id"].should eq("1")
person["id2"].should eq("2")
person["id3"]?.should be_nil
expect_raises(KeyError) { person["id3"] }
name = person.children.find { |node| node.name == "name" }.not_nil!
name.content.should eq("John")
name.parent.should eq(person)
end
it "parses from io" do
io = IO::Memory.new(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<people>
<person id="1" id2="2">
<name>John</name>
</person>
</people>
XML
)
doc = XML.parse(io)
doc.document.should eq(doc)
doc.name.should eq("document")
people = doc.children.find { |node| node.name == "people" }.not_nil!
person = people.children.find { |node| node.name == "person" }.not_nil!
person["id"].should eq("1")
end
it "raises exception on empty string" do
expect_raises XML::Error, "Document is empty" do
XML.parse("")
end
end
it "does to_s" do
string = <<-XML
<?xml version='1.0' encoding='UTF-8'?>\
<people>
<person id="1" id2="2">
<name>John</name>
</person>
</people>
XML
doc = XML.parse(string)
doc.to_s.strip.should eq(<<-XML
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person id="1" id2="2">
<name>John</name>
</person>
</people>
XML
)
end
it "navigates in tree" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<people>
<person id="1" />
<person id="2" />
</people>
XML
)
people = doc.first_element_child.not_nil!
people.name.should eq("people")
person = people.first_element_child.not_nil!
person.name.should eq("person")
person["id"].should eq("1")
text = person.next.not_nil!
text.content.should eq("\n ")
text.previous.should eq(person)
text.previous_sibling.should eq(person)
person.next_sibling.should eq(text)
person2 = text.next.not_nil!
person2.name.should eq("person")
person2["id"].should eq("2")
person.next_element.should eq(person2)
person2.previous_element.should eq(person)
end
it "handles errors" do
xml = XML.parse(%(<people>))
xml.root.not_nil!.name.should eq("people")
errors = xml.errors.not_nil!
errors.size.should eq(1)
errors[0].message.should eq("Premature end of data in tag people line 1")
errors[0].line_number.should eq(1)
errors[0].to_s.should eq("Premature end of data in tag people line 1")
end
it "gets root namespaces scopes" do
doc = XML.parse(<<-XML
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
</feed>
XML
)
namespaces = doc.root.not_nil!.namespace_scopes
namespaces.size.should eq(2)
namespaces[0].href.should eq("http://www.w3.org/2005/Atom")
namespaces[0].prefix.should be_nil
namespaces[1].href.should eq("http://a9.com/-/spec/opensearchrss/1.0/")
namespaces[1].prefix.should eq("openSearch")
end
it "returns empty array if no namespaces scopes exists" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<name>John</name>
XML
)
namespaces = doc.root.not_nil!.namespace_scopes
namespaces.size.should eq(0)
end
it "gets root namespaces as hash" do
doc = XML.parse(<<-XML
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
</feed>
XML
)
namespaces = doc.root.not_nil!.namespaces
namespaces.should eq({
"xmlns" => "http://www.w3.org/2005/Atom",
"xmlns:openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
})
end
it "reads big xml file (#1455)" do
content = "." * 20_000
string = %(<?xml version="1.0"?><root>#{content}</root>)
parsed = XML.parse(IO::Memory.new(string))
parsed.root.not_nil!.children[0].text.should eq(content)
end
it "sets node text/content" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<name>John</name>
XML
)
root = doc.root.not_nil!
root.text = "Peter"
root.text.should eq("Peter")
root.content = "Foo π"
root.content.should eq("Foo π")
end
it "doesn't set invalid node content" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<name>John</name>
XML
)
root = doc.root.not_nil!
expect_raises(Exception, "Cannot escape") do
root.content = "\0"
end
end
it "gets empty content" do
doc = XML.parse("<foo/>")
doc.children.first.content.should eq("")
end
it "sets node name" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<name>John</name>
XML
)
root = doc.root.not_nil!
root.name = "last-name"
root.name.should eq("last-name")
end
it "doesn't set invalid node name" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<name>John</name>
XML
)
root = doc.root.not_nil!
expect_raises(XML::Error, "Invalid node name") do
root.name = " foo bar"
end
expect_raises(XML::Error, "Invalid node name") do
root.name = "foo bar"
end
expect_raises(XML::Error, "Invalid node name") do
root.name = "1foo"
end
expect_raises(XML::Error, "Invalid node name") do
root.name = "\0foo"
end
end
it "gets encoding" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<people>
</people>
XML
)
doc.encoding.should eq("UTF-8")
end
it "gets encoding when nil" do
doc = XML.parse(<<-XML
<?xml version='1.0'>
<people>
</people>
XML
)
doc.encoding.should be_nil
end
it "gets version" do
doc = XML.parse(<<-XML
<?xml version='1.0' encoding='UTF-8'?>
<people>
</people>
XML
)
doc.version.should eq("1.0")
end
it "unlinks nodes" do
xml = <<-XML
<person id="1">
<firstname>Jane</firstname>
<lastname>Doe</lastname>
</person>
XML
document = XML.parse(xml)
node = document.xpath_node("//lastname").not_nil!
node.unlink
document.xpath_node("//lastname").should eq(nil)
end
it "does to_s with correct encoding (#2319)" do
xml_str = <<-XML
<?xml version='1.0' encoding='UTF-8'?>
<person>
<name>γγγ</name>
</person>
XML
doc = XML.parse(xml_str)
doc.root.to_s.should eq("<person>\n <name>γγγ</name>\n</person>")
end
it "sets an attribute" do
doc = XML.parse(%{<foo />})
root = doc.root.not_nil!
root["bar"] = "baz"
root["bar"].should eq("baz")
root.to_s.should eq(%{<foo bar="baz"/>})
end
it "changes an attribute" do
doc = XML.parse(%{<foo bar="baz"></foo>})
root = doc.root.not_nil!
root["bar"] = "baz"
root["bar"].should eq("baz")
root.to_s.should eq(%{<foo bar="baz"/>})
root["bar"] = 1
root["bar"].should eq("1")
end
end