tests: top-level test comments: specs only

This commit is contained in:
4DBug committed 2026-11-01 01:49:12 -05:00
1 parent 0bd7b87d2b
commit e0ab895027
3 files changed
+4 -38

No files matched your search

+2 -17
View File
@@ -1,18 +1,9 @@
--!strict
-- tests/T: the test helper every tests/**/*.luau uses, under both runtimes (luau --codegen -O2 and LuaJIT through the
-- build). A failed check throws, so the runner (tools/test.sh) sees a non-zero exit.
--
-- local T = require("@tests/T")
-- local t = T.suite("png")
-- t:check(cond, "label") -- truthy
-- t:eq(got, want, "label") -- ==, with both values in the message
-- t:fuzz("decode", function(b) return Png.decode(Bytes.reader(b)) end, samples) -- the no-throw pass
-- t:done() -- prints "png: 123 checks passed (luajit)"
-- tests/T: the test helper; a failed check throws, so tools/test.sh sees a non-zero exit.
local T = {}
T.runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
-- deterministic LCG (the same numbers on both runtimes): rand(n) -> 0 .. n - 1
function T.rng(seed: number): (n: number) -> number
local s = seed % 2147483648
return function(n: number): number
@@ -21,7 +12,6 @@ function T.rng(seed: number): (n: number) -> number
end
end
-- bytes({ 1, 2, 3 }) -> buffer
function T.bytes(t: { number }): buffer
local b = buffer.create(#t)
for i, v in t do
@@ -30,7 +20,6 @@ function T.bytes(t: { number }): buffer
return b
end
-- hex(buf, max?) -> "89 50 4e 47 ..."
function T.hex(b: buffer, max: number?): string
local n = math.min(buffer.len(b), max or 64)
local out = table.create(n)
@@ -40,7 +29,6 @@ function T.hex(b: buffer, max: number?): string
return table.concat(out, " ") .. (if buffer.len(b) > n then " ..." else "")
end
-- clock() -> seconds (os.clock on both runtimes)
function T.clock(): number
return os.clock()
end
@@ -68,14 +56,11 @@ function Suite.eq(self: Suite, got: any, want: any, label: string)
self.passed += 1
end
-- note(fmt, ...): an indented progress line
function Suite.note(_self: Suite, fmt: string, ...: any)
print(" " .. string.format(fmt, ...))
end
-- fuzz(label, decode, samples, opts?): the no-throw pass every format test runs. For each sample (buffer or string):
-- truncations at several cuts, opts.mutations (default 8) copies with 1..4 random bytes changed, plus
-- opts.garbage (default 200) random buffers. decode(buf) may return anything except throw. Returns the counts.
-- the no-throw pass: truncations, opts.mutations (default 8) random byte changes, opts.garbage (default 200) random buffers.
function Suite.fuzz(self: Suite, label: string, decode: (buffer) -> any, samples: { any }, opts: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? }?): (number, number)
local o: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? } = opts or {}
local rand = T.rng(o.seed or 1234)
+1 -7
View File
@@ -1,6 +1,4 @@
-- API tests (src/Rosetta/init.luau): load / save / saveString / convert / open / detect / formats / format, the
-- category facades, sniff-then-extension detection, errors as Err values (never a throw), and file paths on hosts with
-- a file system (LuaJIT: a temporary file; the luau CLI has none).
-- API tests: load / save / convert / open / detect, the category facades, errors as values, file paths.
local T = require("@tests/T")
local F = require("@rosetta")
local Err = require("@rosetta/core/Err")
@@ -15,10 +13,8 @@ local function image(w: number, h: number): any
return img
end
-- host
t:eq(F.host.name, if T.runtime == "luajit" then "luajit" else "luau", "host detected")
-- formats()
do
local all = F.formats()
t:check(#all >= 1, "formats() lists the registry")
@@ -57,7 +53,6 @@ do
t:eq(F.detect(buf), "png", "detect")
local conv = F.convert(s, "png", { level = 1 })
t:check(conv and F.load(conv).width == 7, "convert")
-- format options reach the encoder
local stored = F.save(img, nil, { format = "png", level = 0 }) :: buffer
t:check(buffer.len(stored) > buffer.len(buf :: buffer), "opts.level reaches the encoder")
end
@@ -116,7 +111,6 @@ else
t:check(e2 and e2.code == Err.IO, "no file system: load of a path is Err.IO")
end
-- Types constructors
do
local T2 = F.Types
t:eq(buffer.len(T2.Image.new(3, 2, "rgb8").pixels), 18, "Image.new rgb8")
+1 -14
View File
@@ -1,12 +1,8 @@
-- compat tests: the Luau semantics the library relies on, checked under both runtimes. Under luau these pin down what
-- Luau does; under LuaJIT they check compat/luajit.lua (bit32, buffer, table / math / string / utf8 additions, typeof)
-- and tools/build.py's lowering (generalized iteration, if-expression chains, continue, compound assignment, string
-- interpolation, floor division, number literals).
-- compat tests: the Luau semantics the library relies on, under luau and under LuaJIT (compat/luajit.lua + build.py lowering).
local T = require("@tests/T")
local t = T.suite("compat")
-- ------------------------------------------------------------------------------------------------ bit32
do
t:eq(bit32.band(0xFFFFFFFF, 0x0F0F0F0F), 0x0F0F0F0F, "band unsigned")
t:eq(bit32.band(-1), 0xFFFFFFFF, "band(-1)")
@@ -46,7 +42,6 @@ do
t:check(bit32.btest(6, 2) and not bit32.btest(4, 2), "btest")
end
-- ------------------------------------------------------------------------------------------------ buffer
do
local b = buffer.create(16)
t:eq(buffer.len(b), 16, "len")
@@ -92,7 +87,6 @@ do
t:check(not pcall(buffer.readstring, b, 10, 7), "readstring past the end throws")
t:check(not pcall(buffer.copy, b, 10, b, 0, 7), "copy past the end throws")
t:check(not pcall(buffer.fill, b, 10, 0, 7), "fill past the end throws")
-- strings, NULs, copy, fill
local s = buffer.fromstring("a\0b\255c")
t:eq(buffer.len(s), 5, "fromstring with NUL")
t:eq(buffer.tostring(s), "a\0b\255c", "tostring with NUL")
@@ -112,7 +106,6 @@ do
t:eq(buffer.tostring(c), "ABBA", "fill value wraps")
t:eq(buffer.len(buffer.create(0)), 0, "empty buffer")
t:eq(buffer.tostring(buffer.create(0)), "", "empty tostring")
-- readbits / writebits
local bb = buffer.create(8)
buffer.writeu32(bb, 0, 0xDEADBEEF)
t:eq(buffer.readbits(bb, 4, 12), 3054, "readbits")
@@ -130,7 +123,6 @@ do
t:eq(buffer.readu8(big, 500000), 0, "big buffer zero-filled")
end
-- ------------------------------------------------------------------------------------------------ typeof / type
do
local b = buffer.create(1)
t:eq(typeof(b), "buffer", "typeof buffer")
@@ -141,7 +133,6 @@ do
t:eq(typeof("s"), "string", "typeof string")
end
-- ------------------------------------------------------------------------------------------------ table / math / string
do
local c = table.create(3, 7)
t:eq(#c, 3, "table.create size")
@@ -177,7 +168,6 @@ do
t:eq(math.floor(-3.5), -4, "floor")
end
-- ------------------------------------------------------------------------------------------------ utf8
do
t:eq(utf8.char(72, 228, 8364, 128512), "H\xC3\xA4\xE2\x82\xAC\xF0\x9F\x98\x80", "utf8.char")
t:eq(utf8.len("h\xC3\xA4ll\xE2\x82\xAC"), 5, "utf8.len")
@@ -193,9 +183,7 @@ do
t:check(string.find("\xC3\xA4", utf8.charpattern) == 1, "utf8.charpattern")
end
-- ------------------------------------------------------------------------------------------------ syntax lowering
do
-- generalized iteration: arrays (in order), dictionaries, mixed, iterator functions, nested
local order = {}
for i, v in { 10, 20, 30 } do
table.insert(order, i * 100 + v)
@@ -253,7 +241,6 @@ do
local v = 5
local arith = if v > 3 then v * 2 else v - 1
t:eq(arith, 10, "if-expression arithmetic")
-- continue, compound assignment, interpolation, floor division, literals
local odd = 0
for i = 1, 10 do
if i % 2 == 0 then