Debian Codegraph @

chan_test.go

raw
1 package luar
2
3 import (
4 "testing"
5
6 "github.com/yuin/gopher-lua"
7 )
8
9 func Test_chan(t *testing.T) {
10 L := lua.NewState()
11 defer L.Close()
12
13 ch := make(chan string)
14 go func() {
15 ch <- "Tim"
16 name, ok := <-ch
17 if name != "John" || !ok {
18 t.Fatal("invalid value")
19 }
20
21 close(ch)
22 }()
23
24 L.SetGlobal("ch", New(L, ch))
25
26 testReturn(t, L, `return ch()`, "Tim", "true")
27 testReturn(t, L, `ch("John")`)
28 testReturn(t, L, `return ch()`, "nil", "false")
29 }
30
31 type TestChanString chan string
32
33 func (*TestChanString) Test() string {
34 return "TestChanString.Test"
35 }
36
37 func (TestChanString) Test2() string {
38 return "TestChanString.Test2"
39 }
40
41 func Test_chan_pointermethod(t *testing.T) {
42 L := lua.NewState()
43 defer L.Close()
44
45 a := make(TestChanString)
46 b := &a
47
48 L.SetGlobal("b", New(L, b))
49
50 testReturn(t, L, `return b:Test()`, "TestChanString.Test")
51 testReturn(t, L, `return b:Test2()`, "TestChanString.Test2")
52 }
53
54 func Test_chan_invaliddirection(t *testing.T) {
55 L := lua.NewState()
56 defer L.Close()
57
58 ch := make(chan string)
59
60 L.SetGlobal("send", New(L, (chan<- string)(ch)))
61 testError(t, L, `send()`, "receive from send-only type chan<- string")
62
63 L.SetGlobal("receive", New(L, (<-chan string)(ch)))
64 testError(t, L, `receive("hello")`, "send to receive-only type <-chan string")
65 }