1 Star 0 Fork 0

落雪无痕/gopher-luar

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
type_test.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
Tim Cooper 提交于 2019-04-22 12:50 . hide NewType metatable
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")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/8704612/gopher-luar.git
[email protected]:8704612/gopher-luar.git
8704612
gopher-luar
gopher-luar
master

搜索帮助