287 lines
10 KiB
Luau
287 lines
10 KiB
Luau
-- tests/model/_rbx: helpers shared by the model/rbxm and model/rbxmx tests: instance paths (the scheme of the lune dump
|
|
-- in tools/fixtures/rbxmx.py), our property values in lune's canonical form, a dump comparison, a deep tree comparison,
|
|
-- and a tree with a value of every type built from scratch.
|
|
local Rbx = require("@rosetta/codecs/model/Rbx")
|
|
|
|
local H = {}
|
|
|
|
local f32buf = buffer.create(4)
|
|
local function f32(v: number): number
|
|
buffer.writef32(f32buf, 0, v)
|
|
return buffer.readf32(f32buf, 0)
|
|
end
|
|
|
|
local function hex(s: string): string
|
|
return (string.gsub(s, ".", function(c: string): string
|
|
return string.format("%02x", string.byte(c))
|
|
end))
|
|
end
|
|
|
|
-- paths(tree) -> { [path] = instance }, { [instance] = path }: names joined with "/", "#k" for the k-th sibling of a name
|
|
function H.paths(tree: any): ({ [string]: any }, { [any]: string })
|
|
local byPath, ofInst = {}, {}
|
|
local function walk(list: { any }, prefix: string)
|
|
local seen: { [string]: number } = {}
|
|
for _, inst in list do
|
|
local n = inst.name
|
|
seen[n] = (seen[n] or 0) + 1
|
|
local p = prefix .. (if prefix == "" then "" else "/") .. n .. (if seen[n] > 1 then "#" .. seen[n] else "")
|
|
byPath[p] = inst
|
|
ofInst[inst] = p
|
|
walk(inst.children, p)
|
|
end
|
|
end
|
|
walk(tree.roots, "")
|
|
return byPath, ofInst
|
|
end
|
|
|
|
local function numbers(t: string, v: any): { number }?
|
|
if type(v) == "number" then
|
|
return { v }
|
|
elseif type(v) ~= "table" then
|
|
return nil
|
|
end
|
|
if t == "NumberSequence" or t == "ColorSequence" then
|
|
local out = {}
|
|
for _, k in v do
|
|
local n = if t == "ColorSequence" then 4 else 3 -- lune has no ColorSequence envelope
|
|
for c = 1, n do
|
|
table.insert(out, k[c])
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
return v
|
|
end
|
|
|
|
-- same(ours: Value, want: lune canon, pathOf, propName) -> ok, detail
|
|
function H.same(v: any, want: any, pathOf: { [any]: string }, pname: string): (boolean, string)
|
|
local t = if v then v.type else "nil"
|
|
local x = if v then v.value else nil
|
|
local wt = want.t
|
|
if wt == "nil" then
|
|
return x == nil, "want nil, got " .. t
|
|
elseif wt == "string" then
|
|
return type(x) == "string" and hex(x) == want.s, "string differs (" .. t .. ")"
|
|
elseif wt == "bool" then
|
|
return x == want.b, "bool differs"
|
|
elseif wt == "Instance" then
|
|
return type(x) == "table" and pathOf[x] == want.s, "ref differs: " .. tostring(x and pathOf[x]) .. " vs " .. tostring(want.s)
|
|
elseif wt == "Content" then
|
|
if want.kind == "None" then
|
|
return x == nil, "content not none"
|
|
elseif want.kind == "Uri" then
|
|
return x == want.s, "content uri " .. tostring(x)
|
|
end
|
|
return type(x) == "table" and pathOf[x] == want.s, "content object"
|
|
elseif wt == "UniqueId" then
|
|
return x == want.s, "uniqueid " .. tostring(x) .. " vs " .. tostring(want.s)
|
|
elseif wt == "Font" then
|
|
return type(x) == "table" and x.family == want.s and x.weight == want.n[1] and x.style == want.style, "font differs"
|
|
end
|
|
local got = numbers(t, x)
|
|
if t == "Color3uint8" and got then
|
|
got = { f32(got[1] / 255), f32(got[2] / 255), f32(got[3] / 255) }
|
|
end
|
|
local n = want.n
|
|
if not got or not n then
|
|
return false, "no numbers: " .. t .. " vs " .. tostring(wt)
|
|
end
|
|
local count = #n
|
|
if t == "PhysicalProperties" then
|
|
count = math.min(#got, #n)
|
|
elseif #got ~= #n then
|
|
return false, string.format("%d numbers, want %d (%s / %s)", #got, #n, t, wt)
|
|
end
|
|
for i = 1, count do
|
|
if got[i] ~= n[i] then
|
|
return false, string.format("%s[%d] = %.9g, want %.9g", t, i, got[i], n[i])
|
|
end
|
|
end
|
|
return true, ""
|
|
end
|
|
|
|
-- compare(t, tree, dump, label) -> properties compared: every instance / property of a lune dump against our tree
|
|
function H.compare(t: any, tree: any, dump: any, label: string): number
|
|
local byPath, pathOf = H.paths(tree)
|
|
local n = 0
|
|
for path, e in dump do
|
|
local inst = byPath[path]
|
|
t:check(inst ~= nil, label .. ": instance " .. path)
|
|
if inst then
|
|
t:check(inst.className == e.class, label .. ": " .. path .. " class " .. inst.className .. ", want " .. e.class)
|
|
for pname, want in e.props do
|
|
local v = if pname == "Name" then { type = "String", value = inst.name } else inst.properties[pname]
|
|
local ok, detail = H.same(v, want, pathOf, pname)
|
|
t:check(ok, label .. ": " .. path .. "." .. pname .. ": " .. detail)
|
|
n += 1
|
|
end
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
-- the type family a value compares under (a binary file has one string type; XML writes BrickColor as an int)
|
|
local FAMILY: { [string]: string } = {
|
|
String = "string",
|
|
BinaryString = "string",
|
|
ProtectedString = "string",
|
|
ContentId = "string",
|
|
Bytecode = "string",
|
|
BrickColor = "int",
|
|
Int32 = "int",
|
|
}
|
|
local function family(t: string): string
|
|
return FAMILY[t] or t
|
|
end
|
|
|
|
-- equal(a, b, pathA, pathB) -> ok, where: values compared deeply (Refs / Content objects by path)
|
|
local function equal(a: any, b: any, pa: { [any]: string }, pb: { [any]: string }): boolean
|
|
if type(a) ~= type(b) then
|
|
return false
|
|
end
|
|
if type(a) == "buffer" then
|
|
return buffer.tostring(a) == buffer.tostring(b)
|
|
elseif type(a) ~= "table" then
|
|
return a == b or (a ~= a and b ~= b)
|
|
end
|
|
if a.className and b.className then
|
|
return pa[a] == pb[b]
|
|
end
|
|
for k, v in a do
|
|
if not equal(v, b[k], pa, pb) then
|
|
return false
|
|
end
|
|
end
|
|
for k in b do
|
|
if a[k] == nil then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
-- sameTree(t, a, b, label, opts?) -> instances compared: class, name, children and every property (opts.loose:
|
|
-- string / int families compare as one type, properties only b has are allowed when another instance of the class has them in a; opts.skipRaw: Raw values are not compared)
|
|
function H.sameTree(t: any, a: any, b: any, label: string, opts: any?): number
|
|
local o = opts or {}
|
|
local pa = select(2, H.paths(a))
|
|
local pb = select(2, H.paths(b))
|
|
local n = 0
|
|
local bad = 0
|
|
local classProps: { [string]: { [string]: boolean } } = {}
|
|
for inst in Rbx.descendants(a) do
|
|
local i = inst :: any
|
|
classProps[i.className] = classProps[i.className] or {}
|
|
for k in i.properties do
|
|
classProps[i.className][k] = true
|
|
end
|
|
end
|
|
local function walk(la: { any }, lb: { any }, where: string)
|
|
if #la ~= #lb then
|
|
t:check(false, string.format("%s: %s has %d children, want %d", label, where, #lb, #la))
|
|
return
|
|
end
|
|
for i, x in la do
|
|
local y = lb[i]
|
|
n += 1
|
|
local w = where .. "/" .. x.name
|
|
if x.className ~= y.className or x.name ~= y.name then
|
|
t:check(false, label .. ": " .. w .. ": " .. y.className .. " " .. y.name)
|
|
else
|
|
for k, v in x.properties do
|
|
local u = y.properties[k]
|
|
local skip = o.skipRaw and v.type == "Raw"
|
|
if not skip then
|
|
local tt = if o.loose then family(v.type) == family(u and u.type or "") else u and u.type == v.type
|
|
if not tt or not equal(v.value, u.value, pa, pb) then
|
|
bad += 1
|
|
if bad < 10 then
|
|
t:check(false, label .. ": " .. w .. "." .. k .. " (" .. v.type .. " / " .. tostring(u and u.type) .. ")")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for k, u in y.properties do
|
|
-- a binary file stores a value for every instance of a class: the writer fills in the ones the XML
|
|
-- twin leaves out (when another instance of the class has the property)
|
|
local filled = o.loose and classProps[y.className] and classProps[y.className][k]
|
|
if x.properties[k] == nil and not (o.skipRaw and u.type == "Raw") and not filled then
|
|
bad += 1
|
|
if bad < 10 then
|
|
t:check(false, label .. ": " .. w .. "." .. k .. " is extra")
|
|
end
|
|
end
|
|
end
|
|
walk(x.children, y.children, w)
|
|
end
|
|
end
|
|
end
|
|
walk(a.roots, b.roots, "")
|
|
t:check(bad == 0, label .. ": " .. bad .. " property differences")
|
|
return n
|
|
end
|
|
|
|
-- everything() -> a model with one value of every type (and several instances per class)
|
|
function H.everything(): any
|
|
local tree = Rbx.tree()
|
|
local root = Rbx.add(tree, Rbx.instance("Model", "Everything"))
|
|
local a = Rbx.add(root, Rbx.instance("Folder", "A"))
|
|
local b = Rbx.add(root, Rbx.instance("Folder", "B"))
|
|
local c = Rbx.add(a, Rbx.instance("Part", "C"))
|
|
local S = Rbx.set
|
|
S(a, "Str", "String", "plain")
|
|
S(a, "Bin", "BinaryString", "\0\1\2\255")
|
|
S(a, "Prot", "ProtectedString", "print('x')")
|
|
S(a, "Cid", "ContentId", "rbxassetid://1")
|
|
S(a, "Flag", "Bool", true)
|
|
S(a, "I32", "Int32", -2147483648)
|
|
S(a, "I64", "Int64", 2 ^ 52 + 1)
|
|
S(a, "F32", "Float32", f32(0.1))
|
|
S(a, "F64", "Float64", 0.1)
|
|
S(a, "En", "Enum", 7)
|
|
S(a, "Brick", "BrickColor", 1004)
|
|
S(a, "Sec", "SecurityCapabilities", 2 ^ 40)
|
|
S(a, "Fc", "Faces", 63)
|
|
S(a, "Ax", "Axes", 7)
|
|
S(a, "Ud", "UDim", { 0.5, -7 })
|
|
S(a, "Ud2", "UDim2", { 0.25, 1, -0.5, 1000 })
|
|
S(a, "Nr", "NumberRange", { -1, 1 })
|
|
S(a, "V2", "Vector2", { 1.5, -2.5 })
|
|
S(a, "V2i", "Vector2int16", { -32768, 32767 })
|
|
S(a, "V3", "Vector3", { 1, 2, 3 })
|
|
S(a, "V3i", "Vector3int16", { -5, 0, 5 })
|
|
S(a, "C3", "Color3", { 0.5, 0.25, 1 })
|
|
S(a, "C3u", "Color3uint8", { 0, 128, 255 })
|
|
S(a, "Rc", "Rect", { 1, 2, 3, 4 })
|
|
S(a, "Ry", "Ray", { 1, 2, 3, 0, 0, -1 })
|
|
S(a, "Cf", "CFrame", { 1, 2, 3, 0, -1, 0, 1, 0, 0, 0, 0, 1 })
|
|
S(a, "Cf2", "CFrame", { 1, 2, 3, f32(0.36), f32(0.48), f32(-0.8), f32(-0.8), f32(0.6), 0, f32(0.48), f32(0.64), f32(0.6) })
|
|
S(a, "Ocf", "OptionalCFrame", { 4, 5, 6, 1, 0, 0, 0, 1, 0, 0, 0, 1 })
|
|
S(b, "Ocf", "OptionalCFrame", nil)
|
|
S(a, "Ns", "NumberSequence", { { 0, 1, 0 }, { 0.5, 2, 0.25 }, { 1, 3, 0 } })
|
|
S(a, "Cs", "ColorSequence", { { 0, 1, 0, 0, 0 }, { 1, 0, 0, 1, 0 } })
|
|
S(a, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 1 })
|
|
S(b, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 0.75 })
|
|
S(c, "Phys", "PhysicalProperties", nil)
|
|
S(a, "Ref", "Ref", c)
|
|
S(b, "Ref", "Ref", nil)
|
|
S(a, "Sh", "SharedString", "shared\0bytes")
|
|
S(b, "Sh", "SharedString", "shared\0bytes")
|
|
S(a, "Uid", "UniqueId", "7fb188dace632b4702e9c68d004815fd")
|
|
S(b, "Uid", "UniqueId", "00000000000000010000000200000003")
|
|
S(a, "Font", "Font", { family = "rbxasset://fonts/families/Arial.json", weight = 700, style = "Italic", cachedFaceId = "rbxasset://fonts/arialbd.ttf" })
|
|
S(a, "Ct", "Content", "rbxassetid://5")
|
|
S(b, "Ct", "Content", c)
|
|
S(c, "Ct", "Content", nil)
|
|
-- the same properties on B (columns of two values), so every type is also written for a second instance
|
|
for k, v in a.properties do
|
|
if b.properties[k] == nil then
|
|
b.properties[k] = v
|
|
end
|
|
end
|
|
return tree
|
|
end
|
|
|
|
return H
|