mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-22 09:58:10 +00:00
添加说明文档,为cangjie层添加单元测试
This commit is contained in:
176
src/lua_runner_test.cj
Normal file
176
src/lua_runner_test.cj
Normal file
@ -0,0 +1,176 @@
|
||||
package luacangjie_api
|
||||
|
||||
import std.unittest.*
|
||||
import std.fs.*
|
||||
|
||||
let scriptsDir = "./test/scripts"
|
||||
|
||||
// ====================
|
||||
// 1. 基础运行与参数测试
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testRunScript(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/simple_return.lua"
|
||||
let res = runner.runScript(path, "")
|
||||
@Expect(res, "hello")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testRunScriptWithArg(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/args_test.lua"
|
||||
let res = runner.runScript(path, "world")
|
||||
@Expect(res, "world")
|
||||
}
|
||||
|
||||
// ====================
|
||||
// 2. 异常处理测试
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testRunScriptSyntaxError(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/syntax_error.lua"
|
||||
try {
|
||||
runner.runScript(path, "")
|
||||
fail("Should throw syntax error")
|
||||
} catch (e: LuaError) {
|
||||
@Expect(e.code, 5010)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testRunScriptRuntimeError(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/runtime_error.lua"
|
||||
try {
|
||||
runner.runScript(path, "")
|
||||
fail("Should throw runtime error")
|
||||
} catch (e: LuaError) {
|
||||
@Expect(e.code, 5010)
|
||||
}
|
||||
}
|
||||
|
||||
// ====================
|
||||
// 3. 库加载测试
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testLoadLib(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/mylib.lua"
|
||||
runner.load(path, "mylib")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testLoadLibFileNotFound(): Unit {
|
||||
let runner = LuaRunner()
|
||||
try {
|
||||
runner.load("/no/such/file.lua", "badlib")
|
||||
fail("Should throw file not found error")
|
||||
} catch (e: LuaError) {
|
||||
@Expect(e.code, 5003)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testUnloadLib(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let path = scriptsDir + "/mylib.lua"
|
||||
runner.load(path, "mylib")
|
||||
runner.unload("mylib")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testUnloadLibNotFound(): Unit {
|
||||
let runner = LuaRunner()
|
||||
try {
|
||||
runner.unload("nosuchlib")
|
||||
fail("Should throw exception")
|
||||
} catch (e: LuaError) {
|
||||
@Expect(e.code, 5012)
|
||||
}
|
||||
}
|
||||
|
||||
// ====================
|
||||
// 4. 状态管理与数据交互
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testCleanup(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let setPath = scriptsDir + "/set_global.lua"
|
||||
let getPath = scriptsDir + "/get_global.lua"
|
||||
|
||||
runner.runScript(setPath, "")
|
||||
runner.clear()
|
||||
let res = runner.runScript(getPath, "")
|
||||
@Expect(res, "nil")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testCrossCallDataPassing(): Unit {
|
||||
let runner = LuaRunner()
|
||||
let setPath = scriptsDir + "/cross_call_set.lua"
|
||||
let getPath = scriptsDir + "/cross_call_get.lua"
|
||||
|
||||
runner.runScript(setPath, "")
|
||||
let res = runner.runScript(getPath, "")
|
||||
@Expect(res, "1,2,3")
|
||||
}
|
||||
|
||||
// ====================
|
||||
// 5. I/O 重定向测试
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testRedirectPrint(): Unit {
|
||||
let runner = LuaRunner(pathio: "/tmp")
|
||||
let path = scriptsDir + "/print_test.lua"
|
||||
runner.runScript(path, "")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testRedirectIoWrite(): Unit {
|
||||
let runner = LuaRunner(pathio: "/tmp")
|
||||
let path = scriptsDir + "/io_write_test.lua"
|
||||
runner.runScript(path, "")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testRedirectIoRead(): Unit {
|
||||
// 1. 准备输入文件
|
||||
let inputFile = "/tmp/input"
|
||||
|
||||
// [修正点]:移除了不存在的 option 参数。
|
||||
// File.writeTo 默认行为即为 CreateOrTruncate:
|
||||
// 文件存在会截断(覆盖),不存在则创建。
|
||||
File.writeTo(inputFile, "MockInputData".toArray())
|
||||
|
||||
// 2. 运行测试
|
||||
let runner = LuaRunner(pathio: "/tmp")
|
||||
let path = scriptsDir + "/io_read_test.lua"
|
||||
|
||||
let res = runner.runScript(path, "")
|
||||
@Expect(res, "MockInputData")
|
||||
}
|
||||
|
||||
// ====================
|
||||
// 6. 错误码与回调
|
||||
// ====================
|
||||
|
||||
@Test
|
||||
func testErrorCodesMapping(): Unit {
|
||||
@Expect(LuaError.getErrorMessage(5001), "JSON file load failed")
|
||||
@Expect(LuaError.getErrorMessage(5002), "Memory allocation failed")
|
||||
@Expect(LuaError.getErrorMessage(5010), "Script internal error, check result for details")
|
||||
@Expect(LuaError.getErrorMessage(9999), "Unknown error")
|
||||
}
|
||||
|
||||
@Test
|
||||
func testDefaultIOCallback(): Unit {
|
||||
let result = unsafe { defaultIO() }
|
||||
@Expect(result, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user