v / vlib / encoding / xml / parser_test.v
130 lines · 123 sloc · 1.77 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1module xml
2
3const sample_doc = '
4<root>
5 <c id="c1"/>
6 <c id="c2">
7 Sample Text
8 </c>
9 <empty/>
10 <c id="c3"/>
11 <abc id="c4"/>
12 <xyz id="c5"/>
13 <c id="c6"/>
14 <cx id="c7"/>
15 <cd id="c8"/>
16 <child id="c9">
17 More Sample Text
18 </child>
19 <cz id="c10"/>
20</root>'
21
22const xml_elements = [
23 XMLNode{
24 name: 'c'
25 attributes: {
26 'id': 'c1'
27 }
28 },
29 XMLNode{
30 name: 'c'
31 attributes: {
32 'id': 'c2'
33 }
34 children: [
35 'Sample Text',
36 ]
37 },
38 XMLNode{
39 name: 'empty'
40 attributes: {}
41 },
42 XMLNode{
43 name: 'c'
44 attributes: {
45 'id': 'c3'
46 }
47 },
48 XMLNode{
49 name: 'abc'
50 attributes: {
51 'id': 'c4'
52 }
53 },
54 XMLNode{
55 name: 'xyz'
56 attributes: {
57 'id': 'c5'
58 }
59 },
60 XMLNode{
61 name: 'c'
62 attributes: {
63 'id': 'c6'
64 }
65 },
66 XMLNode{
67 name: 'cx'
68 attributes: {
69 'id': 'c7'
70 }
71 },
72 XMLNode{
73 name: 'cd'
74 attributes: {
75 'id': 'c8'
76 }
77 },
78 XMLNode{
79 name: 'child'
80 attributes: {
81 'id': 'c9'
82 }
83 children: [
84 'More Sample Text',
85 ]
86 },
87 XMLNode{
88 name: 'cz'
89 attributes: {
90 'id': 'c10'
91 }
92 },
93]
94
95fn test_single_element_parsing() ! {
96 mut reader := FullBufferReader{
97 contents: sample_doc.bytes()
98 }
99 // Skip the "<root>" tag
100 mut skip := []u8{len: 6}
101 reader.read(mut skip)!
102
103 mut local_buf := [u8(0)]
104 mut ch := next_char(mut reader, mut local_buf)!
105
106 mut count := 0
107
108 for count < xml_elements.len {
109 match ch {
110 `<` {
111 next_ch := next_char(mut reader, mut local_buf)!
112 match next_ch {
113 `/` {}
114 else {
115 parsed_element := parse_single_node(next_ch, mut reader)!
116 assert xml_elements[count] == parsed_element
117 count++
118 }
119 }
120
121 ch = next_char(mut reader, mut local_buf)!
122 }
123 else {
124 for ch != `<` {
125 ch = next_char(mut reader, mut local_buf)!
126 }
127 }
128 }
129 }
130}
131