修复描述性错误,完善测试

This commit is contained in:
2026-03-21 22:53:03 +08:00
parent 8998ad6900
commit 8e9deb2865
34 changed files with 202 additions and 95 deletions

View File

@ -4,6 +4,22 @@ import std.unittest.*
import std.fs.*
let scriptsDir = "./test/scripts"
let generatedScriptsDir = "./test/generated_scripts"
func scriptPath(name: String): String {
scriptsDir + "/" + name
}
func generatedScriptPath(name: String): String {
generatedScriptsDir + "/" + name
}
func ensureGeneratedScriptsDir(): Unit {
let dir = Path(generatedScriptsDir)
if (!exists(dir)) {
Directory.create(dir, recursive: true)
}
}
// ====================
// 1. 基础运行与参数测试
@ -12,7 +28,7 @@ let scriptsDir = "./test/scripts"
@Test
func testRunScript(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/simple_return.lua"
let path = scriptPath("simple_return.lua")
let res = runner.runScript(path, "")
@Expect(res, "hello")
}
@ -20,7 +36,7 @@ func testRunScript(): Unit {
@Test
func testRunScriptWithArg(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/args_test.lua"
let path = scriptPath("args_test.lua")
let res = runner.runScript(path, "world")
@Expect(res, "world")
}
@ -32,7 +48,7 @@ func testRunScriptWithArg(): Unit {
@Test
func testRunScriptSyntaxError(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/syntax_error.lua"
let path = scriptPath("syntax_error.lua")
try {
runner.runScript(path, "")
fail("Should throw syntax error")
@ -44,7 +60,7 @@ func testRunScriptSyntaxError(): Unit {
@Test
func testRunScriptRuntimeError(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/runtime_error.lua"
let path = scriptPath("runtime_error.lua")
try {
runner.runScript(path, "")
fail("Should throw runtime error")
@ -60,7 +76,7 @@ func testRunScriptRuntimeError(): Unit {
@Test
func testLoadLib(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/mylib.lua"
let path = scriptPath("mylib.lua")
runner.load(path, "mylib")
}
@ -78,7 +94,7 @@ func testLoadLibFileNotFound(): Unit {
@Test
func testUnloadLib(): Unit {
let runner = LuaRunner()
let path = scriptsDir + "/mylib.lua"
let path = scriptPath("mylib.lua")
runner.load(path, "mylib")
runner.unload("mylib")
}
@ -101,8 +117,8 @@ func testUnloadLibNotFound(): Unit {
@Test
func testCleanup(): Unit {
let runner = LuaRunner()
let setPath = scriptsDir + "/set_global.lua"
let getPath = scriptsDir + "/get_global.lua"
let setPath = scriptPath("set_global.lua")
let getPath = scriptPath("get_global.lua")
runner.runScript(setPath, "")
runner.clear()
@ -113,14 +129,92 @@ func testCleanup(): Unit {
@Test
func testCrossCallDataPassing(): Unit {
let runner = LuaRunner()
let setPath = scriptsDir + "/cross_call_set.lua"
let getPath = scriptsDir + "/cross_call_get.lua"
let setPath = scriptPath("cross_call_set.lua")
let getPath = scriptPath("cross_call_get.lua")
runner.runScript(setPath, "")
let res = runner.runScript(getPath, "")
@Expect(res, "1,2,3")
}
@Test
func testPipelineDocumentedFlow(): Unit {
let runner = LuaRunner()
runner.load(scriptPath("pipeline_add_suffix.lua"), "add_suffix")
.load(scriptPath("pipeline_to_upper.lua"), "to_upper")
.load(scriptPath("pipeline_add_prefix.lua"), "add_prefix")
let initialData = runner.runScript(scriptPath("pipeline_generate_data.lua"), "hello")
@Expect(initialData, "hello")
let afterA = runner.runScript("", "")
@Expect(afterA, "[PREFIX] hello")
let afterB = runner.runScript("", "")
@Expect(afterB, "[PREFIX] HELLO")
let finalResult = runner.runScript("", "")
@Expect(finalResult, "[PREFIX] HELLO [SUFFIX]")
}
@Test
func testPipelineStepByStepResults(): Unit {
let runner = LuaRunner()
runner.load(scriptPath("pipeline_add_suffix.lua"), "add_suffix")
.load(scriptPath("pipeline_to_upper.lua"), "to_upper")
.load(scriptPath("pipeline_add_prefix.lua"), "add_prefix")
let initialData = runner.runScript(scriptPath("pipeline_generate_data.lua"), "hello")
@Expect(initialData, "hello")
let afterPrefix = runner.runScript("", "")
@Expect(afterPrefix, "[PREFIX] hello")
let afterUpper = runner.runScript("", "")
@Expect(afterUpper, "[PREFIX] HELLO")
let afterSuffix = runner.runScript("", "")
@Expect(afterSuffix, "[PREFIX] HELLO [SUFFIX]")
}
@Test
func testLoadLibOverflow(): Unit {
ensureGeneratedScriptsDir()
let runner = LuaRunner()
for (i in 0..20) {
let name = "overflow_lib_${i}"
let path = generatedScriptPath(name + ".lua")
File.writeTo(path, ("return \"" + name + "\"").toArray())
runner.load(path, name)
}
let overflowName = "overflow_lib_20"
let overflowPath = generatedScriptPath(overflowName + ".lua")
File.writeTo(overflowPath, "return \"overflow\"".toArray())
try {
runner.load(overflowPath, overflowName)
fail("Should throw overflow error")
} catch (e: LuaError) {
@Expect(e.code, 5016)
}
}
@Test
func testPackagePathConfig(): Unit {
let runner = LuaRunner(pkgpath: scriptsDir + "/pkgmods/?.lua")
let res = runner.runScript(scriptPath("pkgpath_require_mylib.lua"), "")
@Expect(res, "LoadedViaPkgPath")
}
@Test
func testPackagePathInitLuaPattern(): Unit {
let runner = LuaRunner(pkgpath: scriptsDir + "/pkgmods_init/?/init.lua")
let res = runner.runScript(scriptPath("pkgpath_require_nested.lua"), "")
@Expect(res, "LoadedViaInit")
}
// ====================
// 5. I/O 重定向测试
// ====================
@ -128,14 +222,14 @@ func testCrossCallDataPassing(): Unit {
@Test
func testRedirectPrint(): Unit {
let runner = LuaRunner(pathio: "/tmp")
let path = scriptsDir + "/print_test.lua"
let path = scriptPath("print_test.lua")
runner.runScript(path, "")
}
@Test
func testRedirectIoWrite(): Unit {
let runner = LuaRunner(pathio: "/tmp")
let path = scriptsDir + "/io_write_test.lua"
let path = scriptPath("io_write_test.lua")
runner.runScript(path, "")
}
@ -151,7 +245,7 @@ func testRedirectIoRead(): Unit {
// 2. 运行测试
let runner = LuaRunner(pathio: "/tmp")
let path = scriptsDir + "/io_read_test.lua"
let path = scriptPath("io_read_test.lua")
let res = runner.runScript(path, "")
@Expect(res, "MockInputData")