Debian Codegraph @

config_test.go

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