t.test('stream factory: concat', async t => { const s = stream(counterGenerator()) .concat(counterGenerator(), 666, [1, 2], counterGenerator()); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 1, 2, 0, 1, 2, 666, [1, 2], 0, 1, 2], 'should have seen the concatenated stream'); t.equal(result.length, 11, 'should have seen 11 iterations'); });
t.test('stream factory: slice (one argument)', async t => { const s = stream(counterGenerator(5)) .slice(2); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [2, 3, 4], 'should have seen starting from third item to the end'); t.equal(result.length, 3, 'should have seen 3 iterations'); });
t.test('stream factory: filterMap', async t => { const s = stream([counterGenerator(), counterGenerator(5)]) .flatMap(i => i * i); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 1, 4, 0, 1, 4, 9, 16], 'should have seen the flattened squared numbers'); t.equal(result.length, 8, 'should have seen 8 iterations'); });
t.deepEqual(result, [666, [1, 2], {foo: 'bar'}, 0, 1, 2, 'foo'], 'should have seen the flattened items'); }); t.deepEqual(result, [0, 1, 2, 0], 'should have collected the flattened items'); t.equal(to3.done, false, 'should have been exhausted regularly'); t.equal(to3Bis.done, true, 'should have called the return hook');
t.deepEqual(result, [0, 2], 'should have kept odd numbers'); t.deepEqual(result, [0, 2], 'should have kept odd numbers'); t.deepEqual(result, [0, 2], 'should have kept odd numbers again'); t.equal(i, 4, 'should have seen 6 iterations'); t.deepEqual(result, [0, 2], 'should have kept odd numbers only'); t.deepEqual(result, [0, 2], 'should have kept odd numbers only'); }); })
t.deepEqual(result, [1, 2, 3], 'should have seen the items'); }); t.deepEqual(result, [1, 2, 3, 4], 'should have gone to the end of the stream'); }); t.deepEqual(result, [0, 1, 2, 3, 4], 'should have seen all the items'); }); t.deepEqual(result, [1, 2, 3], 'should have seen the items'); }); t.deepEqual(result, [1, 2, 3, 4], 'should have gone to the end of the stream'); }); t.deepEqual(result, [0, 1, 2, 3, 4], 'should have seen all the items'); }); t.deepEqual(result, [1, 2, 3]); }); t.deepEqual(result, [1, 2, 3, 1, 2, 3]); });
t.deepEqual(result, [0, 'foo', ['bar'], false]); }); t.deepEqual(result, [666, 0, 1, 2, ['bar'], false]); }); t.deepEqual(result, [666, 0, 1, 2, ['bar'], false]); t.deepEqual(result, [666, 0, 1, 2, ['bar'], false]); }); t.deepEqual(result, [0], 'should have iterated only on the first item of the sub stream'); t.equal(done, true, 'should have called the return hook of the source stream');
t.test('stream factory: filter', async t => { const s = stream(counterGenerator()) .filter(i => i % 2 === 0); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 2], 'should have seen the odd numbers only'); t.equal(result.length, 2, 'should have seen 2 iterations'); });
t.test('stream factory: slice (two arguments)', async t => { const s = stream(counterGenerator(5)) .slice(2, 3); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [2], 'should have seen starting from third item ending to the third'); t.equal(result.length, 1, 'should have seen 1 iteration'); });