Debian Codegraph @

interp/interp_chan_test.go

raw symbols SCIP
1 package interp_test
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/traefik/yaegi/interp"
8 )
9
10 // IntChan is a channel type alias for testing.
11 type IntChan chan int
12
13 // NewIntChan creates a new IntChan.
14 func NewIntChan() IntChan {
15 return make(IntChan, 1)
16 }
17
18 func TestSendToBinaryChannelTypeAlias(t *testing.T) {
19 i := interp.New(interp.Options{})
20
21 err := i.Use(interp.Exports{
22 "mypkg/mypkg": {
23 "IntChan": reflect.ValueOf((*IntChan)(nil)),
24 "NewIntChan": reflect.ValueOf(NewIntChan),
25 },
26 })
27 if err != nil {
28 t.Fatal(err)
29 }
30
31 _, err = i.Eval(`
32 package main
33
34 import "mypkg"
35
36 func main() {
37 ch := mypkg.NewIntChan()
38 ch <- 42
39 val := <-ch
40 if val != 42 {
41 panic("unexpected value")
42 }
43 }
44 `)
45 if err != nil {
46 t.Fatal(err)
47 }
48 }
49
50 func TestSendToSourceDefinedChannel(t *testing.T) {
51 i := interp.New(interp.Options{})
52
53 // Test with a channel defined purely in interpreted code
54 _, err := i.Eval(`
55 package main
56
57 func main() {
58 ch := make(chan int, 1)
59 ch <- 42
60 val := <-ch
61 if val != 42 {
62 panic("unexpected value")
63 }
64 }
65 `)
66 if err != nil {
67 t.Fatal(err)
68 }
69 }
70
71 func TestSendToSourceDefinedChannelTypeAlias(t *testing.T) {
72 i := interp.New(interp.Options{})
73
74 // Test with a channel type alias defined in interpreted code
75 _, err := i.Eval(`
76 package main
77
78 type MyChan chan int
79
80 func main() {
81 ch := make(MyChan, 1)
82 ch <- 42
83 val := <-ch
84 if val != 42 {
85 panic("unexpected value")
86 }
87 }
88 `)
89 if err != nil {
90 t.Fatal(err)
91 }
92 }