| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 'use strict';
- const unit = require('heya-unit');
- const Chain = require('../main');
- const {streamFromArray, streamToArray} = require('./helper');
- const {Transform} = require('stream');
- unit.add(module, [
- function test_simple(t) {
- const async = t.startAsync('test_simple');
- const chain = new Chain([x => x * x]),
- output1 = [],
- output2 = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.output.pipe(streamToArray(output1));
- chain.on('data', value => output2.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output1, [1, 4, 9])'));
- eval(t.TEST('t.unify(output2, [1, 4, 9])'));
- async.done();
- });
- },
- function test_gen(t) {
- const async = t.startAsync('test_gen');
- const chain = new Chain([
- function*(x) {
- yield x * x;
- yield x * x * x;
- return 2 * x;
- }
- ]),
- output = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.on('data', value => output.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
- async.done();
- });
- },
- function test_async(t) {
- const async = t.startAsync('test_async');
- const chain = new Chain([
- async x =>
- new Promise(resolve => {
- setTimeout(() => resolve(x + 1), 20);
- })
- ]),
- output = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.on('data', value => output.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output, [2, 3, 4])'));
- async.done();
- });
- },
- function test_array(t) {
- const async = t.startAsync('test_array');
- const chain = new Chain([x => [x * x, x * x * x, 2 * x]]),
- output = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.on('data', value => output.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
- async.done();
- });
- },
- function test_chain(t) {
- const async = t.startAsync('test_chain');
- const chain = new Chain([x => x * x, x => 2 * x + 1]),
- output = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.on('data', value => output.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output, [3, 9, 19])'));
- async.done();
- });
- },
- function test_stream(t) {
- const async = t.startAsync('test_stream');
- const chain = new Chain([
- new Transform({
- objectMode: true,
- transform(x, _, callback) {
- callback(null, x * x);
- }
- }),
- x => 2 * x + 1
- ]),
- output = [];
- streamFromArray([1, 2, 3]).pipe(chain.input);
- chain.on('data', value => output.push(value));
- chain.on('end', () => {
- eval(t.TEST('t.unify(output, [3, 9, 19])'));
- async.done();
- });
- }
- ]);
|