-- model/rbxmx tests: XML models and places built by rojo 7.7 and lune 0.11 (rbx-dom) decode to the values lune reads -- from them; our rbxmx output read back by lune equals the original (fixture verdicts); round trips of every reference -- file and of a tree with every type; XML specifics (BinaryString, CDATA, packed Color3, INF / NAN, unknown property -- tags kept raw, referents, SharedStrings, floats written exactly); errors and the fuzz pass. -- Fixtures: tools/fixtures/rbxmx.py. local T = require("@tests/T") local Rbxmx = require("@formats/model/rbxmx") local Rbx = require("@formats/codecs/model/Rbx") local Bytes = require("@formats/core/Bytes") local Err = require("@formats/core/Err") local H = require("@tests/model/_rbx") local Fx = require("@tests/gen/rbx_files") local Json = require("@formats/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 ") 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, '', 1, true) == 1, "rbx-dom's root element") t:check(string.find(s, "", 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 true null ]]> 4294934528 INF -INF NAN RBXA null AAEC AwQ= rbxasset://textures/a.png 1 true20.30.511 ]==] 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 ", "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 ") 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, " kept") local out = encode(tree) t:check(string.find(out, '1', 1, true) ~= nil or string.find(out, "true', 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, '', 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, '0.1', 1, true) ~= nil, "double 0.1") t:check(string.find(fs, '0.1', 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 -- sniff, errors 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("\n"))), 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(""))), 0, "sniff other XML") local _, e1 = decode("") t:check(e1 and e1.code == Err.CORRUPT, "not : " .. tostring(e1)) local _, e2 = decode("") t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2)) local _, e3 = decode("") 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()