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]>
48 lines
2.1 KiB
Luau
48 lines
2.1 KiB
Luau
-- 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()
|