Files
rosetta/tests/codecs/xml.luau
T

189 lines
8.4 KiB
Luau

-- codecs/text/Xml tests: random documents against Python's ElementTree, well-formedness against expat, the API.
-- Fixtures: tools/fixtures/xml.py.
local T = require("@tests/T")
local Xml = require("@rosetta/codecs/text/Xml")
local Types = require("@rosetta/Types")
local Err = require("@rosetta/core/Err")
local Docs = require("@tests/gen/xml_docs")
local Wf = require("@tests/gen/xml_wf")
local t = T.suite("xml")
-- ElementTree's view of an element (tools/fixtures/xml.py canon): Clark names, xmlns attributes dropped, text / tail
local function s(x: string?): string
x = x or ""
return #(x :: string) .. ":" .. (x :: string)
end
local function canon(e: any, tail: string?): string
local out = { "(" .. s(Xml.clark(e)) }
local keys = {}
local vals = {}
for _, k in Types.keys(e.attrs) do
if k ~= "xmlns" and string.sub(k, 1, 6) ~= "xmlns:" then
local ck = Xml.clark(e, k)
table.insert(keys, ck)
vals[ck] = e.attrs[k]
end
end
table.sort(keys)
for _, k in keys do
table.insert(out, "|" .. s(k) .. "=" .. s(vals[k]))
end
-- text: strings before the first element child; each child's tail: strings after it
local text = {}
local i = 1
local ch = e.children
while i <= #ch and (type(ch[i]) == "string" or ch[i].name == "#cdata") do
table.insert(text, Xml.text(ch[i]))
i += 1
end
table.insert(out, "|t" .. s(table.concat(text)))
while i <= #ch do
local c = ch[i]
i += 1
local tl = {}
while i <= #ch and (type(ch[i]) == "string" or ch[i].name == "#cdata") do
table.insert(tl, Xml.text(ch[i]))
i += 1
end
table.insert(out, canon(c, table.concat(tl)))
end
return table.concat(out) .. ")t" .. s(tail)
end
for i, d in Docs do
local doc, err = Xml.decode(d.data, { whitespace = true })
t:check(doc, "doc " .. i .. ": " .. tostring(err))
t:eq(canon((doc :: any).root), d.canon, "doc " .. i .. ": tree")
-- keep everything, encode, decode again: the same tree
local full = Xml.decode(d.data, { whitespace = true, comments = true, cdata = true, pi = true }) :: any
local out, eerr = Xml.encode(full)
t:check(out, "doc " .. i .. ": encodes (" .. tostring(eerr) .. ")")
local again, aerr = Xml.decode(out :: string, { whitespace = true })
t:check(again, "doc " .. i .. ": re-decodes (" .. tostring(aerr) .. ")")
t:eq(canon((again :: any).root), d.canon, "doc " .. i .. ": round trip")
local pretty = Xml.encode(full, { indent = 2 }) :: string
t:check(Xml.decode(pretty), "doc " .. i .. ": pretty output parses")
-- the namespace option agrees with the clark helper
local nsdoc = Xml.decode(d.data, { namespaces = true }) :: any
local root = nsdoc.root
local want = string.match(Xml.clark(root), "^{([^}]*)}")
t:eq(root.ns, want, "doc " .. i .. ": node.ns")
end
local agree, total, accepted, rejected = 0, 0, 0, 0
for i, c in Wf do
local doc = Xml.decode(c.data)
local ok = doc ~= nil
total += 1
if ok == c.ok then
agree += 1
elseif i <= 60 then
t:check(false, "wf case " .. i .. " " .. string.format("%q", c.data) .. ": expat says " .. tostring(c.ok))
end
if c.ok then
accepted += 1
else
rejected += 1
end
end
t:note("well-formedness: %d of %d verdicts agree with expat (%d well-formed, %d not)", agree, total, accepted, rejected)
t:check(agree >= total * 0.99, "well-formedness agreement >= 99%")
do
local src = '<?xml version="1.0"?>\n<!DOCTYPE r [<!ENTITY e "E&#38;#38;">]>\n<!--c--><r xmlns="urn:d" xmlns:p="urn:p" z="1" a="2"><p:x p:k="v">t<![CDATA[<c>]]>u</p:x><?go now?><y/>\n</r>'
local doc = Xml.decode(src, { comments = true, cdata = true, pi = true, namespaces = true }) :: any
t:eq(doc.declaration.version, "1.0", "declaration")
t:eq(doc.doctype.name, "r", "doctype name")
t:eq(doc.doctype.entities.e, "E&#38;", "doctype entities (char refs expanded once)")
t:eq(doc.children[1].name, "#comment", "prolog comment kept")
local r = doc.root
t:eq(table.concat(Types.keys(r.attrs), ","), "xmlns,xmlns:p,z,a", "attribute order")
t:eq(r.ns, "urn:d", "default namespace")
local x = Xml.child(r, "x") :: any
t:eq(x.name, "p:x", "child by local name")
t:eq(x.ns, "urn:p", "prefixed namespace")
t:eq(Xml.clark(x, "p:k"), "{urn:p}k", "attribute clark")
t:eq(x.children[2].name, "#cdata", "cdata node")
t:eq(Xml.text(x), "t<c>u", "text with cdata")
t:eq(r.children[2].name, "#pi", "pi node")
t:eq(r.children[2].target .. "|" .. r.children[2].text, "go|now", "pi target / data")
t:eq(#Xml.children(r), 2, "element children")
t:eq(#Xml.descendants(doc), 3, "descendants")
t:eq(x.parent, r, "parent link")
t:eq(Xml.decode("<a>&e;</a>") == nil, true, "undefined entity")
local d2 = Xml.decode('<!DOCTYPE r [<!ENTITY e "E&#38;#38;">]><r a="&e;">&e;</r>') :: any
t:eq(d2.root.attrs.a, "E&", "entity in attribute")
t:eq(Xml.text(d2.root), "E&", "entity in text")
-- whitespace handling
local w = Xml.decode("<a>\n <b> x </b>\n <c xml:space='preserve'> </c>\n</a>") :: any
t:eq(#w.root.children, 2, "whitespace-only text dropped")
t:eq(Xml.text(w.root.children[1]), " x ", "other text kept as is")
t:eq(w.root.children[2].children[1], " ", "xml:space preserve")
t:eq(#(Xml.decode("<a>\n <b/>\n</a>", { whitespace = true }) :: any).root.children, 3, "opts.whitespace")
-- attribute normalization and line endings
local n = Xml.decode("<a b='x\ty\r\nz&#10;'>l1\r\nl2\rl3</a>") :: any
t:eq(n.root.attrs.b, "x y z\n", "attribute value normalization")
t:eq(Xml.text(n.root), "l1\nl2\nl3", "line endings")
local e = Xml.element("svg", { width = "10", height = "5" }, { Xml.element("rect", { x = "1" }), "a<b & \"c\"" })
Xml.setAttr(e, "id", "q\"\n")
t:eq(Xml.encode(e), '<svg height="5" width="10" id="q&quot;&#10;"><rect x="1"/>a&lt;b &amp; "c"</svg>', "encode element")
local d3 = Xml.document(Xml.element("r", nil, { Xml.element("a"), Xml.element("b", nil, { "t" }) }))
t:eq(Xml.encode(d3, { indent = 2 }), '<?xml version="1.0" encoding="UTF-8"?>\n<r>\n <a/>\n <b>t</b>\n</r>\n', "pretty document")
t:eq(Xml.encode(Xml.element("a"), { selfClose = false }), "<a></a>", "selfClose false")
local _, be = Xml.encode(Xml.element("1bad"))
t:eq(be and be.code, Err.ARG, "bad name on encode")
local cyc = Xml.element("a")
table.insert(cyc.children, cyc)
local _, ce = Xml.encode(cyc)
t:eq(ce and ce.code, Err.ARG, "cycle on encode")
t:eq(Xml.encode({ name = "#cdata", attrs = Types.object(), children = {}, text = "a]]>b" } :: any), "<![CDATA[a]]]]><![CDATA[>b]]>", "cdata split")
local evs = {}
for ev, a in Xml.events("<a x='1'><b>t</b><!--c--></a>") do
table.insert(evs, ev .. (if type(a) == "string" then ":" .. a else ""))
end
t:eq(table.concat(evs, " "), "start:a start:b text:t end:b comment:c end:a", "events")
local last
for ev, a in Xml.events("<a><b></a>") do
last = { ev, a }
end
t:eq(last[1], "error", "events error")
t:eq(last[2].code, Err.CORRUPT, "events error code")
-- UTF-16 without BOM and encodings
local u16 = buffer.create(8)
for k, ch in { 60, 0, 97, 0, 47, 0, 62, 0 } do
buffer.writeu8(u16, k - 1, ch)
end
t:eq((Xml.decode(u16) :: any).root.name, "a", "UTF-16LE without BOM")
local _, ue = Xml.decode("<?xml version='1.0' encoding='EBCDIC'?><a/>")
t:eq(ue and ue.code, Err.UNSUPPORTED, "unsupported encoding")
end
local function code(src: string, opts: any?): string?
local _, e = Xml.decode(src, opts)
return e and e.code
end
t:eq(code("<a><b>"), Err.TRUNCATED, "unclosed")
t:eq(code("<a></b>"), Err.CORRUPT, "mismatch")
t:eq(code("<a b='1' b='2'/>"), Err.CORRUPT, "duplicate attribute")
t:eq(code(string.rep("<a>", 60) .. string.rep("</a>", 60), { maxDepth = 50 }), Err.LIMIT, "maxDepth")
t:eq(code('<!DOCTYPE r [<!ENTITY a "aaaaaaaaaa"><!ENTITY b "&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;"><!ENTITY c "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;"><!ENTITY d "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;"><!ENTITY e "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">]><r>&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;</r>', { maxExpansion = 100000 }), Err.LIMIT, "billion laughs")
t:eq(code('<!DOCTYPE r [<!ENTITY a "&a;">]><r>&a;</r>'), Err.CORRUPT, "recursive entity")
t:eq(code('<!DOCTYPE r [<!ENTITY x SYSTEM "file.xml">]><r>&x;</r>'), Err.UNSUPPORTED, "external entity")
t:eq(code("<p:a/>", { namespaces = true }), Err.CORRUPT, "undeclared prefix")
local _, ae = Xml.decode(5 :: any)
t:eq(ae and ae.code, Err.ARG, "bad argument")
local samples = {}
for i = 1, 40 do
table.insert(samples, Docs[i].data)
end
t:fuzz("decode", function(b: buffer)
return Xml.decode(b, { comments = true, namespaces = true })
end, samples)
t:fuzz("encode(decode)", function(b: buffer)
local d = Xml.decode(b, { comments = true, cdata = true, pi = true })
return d and Xml.encode(d, { indent = 1 })
end, samples, { garbage = 20 })
t:done()