2023-11-02 13:57:11 +09:00
|
|
|
import ava from 'ava'
|
2023-09-20 17:18:01 +09:00
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
const { Fib, Fib2, Fib3 } = (await import('../index.js')).default
|
2022-05-06 18:40:46 +09:00
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
const test = process.env.WASI_TEST ? ava.skip : ava
|
2022-05-06 18:40:46 +09:00
|
|
|
|
2022-05-10 22:46:16 +09:00
|
|
|
for (const [index, factory] of [
|
|
|
|
() => new Fib(),
|
|
|
|
() => Fib2.create(0),
|
|
|
|
() => new Fib3(0, 1),
|
|
|
|
].entries()) {
|
|
|
|
test(`should be able to stop a generator #${index}`, (t) => {
|
|
|
|
const fib = factory()
|
|
|
|
const gen = fib[Symbol.iterator]
|
|
|
|
t.is(typeof gen, 'function')
|
|
|
|
const iterator = gen()
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: false,
|
|
|
|
value: 1,
|
|
|
|
})
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: false,
|
|
|
|
value: 8,
|
|
|
|
})
|
|
|
|
t.deepEqual(iterator.return?.(), {
|
|
|
|
done: true,
|
|
|
|
})
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: true,
|
|
|
|
})
|
2022-05-06 18:40:46 +09:00
|
|
|
})
|
|
|
|
|
2022-05-10 22:46:16 +09:00
|
|
|
test(`should be able to throw to generator #${index}`, (t) => {
|
|
|
|
const fib = factory()
|
|
|
|
const gen = fib[Symbol.iterator]
|
|
|
|
t.is(typeof gen, 'function')
|
|
|
|
const iterator = gen()
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: false,
|
|
|
|
value: 1,
|
|
|
|
})
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
iterator.next()
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: false,
|
|
|
|
value: 8,
|
|
|
|
})
|
|
|
|
t.throws(() => iterator.throw!(new Error()))
|
|
|
|
t.deepEqual(iterator.next(), {
|
|
|
|
done: true,
|
|
|
|
})
|
2022-05-06 18:40:46 +09:00
|
|
|
})
|
2022-05-10 22:46:16 +09:00
|
|
|
}
|