Files
rosetta/tests/data/toml.luau
T

115 lines
4.8 KiB
Luau

-- data/toml tests: the toml-test suite (TOML 1.0) and a tomli-w corpus against Python's tomllib.
-- Fixtures: tools/fixtures/toml.py.
local T = require("@tests/T")
local Toml = require("@rosetta/data/toml")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Canon = require("@tests/data/Canon")
local Suite = require("@tests/gen/toml_suite")
local Corpus = require("@tests/gen/toml_corpus")
local t = T.suite("data/toml")
t:eq(Toml.name, "toml", "name")
t:eq(Toml.category, "data", "category")
t:check(Toml.caps.decode and Toml.caps.encode, "caps")
local function dec(s: string, opts: any?): (any, any)
return Toml.decode(Bytes.reader(buffer.fromstring(s)), opts)
end
local function enc(v: any, opts: any?): (string?, any)
local b, e = Toml.encode(v, opts)
return if b then buffer.tostring(b) else nil, e
end
local nv, ni, rt = 0, 0, 0
for _, c in Suite do
local v, err = dec(c.data)
if c.valid then
t:check(v ~= nil, c.name .. ": parses (" .. tostring(err) .. ")")
t:eq(Canon.of(v), c.canon, c.name .. ": value")
-- integral floats read back as integers, so compare decoded values, not the toml-test canon
local s, eerr = enc(v)
t:check(s ~= nil, c.name .. ": encodes (" .. tostring(eerr) .. ")")
local back, berr = dec(s :: string)
t:check(back ~= nil, c.name .. ": re-decodes (" .. tostring(berr) .. ")\n" .. tostring(s))
t:eq(Canon.of(back), Canon.of(v), c.name .. ": round trip")
nv += 1
rt += 1
else
t:check(v == nil and Err.is(err), c.name .. ": rejected")
ni += 1
end
end
t:note("toml-test 1.0: %d valid decoded and round-tripped, %d invalid rejected", nv, ni)
for i, c in Corpus do
local v, err = dec(c.data)
t:check(v ~= nil, "corpus " .. i .. ": " .. tostring(err))
t:eq(Canon.of(v), c.canon, "corpus " .. i .. ": value")
local s = enc(v) :: string
t:eq(Canon.of(dec(s)), c.canon, "corpus " .. i .. ": round trip")
t:eq(Canon.of(dec(enc(v, { inline = true }) :: string)), c.canon, "corpus " .. i .. ": inline round trip")
end
do
local v = dec('z = 1\na = 2\n[t]\ny = "x"\nb = 1979-05-27T07:32:00.5-07:00\n[[arr]]\nk = 1\n[[arr]]\nk = 2\n')
t:eq(table.concat(Types.keys(v), ","), "z,a,t,arr", "key order")
t:check(Types.Date.is(v.t.b) and v.t.b.kind == "datetime" and v.t.b.offset == -420, "datetime value")
t:eq(tostring(v.t.b), "1979-05-27T07:32:00.5-07:00", "datetime text")
t:eq(v.arr[2].k, 2, "array of tables")
t:eq(enc(v), 'z = 1\na = 2\n\n[t]\ny = "x"\nb = 1979-05-27T07:32:00.5-07:00\n\n[[arr]]\nk = 1\n\n[[arr]]\nk = 2\n', "encode text")
t:eq(enc({ b = { 1, 2.5, "s" }, a = { x = true } }), 'b = [1, 2.5, "s"]\n\n[a]\nx = true\n', "plain tables: scalars first, sorted")
t:eq(enc({ ["a b"] = "q\"\n\1" }), '"a b" = "q\\"\\n\\u0001"\n', "key / string escapes")
t:eq(enc({ a = { x = 1 } }, { inline = true }), "a = { x = 1 }\n", "inline option")
t:eq(enc({ f = 0 / 0, g = math.huge, h = 1e300, i = -0.5 }), "f = nan\ng = inf\nh = 1e+300\ni = -0.5\n", "floats")
t:eq(enc({ a = { b = { c = 1 } } }, { indent = 2 }), "[a]\n\n [a.b]\n c = 1\n", "indent")
local big = dec("a = 9223372036854775807\nb = 0x7fffffffffffffff\nc = 12", { bigInts = "string" })
t:eq(big.a, "9223372036854775807", "bigInts string")
t:eq(big.b, "0x7fffffffffffffff", "bigInts string hex")
t:eq(big.c, 12, "small ints stay numbers")
t:eq(dec("a = 9223372036854775807").a, 9223372036854775807, "bigInts number")
local _, e1 = enc({ a = Types.null })
t:eq(e1 and e1.code, Err.ARG, "null is an error")
local cyc = {}
cyc.self = cyc
local _, e2 = enc(cyc)
t:eq(e2 and e2.code, Err.ARG, "cycle is an error")
local _, e3 = enc({ 1, 2 })
t:eq(e3 and e3.code, Err.ARG, "top level must be a table")
local _, e4 = enc({ [1] = 1, x = 2 })
t:eq(e4 and e4.code, Err.ARG, "non-string key")
end
local function code(s: string): (string?, number?)
local _, e = dec(s)
return e and e.code, e and e.offset
end
t:eq(code('a = "abc'), Err.TRUNCATED, "unterminated string")
t:eq(code("a = 1\na = 2"), Err.CORRUPT, "duplicate key")
t:eq(code("[a]\n[a]"), Err.CORRUPT, "duplicate table")
t:eq(code("a = 01"), Err.CORRUPT, "leading zero")
t:eq(code("a = 9223372036854775808"), Err.CORRUPT, "int64 overflow")
t:eq(select(2, code("x = 1\na = tru")), 10, "error offset")
t:eq(Toml.sniff(Bytes.reader(buffer.fromstring("# c\n[server]\nport = 80\n"))), 0.2, "sniff")
t:eq(Toml.sniff(Bytes.reader(buffer.fromstring("{\"a\": 1}"))), 0, "sniff json")
local samples = {}
for _, c in Suite do
if c.valid and #samples < 80 and #c.data < 400 then
table.insert(samples, c.data)
end
end
for i = 1, 20 do
table.insert(samples, Corpus[i].data)
end
t:fuzz("decode", function(b: buffer)
return Toml.decode(Bytes.reader(b))
end, samples)
t:fuzz("encode(decode)", function(b: buffer)
local v = Toml.decode(Bytes.reader(b))
return v and Toml.encode(v)
end, samples, { garbage = 20 })
t:done()