test: add TestCallPluginFunction + final run - all 13 tests pass

- TestCallPluginFunction: verifies DoString calls Lua functions correctly
  - add(3,4) → 7, greet('World') → 'Hello, World', get_table() → table
- Full suite: 13 tests, 0 failures
- go build ./... clean
This commit is contained in:
mirivlad 2026-06-07 16:41:47 +08:00
parent a1d7c7b88b
commit 7b9c9647ac
1 changed files with 83 additions and 0 deletions

View File

@ -390,3 +390,86 @@ func copyDir(src, dst string) error {
return os.WriteFile(target, data, info.Mode())
})
}
// TestCallPluginFunction verifies that DoString can call Lua functions
// and return results through the VM.
func TestCallPluginFunction(t *testing.T) {
dir := t.TempDir()
// Create a simple plugin with a global function
pluginDir := filepath.Join(dir, "testfunc")
if err := os.MkdirAll(pluginDir, 0755); err != nil {
t.Fatal(err)
}
luaCode := `
function add(a, b)
return a + b
end
function greet(name)
return "Hello, " .. name
end
function get_table()
return { x = 42, y = "test" }
end
`
if err := os.WriteFile(filepath.Join(pluginDir, "main.lua"), []byte(luaCode), 0644); err != nil {
t.Fatal(err)
}
p := &Plugin{
Meta: Meta{
Name: "testfunc",
Hooks: map[string]string{
"on_install": "on_install",
},
},
Dir: pluginDir,
DataDir: filepath.Join(dir, "data"),
Active: true,
HasInstall: true,
}
os.MkdirAll(p.DataDir, 0755)
vm, err := NewLuaVM(p)
if err != nil {
t.Fatalf("NewLuaVM: %v", err)
}
defer vm.Close()
// Load main.lua
if err := vm.LoadScript("main.lua"); err != nil {
t.Fatalf("LoadScript: %v", err)
}
// Test 1: add(3, 4) → "7"
result, err := vm.DoString("return add(3, 4)")
if err != nil {
t.Fatalf("DoString(add): %v", err)
}
if result != "7" {
t.Errorf("add(3,4) = %q, want %q", result, "7")
}
// Test 2: greet("World") → "Hello, World"
result, err = vm.DoString("return greet('World')")
if err != nil {
t.Fatalf("DoString(greet): %v", err)
}
if result != "Hello, World" {
t.Errorf("greet('World') = %q, want %q", result, "Hello, World")
}
// Test 3: get_table() → table string (just verify no error)
result, err = vm.DoString("return get_table()")
if err != nil {
t.Fatalf("DoString(get_table): %v", err)
}
if result == "" || result == "nil" {
t.Errorf("get_table() returned empty/nil: %q", result)
}
t.Logf("DoString tests passed: add=%q, greet=%q, get_table=%q", result, result, result)
}