133 lines
5.8 KiB
Luau
133 lines
5.8 KiB
Luau
-- API tests (src/Formats/init.luau): load / save / saveString / convert / open / detect / formats / format, the
|
|
-- category facades, sniff-then-extension detection, errors as Err values (never a throw), and file paths on hosts with
|
|
-- a file system (LuaJIT: a temporary file; the luau CLI has none).
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Err = require("@formats/core/Err")
|
|
|
|
local t = T.suite("api")
|
|
|
|
local function image(w: number, h: number): any
|
|
local img = F.Types.Image.new(w, h)
|
|
for i = 0, w * h * 4 - 1 do
|
|
buffer.writeu8(img.pixels, i, (i * 37) % 256)
|
|
end
|
|
return img
|
|
end
|
|
|
|
-- host
|
|
t:eq(F.host.name, if T.runtime == "luajit" then "luajit" else "luau", "host detected")
|
|
|
|
-- formats()
|
|
do
|
|
local all = F.formats()
|
|
t:check(#all >= 1, "formats() lists the registry")
|
|
local png
|
|
for _, f in all do
|
|
if f.name == "png" then
|
|
png = f
|
|
end
|
|
end
|
|
t:check(png and png.category == "image" and png.caps.encode and table.find(png.extensions, "png"), "png in formats()")
|
|
t:eq(#F.image.formats(), #F.formats("image"), "facade formats()")
|
|
t:eq(#F.formats("nonexistent"), 0, "unknown category")
|
|
t:check(F.format("png") and F.format("png").name == "png", "format(name)")
|
|
t:check(F.format("apng") and F.format("apng").name == "png", "format(extension)")
|
|
local _, e = F.format("nope")
|
|
t:check(e and e.code == Err.UNKNOWN_FORMAT, "format(unknown)")
|
|
end
|
|
|
|
-- save -> load round trip through buffers and strings
|
|
do
|
|
local img = image(7, 5)
|
|
local buf, err = F.save(img, nil, { format = "png" })
|
|
t:check(buf and type(buf) == "buffer", "save to a buffer: " .. tostring(err))
|
|
local back = F.load(buf)
|
|
t:check(back and back.kind == "image" and back.width == 7 and back.height == 5, "load sniffs png")
|
|
t:eq(buffer.tostring(back.pixels), buffer.tostring(img.pixels), "pixels round trip")
|
|
t:eq(back.meta.source.format, "png", "meta.source.format")
|
|
-- save without a format: the source format
|
|
local again = F.save(back)
|
|
t:check(again and type(again) == "buffer", "save uses the source format")
|
|
local s = F.saveString(back)
|
|
t:check(type(s) == "string" and string.sub(s, 2, 4) == "PNG", "saveString")
|
|
local fromString = F.load(s)
|
|
t:check(fromString and fromString.width == 7, "load a string of bytes")
|
|
t:check(F.image.load(buf) ~= nil, "F.image.load")
|
|
t:eq(F.detect(buf), "png", "detect")
|
|
local conv = F.convert(s, "png", { level = 1 })
|
|
t:check(conv and F.load(conv).width == 7, "convert")
|
|
-- format options reach the encoder
|
|
local stored = F.save(img, nil, { format = "png", level = 0 }) :: buffer
|
|
t:check(buffer.len(stored) > buffer.len(buf :: buffer), "opts.level reaches the encoder")
|
|
end
|
|
|
|
-- detection: sniff beats the extension; the extension is the fallback; category restrictions
|
|
do
|
|
local png = F.save(image(2, 2), nil, { format = "png" }) :: buffer
|
|
t:eq(F.detect(png, { name = "photo.jpg" }), "png", "content beats a wrong extension")
|
|
local _, e = F.load("not an image at all")
|
|
t:check(e and e.code == Err.UNKNOWN_FORMAT, "unknown data: " .. tostring(e))
|
|
local _, e2 = F.load(png, { category = "audio" })
|
|
t:check(e2 and e2.code == Err.UNKNOWN_FORMAT, "png is not audio")
|
|
local _, e3 = F.audio.load(png)
|
|
t:check(e3 and e3.code == Err.UNKNOWN_FORMAT, "facade restricts the category")
|
|
-- forced format on bad data: the format's error, tagged with its name
|
|
local _, e4 = F.load("garbage!", { format = "png" })
|
|
t:check(e4 and e4.format == "png" and e4.code == Err.CORRUPT, "forced format error: " .. tostring(e4))
|
|
t:check(string.find(tostring(e4), "^png: ") ~= nil, "error message names the format")
|
|
local _, e5 = F.load("x", { format = "nope" })
|
|
t:check(e5 and e5.code == Err.UNKNOWN_FORMAT, "unknown forced format")
|
|
end
|
|
|
|
-- errors are values, never throws
|
|
do
|
|
local ok, v, e = pcall(F.load, 42 :: any)
|
|
t:check(ok and v == nil and e.code == Err.ARG, "load(42)")
|
|
local ok2, v2, e2 = pcall(F.save, image(1, 1))
|
|
t:check(ok2 and v2 == nil and e2.code == Err.ARG, "save without a format")
|
|
local ok3, _, e3 = pcall(F.save, { kind = "image" } :: any, nil, { format = "png" })
|
|
t:check(ok3 and e3 and e3.code == Err.ARG, "save of a broken image")
|
|
local ok4, _, e4 = pcall(F.save, image(1, 1), nil, { format = "nope" })
|
|
t:check(ok4 and e4.code == Err.UNKNOWN_FORMAT, "save to an unknown format")
|
|
local _, e5 = F.open(F.save(image(1, 1), nil, { format = "png" }))
|
|
t:check(e5 and e5.code == Err.UNSUPPORTED, "open() of a non-streaming format")
|
|
t:fuzz("F.load", function(b: buffer)
|
|
return F.load(b)
|
|
end, { F.saveString(image(9, 4), { format = "png" }) :: string }, { mutations = 20 })
|
|
end
|
|
|
|
-- files (hosts with a file system)
|
|
if F.host.hasFiles then
|
|
local path = os.tmpname() .. ".png"
|
|
local img = image(3, 3)
|
|
local ok, err = F.save(img, path)
|
|
t:check(ok == true, "save to a path (format from the extension): " .. tostring(err))
|
|
local back = F.load(path)
|
|
t:check(back and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "load from a path")
|
|
t:check(F.image.load(path) ~= nil, "facade load from a path")
|
|
local _, e = F.load("/nonexistent/dir/x.png", { path = true })
|
|
t:check(e and e.code == Err.IO, "missing file is Err.IO")
|
|
os.remove(path)
|
|
else
|
|
local _, e = F.save(image(1, 1), "out.png")
|
|
t:check(e and e.code == Err.IO, "no file system: save to a path is Err.IO")
|
|
local _, e2 = F.load("x.png", { path = true })
|
|
t:check(e2 and e2.code == Err.IO, "no file system: load of a path is Err.IO")
|
|
end
|
|
|
|
-- Types constructors
|
|
do
|
|
local T2 = F.Types
|
|
t:eq(buffer.len(T2.Image.new(3, 2, "rgb8").pixels), 18, "Image.new rgb8")
|
|
t:eq(buffer.len(T2.Image.new(3, 3, "yuv420p").pixels), 9 + 2 * 4, "Image.new yuv420p")
|
|
local a = T2.Audio.new(8000, 2, 100)
|
|
t:check(a.kind == "audio" and buffer.len(a.samples) == 800, "Audio.new")
|
|
local sc = T2.Scene.fromPrimitive({ attributes = {}, mode = "triangles" })
|
|
t:check(sc.kind == "scene" and #sc.nodes == 1 and sc.nodes[1].mesh == 1, "Scene.fromPrimitive")
|
|
t:eq(tostring(F.null), "null", "null sentinel")
|
|
t:check(F.data.null == F.null, "F.data.null")
|
|
end
|
|
|
|
t:done()
|