tests: top-level test comments: specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
e0ab895027
3 files changed
+4
-38
No files matched your search
+2
-17
@@ -1,18 +1,9 @@
|
|||||||
--!strict
|
--!strict
|
||||||
-- tests/T: the test helper every tests/**/*.luau uses, under both runtimes (luau --codegen -O2 and LuaJIT through the
|
-- tests/T: the test helper; a failed check throws, so tools/test.sh sees a non-zero exit.
|
||||||
-- 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)"
|
|
||||||
local T = {}
|
local T = {}
|
||||||
|
|
||||||
T.runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
|
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
|
function T.rng(seed: number): (n: number) -> number
|
||||||
local s = seed % 2147483648
|
local s = seed % 2147483648
|
||||||
return function(n: number): number
|
return function(n: number): number
|
||||||
@@ -21,7 +12,6 @@ function T.rng(seed: number): (n: number) -> number
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- bytes({ 1, 2, 3 }) -> buffer
|
|
||||||
function T.bytes(t: { number }): buffer
|
function T.bytes(t: { number }): buffer
|
||||||
local b = buffer.create(#t)
|
local b = buffer.create(#t)
|
||||||
for i, v in t do
|
for i, v in t do
|
||||||
@@ -30,7 +20,6 @@ function T.bytes(t: { number }): buffer
|
|||||||
return b
|
return b
|
||||||
end
|
end
|
||||||
|
|
||||||
-- hex(buf, max?) -> "89 50 4e 47 ..."
|
|
||||||
function T.hex(b: buffer, max: number?): string
|
function T.hex(b: buffer, max: number?): string
|
||||||
local n = math.min(buffer.len(b), max or 64)
|
local n = math.min(buffer.len(b), max or 64)
|
||||||
local out = table.create(n)
|
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 "")
|
return table.concat(out, " ") .. (if buffer.len(b) > n then " ..." else "")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- clock() -> seconds (os.clock on both runtimes)
|
|
||||||
function T.clock(): number
|
function T.clock(): number
|
||||||
return os.clock()
|
return os.clock()
|
||||||
end
|
end
|
||||||
@@ -68,14 +56,11 @@ function Suite.eq(self: Suite, got: any, want: any, label: string)
|
|||||||
self.passed += 1
|
self.passed += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- note(fmt, ...): an indented progress line
|
|
||||||
function Suite.note(_self: Suite, fmt: string, ...: any)
|
function Suite.note(_self: Suite, fmt: string, ...: any)
|
||||||
print(" " .. string.format(fmt, ...))
|
print(" " .. string.format(fmt, ...))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- fuzz(label, decode, samples, opts?): the no-throw pass every format test runs. For each sample (buffer or string):
|
-- the no-throw pass: truncations, opts.mutations (default 8) random byte changes, opts.garbage (default 200) random buffers.
|
||||||
-- 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.
|
|
||||||
function Suite.fuzz(self: Suite, label: string, decode: (buffer) -> any, samples: { any }, opts: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? }?): (number, number)
|
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 o: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? } = opts or {}
|
||||||
local rand = T.rng(o.seed or 1234)
|
local rand = T.rng(o.seed or 1234)
|
||||||
|
|||||||
+1
-7
@@ -1,6 +1,4 @@
|
|||||||
-- API tests (src/Rosetta/init.luau): load / save / saveString / convert / open / detect / formats / format, the
|
-- API tests: load / save / convert / open / detect, the category facades, errors as values, file paths.
|
||||||
-- 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).
|
|
||||||
local T = require("@tests/T")
|
local T = require("@tests/T")
|
||||||
local F = require("@rosetta")
|
local F = require("@rosetta")
|
||||||
local Err = require("@rosetta/core/Err")
|
local Err = require("@rosetta/core/Err")
|
||||||
@@ -15,10 +13,8 @@ local function image(w: number, h: number): any
|
|||||||
return img
|
return img
|
||||||
end
|
end
|
||||||
|
|
||||||
-- host
|
|
||||||
t:eq(F.host.name, if T.runtime == "luajit" then "luajit" else "luau", "host detected")
|
t:eq(F.host.name, if T.runtime == "luajit" then "luajit" else "luau", "host detected")
|
||||||
|
|
||||||
-- formats()
|
|
||||||
do
|
do
|
||||||
local all = F.formats()
|
local all = F.formats()
|
||||||
t:check(#all >= 1, "formats() lists the registry")
|
t:check(#all >= 1, "formats() lists the registry")
|
||||||
@@ -57,7 +53,6 @@ do
|
|||||||
t:eq(F.detect(buf), "png", "detect")
|
t:eq(F.detect(buf), "png", "detect")
|
||||||
local conv = F.convert(s, "png", { level = 1 })
|
local conv = F.convert(s, "png", { level = 1 })
|
||||||
t:check(conv and F.load(conv).width == 7, "convert")
|
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
|
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")
|
t:check(buffer.len(stored) > buffer.len(buf :: buffer), "opts.level reaches the encoder")
|
||||||
end
|
end
|
||||||
@@ -116,7 +111,6 @@ else
|
|||||||
t:check(e2 and e2.code == Err.IO, "no file system: load of a path is Err.IO")
|
t:check(e2 and e2.code == Err.IO, "no file system: load of a path is Err.IO")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Types constructors
|
|
||||||
do
|
do
|
||||||
local T2 = F.Types
|
local T2 = F.Types
|
||||||
t:eq(buffer.len(T2.Image.new(3, 2, "rgb8").pixels), 18, "Image.new rgb8")
|
t:eq(buffer.len(T2.Image.new(3, 2, "rgb8").pixels), 18, "Image.new rgb8")
|
||||||
|
|||||||
+1
-14
@@ -1,12 +1,8 @@
|
|||||||
-- compat tests: the Luau semantics the library relies on, checked under both runtimes. Under luau these pin down what
|
-- compat tests: the Luau semantics the library relies on, under luau and under LuaJIT (compat/luajit.lua + build.py lowering).
|
||||||
-- 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).
|
|
||||||
local T = require("@tests/T")
|
local T = require("@tests/T")
|
||||||
|
|
||||||
local t = T.suite("compat")
|
local t = T.suite("compat")
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ bit32
|
|
||||||
do
|
do
|
||||||
t:eq(bit32.band(0xFFFFFFFF, 0x0F0F0F0F), 0x0F0F0F0F, "band unsigned")
|
t:eq(bit32.band(0xFFFFFFFF, 0x0F0F0F0F), 0x0F0F0F0F, "band unsigned")
|
||||||
t:eq(bit32.band(-1), 0xFFFFFFFF, "band(-1)")
|
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")
|
t:check(bit32.btest(6, 2) and not bit32.btest(4, 2), "btest")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ buffer
|
|
||||||
do
|
do
|
||||||
local b = buffer.create(16)
|
local b = buffer.create(16)
|
||||||
t:eq(buffer.len(b), 16, "len")
|
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.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.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")
|
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")
|
local s = buffer.fromstring("a\0b\255c")
|
||||||
t:eq(buffer.len(s), 5, "fromstring with NUL")
|
t:eq(buffer.len(s), 5, "fromstring with NUL")
|
||||||
t:eq(buffer.tostring(s), "a\0b\255c", "tostring 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.tostring(c), "ABBA", "fill value wraps")
|
||||||
t:eq(buffer.len(buffer.create(0)), 0, "empty buffer")
|
t:eq(buffer.len(buffer.create(0)), 0, "empty buffer")
|
||||||
t:eq(buffer.tostring(buffer.create(0)), "", "empty tostring")
|
t:eq(buffer.tostring(buffer.create(0)), "", "empty tostring")
|
||||||
-- readbits / writebits
|
|
||||||
local bb = buffer.create(8)
|
local bb = buffer.create(8)
|
||||||
buffer.writeu32(bb, 0, 0xDEADBEEF)
|
buffer.writeu32(bb, 0, 0xDEADBEEF)
|
||||||
t:eq(buffer.readbits(bb, 4, 12), 3054, "readbits")
|
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")
|
t:eq(buffer.readu8(big, 500000), 0, "big buffer zero-filled")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ typeof / type
|
|
||||||
do
|
do
|
||||||
local b = buffer.create(1)
|
local b = buffer.create(1)
|
||||||
t:eq(typeof(b), "buffer", "typeof buffer")
|
t:eq(typeof(b), "buffer", "typeof buffer")
|
||||||
@@ -141,7 +133,6 @@ do
|
|||||||
t:eq(typeof("s"), "string", "typeof string")
|
t:eq(typeof("s"), "string", "typeof string")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ table / math / string
|
|
||||||
do
|
do
|
||||||
local c = table.create(3, 7)
|
local c = table.create(3, 7)
|
||||||
t:eq(#c, 3, "table.create size")
|
t:eq(#c, 3, "table.create size")
|
||||||
@@ -177,7 +168,6 @@ do
|
|||||||
t:eq(math.floor(-3.5), -4, "floor")
|
t:eq(math.floor(-3.5), -4, "floor")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ utf8
|
|
||||||
do
|
do
|
||||||
t:eq(utf8.char(72, 228, 8364, 128512), "H\xC3\xA4\xE2\x82\xAC\xF0\x9F\x98\x80", "utf8.char")
|
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")
|
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")
|
t:check(string.find("\xC3\xA4", utf8.charpattern) == 1, "utf8.charpattern")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ syntax lowering
|
|
||||||
do
|
do
|
||||||
-- generalized iteration: arrays (in order), dictionaries, mixed, iterator functions, nested
|
|
||||||
local order = {}
|
local order = {}
|
||||||
for i, v in { 10, 20, 30 } do
|
for i, v in { 10, 20, 30 } do
|
||||||
table.insert(order, i * 100 + v)
|
table.insert(order, i * 100 + v)
|
||||||
@@ -253,7 +241,6 @@ do
|
|||||||
local v = 5
|
local v = 5
|
||||||
local arith = if v > 3 then v * 2 else v - 1
|
local arith = if v > 3 then v * 2 else v - 1
|
||||||
t:eq(arith, 10, "if-expression arithmetic")
|
t:eq(arith, 10, "if-expression arithmetic")
|
||||||
-- continue, compound assignment, interpolation, floor division, literals
|
|
||||||
local odd = 0
|
local odd = 0
|
||||||
for i = 1, 10 do
|
for i = 1, 10 do
|
||||||
if i % 2 == 0 then
|
if i % 2 == 0 then
|
||||||
|
|||||||
Reference in new issue
Block a user