Files
rosetta/tests/data/yaml.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

199 lines
8.2 KiB
Luau

-- data/yaml tests: the yaml-test-suite (every case with an in.json must decode to it, every error case must be
-- rejected, the valid cases without JSON must decode), a random corpus dumped by PyYAML in five styles (block, flow,
-- indent 4 with explicit markers, sorted / narrow, multi-document) compared with Python's values, encoder round trips,
-- options, errors and limits, the API, and the fuzz pass. Fixtures: tools/fixtures/yaml.py.
local T = require("@tests/T")
local F = require("@formats")
local Yaml = require("@formats/data/yaml")
local Json = require("@formats/codecs/text/Json")
local Types = require("@formats/Types")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Canon = require("@tests/data/Canon")
local Suite = require("@tests/gen/yaml_suite")
local Corpus = require("@tests/gen/yaml_corpus")
local t = T.suite("data/yaml")
t:eq(Yaml.name, "yaml", "name")
t:eq(Yaml.category, "data", "category")
t:check(Yaml.caps.decode and Yaml.caps.encode, "caps")
local function dec(s: string, opts: any?): (any, any)
return Yaml.decode(Bytes.reader(buffer.fromstring(s)), opts)
end
local function enc(v: any, opts: any?): string?
local b = Yaml.encode(v, opts)
return if b then buffer.tostring(b) else nil
end
-- 1. yaml-test-suite
local SKIP = { -- !!binary decodes to a buffer; the suite's JSON keeps the base64 text
["565N"] = true,
["name/construct-binary"] = true,
}
local pass, valid, rejected, errors, other, otherOk = 0, 0, 0, 0, 0, 0
for _, c in Suite do
local v, e = dec(c.yaml, { all = true, tags = "ignore" })
t:check(e == nil or not string.find(e.message, "internal error", 1, true), c.name .. ": " .. tostring(e))
if c.error then
errors += 1
t:check(v == nil, c.name .. ": an error case is rejected")
rejected += if v == nil then 1 else 0
elseif c.json and not SKIP[c.name] then
valid += 1
t:check(v ~= nil, c.name .. ": decodes (" .. tostring(e) .. ")")
local want = {}
for _, x in Json.lines(c.json) do
table.insert(want, Canon.of(x))
end
local got = {}
for _, d in v do
local s = Json.encode(d, { nan = "null" })
table.insert(got, if s then Canon.of(Json.decode(s)) else "?")
end
t:eq(table.concat(got, "|"), table.concat(want, "|"), c.name .. ": value")
pass += 1
elseif not c.json then
other += 1
otherOk += if v ~= nil or (e and e.code == Err.UNSUPPORTED) then 1 else 0
end
end
t:note("yaml-test-suite: %d of %d JSON cases match, %d of %d error cases rejected, %d of %d other valid cases decode or say UNSUPPORTED", pass, valid, rejected, errors, otherOk, other)
t:check(otherOk == other, "valid cases without JSON never fail as CORRUPT")
-- 2. PyYAML's output in five styles
for i, c in Corpus do
for style, text in c.texts do
if style == "multi" then
local v, e = dec(text, { all = true })
t:check(v ~= nil, "corpus " .. i .. " " .. style .. ": " .. tostring(e))
t:eq(Canon.of(v), c.multi, "corpus " .. i .. " " .. style)
else
local v, e = dec(text)
t:check(v ~= nil or e == nil, "corpus " .. i .. " " .. style .. ": " .. tostring(e))
t:eq(Canon.of(v), c.canon, "corpus " .. i .. " " .. style)
end
end
-- our own output reads back the same (indent 2 and 4, explicit start)
local v = dec(c.texts.block)
for _, o in { {}, { indent = 4 }, { explicitStart = true } } do
local s = enc(v, o)
t:check(s ~= nil, "corpus " .. i .. ": encodes")
t:eq(Canon.of(dec(s :: string)), c.canon, "corpus " .. i .. ": round trip " .. tostring(s))
end
local m = dec(c.texts.multi, { all = true })
t:eq(Canon.of(dec(enc(m, { all = true }) :: string, { all = true })), c.multi, "corpus " .. i .. ": multi round trip")
end
-- 3. details
do
local v = dec("b: 1\na: 2\nc: {}\nd: []\ne:\nf: ~\n")
t:eq(table.concat(Types.keys(v), ","), "b,a,c,d,e,f", "key order")
t:check(Types.isObject(v.c), "empty flow map is an object")
t:eq(v.e, Types.null, "empty value is null")
t:eq(enc(v), "b: 1\na: 2\nc: {}\nd: []\ne: null\nf: null\n", "encode keeps the order")
local s = dec("a: &x [1, 2]\nb: *x\n")
t:check(s.a == s.b, "aliases are shared references")
t:eq(enc(s), "a: &id001\n - 1\n - 2\nb: *id001\n", "shared tables get anchors")
local n = dec("[0o17, 0x1F, +12, -0.5e1, .5, .inf, -.Inf, .nan, 1_000, 0b11, yes, ~, Null, TRUE]")
t:eq(n[1], 15, "0o")
t:eq(n[2], 31, "0x")
t:eq(n[3], 12, "+int")
t:eq(n[4], -5, "float exp")
t:eq(n[5], 0.5, ".5")
t:eq(n[6], math.huge, ".inf")
t:eq(n[7], -math.huge, "-.Inf")
t:check(n[8] ~= n[8], ".nan")
t:eq(n[9], "1_000", "1.1 ints are strings")
t:eq(n[10], "0b11", "0b is a string")
t:eq(n[11], "yes", "yes is a string")
t:eq(n[12], Types.null, "~")
t:eq(n[13], Types.null, "Null")
t:eq(n[14], true, "TRUE")
local tg = dec("a: !!str 12\nb: !!float 3\nc: !!binary aGVsbG8=\nd: !!timestamp 2001-12-14T21:59:43Z\ne: !!int '7'\n")
t:eq(tg.a, "12", "!!str")
t:eq(tg.b, 3, "!!float")
t:eq(buffer.tostring(tg.c), "hello", "!!binary")
t:check(Types.Date.is(tg.d), "!!timestamp")
t:eq(tg.e, 7, "!!int on a quoted scalar")
local _, te = dec("a: !custom x")
t:eq(te and te.code, Err.UNSUPPORTED, "unknown tags are UNSUPPORTED")
t:eq(dec("a: !custom x", { tags = "ignore" }).a, "x", "tags = ignore")
t:eq(dec("--- 1\n--- 2\n"), 1, "the first document")
t:eq(#dec("--- 1\n--- 2\n", { all = true }), 2, "all documents")
t:eq(dec(""), Types.null, "empty stream")
t:eq(dec("k: |\n a\n b\n\nz: >+\n f\n g\n\n").k, "a\nb\n", "literal")
t:eq(dec("k: |\n a\n b\n\nz: >+\n f\n g\n\n").z, "f g\n\n", "folded keep")
t:eq(dec("? a\n: b\n").a, "b", "explicit key")
t:eq(dec("[a: 1, {b: 2}]")[1].a, 1, "single-pair flow mapping")
-- strings that need quoting survive
for _, str in { "", "true", "null", "12", "1.5", "- x", "a: b", "# c", "x #y", " lead", "tail ", "multi\nline", "two\n\n", "\ttab", "é", "\0\1", "'", '"', "yes", "on", "~", "[", "@", "&a", "*a", "!t", "%", "---", "...", "a\u{2028}b", ".inf", "0x10", "0o7", "-", "?", ":" } do
local v2 = dec(enc({ k = str }) :: string)
t:eq(v2 and v2.k, str, "string round trip " .. string.format("%q", str))
end
for _, x in { 0.1, -2.5e-8, 1e300, 2 ^ 53, -0.0, 123456789, math.huge, -math.huge } do
t:eq(dec(enc({ x }) :: string)[1], x, "number round trip " .. tostring(x))
end
local cyc = {}
cyc.self = cyc
local _, ce = Yaml.encode(cyc)
t:eq(ce and ce.code, Err.ARG, "cycles are an error")
local _, fe = Yaml.encode({ f = print })
t:eq(fe and fe.code, Err.ARG, "functions are an error")
t:eq(enc(buffer.fromstring("hi")), "!!binary aGk=\n", "buffers as !!binary")
end
-- 4. errors and limits
local function code(s: string, opts: any?): string?
local _, e = dec(s, opts)
return e and e.code
end
t:eq(code("a: [1, 2"), Err.TRUNCATED, "unterminated flow")
t:eq(code('a: "x'), Err.TRUNCATED, "unterminated quote")
t:eq(code("a: b: c"), Err.CORRUPT, "mapping on its key's line")
t:eq(code("a: *nope"), Err.CORRUPT, "undefined alias")
t:eq(code("a:\n b: 1\n c: 2"), Err.CORRUPT, "bad indentation")
t:eq(code("[[1]]: x"), Err.UNSUPPORTED, "collection keys")
t:eq(code(string.rep("[", 600) .. string.rep("]", 600)), Err.LIMIT, "depth limit")
do
local bomb = { "a: &a [x, x, x, x, x, x, x, x, x, x]" }
for i = 2, 8 do
local prev = string.char(96 + i - 1)
table.insert(bomb, string.format("%s: &%s [*%s, *%s, *%s, *%s, *%s, *%s, *%s, *%s, *%s, *%s]", string.char(96 + i), string.char(96 + i), prev, prev, prev, prev, prev, prev, prev, prev, prev, prev))
end
t:eq(code(table.concat(bomb, "\n")), Err.LIMIT, "alias bomb")
t:check(dec(table.concat(bomb, "\n", 1, 3)) ~= nil, "small alias trees are fine")
end
do
local _, e = dec("a: 1\nb: [1, }\n")
t:eq(e and e.offset, 12, "error offset")
end
-- 5. API
do
local v, err = F.load(buffer.fromstring("---\nname: x\nlist:\n - 1\n"))
t:check(v ~= nil, "F.load sniffs yaml: " .. tostring(err))
t:eq(v.list[1], 1, "F.load value")
t:eq(F.saveString(v, { format = "yaml" }), "name: x\nlist:\n - 1\n", "F.saveString yaml")
end
local samples = {}
for i = 1, 30 do
table.insert(samples, Corpus[i].texts.block)
table.insert(samples, Corpus[i].texts.flow)
end
for i = 1, 400, 7 do
if Suite[i] then
table.insert(samples, Suite[i].yaml)
end
end
t:fuzz("decode", function(b: buffer)
return Yaml.decode(Bytes.reader(b), { all = true })
end, samples)
t:fuzz("encode(decode)", function(b: buffer)
local v = Yaml.decode(Bytes.reader(b))
return v and Yaml.encode(v)
end, samples, { garbage = 20 })
t:done()