1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
package luar
import (
"reflect"
"testing"
"github.com/yuin/gopher-lua"
"strings"
)
func Test_config(t *testing.T) {
L := lua.NewState()
defer L.Close()
config := GetConfig(L)
if config == nil {
t.Fatal("expecting non-nil config")
}
if config.FieldNames != nil {
t.Fatal("expected config.FieldName to be nil")
}
if config.MethodNames != nil {
t.Fatal("expected config.MethodName to be nil")
}
}
func Test_config_fieldnames(t *testing.T) {
L := lua.NewState()
defer L.Close()
config := GetConfig(L)
config.FieldNames = func(s reflect.Type, f reflect.StructField) []string {
return []string{strings.ToLower(f.Name)}
}
type S struct {
Name string
Age int `luar:"AGE"`
}
s := S{
Name: "Tim",
Age: 89,
}
L.SetGlobal("s", New(L, &s))
testReturn(t, L, `return s.Name`, `nil`)
testReturn(t, L, `return s.name`, `Tim`)
testReturn(t, L, `return s.AGE`, `nil`)
testReturn(t, L, `return s.age`, `89`)
}
type TestConfigMethodnames []string
func (t TestConfigMethodnames) Len() int {
return len(t)
}
func Test_config_methodnames(t *testing.T) {
L := lua.NewState()
defer L.Close()
config := GetConfig(L)
config.MethodNames = func(s reflect.Type, m reflect.Method) []string {
return []string{strings.ToLower(m.Name) + "gth"}
}
v := TestConfigMethodnames{
"hello",
"world",
}
L.SetGlobal("v", New(L, v))
testError(t, L, `return v:len()`, `attempt to call a non-function object`)
testReturn(t, L, `return v:length()`, `2`)
}
|