Files
plainleaf/client/space_lua/lua.test.ts
T
Matouš Jan FialkaandGitHub fd4ff8518d [Space Lua] Implement string.pack, string.unpack and string.packsize (#1851)
The three binary-packing functions from Lua are now implemented in
a dedicated `string_pack.ts` module and wired into `stringApi`.

Format parsing follows the exact option set of Lua:

* signed and unsigned integers of configurable width
  (`b`/`B`/`h`/`H`/`l`/`L`/`j`/`J`/`T`/`i`/`I`),
* IEEE 754 single and double precision floats (`f`/`d`/`n`),
* fixed-length strings (`c`),
* length-prefixed strings (`s`),
* zero-terminated strings (`z`),
* padding bytes (`x`),
* alignment padding (`X`), and
* endianness/alignment modifiers (`<`/`>`/`=`/`!`).

`string.packsize` rejects variable-length formats (`s` and `z`) as Lua
requires.

`string.unpack` returns all unpacked values followed by the
next position integer, matching the Lua contract exactly.

Test suite extended in separates `string_pack_test.lua` file. Passes
both Lua and Space Lua execution.

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
2026-02-25 09:29:14 +01:00

111 lines
3.0 KiB
TypeScript

import { parse } from "./parse.ts";
import { luaBuildStandardEnv } from "./stdlib.ts";
import { LuaEnv, LuaRuntimeError, LuaStackFrame } from "./runtime.ts";
import { evalStatement } from "./eval.ts";
import { assert } from "@std/assert/assert";
import { fileURLToPath } from "node:url";
Deno.test("[Lua] Core language", async () => {
await runLuaTest("./language_core_test.lua");
});
Deno.test("[Lua] Core language (labels and goto)", async () => {
await runLuaTest("./goto_test.lua");
});
Deno.test("[Lua] Core language (length)", async () => {
await runLuaTest("./len_test.lua");
});
Deno.test("[Lua] Core language (truthiness)", async () => {
await runLuaTest("./truthiness_test.lua");
});
Deno.test("[Lua] Core language (arithmetic)", async () => {
await runLuaTest("./arithmetic_test.lua");
});
Deno.test("[Lua] Core language (metamethods)", async () => {
await runLuaTest("./metamethods_test.lua");
});
Deno.test("[Lua] Load tests", async () => {
await runLuaTest("./stdlib/load_test.lua");
});
Deno.test("[Lua] Core language (length)", async () => {
await runLuaTest("./len_test.lua");
});
Deno.test("[Lua] Format tests", async () => {
await runLuaTest("./stdlib/format_test.lua");
});
Deno.test("[Lua] String to number tests", async () => {
await runLuaTest("./tonumber_test.lua");
});
Deno.test("[Lua] Pattern tests", async () => {
await runLuaTest("./stdlib/pattern_test.lua");
});
Deno.test("[Lua] String tests", async () => {
await runLuaTest("./stdlib/string_test.lua");
});
Deno.test("[Lua] String pack/unpack/packsize tests", async () => {
await runLuaTest("./stdlib/string_pack_test.lua");
});
Deno.test("[Lua] Space Lua tests", async () => {
await runLuaTest("./stdlib/space_lua_test.lua");
});
Deno.test("[Lua] OS tests", async () => {
await runLuaTest("./stdlib/os_test.lua");
});
Deno.test("[Lua] Math tests", async () => {
await runLuaTest("./stdlib/math_test.lua");
});
Deno.test("[Lua] JS tests", async () => {
await runLuaTest("./stdlib/js_test.lua");
});
Deno.test("[Lua] Global functions tests", async () => {
await runLuaTest("./stdlib/global_test.lua");
});
Deno.test("[Lua] Encoding functions tests", async () => {
await runLuaTest("./stdlib/encoding_test.lua");
});
Deno.test("[Lua] Crypto functions tests", async () => {
await runLuaTest("./stdlib/crypto_test.lua");
});
Deno.test("[Lua] Lume functions tests", async () => {
await runLuaTest("./lume_test.lua");
});
async function runLuaTest(luaPath: string) {
const luaFile = await Deno.readTextFile(
fileURLToPath(new URL(luaPath, import.meta.url)),
);
const chunk = parse(luaFile, {});
const env = new LuaEnv(luaBuildStandardEnv());
const sf = LuaStackFrame.createWithGlobalEnv(env, chunk.ctx);
try {
await evalStatement(chunk, env, sf);
} catch (e: any) {
if (e instanceof LuaRuntimeError) {
console.error(`Error evaluating script:`, e.toPrettyString(luaFile));
} else {
console.error(`Error evaluating script:`, e);
}
assert(false);
}
}