-- data/tres tests: Godot text resources against Godot 4's loader (every stored property, var_to_str text parsed back -- by codecs/text/GodotVariant), int / float keeping, our re-encoded files loaded by Godot with the same properties; -- errors; fuzz. Fixtures: tools/fixtures/godot.py. local T = require("@tests/T") local F = require("@rosetta") local Tres = require("@rosetta/data/tres") local GodotVariant = require("@rosetta/codecs/text/GodotVariant") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/godot") local Ours = require("@tests/gen/godot_ours") local t = T.suite("tres") local samples = {} t:eq(Tres.name, "tres", "name") t:eq(Tres.category, "data", "category") t:check(Tres.caps.decode and Tres.caps.encode, "caps") local function same(a: any, b: any): boolean if type(a) == "number" and type(b) == "number" then return a == b or math.abs(a - b) <= 1e-6 * math.max(1, math.abs(b)) or (a ~= a and b ~= b) elseif type(a) == "table" and type(b) == "table" then if GodotVariant.isCtor(a) ~= GodotVariant.isCtor(b) or (GodotVariant.isCtor(a) and a.type ~= b.type) then return false end if GodotVariant.isCtor(a) and a.type == "Object" then return a.class == b.class -- Godot lists every stored property of the object end local x, y = if GodotVariant.isCtor(a) then a.args else a, if GodotVariant.isCtor(b) then b.args else b for k, v in x do if not same(v, y[k]) then return false end end for k in y do if x[k] == nil then return false end end return true end return a == b end for _, c in Fx.tres do local v, err = Tres.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(v, c.name .. ": decode: " .. tostring(err)) t:eq(Tres.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff") table.insert(samples, c.data) local g = c.godot t:check(g.ok, c.name .. ": Godot loads it") if v and g.ok then local n = 0 for k, text in g.props do local mine = v.properties[k] if mine ~= nil and not (GodotVariant.isCtor(mine) and (mine.type == "SubResource" or mine.type == "ExtResource")) then local theirs, perr = GodotVariant.value(text) t:check(perr == nil and same(mine, theirs), c.name .. ": " .. k .. " = Godot's " .. text) n += 1 end end t:note("%s: %d properties against Godot", c.name, n) local again = assert(Tres.decode(Bytes.reader(assert(Tres.encode(v))))) t:eq(GodotVariant.format(again.properties), GodotVariant.format(v.properties), c.name .. ": round trip") end local ours for _, o in Ours do if o.name == "ours_" .. c.name then ours = o end end t:check(ours and ours.godot.ok, c.name .. ": Godot loads our .tres") if ours and ours.godot.ok and g.ok then for k, text in g.props do t:eq(ours.godot.props[k], text, c.name .. ": our .tres in Godot: " .. k) end end end local data = assert(Tres.decode(Bytes.reader(buffer.fromstring(Fx.tres[2].data)))) t:check(data.type == "Resource" and data.scriptClass == "Stats" and data.properties.hp == 12, "tres header and values") t:eq(data.subresources[1].properties.point_count, 2, "sub-resource properties") t:eq(data.external[1].path, "res://stats.gd", "external resources") local _, e = Tres.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].data))) t:check(e and e.code == Err.UNSUPPORTED, "a scene is not a resource") t:eq(F.detect(Fx.tres[1].data), "tres", "F.detect tres") do local v = assert(Tres.decode(Bytes.reader(buffer.fromstring("[gd_resource type=\"Curve\" format=3]\n[resource]\n_data = [Vector2(0, 0), 0.0, 1.0, 0, 0]\nx = 2.0\n")))) local s = buffer.tostring(assert(Tres.encode(v))) t:check(string.find(s, "0.0, 1.0, 0, 0", 1, true) ~= nil and string.find(s, "x = 2.0", 1, true) ~= nil, "integral floats stay floats") local _, e = Tres.decode(Bytes.reader(buffer.fromstring("[gd_resource type=\"X\" format=3]\n[resource]\nx = [1, 2\n"))) t:check(e ~= nil, "truncated array") local _, e2 = Tres.encode({} :: any) t:check(e2 and e2.code == Err.ARG, "encode arg") end t:fuzz("decode", function(b: buffer) return Tres.decode(Bytes.reader(b)) end, samples, { mutations = 80, garbage = 20 }) t:done()