File: type_test.go

package info (click to toggle)
golang-layeh-gopher-luar 1.0.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 224 kB
  • sloc: makefile: 7
file content (42 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download | duplicates (2)
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
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))
	}
}