v2 / vlib / encoding / xml / encoding_test.v
205 lines · 200 sloc · 4.08 KB · 76579f865b6a12e0c816877c8480f4a9dae698c1
Raw
1module main
2
3import encoding.xml
4
5fn test_node() {
6 nodes := [
7 xml.XMLNode{
8 name: 'test'
9 attributes: {
10 'test:key': ' test_value '
11 'test:other': '123456'
12 }
13 children: [
14 xml.XMLNode{
15 name: 'child'
16 attributes: {
17 'child:key': 'child_value'
18 }
19 },
20 'Sample text',
21 ]
22 },
23 xml.XMLNode{
24 name: 's'
25 attributes: {
26 'k': 'v'
27 }
28 children: [
29 'Hello, world!',
30 xml.XMLNode{
31 name: 'c'
32 attributes: {
33 'k2': 'v2'
34 }
35 },
36 ]
37 },
38 xml.XMLNode{
39 name: 'ext'
40 attributes: {
41 'uri': '{B58B0392-4F1F-4190-BB64-5DF3571DCE5F}'
42 'xmlns:xcalcf': 'http://schemas.microsoft.com/office/spreadsheetml/2018/calcfeatures'
43 }
44 children: [
45 xml.XMLNode{
46 name: 'xcalcf:calcFeatures'
47 children: [
48 xml.XMLNode{
49 name: 'xcalcf:feature'
50 attributes: {
51 'name': 'microsoft.com:RD'
52 }
53 },
54 xml.XMLNode{
55 name: 'xcalcf:feature'
56 attributes: {
57 'name': 'microsoft.com:Single'
58 }
59 },
60 xml.XMLNode{
61 name: 'xcalcf:feature'
62 attributes: {
63 'name': 'microsoft.com:FV'
64 }
65 },
66 xml.XMLNode{
67 name: 'xcalcf:feature'
68 attributes: {
69 'name': 'microsoft.com:CNMTM'
70 }
71 },
72 xml.XMLNode{
73 name: 'xcalcf:feature'
74 attributes: {
75 'name': 'microsoft.com:LET_WF'
76 }
77 },
78 xml.XMLNode{
79 name: 'xcalcf:feature'
80 attributes: {
81 'name': 'microsoft.com:LAMBDA_WF'
82 }
83 },
84 xml.XMLNode{
85 name: 'xcalcf:feature'
86 attributes: {
87 'name': 'microsoft.com:ARRAYTEXT_WF'
88 }
89 },
90 ]
91 },
92 ]
93 },
94 ]
95 values := [
96 '
97 <test test:key=" test_value " test:other="123456">
98 <child child:key="child_value"/>
99 Sample text
100 </test>'.trim_indent(),
101 '
102 <s k="v">
103 Hello, world!
104 <c k2="v2"/>
105 </s>'.trim_indent(),
106 '
107 <ext uri="{B58B0392-4F1F-4190-BB64-5DF3571DCE5F}" xmlns:xcalcf="http://schemas.microsoft.com/office/spreadsheetml/2018/calcfeatures">
108 <xcalcf:calcFeatures>
109 <xcalcf:feature name="microsoft.com:RD"/>
110 <xcalcf:feature name="microsoft.com:Single"/>
111 <xcalcf:feature name="microsoft.com:FV"/>
112 <xcalcf:feature name="microsoft.com:CNMTM"/>
113 <xcalcf:feature name="microsoft.com:LET_WF"/>
114 <xcalcf:feature name="microsoft.com:LAMBDA_WF"/>
115 <xcalcf:feature name="microsoft.com:ARRAYTEXT_WF"/>
116 </xcalcf:calcFeatures>
117 </ext>'.trim_indent(),
118 ]
119 for i, node in nodes {
120 assert node.pretty_str('\t', 0, xml.default_entities_reverse) == values[i]
121 }
122}
123
124fn test_doc() {
125 docs := [
126 xml.XMLDocument{
127 root: xml.XMLNode{
128 name: 'test'
129 attributes: {
130 'test:key': ' test_value '
131 'test:other': '123456'
132 }
133 children: [
134 xml.XMLNode{
135 name: 'child'
136 attributes: {
137 'child:key': 'child_value'
138 }
139 },
140 'Sample text',
141 ]
142 }
143 },
144 xml.XMLDocument{
145 root: xml.XMLNode{
146 name: 's'
147 attributes: {
148 'k': 'v'
149 }
150 children: [
151 'Hello, world!',
152 xml.XMLNode{
153 name: 'c'
154 attributes: {
155 'k2': 'v2'
156 }
157 },
158 ]
159 }
160 },
161 ]
162 values := [
163 '
164 <?xml version="1.0" encoding="UTF-8"?>
165 <test test:key=" test_value " test:other="123456">
166 <child child:key="child_value"/>
167 Sample text
168 </test>'.trim_indent(),
169 '
170 <?xml version="1.0" encoding="UTF-8"?>
171 <s k="v">
172 Hello, world!
173 <c k2="v2"/>
174 </s>'.trim_indent(),
175 ]
176 for i, doc in docs {
177 assert doc.pretty_str('\t') == values[i]
178 }
179}
180
181fn test_large_doc_str() {
182 depth := 1500
183 payload := '0123456789abcdef'.repeat(16)
184 mut node := xml.XMLNode{
185 name: 'leaf'
186 }
187 for i in 0 .. depth {
188 node = xml.XMLNode{
189 name: 'n${i}'
190 children: [
191 payload,
192 node,
193 ]
194 }
195 }
196 doc := xml.XMLDocument{
197 root: node
198 }
199
200 rendered := doc.str()
201 assert rendered.starts_with('<?xml version="1.0" encoding="UTF-8"?>\n<n${depth - 1}>')
202 assert rendered.contains('<leaf/>')
203 assert rendered.ends_with('</n${depth - 1}>')
204 assert rendered.len > 5_000_000
205}
206