forked from layeh/gopher-luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
96 lines (72 loc) · 1.72 KB
/
map_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
package luar
import (
"testing"
"github.com/yuin/gopher-lua"
)
func Test_map(t *testing.T) {
L := lua.NewState()
defer L.Close()
thangs := map[string]int{
"ABC": 123,
"DEF": 456,
}
L.SetGlobal("thangs", New(L, thangs))
testReturn(t, L, `return thangs.ABC`, "123")
testReturn(t, L, `return thangs.DEF`, "456")
testReturn(t, L, `return thangs.GHI`, "nil")
if err := L.DoString(`thangs.GHI = 789`); err != nil {
t.Fatal(err)
}
testReturn(t, L, `thangs.ABC = nil`)
if v := thangs["GHI"]; v != 789 {
t.Fatalf(`expecting thangs["GHI"] = 789, got %d`, v)
}
if _, ok := thangs["ABC"]; ok {
t.Fatal(`expecting thangs["ABC"] to be unset`)
}
}
func Test_map_iterator(t *testing.T) {
L := lua.NewState()
defer L.Close()
countries := map[string]string{
"JP": "Japan",
"CA": "Canada",
"FR": "France",
}
L.SetGlobal("countries", New(L, countries))
testReturn(t, L, `return #countries`, "3")
const code = `
sorted = {}
for k, v in countries() do
table.insert(sorted, v)
end
table.sort(sorted)`
if err := L.DoString(code); err != nil {
t.Fatal(err)
}
testReturn(t, L, `return #sorted, sorted[1], sorted[2], sorted[3]`, "3", "Canada", "France", "Japan")
}
type TestMapUsers map[uint32]string
func (m TestMapUsers) Find(name string) uint32 {
for id, n := range m {
if name == n {
return id
}
}
return 0
}
func Test_map_methods(t *testing.T) {
L := lua.NewState()
defer L.Close()
type User struct {
Name string
}
users := TestMapUsers{
1: "Tim",
}
L.SetGlobal("users", New(L, users))
testReturn(t, L, `return users[1]`, "Tim")
testReturn(t, L, `return users[3]`, "nil")
testReturn(t, L, `return users:Find("Tim")`, "1")
testReturn(t, L, `return users:Find("Steve")`, "0")
}