-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
287 lines (251 loc) · 6.99 KB
/
index.spec.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import * as luacoro from './index'
export interface Test {
wait?: number
value: number
}
function* first (): luacoro.Iterator<Test> {
yield { value: 1, wait: 1 } // wait 1 frame with yielding an object at the first frame
yield // wait 1 frame
yield undefined // wait 1 frame
yield null // wait 1 frame
yield { value: 2, wait: 2 } // wait 2 frames with yielding an object at the first frame
return second() // go to another iterator with terminating this iterator
}
function* second (): luacoro.Iterator<Test> {
yield { value: 3 }
yield third() // go to another iterator and come back after it terminates
yield { value: 6 }
// returns to caller at the end of function
// (but in this case, the iterator stops because 'first' is already terminated)
}
function* third (): luacoro.Iterator<Test> {
const y = yield add(1, 2) // go to 'add' and come back with receiving the returned value
y.value++
yield y // wait 1 frame with yielding an object at the first frame
yield 2 // wait 2 frames
yield { value: 5 } // wait 2 frames with yielding an object at the first frame
// returns to caller at the end of function
}
function* add (a: number, b: number): luacoro.Iterator<Test> {
return { value: a + b } // return value for 'third' (not for resume())
}
describe('Coroutine', () => {
it('runs in the expected order', () => {
const expected = [
{ value: 1, wait: 1 },
null,
null,
null,
{ value: 2, wait: 2 },
null,
{ value: 3 },
{ value: 4 },
null,
null,
{ value: 5 },
{ value: 6 },
null, // 'second' is terminated
null // returns null forever after the coroutine stops
]
const c = luacoro.create(first())
for (let i = 0; i < expected.length; i++) {
const msg = `iteration #${i}`
if (i < expected.length - 1) {
expect(c.isAlive).toBeTruthy(msg)
} else {
expect(c.isAlive).toBeFalsy(msg)
}
expect(c.resume()).toEqual(expected[i], msg)
}
})
it('returns null after stopped', () => {
const c = luacoro.create(first())
expect(c.isAlive).toBeTruthy()
expect(c.resume()).toBeTruthy()
expect(c.isAlive).toBeTruthy()
c.stop()
expect(c.isAlive).toBeFalsy()
expect(c.resume()).toBeNull()
expect(c.isAlive).toBeFalsy()
expect(c.resume()).toBeNull()
})
it('receives and returns the expected values', () => {
let actual = ''
function* g (): luacoro.Iterator<string> {
actual += (yield '1')
actual += (yield '2')
actual += (yield '3')
}
const c = luacoro.create(g())
expect(c.resume()).toEqual('1')
expect(actual).toEqual('')
expect(c.resume('a')).toEqual('2')
expect(actual).toEqual('a')
expect(c.resume('b')).toEqual('3')
expect(actual).toEqual('ab')
expect(c.resume('c')).toBeNull()
expect(actual).toEqual('abc')
})
it('handles error', () => {
let result = ''
function* second (): luacoro.Iterator<{}> {
throw new Error('an error')
}
function* first (): luacoro.Iterator<{}> {
try {
yield second()
} catch (e) {
result += 'caught '
}
yield second()
}
const c = luacoro.create(first())
try {
c.resume()
} catch (e) {
result += e.message
}
expect(result).toEqual('caught an error')
})
})
const generators = [
function* (): Iterator<string> {
yield 'a'
return 'b'
},
function* (): Iterator<string> {
yield 'A'
yield 'B'
return 'C'
},
function* (): Iterator<string> {
return '1'
}
]
describe('Coroutine created by "concurrent"', () => {
it('runs well', () => {
const c = luacoro.concurrent(generators.map(g => luacoro.create(g())))
const actual = []
for (let i = 0; c.isAlive && i < 9; i++) {
if (i === 2) {
c.add(function* (): Iterator<string> {
yield 'x'
yield 'y'
return 'z'
})
}
if (i === 4) {
c.add(function* (): Iterator<string> {
return '!'
})
c.add(function* (): Iterator<string> {
yield 'X'
yield 'Y'
return 'Z'
})
}
if (i === 8) {
c.add(function* (): Iterator<string> {
return '?'
})
}
actual.push(c.resume().map(s => s || '-').join(''))
}
expect(actual.join(' ')).toEqual('aA1 bB Cx y z!X Y Z ?')
})
})
describe('Coroutine created by "all"', () => {
it('runs until the all iterators are dead', () => {
const c = luacoro.all(generators.map(g => luacoro.create(g())))
const actual = []
for (let i = 0; c.isAlive; i++) {
if (i === 2) {
c.add(function* (): Iterator<string> {
yield 'X'
return 'Y'
})
}
actual.push(c.resume().map(s => s || '-').join(''))
}
expect(actual.join(' ')).toEqual('aA1 bB- -C-X ---Y')
})
})
describe('Coroutine created by "race"', () => {
it('runs until one of the iterators is dead', () => {
const c = luacoro.race(generators.map(g => luacoro.create(g())))
const actual = []
while (c.isAlive) {
actual.push(c.resume().map(s => s || '-').join(''))
}
expect(actual.join(' ')).toEqual('aA1')
})
})
describe('Coroutine created by "forever"', () => {
it('runs forever', () => {
const c = luacoro.forever(function* (): Iterator<string> {
yield '1'
yield '2'
})
const actual = []
for (let i = 0; i < 10; i++) {
actual.push(c.resume())
}
expect(actual.join('')).toEqual('1212121212')
})
it('is prevented from the infinite loop without yielding', () => {
const c = luacoro.forever(function* (): Iterator<string> {
return
})
expect(c.resume()).toBeNull()
})
})
describe('Defer', () => {
function* sub (out: string[]) {
luacoro.defer(() => out.push('sd'))
out.push('s')
}
function* main (i: number, out: string[]) {
luacoro.defer(() => out.push('d1'))
out.push('1')
if (i === 0) return
yield sub(out)
if (i === 1) throw new Error('error')
luacoro.defer(() => out.push('d2'))
out.push('2')
}
function* test () {
const result = []
yield main(0, result)
yield result.join(' ')
result.push('|')
try {
yield main(1, result)
} catch (e) {
yield result.join(' ')
result.push('|')
}
yield main(2, result)
return result.join(' ')
}
it('works', () => {
const c = luacoro.create(test())
expect(c.resume()).toEqual('1 d1')
expect(c.resume()).toEqual('1 d1 | 1 s sd d1')
expect(c.resume()).toEqual('1 d1 | 1 s sd d1 | 1 s sd 2 d2 d1')
})
it('handles error', () => {
const c = luacoro.create(function* () {
luacoro.defer(() => {
throw new Error('error overridden by defer')
})
throw new Error('error')
})
let result = ''
try {
c.resume()
} catch (e) {
result = e.message
}
expect(result).toEqual('error overridden by defer')
})
})