forked from layeh/gopher-luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_test.go
51 lines (37 loc) · 1.24 KB
/
type_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
package luar
import (
"testing"
"github.com/yuin/gopher-lua"
)
func Test_type_slice(t *testing.T) {
L := lua.NewState()
defer L.Close()
type ints []int
L.SetGlobal("newInts", NewType(L, ints{}))
testReturn(t, L, `ints = newInts(1); return #ints`, "1")
testReturn(t, L, `ints = newInts(0, 10); return #ints`, "0")
}
func Test_type(t *testing.T) {
L := lua.NewState()
defer L.Close()
tim := &StructTestPerson{
Name: "Tim",
}
L.SetGlobal("user1", New(L, tim))
L.SetGlobal("Person", NewType(L, StructTestPerson{}))
L.SetGlobal("People", NewType(L, map[string]*StructTestPerson{}))
testReturn(t, L, `user2 = Person(); user2.Name = "John"; user2.Friend = user1`)
testReturn(t, L, `return user2.Name`, "John")
testReturn(t, L, `return user2.Friend.Name`, "Tim")
testReturn(t, L, `everyone = People(); everyone["tim"] = user1; everyone["john"] = user2`)
everyone := L.GetGlobal("everyone").(*lua.LUserData).Value.(map[string]*StructTestPerson)
if len(everyone) != 2 {
t.Fatalf("expecting len(everyone) = 2, got %d", len(everyone))
}
}
func Test_type_metatable(t *testing.T) {
L := lua.NewState()
defer L.Close()
L.SetGlobal("newInt", NewType(L, int(0)))
testReturn(t, L, `return getmetatable(newInt) == "gopher-luar"`, "true")
}