forked from beevik/etree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etree_test.go
1138 lines (986 loc) · 30.4 KB
/
etree_test.go
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015-2019 Brett Vickers.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package etree
import (
"encoding/xml"
"io"
"strings"
"testing"
)
func newDocumentFromString(t *testing.T, s string) *Document {
t.Helper()
doc := NewDocument()
err := doc.ReadFromString(s)
if err != nil {
t.Error("etree: failed to parse document")
}
return doc
}
func checkStrEq(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("etree: unexpected result.\nGot:\n%s\nWanted:\n%s\n", got, want)
}
}
func checkStrBinaryEq(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("etree: unexpected result.\nGot:\n%v\nWanted:\n%v\n", []byte(got), []byte(want))
}
}
func checkIntEq(t *testing.T, got, want int) {
t.Helper()
if got != want {
t.Errorf("etree: unexpected integer. Got: %d. Wanted: %d\n", got, want)
}
}
func checkBoolEq(t *testing.T, got, want bool) {
t.Helper()
if got != want {
t.Errorf("etree: unexpected boolean. Got: %v. Wanted: %v\n", got, want)
}
}
func checkElementEq(t *testing.T, got, want *Element) {
t.Helper()
if got != want {
t.Errorf("etree: unexpected element. Got: %v. Wanted: %v.\n", got, want)
}
}
func checkDocEq(t *testing.T, doc *Document, expected string) {
t.Helper()
doc.Indent(NoIndent)
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
if s != expected {
t.Errorf("etree: unexpected document.\nGot:\n%s\nWanted:\n%s\n", s, expected)
}
}
func checkIndexes(t *testing.T, e *Element) {
t.Helper()
for i := 0; i < len(e.Child); i++ {
c := e.Child[i]
if c.Index() != i {
t.Errorf("Child index mismatch. Got %d, expected %d.", c.Index(), i)
}
if ce, ok := c.(*Element); ok {
checkIndexes(t, ce)
}
}
}
func TestDocument(t *testing.T) {
// Create a document
doc := NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`)
store := doc.CreateElement("store")
store.CreateAttr("xmlns:t", "urn:books-com:titles")
store.CreateDirective("Directive")
store.CreateComment("This is a comment")
book := store.CreateElement("book")
book.CreateAttr("lang", "fr")
book.CreateAttr("lang", "en")
title := book.CreateElement("t:title")
title.SetText("Nicholas Nickleby")
title.SetText("Great Expectations")
author := book.CreateElement("author")
author.CreateCharData("Charles Dickens")
review := book.CreateElement("review")
review.CreateCData("<<< Will be replaced")
review.SetCData(">>> Excellent book")
doc.IndentTabs()
checkIndexes(t, &doc.Element)
// Serialize the document to a string
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
// Make sure the serialized XML matches expectation.
expected := `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<store xmlns:t="urn:books-com:titles">
<!Directive>
<!--This is a comment-->
<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
<review><![CDATA[>>> Excellent book]]></review>
</book>
</store>
`
checkStrEq(t, s, expected)
// Test the structure of the XML
if doc.Root() != store {
t.Error("etree: root mismatch")
}
if len(store.ChildElements()) != 1 || len(store.Child) != 7 {
t.Error("etree: incorrect tree structure")
}
if len(book.ChildElements()) != 3 || len(book.Attr) != 1 || len(book.Child) != 7 {
t.Error("etree: incorrect tree structure")
}
if len(title.ChildElements()) != 0 || len(title.Child) != 1 || len(title.Attr) != 0 {
t.Error("etree: incorrect tree structure")
}
if len(author.ChildElements()) != 0 || len(author.Child) != 1 || len(author.Attr) != 0 {
t.Error("etree: incorrect tree structure")
}
if len(review.ChildElements()) != 0 || len(review.Child) != 1 || len(review.Attr) != 0 {
t.Error("etree: incorrect tree structure")
}
if book.parent != store || store.parent != &doc.Element || doc.parent != nil {
t.Error("etree: incorrect tree structure")
}
if title.parent != book || author.parent != book {
t.Error("etree: incorrect tree structure")
}
// Perform some basic queries on the document
elements := doc.SelectElements("store")
if len(elements) != 1 || elements[0] != store {
t.Error("etree: incorrect SelectElements result")
}
element := doc.SelectElement("store")
if element != store {
t.Error("etree: incorrect SelectElement result")
}
elements = store.SelectElements("book")
if len(elements) != 1 || elements[0] != book {
t.Error("etree: incorrect SelectElements result")
}
element = store.SelectElement("book")
if element != book {
t.Error("etree: incorrect SelectElement result")
}
attr := book.SelectAttr("lang")
if attr == nil || attr.Key != "lang" || attr.Value != "en" {
t.Error("etree: incorrect SelectAttr result")
}
if book.SelectAttrValue("lang", "unknown") != "en" {
t.Error("etree: incorrect SelectAttrValue result")
}
if book.SelectAttrValue("t:missing", "unknown") != "unknown" {
t.Error("etree: incorrect SelectAttrValue result")
}
attr = book.RemoveAttr("lang")
if attr.Value != "en" {
t.Error("etree: incorrect RemoveAttr result")
}
book.CreateAttr("lang", "de")
attr = book.RemoveAttr("lang")
if attr.Value != "de" {
t.Error("etree: incorrect RemoveAttr result")
}
element = book.SelectElement("t:title")
if element != title || element.Text() != "Great Expectations" || len(element.Attr) != 0 {
t.Error("etree: incorrect SelectElement result")
}
element = book.SelectElement("title")
if element != title {
t.Error("etree: incorrect SelectElement result")
}
element = book.SelectElement("p:title")
if element != nil {
t.Error("etree: incorrect SelectElement result")
}
element = book.RemoveChildAt(title.Index()).(*Element)
if element != title {
t.Error("etree: incorrect RemoveElement result")
}
element = book.SelectElement("title")
if element != nil {
t.Error("etree: incorrect SelectElement result")
}
element = book.SelectElement("review")
if element != review || element.Text() != ">>> Excellent book" || len(element.Attr) != 0 {
t.Error("etree: incorrect SelectElement result")
}
}
func TestImbalancedXML(t *testing.T) {
cases := []string{
`<test>`,
`</test>`,
`<test></test2>`,
`<doc xmlns:p="xyz"><p:test></test></doc>`,
`<doc xmlns:p="xyz"><test></p:test></doc>`,
`<test>malformed`,
`malformed</test>`,
`<test><test></test>`,
`<test></test></test>`,
`<test><test></test></test2>`,
`<test><test2></test></test2>`,
}
for _, c := range cases {
doc := NewDocument()
err := doc.ReadFromString(c)
if err == nil {
t.Errorf("etree: imbalanced XML should have failed:\n%s", c)
}
}
}
func TestDocumentReadNonUTF8Encodings(t *testing.T) {
s := `<?xml version="1.0" encoding="ISO-8859-1"?>
<store>
<book lang="en">
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
</store>`
doc := NewDocument()
doc.ReadSettings.CharsetReader = func(label string, input io.Reader) (io.Reader, error) {
return input, nil
}
err := doc.ReadFromString(s)
if err != nil {
t.Fatal("etree: incorrect ReadFromString result")
}
}
func TestDocumentReadPermissive(t *testing.T) {
s := "<select disabled></select>"
doc := NewDocument()
err := doc.ReadFromString(s)
if err == nil {
t.Fatal("etree: incorrect ReadFromString result")
}
doc.ReadSettings.Permissive = true
err = doc.ReadFromString(s)
if err != nil {
t.Fatal("etree: incorrect ReadFromString result")
}
}
func TestDocumentReadHTMLEntities(t *testing.T) {
s := `<store>
<book lang="en">
<title>→ Great Expectations</title>
<author>Charles Dickens</author>
</book>
</store>`
doc := NewDocument()
err := doc.ReadFromString(s)
if err == nil {
t.Fatal("etree: incorrect ReadFromString result")
}
doc.ReadSettings.Entity = xml.HTMLEntity
err = doc.ReadFromString(s)
if err != nil {
t.Fatal("etree: incorrect ReadFromString result")
}
}
func TestEscapeCodes(t *testing.T) {
cases := []struct {
input string
normal string
attrCanonical string
textCanonical string
}{
{
"&<>'\"\t\n\r",
"<e a=\"&<>'"\t\n\r\">&<>'"\t\n\r</e>",
"<e a=\"&<>'"	

\">&<>'"\t\n\r</e>",
"<e a=\"&<>'"\t\n\r\">&<>'\"\t\n
</e>",
},
{
"\x00\x1f\x08\x09\x0a\x0d",
"<e a=\"���\t\n\r\">���\t\n\r</e>",
"<e a=\"���	

\">���\t\n\r</e>",
"<e a=\"���\t\n\r\">���\t\n
</e>",
},
}
for _, c := range cases {
doc := NewDocument()
e := doc.CreateElement("e")
e.SetText(c.input)
e.CreateAttr("a", c.input)
doc.WriteSettings.CanonicalText = false
doc.WriteSettings.CanonicalAttrVal = false
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: Escape test produced inocrrect result.")
}
checkStrEq(t, s, c.normal)
doc.WriteSettings.CanonicalText = false
doc.WriteSettings.CanonicalAttrVal = true
s, err = doc.WriteToString()
if err != nil {
t.Error("etree: Escape test produced inocrrect result.")
}
checkStrEq(t, s, c.attrCanonical)
doc.WriteSettings.CanonicalText = true
doc.WriteSettings.CanonicalAttrVal = false
s, err = doc.WriteToString()
if err != nil {
t.Error("etree: Escape test produced inocrrect result.")
}
checkStrEq(t, s, c.textCanonical)
}
}
func TestCanonical(t *testing.T) {
BOM := "\xef\xbb\xbf"
doc := NewDocument()
doc.WriteSettings.CanonicalEndTags = true
doc.WriteSettings.CanonicalText = true
doc.WriteSettings.CanonicalAttrVal = true
doc.CreateCharData(BOM)
doc.CreateProcInst("xml-stylesheet", `type="text/xsl" href="style.xsl"`)
people := doc.CreateElement("People")
people.CreateComment("These are all known people")
jon := people.CreateElement("Person")
jon.CreateAttr("name", "Jon O'Reilly")
jon.SetText("\r<'\">&\u0004\u0005\u001f�")
sally := people.CreateElement("Person")
sally.CreateAttr("name", "Sally")
sally.CreateAttr("escape", "\r\n\t<'\">&")
doc.Indent(2)
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: WriteSettings WriteTo produced incorrect result.")
}
expected := BOM + `<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<People>
<!--These are all known people-->
<Person name="Jon O'Reilly">
<'">&����</Person>
<Person name="Sally" escape="
	<'">&"></Person>
</People>
`
checkStrEq(t, s, expected)
}
func TestCopy(t *testing.T) {
s := `<store>
<book lang="en">
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
</store>`
doc := newDocumentFromString(t, s)
s1, err := doc.WriteToString()
if err != nil {
t.Error("etree: incorrect WriteToString result")
}
doc2 := doc.Copy()
checkIndexes(t, &doc2.Element)
s2, err := doc2.WriteToString()
if err != nil {
t.Error("etree: incorrect Copy result")
}
if s1 != s2 {
t.Error("etree: mismatched Copy result")
t.Error("wanted:\n" + s1)
t.Error("got:\n" + s2)
}
e1 := doc.FindElement("./store/book/title")
e2 := doc2.FindElement("./store/book/title")
if e1 == nil || e2 == nil {
t.Error("etree: incorrect FindElement result")
}
if e1 == e2 {
t.Error("etree: incorrect FindElement result")
}
e1.parent.RemoveChildAt(e1.Index())
s1, _ = doc.WriteToString()
s2, _ = doc2.WriteToString()
if s1 == s2 {
t.Error("etree: incorrect result after RemoveElement")
}
}
func TestGetPath(t *testing.T) {
s := `<a>
<b1>
<c1>
<d1/>
<d1a/>
</c1>
</b1>
<b2>
<c2>
<d2/>
</c2>
</b2>
</a>`
doc := newDocumentFromString(t, s)
cases := []struct {
from string
to string
relpath string
topath string
}{
{"a", ".", "..", "/"},
{".", "a", "./a", "/a"},
{"a/b1/c1/d1", ".", "../../../..", "/"},
{".", "a/b1/c1/d1", "./a/b1/c1/d1", "/a/b1/c1/d1"},
{"a", "a", ".", "/a"},
{"a/b1", "a/b1/c1", "./c1", "/a/b1/c1"},
{"a/b1/c1", "a/b1", "..", "/a/b1"},
{"a/b1/c1", "a/b1/c1", ".", "/a/b1/c1"},
{"a", "a/b1", "./b1", "/a/b1"},
{"a/b1", "a", "..", "/a"},
{"a", "a/b1/c1", "./b1/c1", "/a/b1/c1"},
{"a/b1/c1", "a", "../..", "/a"},
{"a/b1/c1/d1", "a", "../../..", "/a"},
{"a", "a/b1/c1/d1", "./b1/c1/d1", "/a/b1/c1/d1"},
{"a/b1", "a/b2", "../b2", "/a/b2"},
{"a/b2", "a/b1", "../b1", "/a/b1"},
{"a/b1/c1/d1", "a/b2/c2/d2", "../../../b2/c2/d2", "/a/b2/c2/d2"},
{"a/b2/c2/d2", "a/b1/c1/d1", "../../../b1/c1/d1", "/a/b1/c1/d1"},
{"a/b1/c1/d1", "a/b1/c1/d1a", "../d1a", "/a/b1/c1/d1a"},
}
for _, c := range cases {
fe := doc.FindElement(c.from)
te := doc.FindElement(c.to)
rp := te.GetRelativePath(fe)
if rp != c.relpath {
t.Errorf("GetRelativePath from '%s' to '%s'. Expected '%s', got '%s'.\n", c.from, c.to, c.relpath, rp)
}
p := te.GetPath()
if p != c.topath {
t.Errorf("GetPath for '%s'. Expected '%s', got '%s'.\n", c.to, c.topath, p)
}
}
}
func TestInsertChild(t *testing.T) {
s := `<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
</book>
`
doc := newDocumentFromString(t, s)
year := NewElement("year")
year.SetText("1861")
book := doc.FindElement("//book")
book.InsertChildAt(book.SelectElement("t:title").Index(), year)
expected1 := `<book lang="en">
<year>1861</year>
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
</book>
`
doc.Indent(2)
s1, _ := doc.WriteToString()
checkStrEq(t, s1, expected1)
book.RemoveChildAt(year.Index())
book.InsertChildAt(book.SelectElement("author").Index(), year)
expected2 := `<book lang="en">
<t:title>Great Expectations</t:title>
<year>1861</year>
<author>Charles Dickens</author>
</book>
`
doc.Indent(2)
s2, _ := doc.WriteToString()
checkStrEq(t, s2, expected2)
book.RemoveChildAt(year.Index())
book.InsertChildAt(len(book.Child), year)
expected3 := `<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
<year>1861</year>
</book>
`
doc.Indent(2)
s3, _ := doc.WriteToString()
checkStrEq(t, s3, expected3)
book.RemoveChildAt(year.Index())
book.InsertChildAt(999, year)
expected4 := `<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
<year>1861</year>
</book>
`
doc.Indent(2)
s4, _ := doc.WriteToString()
checkStrEq(t, s4, expected4)
}
func TestCdata(t *testing.T) {
var tests = []struct {
in, out string
}{
{`<tag>1234567</tag>`, "1234567"},
{`<tag><![CDATA[1234567]]></tag>`, "1234567"},
{`<tag>1<![CDATA[2]]>3<![CDATA[4]]>5<![CDATA[6]]>7</tag>`, "1234567"},
{`<tag>1<![CDATA[2]]>3<inner>4</inner>5<![CDATA[6]]>7</tag>`, "123"},
{`<tag>1<inner>4</inner>5<![CDATA[6]]>7</tag>`, "1"},
{`<tag><![CDATA[1]]><inner>4</inner>5<![CDATA[6]]>7</tag>`, "1"},
}
for _, test := range tests {
doc := NewDocument()
err := doc.ReadFromString(test.in)
if err != nil {
t.Fatal("etree ReadFromString: " + err.Error())
}
tag := doc.FindElement("tag")
if tag.Text() != test.out {
t.Fatalf("etree invalid cdata. Expected: %v. Got: %v\n", test.out, tag.Text())
}
}
}
func TestAddChild(t *testing.T) {
s := `<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
</book>
`
doc1 := newDocumentFromString(t, s)
doc2 := NewDocument()
root := doc2.CreateElement("root")
for _, e := range doc1.FindElements("//book/*") {
root.AddChild(e)
}
expected1 := `<book lang="en"/>
`
doc1.Indent(2)
s1, _ := doc1.WriteToString()
checkStrEq(t, s1, expected1)
expected2 := `<root>
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
</root>
`
doc2.Indent(2)
s2, _ := doc2.WriteToString()
checkStrEq(t, s2, expected2)
}
func TestSetRoot(t *testing.T) {
s := `<?test a="wow"?>
<book>
<title>Great Expectations</title>
<author>Charles Dickens</author>
</book>
`
doc := newDocumentFromString(t, s)
origroot := doc.Root()
if origroot.Parent() != &doc.Element {
t.Error("Root incorrect")
}
newroot := NewElement("root")
doc.SetRoot(newroot)
if doc.Root() != newroot {
t.Error("doc.Root() != newroot")
}
if origroot.Parent() != nil {
t.Error("origroot.Parent() != nil")
}
expected1 := `<?test a="wow"?>
<root/>
`
doc.Indent(2)
s1, _ := doc.WriteToString()
checkStrEq(t, s1, expected1)
doc.SetRoot(origroot)
doc.Indent(2)
expected2 := s
s2, _ := doc.WriteToString()
checkStrEq(t, s2, expected2)
doc2 := NewDocument()
doc2.CreateProcInst("test", `a="wow"`)
doc2.SetRoot(NewElement("root"))
doc2.Indent(2)
expected3 := expected1
s3, _ := doc2.WriteToString()
checkStrEq(t, s3, expected3)
doc2.SetRoot(doc.Root())
doc2.Indent(2)
expected4 := s
s4, _ := doc2.WriteToString()
checkStrEq(t, s4, expected4)
expected5 := `<?test a="wow"?>
`
doc.Indent(2)
s5, _ := doc.WriteToString()
checkStrEq(t, s5, expected5)
}
func TestSortAttrs(t *testing.T) {
s := `<el foo='5' Foo='2' aaa='4' สวัสดี='7' AAA='1' a01='3' z='6' a:ZZZ='9' a:AAA='8'/>`
doc := newDocumentFromString(t, s)
doc.Root().SortAttrs()
doc.Indent(2)
out, _ := doc.WriteToString()
checkStrEq(t, out, `<el AAA="1" Foo="2" a01="3" aaa="4" foo="5" z="6" สวัสดี="7" a:AAA="8" a:ZZZ="9"/>`+"\n")
}
func TestCharsetReaderEncoding(t *testing.T) {
cases := []string{
`<?xml version="1.0" encoding="ISO-8859-1"?><foo></foo>`,
`<?xml version="1.0" encoding="UTF-8"?><foo></foo>`,
`<?xml version="1.0" encoding="US-ASCII"?><foo></foo>`,
}
for _, c := range cases {
doc := NewDocument()
if err := doc.ReadFromBytes([]byte(c)); err != nil {
t.Error(err)
}
}
}
func TestCharData(t *testing.T) {
doc := NewDocument()
root := doc.CreateElement("root")
root.CreateCharData("This ")
root.CreateCData("is ")
e1 := NewText("a ")
e2 := NewCData("text ")
root.AddChild(e1)
root.AddChild(e2)
root.CreateCharData("Element!!")
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
checkStrEq(t, s, `<root>This <![CDATA[is ]]>a <![CDATA[text ]]>Element!!</root>`)
// Check we can parse the output
err = doc.ReadFromString(s)
if err != nil {
t.Fatal("etree: incorrect ReadFromString result")
}
if doc.Root().Text() != "This is a text Element!!" {
t.Error("etree: invalid text")
}
}
func TestIndentSettings(t *testing.T) {
doc := NewDocument()
root := doc.CreateElement("root")
ch1 := root.CreateElement("child1")
ch1.CreateElement("child2")
// First test with NoIndent.
doc.Indent(NoIndent)
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
expected := "<root><child1><child2/></child1></root>"
checkStrEq(t, s, expected)
// Run all indent test cases.
tests := []struct {
useTabs, useCRLF bool
ws, nl string
}{
{false, false, " ", "\n"},
{false, true, " ", "\r\n"},
{true, false, "\t", "\n"},
{true, true, "\t", "\r\n"},
}
for _, test := range tests {
doc.WriteSettings.UseCRLF = test.useCRLF
if test.useTabs {
doc.IndentTabs()
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
tab := test.ws
expected := "<root>" + test.nl + tab + "<child1>" + test.nl +
tab + tab + "<child2/>" + test.nl + tab +
"</child1>" + test.nl + "</root>" + test.nl
checkStrEq(t, s, expected)
} else {
for i := 0; i < 256; i++ {
doc.Indent(i)
s, err := doc.WriteToString()
if err != nil {
t.Error("etree: failed to serialize document")
}
tab := strings.Repeat(test.ws, i)
expected := "<root>" + test.nl + tab + "<child1>" + test.nl +
tab + tab + "<child2/>" + test.nl + tab +
"</child1>" + test.nl + "</root>" + test.nl
checkStrEq(t, s, expected)
}
}
}
}
func TestTokenIndexing(t *testing.T) {
s := `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<store xmlns:t="urn:books-com:titles">
<!Directive>
<!--This is a comment-->
<book lang="en">
<t:title>Great Expectations</t:title>
<author>Charles Dickens</author>
<review/>
</book>
</store>`
doc := newDocumentFromString(t, s)
review := doc.FindElement("/store/book/review")
review.SetText("Excellent")
checkIndexes(t, &doc.Element)
doc.Indent(4)
checkIndexes(t, &doc.Element)
doc.Indent(NoIndent)
checkIndexes(t, &doc.Element)
e := NewElement("foo")
store := doc.SelectElement("store")
store.InsertChildAt(0, e)
checkIndexes(t, &doc.Element)
store.RemoveChildAt(0)
checkIndexes(t, &doc.Element)
}
func TestSetText(t *testing.T) {
doc := NewDocument()
root := doc.CreateElement("root")
checkDocEq(t, doc, `<root/>`)
checkStrEq(t, root.Text(), "")
checkIntEq(t, len(root.Child), 0)
root.SetText("foo")
checkDocEq(t, doc, `<root>foo</root>`)
checkStrEq(t, root.Text(), "foo")
checkIntEq(t, len(root.Child), 1)
root.SetText("bar")
checkDocEq(t, doc, `<root>bar</root>`)
checkStrEq(t, root.Text(), "bar")
checkIntEq(t, len(root.Child), 1)
root.CreateCData("cdata")
checkDocEq(t, doc, `<root>bar<![CDATA[cdata]]></root>`)
checkStrEq(t, root.Text(), "barcdata")
checkIntEq(t, len(root.Child), 2)
root.SetText("qux")
checkDocEq(t, doc, `<root>qux</root>`)
checkStrEq(t, root.Text(), "qux")
checkIntEq(t, len(root.Child), 1)
root.CreateCData("cdata")
checkDocEq(t, doc, `<root>qux<![CDATA[cdata]]></root>`)
checkStrEq(t, root.Text(), "quxcdata")
checkIntEq(t, len(root.Child), 2)
root.SetCData("baz")
checkDocEq(t, doc, `<root><![CDATA[baz]]></root>`)
checkStrEq(t, root.Text(), "baz")
checkIntEq(t, len(root.Child), 1)
root.CreateText("corge")
root.CreateCData("grault")
root.CreateText("waldo")
root.CreateCData("fred")
root.CreateElement("child")
checkDocEq(t, doc, `<root><![CDATA[baz]]>corge<![CDATA[grault]]>waldo<![CDATA[fred]]><child/></root>`)
checkStrEq(t, root.Text(), "bazcorgegraultwaldofred")
checkIntEq(t, len(root.Child), 6)
root.SetText("plugh")
checkDocEq(t, doc, `<root>plugh<child/></root>`)
checkStrEq(t, root.Text(), "plugh")
checkIntEq(t, len(root.Child), 2)
root.SetText("")
checkDocEq(t, doc, `<root><child/></root>`)
checkStrEq(t, root.Text(), "")
checkIntEq(t, len(root.Child), 1)
root.SetText("")
checkDocEq(t, doc, `<root><child/></root>`)
checkStrEq(t, root.Text(), "")
checkIntEq(t, len(root.Child), 1)
root.RemoveChildAt(0)
root.CreateText("corge")
root.CreateCData("grault")
root.CreateText("waldo")
root.CreateCData("fred")
root.CreateElement("child")
checkDocEq(t, doc, `<root>corge<![CDATA[grault]]>waldo<![CDATA[fred]]><child/></root>`)
checkStrEq(t, root.Text(), "corgegraultwaldofred")
checkIntEq(t, len(root.Child), 5)
root.SetText("")
checkDocEq(t, doc, `<root><child/></root>`)
checkStrEq(t, root.Text(), "")
checkIntEq(t, len(root.Child), 1)
}
func TestSetTail(t *testing.T) {
doc := NewDocument()
root := doc.CreateElement("root")
child := root.CreateElement("child")
root.CreateText("\n\t")
child.SetText("foo")
checkDocEq(t, doc, "<root><child>foo</child>\n\t</root>")
checkStrEq(t, child.Tail(), "\n\t")
checkIntEq(t, len(root.Child), 2)
checkIntEq(t, len(child.Child), 1)
root.CreateCData(" ")
checkDocEq(t, doc, "<root><child>foo</child>\n\t<![CDATA[ ]]></root>")
checkStrEq(t, child.Tail(), "\n\t ")
checkIntEq(t, len(root.Child), 3)
checkIntEq(t, len(child.Child), 1)
child.SetTail("")
checkDocEq(t, doc, "<root><child>foo</child></root>")
checkStrEq(t, child.Tail(), "")
checkIntEq(t, len(root.Child), 1)
checkIntEq(t, len(child.Child), 1)
child.SetTail("\t\t\t")
checkDocEq(t, doc, "<root><child>foo</child>\t\t\t</root>")
checkStrEq(t, child.Tail(), "\t\t\t")
checkIntEq(t, len(root.Child), 2)
checkIntEq(t, len(child.Child), 1)
child.SetTail("\t\n\n\t")
checkDocEq(t, doc, "<root><child>foo</child>\t\n\n\t</root>")
checkStrEq(t, child.Tail(), "\t\n\n\t")
checkIntEq(t, len(root.Child), 2)
checkIntEq(t, len(child.Child), 1)
child.SetTail("")
checkDocEq(t, doc, "<root><child>foo</child></root>")
checkStrEq(t, child.Tail(), "")
checkIntEq(t, len(root.Child), 1)
checkIntEq(t, len(child.Child), 1)
}
func TestAttrParent(t *testing.T) {
doc := NewDocument()
root := doc.CreateElement("root")
attr1 := root.CreateAttr("bar", "1")
attr2 := root.CreateAttr("qux", "2")
checkIntEq(t, len(root.Attr), 2)
checkElementEq(t, attr1.Element(), root)
checkElementEq(t, attr2.Element(), root)
attr1 = root.RemoveAttr("bar")
attr2 = root.RemoveAttr("qux")
checkElementEq(t, attr1.Element(), nil)
checkElementEq(t, attr2.Element(), nil)
s := `<root a="1" b="2" c="3" d="4"/>`
err := doc.ReadFromString(s)
if err != nil {
t.Error("etree: failed to parse document")
}
root = doc.SelectElement("root")
for i := range root.Attr {
checkElementEq(t, root.Attr[i].Element(), root)
}
}
func TestDefaultNamespaceURI(t *testing.T) {
s := `
<root xmlns="https://root.example.com" xmlns:attrib="https://attrib.example.com" attrib:a="foo" b="bar">
<child1 xmlns="https://child.example.com" attrib:a="foo">
<grandchild1 xmlns="https://grandchild.example.com" a="foo">
</grandchild1>
<grandchild2 a="foo">
<greatgrandchild1 attrib:a="foo"/>
</grandchild2>
</child1>
<child2 a="foo"/>
</root>`
doc := newDocumentFromString(t, s)
root := doc.SelectElement("root")
child1 := root.SelectElement("child1")
child2 := root.SelectElement("child2")
grandchild1 := child1.SelectElement("grandchild1")
grandchild2 := child1.SelectElement("grandchild2")
greatgrandchild1 := grandchild2.SelectElement("greatgrandchild1")
checkStrEq(t, doc.NamespaceURI(), "")
checkStrEq(t, root.NamespaceURI(), "https://root.example.com")
checkStrEq(t, child1.NamespaceURI(), "https://child.example.com")
checkStrEq(t, child2.NamespaceURI(), "https://root.example.com")
checkStrEq(t, grandchild1.NamespaceURI(), "https://grandchild.example.com")
checkStrEq(t, grandchild2.NamespaceURI(), "https://child.example.com")
checkStrEq(t, greatgrandchild1.NamespaceURI(), "https://child.example.com")
checkStrEq(t, root.Attr[0].NamespaceURI(), "")
checkStrEq(t, root.Attr[1].NamespaceURI(), "")
checkStrEq(t, root.Attr[2].NamespaceURI(), "https://attrib.example.com")
checkStrEq(t, root.Attr[3].NamespaceURI(), "")
checkStrEq(t, child1.Attr[0].NamespaceURI(), "")
checkStrEq(t, child1.Attr[1].NamespaceURI(), "https://attrib.example.com")
checkStrEq(t, child2.Attr[0].NamespaceURI(), "")
checkStrEq(t, grandchild1.Attr[0].NamespaceURI(), "")
checkStrEq(t, grandchild1.Attr[1].NamespaceURI(), "")
checkStrEq(t, grandchild2.Attr[0].NamespaceURI(), "")
checkStrEq(t, greatgrandchild1.Attr[0].NamespaceURI(), "https://attrib.example.com")