ws-voxel: codecs/text/Nbt (NBT Java / Bedrock / network varint, SNBT parse + print, typed values), data/nbt, data/snbt
Shared change: codecs/compress/Deflate gains Deflate.peek(buf, off, len, n) (the first n inflated bytes of a gzip / zlib / raw stream, for sniffing gzip-wrapped NBT) through a backward-compatible prefix flag on Inflate.raw; gunzip's header parsing moved into a local helper both use. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d3dbba500b
commit
512cbc3dc8
15 files changed
+2745
-26
No files matched your search
@@ -0,0 +1,60 @@
|
||||
-- data/nbt tests: the contract, nbtlib files through the api (F.load / F.save, NBT -> SNBT -> NBT), detection by
|
||||
-- content and extension, exact round trips (root name, edition, compression and the Bedrock header kept), error
|
||||
-- codes, and the fuzz pass. The engine itself: tests/codecs/nbt.luau; SNBT files: tests/data/snbt.luau.
|
||||
-- Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local NbtF = require("@formats/data/nbt")
|
||||
local SnbtF = require("@formats/data/snbt")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("data/nbt")
|
||||
|
||||
t:eq(NbtF.name, "nbt", "name")
|
||||
t:eq(SnbtF.name, "snbt", "snbt name")
|
||||
t:check(NbtF.caps.decode and NbtF.caps.encode and SnbtF.caps.decode and SnbtF.caps.encode, "caps")
|
||||
|
||||
for i = 1, 12 do
|
||||
local c = Fx.cases[i]
|
||||
local v = F.load(c.gzip, { name = "level.dat" })
|
||||
t:check(v ~= nil, c.name .. ": F.load gzip")
|
||||
local text = assert(F.saveString(v, { format = "snbt" }))
|
||||
local back = assert(F.load(text, { format = "snbt" }))
|
||||
t:eq(Nbt.print(back), Nbt.print(v), c.name .. ": NBT -> SNBT -> value")
|
||||
local again = assert(F.save(v, nil, { format = "nbt" })) :: buffer
|
||||
t:eq(buffer.readu8(again, 0), 0x1f, c.name .. ": saved gzip like the source")
|
||||
t:eq(Nbt.print(F.load(again, { format = "nbt" })), Nbt.print(v), c.name .. ": saved file reads back")
|
||||
t:eq(Nbt.info(F.load(again, { format = "nbt" })).name, c.root, c.name .. ": root name kept")
|
||||
local raw = assert(F.save(v, nil, { format = "nbt", compression = "none" })) :: buffer
|
||||
t:eq(buffer.tostring(raw), c.raw, c.name .. ": uncompressed = nbtlib's bytes")
|
||||
local le = assert(F.load(c.header, { format = "nbt" }))
|
||||
t:eq(buffer.tostring(assert(F.save(le, nil, { format = "nbt" })) :: buffer), c.header, c.name .. ": Bedrock level.dat same bytes")
|
||||
end
|
||||
|
||||
-- detection
|
||||
do
|
||||
local c = Fx.cases[3]
|
||||
t:eq(F.detect(c.gzip, { name = "level.dat" }), "nbt", "detect gzip .dat")
|
||||
t:eq(F.detect(c.raw, { name = "x.nbt" }), "nbt", "detect raw .nbt")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = NbtF.decode(Bytes.reader(buffer.fromstring("\10\0\0\1")))
|
||||
t:check(e and e.code == Err.TRUNCATED, "truncated")
|
||||
local _, e3 = NbtF.encode(nil :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "encode nil")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(samples, Fx.cases[i].raw)
|
||||
table.insert(samples, Fx.cases[i].header)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return NbtF.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,47 @@
|
||||
-- data/snbt tests: the contract, nbtlib's SNBT text (indented and compact) through the api, conversion to and from
|
||||
-- binary NBT (F.convert), detection against JSON, options (indent, quote), error codes, and the fuzz pass. The
|
||||
-- grammar itself: tests/codecs/nbt.luau. Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local SnbtF = require("@formats/data/snbt")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("data/snbt")
|
||||
|
||||
t:eq(SnbtF.name, "snbt", "name")
|
||||
t:check(SnbtF.caps.decode and SnbtF.caps.encode, "caps")
|
||||
|
||||
for i = 1, 12 do
|
||||
local c = Fx.cases[i]
|
||||
local v = assert(F.load(c.snbt, { format = "snbt" }))
|
||||
local bin = assert(F.convert(c.compact, "nbt", { format = "snbt", compression = "none" })) :: buffer
|
||||
t:eq(Nbt.print(assert(Nbt.decode(bin))), Nbt.print(v), c.name .. ": SNBT -> NBT")
|
||||
local text = assert(F.convert(c.raw, "snbt", { format = "nbt" })) :: buffer
|
||||
t:eq(Nbt.print(assert(Nbt.parse(text))), Nbt.print(v), c.name .. ": NBT -> SNBT")
|
||||
local pretty = assert(F.saveString(v, { format = "snbt", indent = "\t" }))
|
||||
t:eq(Nbt.print(assert(F.load(pretty, { format = "snbt" }))), Nbt.print(v), c.name .. ": indented")
|
||||
end
|
||||
|
||||
do
|
||||
t:eq(F.detect("{DataVersion:3465,size:[I;1,2,3]}"), "snbt", "detect snbt")
|
||||
t:eq(F.detect('{"a": 1}'), "json", "json is not snbt")
|
||||
t:eq(F.detect('{"a": 1b}'), "snbt", "typed number")
|
||||
t:eq(F.detect(Fx.cases[3].compact, { name = "x.snbt" }), "snbt", "detect .snbt")
|
||||
t:eq(F.saveString(Nbt.parse("{a:'x'}"), { format = "snbt", quote = "'" }), "{a:'x'}", "quote option")
|
||||
local _, e = SnbtF.decode(Bytes.reader(buffer.fromstring("{a:")))
|
||||
t:check(e and e.code == Err.CORRUPT, "corrupt")
|
||||
local _, e2 = SnbtF.encode({ a = function() end } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "encode a function")
|
||||
end
|
||||
|
||||
local texts = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(texts, Fx.cases[i].snbt)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return SnbtF.decode(Bytes.reader(b))
|
||||
end, texts)
|
||||
t:done()
|
||||
Reference in new issue
Block a user