-- data/xml tests: the contract, the format through the API (F.load / F.save / sniffing), documents from Python's
-- ElementTree fixtures decoded and re-encoded through the format, and the fuzz pass. The codec itself is tested in
-- tests/codecs/xml.luau. Fixtures: tools/fixtures/xml.py.
local T = require("@tests/T")
local F = require("@formats")
local XmlF = require("@formats/data/xml")
local Xml = require("@formats/codecs/text/Xml")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Docs = require("@tests/gen/xml_docs")
local t = T.suite("data/xml")
t:eq(XmlF.name, "xml", "name")
t:eq(XmlF.category, "data", "category")
t:check(XmlF.caps.decode and XmlF.caps.encode, "caps")
for i = 1, 60 do
local d = Docs[i]
local doc, err = XmlF.decode(Bytes.reader(buffer.fromstring(d.data)), { comments = true, cdata = true, whitespace = true })
t:check(doc ~= nil, "doc " .. i .. ": " .. tostring(err))
local b, eerr = XmlF.encode(doc :: any)
t:check(b ~= nil, "doc " .. i .. ": encode " .. tostring(eerr))
local again = XmlF.decode(Bytes.reader(b :: buffer), { comments = true, cdata = true, whitespace = true })
t:check(again ~= nil, "doc " .. i .. ": re-decode")
t:eq(Xml.text((again :: any).root), Xml.text((doc :: any).root), "doc " .. i .. ": same text")
end
do
local doc, err = F.load(buffer.fromstring('t'))
t:check(doc ~= nil, "F.load sniffs xml: " .. tostring(err))
t:eq(doc.root.name, "r", "root")
local s = F.saveString(doc, { format = "xml" })
t:eq(s, '\nt', "F.saveString")
local _, e = XmlF.encode(5 :: any)
t:eq(e and e.code, Err.ARG, "encode argument check")
local _, e2 = F.load(buffer.fromstring(""), { format = "xml" })
t:eq(e2 and e2.code, Err.CORRUPT, "error through the API")
t:eq(e2 and e2.format, "xml", "error tagged with the format")
end
local samples = {}
for i = 1, 30 do
table.insert(samples, Docs[i].data)
end
t:fuzz("decode", function(b: buffer)
return XmlF.decode(Bytes.reader(b))
end, samples)
t:done()