127 lines
4 KiB
JavaScript
127 lines
4 KiB
JavaScript
import { Stream } from '../src/iterator.js'
|
|
import { describe, it } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
|
|
describe('Stream class', () => {
|
|
it('should iterate a range', () => {
|
|
const stream = new Stream([1, 2, 3])
|
|
|
|
assert.equal(stream.index, 0)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 1, done: false })
|
|
assert.equal(stream.index, 1)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 2, done: false })
|
|
assert.equal(stream.index, 2)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 3, done: false })
|
|
assert.equal(stream.index, 3)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: undefined, done: true })
|
|
assert.equal(stream.index, 3)
|
|
})
|
|
|
|
it('should handle an empty stream', () => {
|
|
const stream = new Stream([])
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: undefined, done: true })
|
|
assert.equal(stream.index, 0)
|
|
})
|
|
|
|
it('should handle a single item', () => {
|
|
const stream = new Stream([1])
|
|
|
|
assert.equal(stream.index, 0)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 1, done: false })
|
|
assert.equal(stream.index, 1)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: undefined, done: true })
|
|
assert.equal(stream.index, 1)
|
|
})
|
|
|
|
it('should should an independent stream', () => {
|
|
const stream = new Stream([1, 2, 3])
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 1, done: false })
|
|
assert.equal(stream.index, 1)
|
|
|
|
const clone = stream.clone()
|
|
assert.equal(clone.index, 1)
|
|
|
|
assert.deepStrictEqual(clone.next(), { value: 2, done: false })
|
|
assert.equal(clone.index, 2)
|
|
|
|
assert.deepStrictEqual(clone.next(), { value: 3, done: false })
|
|
assert.equal(clone.index, 3)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 2, done: false })
|
|
assert.equal(stream.index, 2)
|
|
})
|
|
|
|
it('should support multiple clones', () => {
|
|
const stream = new Stream('abcdefg')
|
|
stream.next()
|
|
stream.next()
|
|
|
|
assert.deepStrictEqual(stream.index, 2)
|
|
|
|
const clone1 = stream.clone()
|
|
assert.deepStrictEqual(clone1.index, 2)
|
|
|
|
stream.next()
|
|
stream.next()
|
|
assert.deepStrictEqual(stream.index, 4)
|
|
|
|
const clone2 = stream.clone()
|
|
assert.deepStrictEqual(clone2.index, 4)
|
|
|
|
assert.deepStrictEqual(clone1.next(), { value: 'c', done: false })
|
|
assert.deepStrictEqual(clone1.next(), { value: 'd', done: false })
|
|
assert.deepStrictEqual(clone1.next(), { value: 'e', done: false })
|
|
assert.deepStrictEqual(clone1.index, 5)
|
|
|
|
assert.deepStrictEqual(clone2.next(), { value: 'e', done: false })
|
|
assert.deepStrictEqual(clone2.next(), { value: 'f', done: false })
|
|
assert.deepStrictEqual(clone2.index, 6)
|
|
|
|
assert.deepStrictEqual(stream.next(), { value: 'e', done: false })
|
|
assert.deepStrictEqual(stream.index, 5)
|
|
})
|
|
|
|
it('should yield a value on consume', () => {
|
|
const stream = new Stream('abc')
|
|
assert.deepStrictEqual(stream.consume().unwrap(), 'a')
|
|
assert.deepStrictEqual(stream.consume().unwrap(), 'b')
|
|
assert.deepStrictEqual(stream.consume().unwrap(), 'c')
|
|
assert(stream.consume().isNone())
|
|
})
|
|
|
|
it('should take from a stream', () => {
|
|
const stream5 = new Stream([1, 2, 3, 4, 5])
|
|
const take3 = stream5.take(3)
|
|
assert.deepStrictEqual(Array.from(take3), [1, 2, 3])
|
|
|
|
assert.deepStrictEqual(stream5.index, 3)
|
|
assert.deepStrictEqual(Array.from(stream5), [4, 5])
|
|
|
|
const emptyStream = new Stream([])
|
|
const take0 = emptyStream.take(3)
|
|
assert.deepStrictEqual(Array.from(take0), [])
|
|
assert.deepStrictEqual(emptyStream.index, 0)
|
|
assert.deepStrictEqual(Array.from(emptyStream), [])
|
|
|
|
const streamDefault = new Stream('abc')
|
|
const genDefault = streamDefault.take()
|
|
assert.deepStrictEqual(Array.from(genDefault), ['a'])
|
|
assert.deepStrictEqual(streamDefault.index, 1)
|
|
assert.deepStrictEqual(Array.from(streamDefault), ['b', 'c'])
|
|
|
|
const stream3 = new Stream([10, 20, 30])
|
|
const genZero = streamDefault.take(0)
|
|
assert.deepStrictEqual(Array.from(genZero), [])
|
|
assert.deepStrictEqual(stream3.index, 0)
|
|
assert.deepStrictEqual(Array.from(stream3), [10, 20, 30])
|
|
})
|
|
})
|
|
|