napi-rs/examples/napi/__test__/generator.spec.ts
2022-05-06 18:03:04 +08:00

51 lines
1 KiB
TypeScript

import test from 'ava'
import { Fib } from '../index'
test('should be able to stop a generator', (t) => {
const fib = new Fib()
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,
})
})
test('should be able to throw to generator', (t) => {
const fib = new Fib()
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,
})
})