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

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

@ -7,22 +7,17 @@
#include <string>
#include <fstream>
#include <streambuf>
#include <stdexcept> // 新增:用于异常处理
#include <stdexcept>
extern "C" {
#include "lua_cj_api.h"
#include "errors.h"
}
// 辅助函数:创建临时目录
static std::string create_temp_dir() {
// 修正点:
// 1. mkdtemp 要求模板字符串最后6个字符必须是 'XXXXXX'
// 2. 使用 /tmp 目录确保路径存在,避免 "test/" 目录不存在导致的失败
char tmpl[] = "/tmp/lua_cj_test_XXXXXX";
char tmpl[] = "/tmp/lua_cj_test_XXXXXX";
char *dir = mkdtemp(tmpl);
// 修正点:检查 mkdtemp 是否成功,防止传入 NULL 给 std::string 导致崩溃
if (dir == nullptr) {
perror("mkdtemp failed");
throw std::runtime_error("Failed to create temp directory");
@ -31,15 +26,17 @@ static std::string create_temp_dir() {
return std::string(dir);
}
// 全局计数器,用于验证回调是否被调用
static int callback_invoke_count = 0;
// 模拟输入/输出回调
static int mock_callback() {
callback_invoke_count++;
return 0;
}
static std::string get_script_path(const std::string& script_name) {
return std::string(SCRIPT_DIR) + "/" + script_name;
}
class LuaCjApiTest : public ::testing::Test {
protected:
void* runner;
@ -47,10 +44,7 @@ protected:
void SetUp() override {
temp_dir = create_temp_dir();
callback_invoke_count = 0; // 重置计数器
// 修改:传入 temp_dir 作为 pathio确保 I/O 重定向路径有效
// 仓颉层封装中 pathio 和 pkgpath 都是可配置的,这里测试基础情况
callback_invoke_count = 0;
runner = init_lua_runner(temp_dir.c_str(), NULL, mock_callback, mock_callback);
ASSERT_NE(runner, nullptr) << "init_lua_runner failed, errno=" << get_errno();
}
@ -63,16 +57,7 @@ protected:
std::string cmd = "rm -rf " + temp_dir;
system(cmd.c_str());
}
void write_lua(const std::string& filename, const std::string& content) {
std::string path = temp_dir + "/" + filename;
FILE* f = fopen(path.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "%s", content.c_str());
fclose(f);
}
// 辅助函数:读取文件内容
std::string read_file(const std::string& filename) {
std::string path = temp_dir + "/" + filename;
std::ifstream t(path);
@ -89,8 +74,7 @@ TEST_F(LuaCjApiTest, InitFree) {
// 测试加载库
TEST_F(LuaCjApiTest, LoadLib) {
write_lua("mylib.lua", "function foo() return 42 end");
int ret = load_lib(runner, (temp_dir + "/mylib.lua").c_str(), "mylib");
int ret = load_lib(runner, get_script_path("mylib.lua").c_str(), "mylib");
EXPECT_EQ(ret, 0);
}
@ -106,14 +90,20 @@ TEST_F(LuaCjApiTest, LoadLibOverflow) {
for (int i = 0; i < MAX_LUA_LIB; ++i) {
char name[20];
snprintf(name, sizeof(name), "lib%d", i);
std::string fname = std::string(name) + ".lua";
write_lua(fname, "return {}");
std::string path = temp_dir + "/" + fname;
std::string content = "return " + std::to_string(i);
std::string path = temp_dir + "/" + std::string(name) + ".lua";
FILE* f = fopen(path.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "%s", content.c_str());
fclose(f);
int ret = load_lib(runner, path.c_str(), name);
EXPECT_EQ(ret, 0);
}
write_lua("extra.lua", "return {}");
std::string path = temp_dir + "/extra.lua";
FILE* f = fopen(path.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "return {}");
fclose(f);
int ret = load_lib(runner, path.c_str(), "extra");
EXPECT_EQ(ret, -1);
EXPECT_EQ(get_errno(), NAPI_LUALIB_LOAD_OVER_STACK);
@ -121,9 +111,7 @@ TEST_F(LuaCjApiTest, LoadLibOverflow) {
// 测试卸载库
TEST_F(LuaCjApiTest, UnloadLib) {
write_lua("mylib.lua", "function foo() end");
std::string path = temp_dir + "/mylib.lua";
ASSERT_EQ(load_lib(runner, path.c_str(), "mylib"), 0);
ASSERT_EQ(load_lib(runner, get_script_path("mylib.lua").c_str(), "mylib"), 0);
int ret = unload_lib(runner, "mylib");
EXPECT_EQ(ret, 0);
}
@ -137,8 +125,7 @@ TEST_F(LuaCjApiTest, UnloadLibNotFound) {
// 测试运行脚本
TEST_F(LuaCjApiTest, RunScript) {
write_lua("test.lua", "return 'hello'");
int ret = run(runner, (temp_dir + "/test.lua").c_str(), nullptr);
int ret = run(runner, get_script_path("simple_return.lua").c_str(), nullptr);
EXPECT_EQ(ret, 0);
char* res = getresult(runner);
EXPECT_STREQ(res, "hello");
@ -146,12 +133,7 @@ TEST_F(LuaCjApiTest, RunScript) {
// 测试带参数的脚本
TEST_F(LuaCjApiTest, RunScriptWithArg) {
// 修正点Lua 中 ... 不能作为变量名。
// 使用 select(1, ...) 或者直接 return ...
// 这里使用 "return (...)" 返回第一个参数
write_lua("test.lua", "return (...)");
int ret = run(runner, (temp_dir + "/test.lua").c_str(), "world");
int ret = run(runner, get_script_path("args_test.lua").c_str(), "world");
EXPECT_EQ(ret, 0);
char* res = getresult(runner);
EXPECT_STREQ(res, "world");
@ -160,8 +142,7 @@ TEST_F(LuaCjApiTest, RunScriptWithArg) {
// 测试脚本语法错误
TEST_F(LuaCjApiTest, RunScriptSyntaxError) {
write_lua("bad.lua", "for i=1,10 print(i) end");
int ret = run(runner, (temp_dir + "/bad.lua").c_str(), NULL);
int ret = run(runner, get_script_path("syntax_error.lua").c_str(), NULL);
EXPECT_EQ(ret, -1);
EXPECT_EQ(get_errno(), NAPI_SCRIPT_ERROR);
char* res = getresult(runner);
@ -170,8 +151,7 @@ TEST_F(LuaCjApiTest, RunScriptSyntaxError) {
// 测试脚本运行时错误
TEST_F(LuaCjApiTest, RunScriptRuntimeError) {
write_lua("bad.lua", "error('oops')");
int ret = run(runner, (temp_dir + "/bad.lua").c_str(), nullptr);
int ret = run(runner, get_script_path("runtime_error.lua").c_str(), nullptr);
EXPECT_EQ(ret, -1);
EXPECT_EQ(get_errno(), NAPI_SCRIPT_ERROR);
char* res = getresult(runner);
@ -180,12 +160,10 @@ TEST_F(LuaCjApiTest, RunScriptRuntimeError) {
// 测试 cleanup 重置状态
TEST_F(LuaCjApiTest, Cleanup) {
write_lua("set.lua", "g = 123");
ASSERT_EQ(run(runner, (temp_dir + "/set.lua").c_str(), nullptr), 0);
ASSERT_EQ(run(runner, get_script_path("set_global.lua").c_str(), nullptr), 0);
int ret = cleanup(runner);
EXPECT_EQ(ret, 0);
write_lua("get.lua", "return tostring(g or 'nil')");
ret = run(runner, (temp_dir + "/get.lua").c_str(), nullptr);
ret = run(runner, get_script_path("get_global.lua").c_str(), nullptr);
EXPECT_EQ(ret, 0);
char* res = getresult(runner);
EXPECT_STREQ(res, "nil");
@ -193,10 +171,8 @@ TEST_F(LuaCjApiTest, Cleanup) {
// 测试跨调用数据传递
TEST_F(LuaCjApiTest, CrossCallDataPassing) {
write_lua("set.lua", "data = {1,2,3}");
ASSERT_EQ(run(runner, (temp_dir + "/set.lua").c_str(), nullptr), 0);
write_lua("get.lua", "return table.concat(data, ',')");
ASSERT_EQ(run(runner, (temp_dir + "/get.lua").c_str(), nullptr), 0);
ASSERT_EQ(run(runner, get_script_path("cross_call_set.lua").c_str(), nullptr), 0);
ASSERT_EQ(run(runner, get_script_path("cross_call_get.lua").c_str(), nullptr), 0);
char* res = getresult(runner);
EXPECT_STREQ(res, "1,2,3");
}
@ -207,9 +183,11 @@ TEST_F(LuaCjApiTest, MultipleLoadUnload) {
for (int i = 0; i < 5; ++i) {
char name[20];
snprintf(name, sizeof(name), "L%d", i);
std::string fname = std::string(name) + ".lua";
write_lua(fname, "return " + std::to_string(i));
std::string path = temp_dir + "/" + fname;
std::string path = temp_dir + "/" + std::string(name) + ".lua";
FILE* f = fopen(path.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "return %d", i);
fclose(f);
ASSERT_EQ(load_lib(runner, path.c_str(), name), 0);
}
@ -219,12 +197,15 @@ TEST_F(LuaCjApiTest, MultipleLoadUnload) {
ASSERT_EQ(unload_lib(runner, name), 0);
for (int j = 0; j < i; ++j) {
char libname[20], scriptname[20];
char libname[20];
snprintf(libname, sizeof(libname), "L%d", j);
snprintf(scriptname, sizeof(scriptname), "check%d.lua", j);
std::string script = "return require '" + std::string(libname) + "'";
write_lua(scriptname, script);
int ret = run(runner, (temp_dir + "/" + scriptname).c_str(), nullptr);
std::string scriptname = temp_dir + "/check" + std::to_string(j) + ".lua";
FILE* f = fopen(scriptname.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "%s", script.c_str());
fclose(f);
int ret = run(runner, scriptname.c_str(), nullptr);
EXPECT_EQ(ret, -1) << "Failed for lib " << libname << " after unloading " << name;
if (ret == 0) {
char expected[10];
@ -239,22 +220,18 @@ TEST_F(LuaCjApiTest, MultipleLoadUnload) {
// 测试 I/O 重定向Print
TEST_F(LuaCjApiTest, RedirectPrint) {
write_lua("print_test.lua", "print('Hello Cangjie')");
int ret = run(runner, (temp_dir + "/print_test.lua").c_str(), nullptr);
int ret = run(runner, get_script_path("print_test.lua").c_str(), nullptr);
EXPECT_EQ(ret, 0);
// 验证:回调函数应该被调用
EXPECT_GT(callback_invoke_count, 0) << "Output callback should be invoked by print";
// 验证temp_dir 下应生成 output 文件
std::string content = read_file("output");
EXPECT_NE(content.find("Hello Cangjie"), std::string::npos);
}
// 测试 I/O 重定向io.write
TEST_F(LuaCjApiTest, RedirectIoWrite) {
write_lua("write_test.lua", "io.write('Data from lua')");
int ret = run(runner, (temp_dir + "/write_test.lua").c_str(), nullptr);
int ret = run(runner, get_script_path("io_write_test.lua").c_str(), nullptr);
EXPECT_EQ(ret, 0);
EXPECT_GT(callback_invoke_count, 0) << "Output callback should be invoked by io.write";
@ -264,51 +241,44 @@ TEST_F(LuaCjApiTest, RedirectIoWrite) {
// 测试 I/O 重定向io.read
TEST_F(LuaCjApiTest, RedirectIoRead) {
// 准备:模拟外部向 input 文件写入数据
std::string input_file = temp_dir + "/input";
FILE* f = fopen(input_file.c_str(), "w");
ASSERT_NE(f, nullptr);
fprintf(f, "MockInputData");
fclose(f);
// Lua 脚本读取输入并返回
write_lua("read_test.lua", "local s = io.read('*a'); return s");
int ret = run(runner, (temp_dir + "/read_test.lua").c_str(), nullptr);
int ret = run(runner, get_script_path("io_read_test.lua").c_str(), nullptr);
EXPECT_EQ(ret, 0);
// 验证:输入回调被调用
EXPECT_GT(callback_invoke_count, 0) << "Input callback should be invoked by io.read";
// 验证:结果正确
char* res = getresult(runner);
EXPECT_STREQ(res, "MockInputData");
}
// 测试 pkgpath 配置是否生效
TEST_F(LuaCjApiTest, PackagePathConfig) {
// 创建子目录存放库
std::string lib_dir = temp_dir + "/mypkgs";
mkdir(lib_dir.c_str(), 0755);
// 在子目录创建库
std::string lib_path = lib_dir + "/mylib.lua";
FILE* f = fopen(lib_path.c_str(), "w");
fprintf(f, "return 'LoadedViaPkgPath'");
fclose(f);
// 销毁旧 runner
free_lua(runner);
runner = nullptr;
// 创建新 runner配置 pkgpath
std::string pkg_path_pattern = lib_dir + "/?.lua";
runner = init_lua_runner(temp_dir.c_str(), pkg_path_pattern.c_str(), mock_callback, mock_callback);
ASSERT_NE(runner, nullptr);
// 脚本直接 require不指定路径
write_lua("main.lua", "return require 'mylib'");
int ret = run(runner, (temp_dir + "/main.lua").c_str(), nullptr);
std::string main_script = temp_dir + "/main.lua";
f = fopen(main_script.c_str(), "w");
fprintf(f, "return require 'mylib'");
fclose(f);
int ret = run(runner, main_script.c_str(), nullptr);
EXPECT_EQ(ret, 0) << "Require failed, maybe pkgpath not set correctly. Errno: " << get_errno();
char* res = getresult(runner);