75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/mattn/anko/env"
|
|
)
|
|
|
|
func TestAnkointerceptor_ExecRequire(t *testing.T) {
|
|
// 创建测试环境
|
|
e := env.NewEnv()
|
|
|
|
// 创建拦截器实例
|
|
interceptor := NewAnkointerceptor(e)
|
|
_, file, _, _ := runtime.Caller(0)
|
|
scriptPath := filepath.Join(filepath.Dir(file), "testdata/test_caller.ank")
|
|
v, _ := interceptor.Exec(scriptPath)
|
|
if v != "Hello, tom from test script" {
|
|
t.Errorf("Not equal")
|
|
}
|
|
|
|
}
|
|
|
|
func TestAnkointerceptor_loadlib(t *testing.T) {
|
|
e := env.NewEnv()
|
|
interceptor := NewAnkointerceptor(e)
|
|
_, file, _, _ := runtime.Caller(0)
|
|
scriptPath := filepath.Join(filepath.Dir(file), "testdata/test_loadso.ank")
|
|
v, _ := interceptor.Exec(scriptPath)
|
|
if v != "Hello, world!" {
|
|
t.Errorf("Loadso test failed")
|
|
}
|
|
}
|
|
|
|
func TestAnkointerceptor_RegistModule(t *testing.T) {
|
|
e := env.NewEnv()
|
|
interceptor := NewAnkointerceptor(e)
|
|
_, file, _, _ := runtime.Caller(0)
|
|
scriptPath := filepath.Join(filepath.Dir(file), "testdata/test_injectmodule.ank")
|
|
m := make(map[string]interface{})
|
|
m["Hi"] = func() string {
|
|
return "Hello, world!"
|
|
}
|
|
interceptor.RegistModule("inject", m)
|
|
v, _ := interceptor.Exec(scriptPath)
|
|
if v != "Hello, world!" {
|
|
t.Errorf("Inject module test failed")
|
|
}
|
|
}
|
|
|
|
type TestStruct struct {
|
|
Name string
|
|
}
|
|
|
|
func (t *TestStruct) Hi() string {
|
|
return fmt.Sprintf("Hello, %s!", t.Name)
|
|
}
|
|
|
|
func TestAnkointerceptor_RegistStructAsModule(t *testing.T) {
|
|
e := env.NewEnv()
|
|
interceptor := NewAnkointerceptor(e)
|
|
_, file, _, _ := runtime.Caller(0)
|
|
scriptPath := filepath.Join(filepath.Dir(file), "testdata/test_injectmodule.ank")
|
|
|
|
interceptor.RegistStructAsModule("inject", &TestStruct{"world"})
|
|
v, _ := interceptor.Exec(scriptPath)
|
|
if v != "Hello, world!" {
|
|
t.Errorf("Inject module test failed")
|
|
}
|
|
|
|
}
|