-- codecs/text/KeyValues tests: text against srctools' Keyvalues.parse, trees written by the python vdf module, binary -- KeyValues by vdf.binary_dumps; write round trips, conditions, #include / #base, the entity lump, errors, fuzz. -- Fixtures: tools/fixtures/keyvalues.py. local T = require("@tests/T") local KeyValues = require("@rosetta/codecs/text/KeyValues") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/keyvalues") local t = T.suite("keyvalues") local function canon(b: KeyValues.Block, typed: boolean?): string local out = { "{" } for _, p in b do table.insert(out, #p.key .. ":" .. p.key .. "=") local v = p.value if type(v) == "table" then table.insert(out, canon(v, typed)) elseif typed and p.type then local tag = ({ int32 = "i32", float32 = "f32", pointer = "ptr", color = "col", uint64 = "u64", int64 = "i64", wstring = "wstr" } :: any)[p.type] if p.type == "float32" then table.insert(out, tag .. "f" .. string.format("%.17g", v)) elseif type(v) == "string" and p.type ~= "wstring" then table.insert(out, tag .. "i" .. v) else table.insert(out, tag .. "i" .. string.format("%d", v)) end else table.insert(out, "s" .. #v .. ":" .. v) end end table.insert(out, "}") return table.concat(out) end for i, c in Fx.text do local b, err = KeyValues.parse(c.data) t:check(b ~= nil, "text " .. i .. ": " .. tostring(err)) if b then t:eq(canon(b), c.canon, "text " .. i .. ": same pairs as srctools") local s = KeyValues.write(b) :: string local b2 = KeyValues.parse(s) t:eq(b2 and canon(b2), c.canon, "text " .. i .. ": write round trip") end end for i, c in Fx.written do local b, err = KeyValues.parse(c.data) t:check(b ~= nil, "vdf.dumps " .. i .. ": " .. tostring(err)) t:eq(b and canon(b), c.canon, "vdf.dumps " .. i .. ": pairs") if b then local b2 = KeyValues.parse(KeyValues.write(b) :: string) t:eq(b2 and canon(b2), c.canon, "vdf.dumps " .. i .. ": round trip") end end for i, c in Fx.binary do local b, err = KeyValues.readBinary(c.data) t:check(b ~= nil, "binary " .. i .. ": " .. tostring(err)) if b then t:eq(canon(b, true), c.canon, "binary " .. i .. ": values as vdf.binary_loads") local w = KeyValues.writeBinary(b) :: buffer t:eq(buffer.tostring(w), c.data, "binary " .. i .. ": identical bytes") end t:check(KeyValues.isBinary(buffer.fromstring(c.data), 0, #c.data), "binary " .. i .. ": sniffed") end do -- conditions t:check(KeyValues.condition("$WIN32"), "WIN32 by default") t:check(not KeyValues.condition("$X360"), "X360 off") t:check(KeyValues.condition("!$X360"), "negation") t:check(KeyValues.condition("$X360 || $WIN32"), "or") t:check(not KeyValues.condition("$WIN32 && $OSX"), "and") t:check(KeyValues.condition("$OSX", { OSX = true }), "defines") local b = KeyValues.parse('"r" { "a" "1" [$X360] "a" "2" [$X360||$WIN32] }') :: KeyValues.Block t:eq(KeyValues.get(b, "r/a"), "2", "|| in a condition") local keep = KeyValues.parse('"r" { "a" "1" [$X360] "a" "2" [$WIN32] }', { defines = false }) :: KeyValues.Block local r = KeyValues.find(keep, "r") :: KeyValues.Pair t:eq(#r.value, 2, "defines = false keeps both") t:eq(r.value[1].cond, "$X360", "cond kept") t:check(string.find(KeyValues.write(keep) :: string, '"a"\t"1" [$X360]', 1, true) ~= nil, "cond written") -- #include / #base local files = { ["base.res"] = '"r" { "a" "base" "b" "base" "sub" { "x" "1" "y" "1" } }', ["inc.res"] = '"extra" { "k" "v" }', } local main = '#base "base.res"\n#include "inc.res"\n"r" { "a" "main" "sub" { "x" "2" } }' local m = KeyValues.parse(main, { resolve = function(n: string): string? return files[n] end }) :: KeyValues.Block t:eq(KeyValues.get(m, "r/a"), "main", "#base: main wins") t:eq(KeyValues.get(m, "r/b"), "base", "#base: missing key added") t:eq(KeyValues.get(m, "r/sub/x"), "2", "#base: nested merge keeps main") t:eq(KeyValues.get(m, "r/sub/y"), "1", "#base: nested merge adds") t:eq(KeyValues.get(m, "extra/k"), "v", "#include appended") t:eq(m[#m].key, "extra", "#include after the file's keys") local raw = KeyValues.parse(main) :: KeyValues.Block t:eq(raw[1].key, "#base", "no resolve: directive kept as a pair") local _, e1 = KeyValues.parse(main, { resolve = function() return nil end }) t:eq(e1 and e1.code, Err.IO, "missing include") local _, e2 = KeyValues.parse('#include "self"', { resolve = function() return '#include "self"' end }) t:eq(e2 and e2.code, Err.LIMIT, "include loop") -- objects local o = KeyValues.toObject(KeyValues.parse('"r" { "d" "1" "d" "2" "x" "y" }') :: KeyValues.Block) t:eq(#o.r.d, 2, "duplicates -> array") local last = KeyValues.toObject(KeyValues.parse('"r" { "d" "1" "d" "2" }') :: KeyValues.Block, { duplicates = "last" }) t:eq(last.r.d, "2", "duplicates last") local back = KeyValues.fromObject(o) :: KeyValues.Block t:eq(canon(back), "{1:r={1:d=s1:11:d=s1:21:x=s1:y}}", "fromObject repeats array keys in order") -- escapes off local ne = KeyValues.parse('"p" "C:\\maps\\x"', { escapes = false }) :: KeyValues.Block t:eq(ne[1].value, "C:\\maps\\x", "escapes off") -- entity lump local ents = KeyValues.entities('{\n"classname" "worldspawn"\n"mapversion" "1"\n}\n{\n"classname" "logic_relay"\n"OnTrigger" "a,b,,0,-1"\n"OnTrigger" "c,d,,0,-1"\n}\n\0') :: { KeyValues.Block } t:eq(#ents, 2, "entities") t:eq(#KeyValues.all(ents[2], "ontrigger"), 2, "repeated outputs kept") local again = KeyValues.entities(KeyValues.writeEntities(ents) :: string) :: { KeyValues.Block } t:eq(canon(again[2]), canon(ents[2]), "entity round trip") -- 64-bit binary values local big = KeyValues.writeBinary({ { key = "id", value = "76561197960287930", type = "uint64" }, { key = "n", value = -5, type = "int64" }, { key = "w", value = "\u{e9}\u{1F600}", type = "wstring" } }) :: buffer local rb = KeyValues.readBinary(big) :: KeyValues.Block t:eq(rb[1].value, "76561197960287930", "uint64 above 2^53 as digits") t:eq(rb[2].value, -5, "negative int64") t:eq(rb[3].value, "\u{e9}\u{1F600}", "wide string") -- errors local function code(s: string): string? local _, e = KeyValues.parse(s) return e and e.code end t:eq(code('"r" { "a" "1"'), Err.TRUNCATED, "unclosed block") t:eq(code('"r" "unterminated'), Err.TRUNCATED, "unterminated string") t:eq(code('"r" { } }'), Err.CORRUPT, "extra brace") t:eq(code("{ }"), Err.CORRUPT, "block without a key") t:eq(code(string.rep('"a" {', 300) .. string.rep("}", 300)), Err.LIMIT, "depth limit") local _, be = KeyValues.readBinary(buffer.fromstring("\1key\0no end")) t:eq(be and be.code, Err.TRUNCATED, "binary truncated") local _, we = KeyValues.write({ { key = "k", value = function() end } } :: any) t:eq(we and we.code, Err.ARG, "unwritable value") end local samples = {} for _, c in Fx.text do table.insert(samples, c.data) end t:fuzz("parse", function(b: buffer) return KeyValues.parse(b) end, samples) local bsamples = {} for _, c in Fx.binary do table.insert(bsamples, c.data) end t:fuzz("readBinary", function(b: buffer) return KeyValues.readBinary(b) end, bsamples) t:fuzz("entities", function(b: buffer) return KeyValues.entities(buffer.tostring(b)) end, { '{\n"classname" "worldspawn"\n}\n{ "a" "b" }' }) t:done()