-- data/vdf tests: KeyValues text written by the python vdf module decodes to what vdf.loads gives, encodes back; -- binary KeyValues by sniffing; pair lists; sniff; fuzz. Fixtures: tools/fixtures/keyvalues.py. local T = require("@tests/T") local Vdf = require("@rosetta/data/vdf") 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/keyvalues") local t = T.suite("data/vdf") t:eq(Vdf.name, "vdf", "name") t:check(Vdf.caps.decode and Vdf.caps.encode, "caps") local function dec(s: string, opts: any?): (any, any) return Vdf.decode(Bytes.reader(buffer.fromstring(s)), opts) end for i, c in Fx.objects do local v, err = dec(c.data) t:check(v ~= nil, "vdf.dumps " .. i .. ": " .. tostring(err)) t:eq(Canon.of(v), c.canon, "vdf.dumps " .. i .. ": as vdf.loads") local back = dec(buffer.tostring(Vdf.encode(v) :: buffer)) t:eq(Canon.of(back), c.canon, "vdf.dumps " .. i .. ": encode round trip") t:check(Vdf.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, "vdf.dumps " .. i .. ": sniff") end for i, c in Fx.binary do local v, err = dec(c.data) t:check(type(v) == "table" and v.shortcuts ~= nil, "binary " .. i .. ": " .. tostring(err)) local p = dec(c.data, { pairs = true }) t:eq(buffer.tostring(Vdf.encode(p, { binary = true }) :: buffer), c.data, "binary " .. i .. ": pairs round trip") end do local v = dec('"AppState"\n{\n\t"appid"\t"440"\n\t"InstalledDepots"\n\t{\n\t\t"441" { "manifest" "1" }\n\t}\n\t"x" "1"\n\t"x" "2"\n}\n') t:eq(v.AppState.appid, "440", "acf value") t:eq(v.AppState.InstalledDepots["441"].manifest, "1", "nested") t:eq(#v.AppState.x, 2, "duplicates as array") t:eq(table.concat(Types.keys(v.AppState), ","), "appid,InstalledDepots,x", "key order") local s = buffer.tostring(Vdf.encode(v) :: buffer) t:check(string.find(s, '\t"x"\t"1"\n\t"x"\t"2"\n', 1, true) ~= nil, "array written as repeated keys") local o = Types.object() Types.set(o, "b", true) Types.set(o, "n", 3) t:eq(buffer.tostring(Vdf.encode({ r = o }) :: buffer), '"r"\n{\n\t"b"\t"1"\n\t"n"\t"3"\n}\n', "encode typed values") local _, e = dec('"r" {') t:eq(e and e.code, Err.TRUNCATED, "truncated") local _, ee = Vdf.encode({ r = { function() end } }) t:eq(ee and ee.code, Err.ARG, "unwritable") t:eq(Vdf.sniff(Bytes.reader(buffer.fromstring("{}"))), 0, "json is not vdf") end local samples = {} for i = 1, 10 do table.insert(samples, Fx.objects[i].data) table.insert(samples, Fx.binary[i].data) end t:fuzz("decode", function(b: buffer) return Vdf.decode(Bytes.reader(b)) end, samples) t:done()