-- 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()