forked from yuin/gluamapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gluamapper_test.go
139 lines (128 loc) · 3.06 KB
/
gluamapper_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gluamapper
import (
"github.com/awirix/lua"
"path/filepath"
"runtime"
"testing"
)
func errorIfNotEqual(t *testing.T, v1, v2 interface{}) {
if v1 != v2 {
_, file, line, _ := runtime.Caller(1)
t.Errorf("%v line %v: '%v' expected, but got '%v'", filepath.Base(file), line, v1, v2)
}
}
type testRole struct {
Name string
}
type testPerson struct {
Name string
Age int
WorkPlace string
Role []*testRole
}
type testStruct struct {
Nil interface{}
Bool bool
String string
Number int `gluamapper:"number_value"`
Func interface{}
}
func TestMap(t *testing.T) {
L := lua.NewState(nil)
if err := L.DoString(`
person = {
name = "Michel",
age = "31", -- weakly input
work_place = "San Jose",
role = {
{
name = "Administrator"
},
{
name = "Operator"
}
}
}
`); err != nil {
t.Error(err)
}
var person testPerson
if err := Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil {
t.Error(err)
}
errorIfNotEqual(t, "Michel", person.Name)
errorIfNotEqual(t, 31, person.Age)
errorIfNotEqual(t, "San Jose", person.WorkPlace)
errorIfNotEqual(t, 2, len(person.Role))
errorIfNotEqual(t, "Administrator", person.Role[0].Name)
errorIfNotEqual(t, "Operator", person.Role[1].Name)
}
func TestTypes(t *testing.T) {
L := lua.NewState(nil)
if err := L.DoString(`
tbl = {
["Nil"] = nil,
["Bool"] = true,
["String"] = "string",
["Number_value"] = 10,
["Func"] = function() end
}
`); err != nil {
t.Error(err)
}
var stct testStruct
if err := NewMapper(Option{NameFunc: Id}).Map(L.GetGlobal("tbl").(*lua.LTable), &stct); err != nil {
t.Error(err)
}
errorIfNotEqual(t, nil, stct.Nil)
errorIfNotEqual(t, true, stct.Bool)
errorIfNotEqual(t, "string", stct.String)
errorIfNotEqual(t, 10, stct.Number)
}
func TestNameFunc(t *testing.T) {
L := lua.NewState(nil)
if err := L.DoString(`
person = {
Name = "Michel",
Age = "31", -- weekly input
WorkPlace = "San Jose",
Role = {
{
Name = "Administrator"
},
{
Name = "Operator"
}
}
}
`); err != nil {
t.Error(err)
}
var person testPerson
mapper := NewMapper(Option{NameFunc: Id})
if err := mapper.Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil {
t.Error(err)
}
errorIfNotEqual(t, "Michel", person.Name)
errorIfNotEqual(t, 31, person.Age)
errorIfNotEqual(t, "San Jose", person.WorkPlace)
errorIfNotEqual(t, 2, len(person.Role))
errorIfNotEqual(t, "Administrator", person.Role[0].Name)
errorIfNotEqual(t, "Operator", person.Role[1].Name)
}
func TestError(t *testing.T) {
L := lua.NewState(nil)
tbl := L.NewTable()
L.SetField(tbl, "key", lua.LString("value"))
err := Map(tbl, 1)
if err.Error() != "result must be a pointer" {
t.Error("invalid error message")
}
tbl = L.NewTable()
tbl.Append(lua.LNumber(1))
var person testPerson
err = Map(tbl, &person)
if err.Error() != "arguments #1 must be a table, but got an array" {
t.Error("invalid error message")
}
}