File: chan_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 (65 lines) | stat: -rw-r--r-- 1,278 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package luar

import (
	"testing"

	"github.com/yuin/gopher-lua"
)

func Test_chan(t *testing.T) {
	L := lua.NewState()
	defer L.Close()

	ch := make(chan string)
	go func() {
		ch <- "Tim"
		name, ok := <-ch
		if name != "John" || !ok {
			t.Fatal("invalid value")
		}

		close(ch)
	}()

	L.SetGlobal("ch", New(L, ch))

	testReturn(t, L, `return ch()`, "Tim", "true")
	testReturn(t, L, `ch("John")`)
	testReturn(t, L, `return ch()`, "nil", "false")
}

type TestChanString chan string

func (*TestChanString) Test() string {
	return "TestChanString.Test"
}

func (TestChanString) Test2() string {
	return "TestChanString.Test2"
}

func Test_chan_pointermethod(t *testing.T) {
	L := lua.NewState()
	defer L.Close()

	a := make(TestChanString)
	b := &a

	L.SetGlobal("b", New(L, b))

	testReturn(t, L, `return b:Test()`, "TestChanString.Test")
	testReturn(t, L, `return b:Test2()`, "TestChanString.Test2")
}

func Test_chan_invaliddirection(t *testing.T) {
	L := lua.NewState()
	defer L.Close()

	ch := make(chan string)

	L.SetGlobal("send", New(L, (chan<- string)(ch)))
	testError(t, L, `send()`, "receive from send-only type chan<- string")

	L.SetGlobal("receive", New(L, (<-chan string)(ch)))
	testError(t, L, `receive("hello")`, "send to receive-only type <-chan string")
}