Files
rosetta/tests/model/rbxmx.luau
T

184 lines
8.0 KiB
Luau

-- model/rbxmx tests: rojo 7.7 / lune 0.11 XML models and places against lune's reading; lune reads ours.
-- Fixtures: tools/fixtures/rbxmx.py.
local T = require("@tests/T")
local Rbxmx = require("@rosetta/model/rbxmx")
local Rbx = require("@rosetta/codecs/model/Rbx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local H = require("@tests/model/_rbx")
local Fx = require("@tests/gen/rbx_files")
local Json = require("@rosetta/codecs/text/Json")
local Ours = require("@tests/gen/rbx_ours")
local t = T.suite("rbxmx")
t:eq(Rbxmx.name, "rbxmx", "name")
t:eq(Rbxmx.category, "model", "category")
t:check(table.find(Rbxmx.extensions, "rbxlx") ~= nil, "extensions")
t:check(Rbxmx.caps.decode and Rbxmx.caps.encode, "caps")
local function decode(data: any, opts: any?): (any, any)
return Rbxmx.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function encode(tree: any, opts: any?): string
return buffer.tostring(Rbxmx.encode(tree, opts) :: buffer)
end
local nprops = 0
local trees: { [string]: any } = {}
for _, f in Fx.files do
if f.xml then
local tree, err = decode(f.data)
t:check(tree ~= nil, f.name .. ": " .. tostring(err))
if tree then
trees[f.name] = tree
t:check(tree.kind == "rbxtree" and tree.meta.source.format == "rbxmx", f.name .. ": an RbxTree")
t:eq(tree.place, f.place, f.name .. ": place")
nprops += H.compare(t, tree, Json.decode(f.dump), f.name)
end
end
end
t:note("3 XML files, %d property values match lune", nprops)
do
local tree = trees["model.rbxmx"]
local P = (Rbx.find(tree, "Kit/Types") :: any).properties
t:eq(P.PBinary.type, "BinaryString", "BinaryString (CDATA)")
t:eq(P.PBinary.value, "\0\1\2\3\4\255", "BinaryString bytes")
t:eq(P.PContentId.type, "ContentId", "ContentId")
t:eq(P.PFloat32.value, 1.5, "float")
t:eq(P.PUniqueId.value, "44b188dace632b4702e9c68d004815fc", "UniqueId")
t:eq(P.PShared.value, "\0\1\2\3", "SharedString")
t:eq(P.PBrickColor.type, "Int32", "a BrickColor of an unknown property reads as its <int>")
local base = (Rbx.find(tree, "Kit/Base") :: any).properties
t:eq(base.Tags.value, "Enemy\0Boss", "Tags")
t:check(#base.AttributesSerialize.value > 10, "AttributesSerialize")
local lune = trees["lune.rbxmx"]
t:eq((Rbx.find(lune, "Kit/Brick") :: any).properties.Value.type, "BrickColor", "BrickColorValue.Value is a BrickColor")
end
local nours = 0
for _, v in Ours do
if string.sub(v.name, -6) == "/rbxmx" then
t:check(v.ok, v.name .. ": " .. v.detail)
nours += 1
end
end
t:note("%d of our rbxmx files read back by lune", nours)
for name, tree in trees do
local back = decode(encode(tree))
H.sameTree(t, tree, back, name .. ": round trip")
t:eq(back.place, tree.place, name .. ": place round trip")
end
do
local tree = H.everything()
local s = encode(tree)
H.sameTree(t, tree, decode(s), "every type", { loose = true })
t:check(string.find(s, '<roblox version="4">', 1, true) == 1, "rbx-dom's root element")
t:check(string.find(s, "<SharedStrings>", 1, true) ~= nil, "SharedStrings")
local again = encode(decode(s))
t:eq(again, s, "a second write is byte-identical")
local flat = encode(tree, { indent = 0 })
t:check(not string.find(flat, "\n <Item", 1, true) and decode(flat) ~= nil, "indent = 0")
end
do
local doc = [==[<?xml version="1.0" encoding="utf-8"?>
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" version="4">
<Meta name="ExplicitAutoJoints">true</Meta>
<External>null</External>
<Item class="Part" referent="RBXA">
<Properties>
<string name="Name"><![CDATA[Odd <name>]]></string>
<Color3 name="Color">4294934528</Color3>
<float name="Inf">INF</float>
<double name="NegInf">-INF</double>
<float name="Nan">NAN</float>
<Ref name="Self">RBXA</Ref>
<Ref name="None">null</Ref>
<BinaryString name="Data">
AAEC
AwQ=
</BinaryString>
<Content name="Old"><url>rbxasset://textures/a.png</url></Content>
<Content name="OldNull"><null></null></Content>
<FutureType name="Mystery"><A>1</A><B x="2"/></FutureType>
<PhysicalProperties name="Phys"><CustomPhysics>true</CustomPhysics><Density>2</Density><Friction>0.3</Friction><Elasticity>0.5</Elasticity><FrictionWeight>1</FrictionWeight><ElasticityWeight>1</ElasticityWeight></PhysicalProperties>
</Properties>
</Item>
</roblox>]==]
local tree, err = decode(doc)
t:check(tree ~= nil, "a hand-written file: " .. tostring(err))
local p = tree.roots[1]
t:eq(p.name, "Odd <name>", "CDATA name")
local c = p.properties.Color.value
t:check(c[1] == 1 and math.abs(c[2] - 0x80 / 255) < 1e-6 and c[3] == 0, "packed ARGB Color3")
t:eq(p.properties.Inf.value, math.huge, "INF")
t:eq(p.properties.NegInf.value, -math.huge, "-INF")
t:check(p.properties.Nan.value ~= p.properties.Nan.value, "NAN")
t:check(p.properties.Self.value == p, "Ref to itself")
t:check(p.properties.None.type == "Ref" and p.properties.None.value == nil, "Ref null")
t:eq(p.properties.Data.value, "\0\1\2\3\4", "BinaryString with whitespace")
t:eq(p.properties.Old.type, "ContentId", "legacy <Content><url>")
t:eq(p.properties.Old.value, "rbxasset://textures/a.png", "url")
t:eq(p.properties.Mystery.type, "XmlRaw", "unknown tag kept")
t:eq(p.properties.Phys.value[6], 1, "acoustic absorption defaults to 1")
t:eq(tree.metadata.ExplicitAutoJoints, "true", "Meta")
t:eq(#tree.extra, 1, "<External> kept")
local out = encode(tree)
t:check(string.find(out, '<FutureType name="Mystery"><A>1</A><B x="2"></B></FutureType>', 1, true) ~= nil
or string.find(out, "<FutureType", 1, true) ~= nil, "unknown tag written back")
local back = decode(out)
t:eq(back.roots[1].properties.Mystery.type, "XmlRaw", "unknown tag round trip")
t:eq(back.roots[1].properties.Inf.value, math.huge, "INF round trip")
t:check(back.roots[1].properties.Self.value == back.roots[1], "Ref round trip")
t:check(string.find(out, '<Meta name="ExplicitAutoJoints">true</Meta>', 1, true) ~= nil, "Meta written")
-- a String with bytes XML cannot hold is written as a BinaryString
local raw = Rbx.tree()
local f = Rbx.add(raw, Rbx.instance("Folder"))
Rbx.set(f, "Blob", "String", "a\0b\255")
local rs = encode(raw)
t:check(string.find(rs, '<BinaryString name="Blob">', 1, true) ~= nil, "binary bytes as BinaryString")
t:eq(decode(rs).roots[1].properties.Blob.value, "a\0b\255", "binary bytes round trip")
-- floats: the shortest text that reads back exactly
local fl = Rbx.tree()
local g = Rbx.add(fl, Rbx.instance("Folder"))
Rbx.set(g, "A", "Float64", 0.1)
Rbx.set(g, "B", "Float32", 0.1)
Rbx.set(g, "C", "Vector3", { 1 / 3, 1e-30, -2 ^ 100 })
local fs = encode(fl)
t:check(string.find(fs, '<double name="A">0.1</double>', 1, true) ~= nil, "double 0.1")
t:check(string.find(fs, '<float name="B">0.1</float>', 1, true) ~= nil, "float 0.1")
local fb = decode(fs).roots[1].properties
t:eq(fb.A.value, 0.1, "double exact")
local v = buffer.create(4)
buffer.writef32(v, 0, 1 / 3)
t:eq(fb.C.value[1], buffer.readf32(v, 0), "f32 exact")
end
do
local data = Fx.files[2].data
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(data))), 1, "sniff")
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<?xml version='1.0'?>\n<roblox version=\"4\">"))), 1, "sniff with a declaration")
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(Fx.files[1].data))), 0, "sniff binary")
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<svg/>"))), 0, "sniff other XML")
local _, e1 = decode("<svg></svg>")
t:check(e1 and e1.code == Err.CORRUPT, "not <roblox>: " .. tostring(e1))
local _, e2 = decode("<roblox version=\"4\"><Item class=\"Part\">")
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
local _, e3 = decode("<roblox><Item><Properties/></Item></roblox>")
t:check(e3 and e3.code == Err.CORRUPT, "an Item without a class: " .. tostring(e3))
local _, e4 = Rbxmx.encode({ kind = "scene" } :: any)
t:check(e4 and e4.code == Err.ARG, "encode a non-tree")
end
do
local samples = { Fx.files[2].data, encode(H.everything()) }
t:fuzz("decode", function(b: buffer)
return Rbxmx.decode(Bytes.reader(b))
end, samples, { mutations = 80, garbage = 300 })
end
t:done()