-- data/ini tests: configs written by Python's configparser read back to configparser's values and re-encoded byte for -- byte, messy hand-written files read as configparser(strict = false) reads them, options (inline comments, keys -- without values, types, strict), top-level keys, error codes, and the fuzz pass. Fixtures: tools/fixtures/ini.py. local T = require("@tests/T") local Ini = require("@rosetta/data/ini") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Canon = require("@tests/data/Canon") local Fx = require("@tests/gen/ini") local t = T.suite("data/ini") t:eq(Ini.name, "ini", "name") t:check(Ini.caps.decode and Ini.caps.encode, "caps") local function dec(s: string, opts: any?): (any, any) return Ini.decode(Bytes.reader(buffer.fromstring(s)), opts) end for i, c in Fx.written do local v, err = dec(c.data) t:check(v ~= nil, "written " .. i .. ": " .. tostring(err)) t:eq(Canon.of(v), c.canon, "written " .. i .. ": values") t:eq(buffer.tostring(Ini.encode(v) :: buffer), c.data, "written " .. i .. ": same text") end for i, c in Fx.read do local v, err = dec(c.data) t:check(v ~= nil, "messy " .. i .. ": " .. tostring(err)) t:eq(Canon.of(v), c.canon, "messy " .. i .. ": values") end do local v = dec("top = 1\n[s]\na = 2 ; c\nflag\nn = 0x10\nb = Yes\n", { inlineComments = true, allowNoValue = true, types = true }) t:eq(v.top, 1, "top-level key, typed") t:eq(v.s.a, 2, "inline comment stripped") t:eq(v.s.flag, true, "key without value") t:eq(v.s.n, 16, "hex number") t:eq(v.s.b, true, "yes -> true") t:eq(table.concat(Types.keys(v), ","), "top,s", "order") local o = Types.object() Types.set(o, "name", "x") local sub = Types.object() Types.set(sub, "k", "multi\nline") Types.set(sub, "inner", { z = 1 }) Types.set(o, "sec", sub) t:eq(buffer.tostring(Ini.encode(o) :: buffer), "name = x\n\n[sec]\nk = multi\n\tline\n\n[sec.inner]\nz = 1\n\n", "encode nested") local function code(s: string, opts: any?): string? local _, e = dec(s, opts) return e and e.code end t:eq(code("[a]\njunk line\n"), Err.CORRUPT, "line without delimiter") t:eq(code("[a]\nk=1\nk=2\n", { strict = true }), Err.CORRUPT, "strict duplicate key") t:eq(code("[a]\n[a]\n", { strict = true }), Err.CORRUPT, "strict duplicate section") t:eq(code("[a]\n= v\n"), Err.CORRUPT, "empty key") local _, ee = Ini.encode({ ["bad=key"] = "v" }) t:eq(ee and ee.code, Err.ARG, "unwritable key") t:eq(Ini.sniff(Bytes.reader(buffer.fromstring("; c\n[section]\nk=v"))), 0.2, "sniff") end local samples = {} for _, c in Fx.read do table.insert(samples, c.data) end for i = 1, 20 do table.insert(samples, Fx.written[i].data) end t:fuzz("decode", function(b: buffer) return Ini.decode(Bytes.reader(b), { types = true, inlineComments = true }) end, samples) t:done()