forked from JuliaData/XML.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntests.jl
More file actions
722 lines (641 loc) · 27.2 KB
/
Copy pathruntests.jl
File metadata and controls
722 lines (641 loc) · 27.2 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
using XML
using XML: Document, Element, Declaration, Comment, CData, DTD, ProcessingInstruction, Text, escape, unescape, OrderedDict, h
using Downloads: download
using Test
import AbstractTrees
AbstractTrees.children(x::Node) = children(x)
#-----------------------------------------------------------------------------# files
xml_xsd = joinpath("data", "xml.xsd")
kml_xsd = joinpath("data", "kml.xsd")
books_xml = joinpath("data", "books.xml")
example_kml = joinpath("data", "example.kml")
simple_dtd = joinpath("data", "simple_dtd.xml")
all_files = [xml_xsd, kml_xsd, books_xml, example_kml, simple_dtd]
#-----------------------------------------------------------------------------# h
@testset "h function" begin
@test h.tag == XML.Element("tag")
@test h.tag(id="id") == XML.Element("tag"; id="id")
@test h.tag(1, 2, a="a", b="b") == XML.Element("tag", 1, 2; a="a", b="b")
end
#-----------------------------------------------------------------------------# escaping/unescaping
@testset "escaping/unescaping" begin
s = "This > string < has & some \" special ' characters"
@test escape(s) == "This > string < has & some " special ' characters"
@test escape(escape(s)) == escape(s)
@test s == unescape(escape(s))
@test s == unescape(unescape(escape(s)))
@test escape(SubString("a&b<c>", 1)) == "a&b<c>" # #60: SubString input (was a MethodError)
n = Element("tag", Text(s))
@test XML.simple_value(n) == s
XML.escape!(n)
@test XML.simple_value(n) == escape(s)
XML.unescape!(n)
@test XML.simple_value(n) == s
end
#-----------------------------------------------------------------------------# DTD
# @testset "DTDBody and friends" begin
# s = read(simple_dtd, String)
# data = read(simple_dtd)
# dtd = XML.DTDBody(data)
# dtd2 = parse(s, XML.DTDBody)
# @test length(dtd.elements) == length(dtd2.elements) == 0
# @test length(dtd.attributes) == length(dtd2.attributes) == 0
# @test length(dtd.entities) == length(dtd2.entities) == 3
# o = read("data/tv.dtd", XML.DTDBody)
# end
#-----------------------------------------------------------------------------# Raw
@testset "Raw tag/attributes/value" begin
examples = [
(xml = "<!DOCTYPE html>",
nodetype = DTD,
tag=nothing,
attributes=nothing,
value="html"),
(xml = "<?xml version=\"1.0\" key=\"value\"?>",
nodetype = Declaration,
tag=nothing,
attributes=Dict("version" => "1.0", "key" => "value"),
value=nothing),
(xml = "<tag _id=\"1\", x=\"abc\" />",
nodetype = Element,
tag="tag",
attributes=Dict("_id" => "1", "x" => "abc"),
value=nothing),
(xml = "<!-- comment -->",
nodetype = Comment,
tag=nothing,
attributes=nothing,
value=" comment "),
(xml = "<![CDATA[cdata test]]>",
nodetype = CData,
tag=nothing,
attributes=nothing,
value="cdata test"),
]
for x in examples
# @info "Testing: $(x.xml)"
data = XML.next(XML.parse(x.xml, XML.Raw))
@test XML.nodetype(data) == x.nodetype
@test XML.tag(data) == x.tag
@test XML.attributes(data) == x.attributes
@test XML.value(data) == x.value
end
end
@testset "Raw with books.xml" begin
data = read(books_xml, XML.Raw)
doc = collect(data)
@test length(doc) > countlines(books_xml)
# Check that the first 5 lines are correct
first_5_lines = [
XML.RawDeclaration => """<?xml version="1.0"?>""",
XML.RawElementOpen => "<catalog>",
XML.RawElementOpen => "<book id=\"bk101\">",
XML.RawElementOpen => "<author>",
XML.RawText => "Gambardella, Matthew"
]
for (i, (typ, str)) in enumerate(first_5_lines)
dt = doc[i]
@test dt.type == typ
@test String(dt) == str
end
# Check that the last line is correct
@test doc[end].type == XML.RawElementClose
@test String(doc[end]) == "</catalog>"
@testset "next and prev" begin
@test XML.prev(doc[1]) == data # can't use === here because prev returns a copy of ctx
@test prev(data) === nothing
@test XML.next(doc[end]) === nothing
n = length(doc)
next_res = [doc[1]]
foreach(_ -> push!(next_res, XML.next(next_res[end])), 1:n-1)
prev_res = [doc[end]]
foreach(_ -> pushfirst!(prev_res, XML.prev(prev_res[1])), 1:n-1)
idx = findall(next_res .!= prev_res)
for (a,b) in zip(next_res, prev_res)
@test a == b
end
lzxml = """<root><text> </text><text2> hello </text2><text3 xml:space="preserve"> hello <text3b> preserve </text3b></text3><text4 xml:space="preserve"></text4><text5/></root>"""
lz = XML.parse(XML.LazyNode, lzxml)
n=XML.next(lz)
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text/>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text2>hello</text2>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "hello"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text3 xml:space=\"preserve\"> hello <text3b> preserve </text3b></text3>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "hello"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text3 xml:space=\"preserve\"> hello <text3b> preserve </text3b></text3>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == " hello "
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text3b> preserve </text3b>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == " preserve "
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text4 xml:space=\"preserve\"/>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == "<text5/>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text4 xml:space=\"preserve\"/>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == " preserve "
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text3b> preserve </text3b>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == " hello "
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text3 xml:space=\"preserve\"> hello <text3b> preserve </text3b></text3>"
n=XML.next(n)
text_content = XML.write(n)
@test text_content == " hello "
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text3 xml:space=\"preserve\"> hello <text3b> preserve </text3b></text3>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "hello"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text2>hello</text2>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<text/>"
n=XML.prev(n)
text_content = XML.write(n)
@test text_content == "<root>\n <text/>\n <text2>hello</text2>\n <text3 xml:space=\"preserve\"> hello <text3b> preserve </text3b></text3>\n <text4 xml:space=\"preserve\"/>\n <text5/>\n</root>"
# #56: prev must cross a CDATA section (the call itself threw before the delimiter fix)
cdoc = XML.parse(XML.LazyNode, "<r><a>x</a><![CDATA[hello]]><b>y</b></r>")
cb = XML.children(XML.children(cdoc)[1])[3] # the <b> element
cp = XML.prev(cb)
@test cp isa XML.LazyNode # does not throw
@test XML.nodetype(cp) == XML.CData
@test XML.value(cp) == "hello"
end
@testset "depth and parent" begin
@test XML.depth(data) == 0
@test isnothing(XML.parent(data))
@test XML.depth(doc[1]) == 1
@test XML.parent(doc[1]) == data
@test XML.depth(doc[2]) == 1
@test XML.depth(doc[3]) == 2
@test XML.parent(doc[3]) == doc[2]
@test XML.depth(doc[end]) == 1
@test XML.parent(doc[end]) == data
end
@testset "tag/attributes/value" begin
x = doc[1] # <?xml version="1.0"?>
@test XML.tag(x) === nothing
@test XML.attributes(x) == Dict("version" => "1.0")
@test XML.value(x) === nothing
x = XML.next(x) # <catalog>
@test XML.tag(x) == "catalog"
@test XML.attributes(x) === nothing
@test XML.value(x) === nothing
x = XML.next(x) # <book id="bk101">
@test XML.tag(x) == "book"
@test XML.attributes(x) == Dict("id" => "bk101")
@test XML.value(x) === nothing
x = XML.next(x) # <author>
@test XML.tag(x) == "author"
@test XML.attributes(x) === nothing
@test XML.value(x) === nothing
x = XML.next(x) # Gambardella, Matthew
@test XML.tag(x) === nothing
@test XML.attributes(x) === nothing
@test XML.value(x) == "Gambardella, Matthew"
end
end
#-----------------------------------------------------------------------------# next! / prev!
@testset "LazyNode next! / prev!" begin
lzxml = """<root><a/><b/><c/></root>"""
lz = XML.parse(XML.LazyNode, lzxml)
# Functional equivalence: walking with `next!` visits the same nodes
# as the allocating `next` chain.
walker = XML.next(lz)
walked = [XML.write(walker)]
while XML.next!(walker) !== nothing
push!(walked, XML.write(walker))
end
expected = String[]
n = XML.next(lz)
while n !== nothing
push!(expected, XML.write(n))
n = XML.next(n)
end
@test walked == expected
# Identity: `next!(o)` returns the very same object
walker = XML.next(lz)
@test XML.next!(walker) === walker
# Memoization fields are reset when the node is repositioned
walker = XML.next(lz)
_ = walker.tag
@test getfield(walker, :tag) !== nothing
XML.next!(walker)
@test getfield(walker, :tag) === nothing
# `nothing` at the document boundary, idempotent there
walker = XML.next(lz)
while XML.next!(walker) !== nothing; end
@test XML.next!(walker) === nothing
# `prev!` is the symmetric counterpart
lz2 = XML.parse(XML.LazyNode, lzxml)
walker = XML.next(lz2)
XML.next!(walker); XML.next!(walker) # root → a → b
@test walker.tag == "b"
@test XML.prev!(walker) === walker # in-place
@test walker.tag == "a"
end
#-----------------------------------------------------------------------------# Preserve whitespace
@testset "xml:space" begin
@testset "Basic xml:space functionality" begin
# Test 1: xml:space="preserve" should preserve entirely empty whitespace
xml1 = """<root><text xml:space="preserve"> </text></root>"""
doc1 = parse(XML.Node, xml1)
text_content = XML.value(doc1[1][1][1])
@test text_content == " "
# Test 2: xml:space="preserve" should preserve leading and trailing whitespace
xml2 = """<root><text xml:space="preserve"> leading and trailing spaces </text></root>"""
doc2 = parse(XML.Node, xml2)
text_content = XML.value(doc2[1][1][1])
@test text_content == " leading and trailing spaces "
# Test 3: Entirely empty tags with and without xml:space="preserve" become self-closing
xml3 = """<root><text> </text><text2 xml:space="preserve"> </text2><text3 xml:space="preserve"></text3><text4/></root>"""
doc3 = XML.parse(XML.Node, xml3)
text_content = XML.write(doc3[1][1])
@test text_content == "<text/>" # without xml:space="preserve", empty text becomes self-closing
text_content = XML.value(doc3[1][2][1])
@test text_content == " " # with xml:space, whitespace is preserved
text_content = XML.write(doc3[1][3])
@test text_content == "<text3 xml:space=\"preserve\"/>" # with xml:space="preserve", empty text becomes self-closing
# Test 4: Without xml:space, whitespace should be normalized
xml4 = """<root><text> gets normalized </text></root>"""
doc4 = XML.parse(XML.Node, xml4)
text_content = XML.value(doc4[1][1][1])
@test text_content == "gets normalized"
# Test 5: xml:space="default" should normalize even with preserve_xml_space=true
xml5 = """<root><text xml:space="default"> gets normalized </text></root>"""
doc5 = XML.parse(XML.Node, xml5)
text_content = XML.value(doc5[1][1][1])
@test text_content == "gets normalized"
end
@testset "xml:space inheritance" begin
# Test 6: Children inherit parent's xml:space="preserve"
xml6 = """<root xml:space="preserve">
<parent> parent text
<child> child text </child>
</parent>
</root>"""
doc6 = XML.parse(XML.Node, xml6)
# Both parent and child should preserve whitespace
@test contains(XML.value(doc6[1][2][1]), "parent text \n")
@test XML.value(doc6[1][2][2][1]) == " child text "
# Test 7: xml:space="default" overrides parent's "preserve"
xml7 = """<root xml:space="preserve">
<child xml:space="default"> normalized despite parent </child>
</root>"""
doc7 = XML.parse(XML.Node, xml7)
@test XML.value(doc7[1][2][1]) == "normalized despite parent"
end
@testset "Nesting scenarios" begin
# Test 8: Multiple levels of xml:space changes
xml8 = """<root xml:space="preserve">
<level1> preserved
<level2 xml:space="default"> normalized
<level3 xml:space="preserve"> preserved again </level3>
</level2>
</level1>
</root>"""
doc8 = XML.parse(XML.Node, xml8)
# level1 should preserve (inherits from root)
level1_text = XML.value(doc8[1][2][1])
@test level1_text == " preserved \n "
# level2 should normalize (explicit xml:space="default")
level2_text = XML.value(doc8[1][2][2][1])
@test level2_text == "normalized"
# level3 should preserve (explicit xml:space="preserve")
level3_text = XML.value(doc8[1][2][2][2][1])
@test level3_text == " preserved again "
# Test 9: repeated multiple levels of xml:space changes
xml9 = """<root xml:space="preserve">
<level1> preserved
<level2 xml:space="default"> normalized
<level3 xml:space="preserve"> preserved again </level3>
</level2>
</level1>
<level1b> preserved b
<level2b xml:space="default"> normalized b
<level3b xml:space="preserve"> preserved again b </level3b>
</level2b>
</level1b>
</root>"""
doc9 = XML.parse(XML.Node, xml9)
# level1b should preserve (inherits from root)
level1b_text = XML.value(doc9[1][4][1])
@test level1b_text == " preserved b \n "
# level2 should normalize (explicit xml:space="default")
level2b_text = XML.value(doc9[1][4][2][1])
@test level2b_text == "normalized b"
# level3 should preserve (explicit xml:space="preserve")
level3b_text = XML.value(doc9[1][4][2][2][1])
@test level3b_text == " preserved again b "
# Test 10: futher repeated multiple levels of xml:space changes
xml10 = """<root>
<level1> normalized
<level2> normalized b
<level3 xml:space="preserve"> preserved </level3>
</level2>
</level1>
<level1b> normalized c
<level2b xml:space="preserve"> preserved b
<level3b xml:space="default"> normalized again b </level3b>
<level3c> preserved c
</level3c>
</level2b>
</level1b>
<level1c> normalized d </level1c>
</root>"""
doc10 = XML.parse(XML.Node, xml10)
# level1 should normalize (as root)
level1_text = XML.value(doc10[end][1][1])
@test level1_text == "normalized"
# level2 should normalize (as root and level1)
level2_text = XML.value(doc10[end][1][2][1])
@test level2_text == "normalized b"
# level3 should preserve (explicit xml:space="preserve")
level3_text = XML.value(doc10[end][1][2][2][1])
@test level3_text == " preserved "
# level1b should normalize (as root)
level1b_text = XML.value(doc10[end][2][1])
@test level1b_text == "normalized c"
# level2b should preserve (explicit xml:space="preserve")
level2b_text = XML.value(doc10[end][2][2][1])
@test level2b_text == " preserved b \n "
# level3 should normalize (explicit xml:space="default")
level3b_text = XML.value(doc10[end][2][2][2][1])
@test level3b_text == "normalized again b"
# level3c should preserve (inherited from level2b)
level3c_text = XML.value(doc10[end][2][2][4][1])
@test level3c_text == " preserved c \n "
# level1c should normalize (as root)
level1c_text = XML.value(doc10[end][3][1])
@test level1c_text == "normalized d"
end
@testset "inter-element gap semantics" begin
# Default parent: gap between siblings should be dropped
s1 = """<root><a> x </a>
<b> y </b></root>"""
d1 = XML.parse(XML.Node, s1)
@test length(d1[1]) == 2
@test XML.value(d1[1][1][1]) == "x"
@test XML.value(d1[1][2][1]) == "y"
# Preserve parent, default child ends: gap after default child dropped
s2 = """<root xml:space="preserve">
<p> keep </p>
<q xml:space="default"> norm </q>
<r> after default gap </r>
</root>"""
d2 = XML.parse(XML.Node, s2)
@test length(d2[1]) == 7
@test XML.value(d2[1][1]) == "\n "
@test XML.value(d2[1][2][1]) == " keep "
@test XML.value(d2[1][3]) == "\n "
@test XML.value(d2[1][4][1]) == "norm"
@test XML.value(d2[1][5]) == "\n "
@test XML.value(d2[1][6][1]) == " after default gap "
@test XML.value(d2[1][7]) == "\n"
end
@testset "XML whitespace vs Unicode whitespace" begin
nbsp = "\u00A0"
s = """<root>
<a> x\t\n </a>
<b>$(nbsp) y $(nbsp)</b>
<c xml:space="default">$(nbsp) z $(nbsp)</c>
</root>"""
d = XML.parse(XML.Node, s)
@test XML.value(d[1][1][1]) == "x"
@test XML.value(d[1][2][1]) == "$(nbsp) y $(nbsp)"
@test XML.value(d[1][3][1]) == "$(nbsp) z $(nbsp)"
end
@testset "CDATA/Comment/PI boundaries" begin
s = """<root>
<a xml:space="default"> pre <![CDATA[ mid ]]> post </a>
<b xml:space="preserve"> pre <!-- cmt --> post </b>
<?xml-stylesheet type="text/css" href="style.css"?>
</root>"""
d = XML.parse(XML.Node, s)
@test XML.value(d[1][1][1]) == "pre"
@test nodetype(d[1][1][2]) == XML.CData
@test XML.value(d[1][1][3]) == "post"
@test XML.value(d[1][2][1]) == " pre "
@test nodetype(d[1][2][2]) == XML.Comment
@test XML.value(d[1][2][3]) == " post "
@test nodetype(d[1][3]) == XML.ProcessingInstruction
end
@testset "nested toggles and sibling sequences" begin
s = """<root xml:space="preserve">
<x> a
<y xml:space="default"> b
<z xml:space="preserve"> c </z>
</y>
<y2 xml:space="default"> d </y2>
<w> e </w>
</x>
</root>"""
d = XML.parse(XML.Node, s)
@test XML.value(d[1][2][1]) == " a \n "
@test XML.value(d[1][2][2][1]) == "b"
@test XML.value(d[1][2][2][2][1]) == " c "
@test d[1][2][4].tag == "y2"
@test XML.value(d[1][2][4][1]) == "d"
@test d[1][2][6].tag == "w"
@test XML.value(d[1][2][6][1]) == " e "
end
@testset "root/document boundaries" begin
s = "\n \n<root> a </root>\n \t "
d = XML.parse(XML.Node, s)
@test length(d) == 1
@test XML.value(d[1][1]) == "a"
end
@testset "entities expanding to whitespace" begin
chr1="\u0020"
chr2="\u000A"
chr3="\u00A0"
s = """<root>
<a> $(chr1) a $(chr2) </a>
<b xml:space="preserve">$(chr1) b $(chr2)</b>
<c>$(chr3)c$(chr3)</c>
</root>"""
d = XML.parse(XML.Node, s)
@test XML.value(d[1][1][1]) == "a"
@test XML.value(d[1][2][1]) == " b \n"
@test XML.value(d[1][3][1]) == "$(chr3)c$(chr3)"
end
@testset "invalid values and placement" begin
s_bad = """<root><x xml:space="weird"> t </x></root>"""
@test_throws ErrorException XML.parse(XML.Node, s_bad)
s_pi = """<?pi xml:space="preserve"?><root> t </root>"""
d = XML.parse(XML.Node, s_pi)
@test XML.value(d[end][1]) == "t"
s_dup = """<root><x xml:space="preserve" xml:space="default"> t </x></root>"""
# @test_throws ErrorException XML.parse(XML.Node, s_dup)
end
@testset "prev()/next() symmetry" begin
xml = """<root xml:space="preserve">
<a> a <b xml:space="default"> b </b> <c> c </c> </a>
<d xml:space="default"> d <e xml:space="preserve"> e </e> f </d>
<g><h/><i xml:space="preserve"> i </i><j/></g>
</root>"""
r = XML.parse(XML.LazyNode, xml).raw
toks=XML.Raw[]
while true
n = XML.next(r)
n === nothing && break
push!(toks, n)
r=n
end
back = XML.Raw[]
r = toks[end]
while true
p = XML.prev(r)
p === nothing && break
push!(back, p)
r = p
end
@test reverse(back)[2:end] == toks[1:end-1]
end
@testset "write/read roundtrip extremes" begin
xml = """<root>
<p xml:space="preserve"> </p>
<q> </q>
<r xml:space="default"> r </r>
<s xml:space="preserve"> pre <t/> post </s>
</root>"""
n = XML.parse(XML.Node, xml)
io = IOBuffer(); XML.write(io, n)
n2 = XML.parse(XML.Node, String(take!(io)))
@test n == n2
@test XML.write(n2[1][1]) == "<p xml:space=\"preserve\"> </p>"
@test XML.write(n2[1][2]) == "<q/>"
@test XML.value(n2[1][3][1]) == "r"
@test XML.write(n2[1][4]) == "<s xml:space=\"preserve\"> pre <t/> post </s>"
end
@testset "self-closing/empty/whitespace-only children" begin
s = """<root>
<a xml:space="default"> </a>
<b xml:space="preserve"></b>
<c xml:space="preserve"> </c>
<d><e/></d>
<f> x <g/> y </f>
</root>"""
d = XML.parse(XML.Node, s)
@test XML.write(d[1][1]) == "<a xml:space=\"default\"/>"
@test XML.write(d[1][2]) == "<b xml:space=\"preserve\"/>"
@test XML.value(d[1][3][1]) == " "
@test XML.value(d[1][5][1]) == "x"
@test XML.value(d[1][5][3]) == "y"
end
@testset "allocation guard: small xml:space doc" begin
xml = "<root><a xml:space=\"default\"> x </a><b xml:space=\"preserve\"> y </b></root>"
f() = XML.parse(XML.Node, xml)
a = @allocated f()
@test a < 500_000 # tune for CI
end
end
#-----------------------------------------------------------------------------# roundtrip
@testset "read/write/read roundtrip" begin
for path in all_files
node = read(path, Node)
temp = tempname() * ".xml"
XML.write(temp, node)
node2 = read(temp, Node)
@test node == node2
#For debugging:
for (a,b) in zip(AbstractTrees.Leaves(node), AbstractTrees.Leaves(node2))
if a != b
@info path
@info a
@info b
error()
end
end
end
end
#-----------------------------------------------------------------------------# Node writing
@testset "Node writing" begin
doc = Document(
DTD("root_tag"),
Declaration(version=1.0),
Comment("comment"),
ProcessingInstruction("xml-stylesheet", href="mystyle.css", type="text/css"),
Element("root_tag", CData("cdata"), Text("text"))
)
@test map(nodetype, children(doc)) == [DTD,Declaration,Comment,ProcessingInstruction,Element]
@test length(children(doc[end])) == 2
@test nodetype(doc[end][1]) == XML.CData
@test nodetype(doc[end][2]) == XML.Text
@test value(doc[end][1]) == "cdata"
@test value(doc[end][2]) == "text"
#set/get index for attributes
o = doc[end]
@test isempty(keys(o))
o["id"] = 1
@test o["id"] == "1"
@test keys(o) == keys(Dict("id" => "1"))
end
#-----------------------------------------------------------------------------# Issues
@testset "Issues" begin
# https://github.com/JuliaComputing/XML.jl/issues/12: DTD content was cut short
s = """
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
"""
doc = parse(Node, s)
@test value(only(doc)) == s[11:end-2] # note [...]
# https://github.com/JuliaComputing/XML.jl/issues/14 (Sorted Attributes)
kw = NamedTuple(OrderedDict(Symbol(k) => Int(k) for k in 'a':'z'))
xyz = XML.Element("point"; kw...)
@test collect(keys(attributes(xyz))) == string.(collect('a':'z'))
# https://github.com/JuliaComputing/XML.jl/issues/62 (UTF-16/BOM input)
text = """<?xml version="1.0"?><root>hello</root>"""
units = transcode(UInt16, text)
le = vcat(UInt8[0xFF, 0xFE], reinterpret(UInt8, units))
be = vcat(UInt8[0xFE, 0xFF], reinterpret(UInt8, bswap.(units)))
utf8_bom = vcat(UInt8[0xEF, 0xBB, 0xBF], Vector{UInt8}(text))
for bytes in (le, be, utf8_bom)
doc = Node(XML.Raw(bytes))
root = only(filter(n -> nodetype(n) == Element, children(doc)))
@test tag(root) == "root"
@test value(only(children(root))) == "hello"
# read(file) round-trip exercises the Mmap.mmap path (the real .xlsx case)
mktemp() do path, io
write(io, bytes)
close(io)
file_doc = read(path, Node)
file_root = only(filter(n -> nodetype(n) == Element, children(file_doc)))
@test tag(file_root) == "root"
@test value(only(children(file_root))) == "hello"
end
end
end