-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule_go_test.go
127 lines (120 loc) · 2.22 KB
/
module_go_test.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package engine
import (
"github.com/ZenLiuCN/fn"
"testing"
"time"
)
func TestChannel_bi(t *testing.T) {
vm := Get()
defer vm.Free()
ch := make(chan int)
vm.Set("ch", NewChan(ch, vm))
fn.Panic1(vm.RunJs(
//language=javascript
`
import {Chan} from 'go'
/** @type {Chan<number>}*/
const cc=ch
cc.recv((v)=>console.log(v)).then(()=>console.log("closed"))
`))
tick := time.Tick(time.Millisecond)
go func() {
i := 0
for {
select {
case <-tick:
close(ch)
return
default:
i++
ch <- i
}
}
}()
vm.Await()
}
func TestChannel_out(t *testing.T) {
vm := Get()
defer vm.Free()
ch := make(chan int)
vm.Set("ch", NewChanWriteOnly(ch))
fn.Panic1(vm.RunJs(
//language=javascript
`
import {WriteOnlyChan} from 'go'
/** @type {WriteOnlyChan<number>}*/
const cc=ch
const i=setInterval(()=>cc.send(1),10)
setTimeout(()=>{
clearInterval(i)
cc.close()
},100)
`))
go func() {
for {
select {
case i, ok := <-ch:
if ok {
println(i)
} else {
return
}
}
}
}()
vm.Await()
}
func TestChannel_in(t *testing.T) {
vm := Get()
defer vm.Free()
ch := make(chan int)
vm.Set("ch", NewChanReadOnly(ch, vm))
fn.Panic1(vm.RunJs(
//language=javascript
`
import {ReadOnlyChan} from 'go'
/** @type {ReadOnlyChan<number>}*/
const cc=ch
cc.recv((v)=>console.log(v)).then(()=>console.log("closed"))
`))
tick := time.Tick(time.Millisecond)
go func() {
i := 0
for {
select {
case <-tick:
close(ch)
return
default:
i++
ch <- i
}
}
}()
vm.Await()
}
func TestConvert(t *testing.T) {
vm := Get()
defer vm.Free()
fn.Panic1(vm.RunTs(
//language=typescript
`
import {runesFromString, stringFromRunes, typeOf, usageOf} from 'go'
const r=runesFromString("123ABCΔ")
console.log(r);
console.log(stringFromRunes(r))
r.push(49)
console.log(stringFromRunes(r))
const type=typeOf(r)
console.log(type)
console.log(type.valid())
console.log(typeOf(null).valid())
const usage=usageOf(type)
console.log(usage.id().identity())
const slice=usage.slice()
console.log(slice)
slice.push(r)
console.log(slice)
`))
vm.Await()
}