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
|
package test_core_xml
import "core:encoding/xml"
import "core:testing"
import "core:strings"
import "core:io"
import "core:fmt"
import "core:log"
import "core:hash"
Silent :: proc(pos: xml.Pos, format: string, args: ..any) {}
OPTIONS :: xml.Options{ flags = { .Ignore_Unsupported, .Intern_Comments, },
expected_doctype = "",
}
TEST :: struct {
filename: string,
options: xml.Options,
err: xml.Error,
crc32: u32,
}
TEST_SUITE_PATH :: ODIN_ROOT + "tests/core/assets/"
@(test)
xml_test_utf8_normal :: proc(t: ^testing.T) {
run_test(t, {
// Tests UTF-8 idents and values.
// Test namespaced ident.
// Tests that nested partial CDATA start doesn't trip up parser.
filename = "XML/utf8.xml",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments,
},
expected_doctype = "恥ずべきフクロウ",
},
crc32 = 0xefa55f27,
})
}
@(test)
xml_test_utf8_unbox_cdata :: proc(t: ^testing.T) {
run_test(t, {
// Same as above.
// Unbox CDATA in data tag.
filename = "XML/utf8.xml",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA,
},
expected_doctype = "恥ずべきフクロウ",
},
crc32 = 0x2dd27770,
})
}
@(test)
xml_test_nl_qt_ts :: proc(t: ^testing.T) {
run_test(t, {
// Simple Qt TS translation file.
// `core:i18n` requires it to be parsed properly.
filename = "I18N/nl_NL-qt-ts.ts",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities,
},
expected_doctype = "TS",
},
crc32 = 0x859b7443,
})
}
@(test)
xml_test_xliff_1_2 :: proc(t: ^testing.T) {
run_test(t, {
// Simple XLiff 1.2 file.
// `core:i18n` requires it to be parsed properly.
filename = "I18N/nl_NL-xliff-1.2.xliff",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities,
},
expected_doctype = "xliff",
},
crc32 = 0x3deaf329,
})
}
@(test)
xml_test_xliff_2_0 :: proc(t: ^testing.T) {
run_test(t, {
// Simple XLiff 2.0 file.
// `core:i18n` requires it to be parsed properly.
filename = "I18N/nl_NL-xliff-2.0.xliff",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities,
},
expected_doctype = "xliff",
},
crc32 = 0x0c55e287,
})
}
@(test)
xml_test_entities :: proc(t: ^testing.T) {
run_test(t, {
filename = "XML/entities.html",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments,
},
expected_doctype = "html",
},
crc32 = 0x98791215,
})
}
@(test)
xml_test_entities_unbox :: proc(t: ^testing.T) {
run_test(t, {
filename = "XML/entities.html",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA,
},
expected_doctype = "html",
},
crc32 = 0x5fd5ab4e,
})
}
@(test)
xml_test_entities_unbox_decode :: proc(t: ^testing.T) {
run_test(t, {
filename = "XML/entities.html",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments, .Unbox_CDATA, .Decode_SGML_Entities,
},
expected_doctype = "html",
},
crc32 = 0xda1ac384,
})
}
@(test)
xml_test_attribute_whitespace :: proc(t: ^testing.T) {
run_test(t, {
// Same as above.
// Unbox CDATA in data tag.
filename = "XML/attribute-whitespace.xml",
options = {
flags = {},
expected_doctype = "foozle",
},
crc32 = 0x8f5fd6c1,
})
}
@(test)
xml_test_invalid_doctype :: proc(t: ^testing.T) {
run_test(t, {
filename = "XML/utf8.xml",
options = {
flags = {
.Ignore_Unsupported, .Intern_Comments,
},
expected_doctype = "Odin",
},
err = .Invalid_DocType,
crc32 = 0x49b83d0a,
})
}
@(test)
xml_test_unicode :: proc(t: ^testing.T) {
run_test(t, {
filename = "XML/unicode.xml",
options = {
flags = {
.Ignore_Unsupported,
},
expected_doctype = "",
},
err = .None,
crc32 = 0x73070b55,
})
}
@(private)
run_test :: proc(t: ^testing.T, test: TEST, loc := #caller_location) {
path := strings.concatenate({TEST_SUITE_PATH, test.filename})
defer delete(path)
doc, err := xml.load_from_file(path, test.options, Silent)
defer xml.destroy(doc)
tree_string := doc_to_string(doc)
tree_bytes := transmute([]u8)tree_string
defer delete(tree_bytes)
crc32 := hash.crc32(tree_bytes)
failed := err != test.err
testing.expectf(t, err == test.err, "%v: Expected return value %v, got %v", test.filename, test.err, err, loc=loc)
failed |= crc32 != test.crc32
testing.expectf(t, crc32 == test.crc32, "%v: Expected CRC 0x%08x, got 0x%08x, with options %v", test.filename, test.crc32, crc32, test.options, loc=loc)
if failed {
// Don't fully print big trees.
tree_string = tree_string[:min(2_048, len(tree_string))]
log.error(tree_string)
}
}
@(private)
doc_to_string :: proc(doc: ^xml.Document) -> (result: string) {
/*
Effectively a clone of the debug printer in the xml package.
We duplicate it here so that the way it prints an XML document to a string is stable.
This way we can hash the output. If it changes, it means that the document or how it was parsed changed,
not how it was printed. One less source of variability.
*/
print :: proc(writer: io.Writer, doc: ^xml.Document) -> (written: int, err: io.Error) {
if doc == nil { return }
written += fmt.wprintf(writer, "[XML Prolog]\n")
for attr in doc.prologue {
written += fmt.wprintf(writer, "\t%v: %v\n", attr.key, attr.val)
}
written += fmt.wprintf(writer, "[Encoding] %v\n", doc.encoding)
if len(doc.doctype.ident) > 0 {
written += fmt.wprintf(writer, "[DOCTYPE] %v\n", doc.doctype.ident)
if len(doc.doctype.rest) > 0 {
fmt.wprintf(writer, "\t%v\n", doc.doctype.rest)
}
}
for comment in doc.comments {
written += fmt.wprintf(writer, "[Pre-root comment] %v\n", comment)
}
if doc.element_count > 0 {
fmt.wprintln(writer, " --- ")
print_element(writer, doc, 0)
fmt.wprintln(writer, " --- ")
}
return written, .None
}
print_element :: proc(writer: io.Writer, doc: ^xml.Document, element_id: xml.Element_ID, indent := 0) -> (written: int, err: io.Error) {
tab :: proc(writer: io.Writer, indent: int) {
for _ in 0..=indent {
fmt.wprintf(writer, "\t")
}
}
tab(writer, indent)
element := doc.elements[element_id]
if element.kind == .Element {
fmt.wprintf(writer, "<%v>\n", element.ident)
for value in element.value {
switch v in value {
case string:
tab(writer, indent + 1)
fmt.wprintf(writer, "[Value] %v\n", v)
case xml.Element_ID:
print_element(writer, doc, v, indent + 1)
}
}
for attr in element.attribs {
tab(writer, indent + 1)
fmt.wprintf(writer, "[Attr] %v: %v\n", attr.key, attr.val)
}
} else if element.kind == .Comment {
fmt.wprintf(writer, "[COMMENT] %v\n", element.value)
}
return written, .None
}
buf: strings.Builder
defer strings.builder_destroy(&buf)
print(strings.to_writer(&buf), doc)
return strings.clone(strings.to_string(buf))
}
|