178 lines
7.2 KiB
Luau
178 lines
7.2 KiB
Luau
-- vector/tiled tests: hand-written TMX maps against Tiled's own TMJ export and TMX re-save.
|
|
-- Fixtures: tools/fixtures/tiled.py.
|
|
local T = require("@tests/T")
|
|
local Tiled = require("@rosetta/vector/tiled")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/tiled")
|
|
local Ours = require("@tests/gen/tiled_ours")
|
|
|
|
local t = T.suite("tiled")
|
|
|
|
local function decode(s: string | buffer): (any, any)
|
|
local b = if type(s) == "string" then buffer.fromstring(s) else s
|
|
return Tiled.decode(Bytes.reader(b))
|
|
end
|
|
|
|
-- ignores the fields that only say who wrote the file
|
|
local SKIP = { version = true, tiledVersion = true, encoding = true, compression = true, meta = true }
|
|
local function same(a: any, b: any, path: string, out: { string })
|
|
if #out > 8 then
|
|
return
|
|
end
|
|
if type(a) == "table" and type(b) == "table" then
|
|
local keys = {}
|
|
for k in pairs(a) do
|
|
keys[k] = true
|
|
end
|
|
for k in pairs(b) do
|
|
keys[k] = true
|
|
end
|
|
for k in pairs(keys) do
|
|
if not SKIP[k] then
|
|
same(a[k], b[k], path .. "." .. tostring(k), out)
|
|
end
|
|
end
|
|
elseif type(a) == "number" and type(b) == "number" then
|
|
if math.abs(a - b) > 1e-9 then
|
|
table.insert(out, path .. ": " .. a .. " vs " .. b)
|
|
end
|
|
elseif a ~= b then
|
|
table.insert(out, path .. ": " .. tostring(a) .. " vs " .. tostring(b))
|
|
end
|
|
end
|
|
local function eqMap(a: any, b: any, label: string)
|
|
local out = {}
|
|
same(a, b, "", out)
|
|
t:check(#out == 0, label .. ": " .. table.concat(out, "; "))
|
|
end
|
|
|
|
t:eq(Tiled.name, "tiled", "name")
|
|
t:eq(Tiled.category, "vector", "category")
|
|
t:check(Tiled.caps.decode and Tiled.caps.encode, "caps")
|
|
|
|
local samples = {}
|
|
local maps = {}
|
|
for _, c in Fx.cases do
|
|
local m, err = decode(c.tmx)
|
|
t:check(m, c.name .. ": tmx: " .. tostring(err))
|
|
t:eq(m.kind, "tilemap", c.name .. ": kind")
|
|
local j, jerr = decode(c.tmj)
|
|
t:check(j, c.name .. ": Tiled's tmj: " .. tostring(jerr))
|
|
local x, xerr = decode(c.tiled_tmx)
|
|
t:check(x, c.name .. ": Tiled's tmx: " .. tostring(xerr))
|
|
eqMap(m, j, c.name .. ": hand TMX = Tiled's TMJ")
|
|
eqMap(m, x, c.name .. ": hand TMX = Tiled's TMX")
|
|
t:eq(Tiled.sniff(Bytes.reader(buffer.fromstring(c.tmx))), 0.9, c.name .. ": sniff tmx")
|
|
t:eq(Tiled.sniff(Bytes.reader(buffer.fromstring(c.tmj))), 0.8, c.name .. ": sniff tmj")
|
|
maps[c.name] = m
|
|
table.insert(samples, c.tmx)
|
|
table.insert(samples, c.tmj)
|
|
end
|
|
|
|
do
|
|
local m = maps.ortho
|
|
t:eq(m.class, "Level", "map class")
|
|
t:eq(m.renderOrder, "left-up", "render order")
|
|
t:eq(m.backgroundColor, "#102030", "background (canonical: opaque alpha dropped)")
|
|
t:eq(m.properties.title, "\u{dc}n\u{ef}code & more", "string property")
|
|
t:eq(m.properties.count, 42, "int property")
|
|
t:eq(m.properties.ratio, 1.5, "float property")
|
|
t:eq(m.properties.on, true, "bool property")
|
|
t:eq(m.propertyTypes.tint, "color", "color type")
|
|
t:eq(m.properties.target, 3, "object property")
|
|
t:eq(m.properties.notes, "line one\nline two", "multi-line property")
|
|
t:eq(#m.tilesets, 2, "tilesets")
|
|
t:eq(m.tilesets[1].imageSource, "tiles.png", "tileset image stays a path")
|
|
t:eq(m.tilesets[1].tiles[2].class, "Door", "tile class")
|
|
t:eq(m.tilesets[2].source, "ext.tsx", "external tileset reference")
|
|
t:eq(m.tilesets[2].firstGid, 9, "firstgid")
|
|
local L = m.layers
|
|
t:eq(L[1].data[4], 0x80000000 + 3, "horizontal flip bit kept")
|
|
t:eq(L[1].opacity, 0.75, "opacity")
|
|
t:eq(L[1].tintColor, "#ff00ff", "tint")
|
|
t:eq(L[2].visible, false, "hidden layer")
|
|
t:eq(L[2].data[8], 1, "zlib data")
|
|
t:eq(L[3].locked, true, "locked")
|
|
t:eq(#L[4].data, 24, "raw base64 data")
|
|
t:eq(L[5].type, "group", "group")
|
|
t:eq(L[5].layers[1].type, "image", "image layer")
|
|
t:eq(L[5].layers[1].imageSource, "sky.png", "image layer path")
|
|
t:eq(L[5].layers[1].repeatX, true, "repeatx")
|
|
local objs = L[5].layers[2].objects
|
|
t:eq(#objs, 7, "objects")
|
|
t:check(objs[1].class == "Crate" and objs[1].rotation == 45 and objs[1].properties.weight == 2.25, "rectangle")
|
|
t:eq(objs[2].shape, "ellipse", "ellipse")
|
|
t:eq(objs[3].shape, "point", "point")
|
|
t:check(objs[4].shape == "polygon" and #objs[4].points == 3 and objs[4].points[3][2] == -12.5, "polygon")
|
|
t:check(objs[5].shape == "polyline" and objs[5].visible == false, "polyline")
|
|
t:check(objs[6].shape == "text" and objs[6].text.text == "Hello <Tiled>" and objs[6].text.bold and objs[6].text.halign == "center", "text")
|
|
t:eq(objs[7].gid, 2147483651, "tile object gid with flip")
|
|
t:eq(maps.hex.hexSideLength, 14, "hex side")
|
|
t:eq(maps.hex.layers[1].data[5], 2, "XML tile data")
|
|
t:eq(#maps.infinite.layers[1].chunks, 3, "chunks")
|
|
t:eq(maps.infinite.layers[1].chunks[2].x, -16, "chunk position")
|
|
end
|
|
|
|
do
|
|
local a = decode(Fx.tsx)
|
|
local b = decode(Fx.tsj)
|
|
t:check(a and a.meta.tileset and b and b.meta.tileset, "tileset files")
|
|
local ta, tb = a.tilesets[1], b.tilesets[1]
|
|
ta.firstGid, tb.firstGid = nil, nil
|
|
eqMap(ta, tb, "TSX = Tiled's TSJ")
|
|
t:eq(ta.tiles[1].animation[2].duration, 150, "animation")
|
|
t:eq(ta.tiles[2].objectGroup.objects[2].shape, "ellipse", "collision shapes")
|
|
t:eq(ta.tileOffset.y, -3, "tile offset")
|
|
for _, fmt in { "tsx", "tsj" } do
|
|
local back = decode(Tiled.encode(a, { format = fmt }) :: buffer)
|
|
back.tilesets[1].firstGid = nil
|
|
eqMap(back.tilesets[1], ta, "tileset round trip " .. fmt)
|
|
end
|
|
end
|
|
|
|
for name, m in maps do
|
|
for _, o in { { format = "tmx" }, { format = "tmx", encoding = "base64" }, { format = "tmx", encoding = "base64", compression = "zlib" }, { format = "tmx", encoding = "base64", compression = "gzip" }, { format = "tmx", encoding = "xml" }, { format = "tmj" }, { format = "tmj", encoding = "base64", compression = "zlib" }, { format = "json", indent = 0 } } do
|
|
local out, err = Tiled.encode(m, o :: any)
|
|
local label = name .. " " .. o.format .. " " .. tostring(o.encoding) .. " " .. tostring(o.compression)
|
|
t:check(out, label .. ": " .. tostring(err))
|
|
local back, berr = decode(out :: buffer)
|
|
t:check(back, label .. ": decode: " .. tostring(berr))
|
|
eqMap(back, m, label .. ": round trip")
|
|
table.insert(samples, buffer.tostring(out :: buffer))
|
|
end
|
|
end
|
|
do
|
|
local m = maps.ortho
|
|
t:eq(m.meta.source.format, "tmx", "source flavour")
|
|
t:eq(string.sub(buffer.tostring(Tiled.encode(m) :: buffer), 1, 5), "<?xml", "default: the source flavour")
|
|
local j = decode(Fx.cases[1].tmj)
|
|
t:eq(string.sub(buffer.tostring(Tiled.encode(j) :: buffer), 1, 1), "{", "a TMJ source writes TMJ")
|
|
end
|
|
|
|
-- Tiled read our TMX and TMJ (checked when the fixtures were made)
|
|
for _, c in Ours.checks do
|
|
t:check(c.ok, c.label)
|
|
end
|
|
|
|
do
|
|
local zstd = string.gsub(Fx.cases[1].tmx, "compression=\"zlib\"", "compression=\"zstd\"")
|
|
local _, e = decode(zstd)
|
|
t:check(e and e.code == Err.UNSUPPORTED, "zstd: " .. tostring(e))
|
|
local _, e2 = decode("<map><layer><data encoding=\"csv\">1,x,2</data></layer></map>")
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad csv: " .. tostring(e2))
|
|
local _, e3 = decode("<notmap/>")
|
|
t:check(e3 and e3.code == Err.CORRUPT, "not a map")
|
|
local _, e4 = decode("{\"type\":\"map\",\"layers\":[{\"type\":\"weird\"}]}")
|
|
t:check(e4 and e4.code == Err.CORRUPT, "unknown layer type")
|
|
local _, e5 = Tiled.encode(maps.ortho, { format = "png" } :: any)
|
|
t:check(e5 and e5.code == Err.ARG, "unknown flavour")
|
|
local _, e6 = Tiled.encode({} :: any)
|
|
t:check(e6 and e6.code == Err.ARG, "not a TileMap")
|
|
end
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Tiled.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 6 })
|
|
t:done()
|