react / wstein / node_modules / browserify / node_modules / labeled-stream-splicer / node_modules / stream-splicer / test / splice.js
80540 viewsvar pipeline = require('../');1var through = require('through2');2var split = require('split');3var concat = require('concat-stream');4var test = require('tape');56test('splice', function (t) {7var expected = {};8expected.replacer = [ '333', '444', '5000', '6000' ];9expected.d = [ 3, 4 ];10expected.thousander = [ 5, 6 ];1112t.plan(4 + 2 + 2 + 1);1314var a = split();15var b = through.obj(function (row, enc, next) {16this.push(JSON.parse(row));17next();18});19var c = through.obj(function (row, enc, next) {20this.push(row.x);21next();22});23var d = through.obj(function (x, enc, next) {24t.equal(x, expected.d.shift(), 'd');25this.push(String(x * 111));26next();27});28var thousander = through.obj(function (x, enc, next) {29t.equal(x, expected.thousander.shift(), 'thousander');30this.push(String(x * 1000));31next();32});3334var replacer = through(function (buf, enc, next) {35var ex = expected.replacer.shift();36t.equal(buf.toString(), ex);37if (expected.replacer.length === 2) {38stream.splice(3, 1, thousander);39}40this.push(buf.toString('hex') + '\n');41next();42});4344var stream = pipeline([ a, b, c, d, replacer ]);45stream.pipe(concat(function (body) {46t.deepEqual(47body.toString(),48'333333\n343434\n35303030\n36303030\n'49);50}));5152stream.write('{"x":3}\n');53stream.write('{"x":4}\n');54stream.write('{"x":5}\n');55stream.write('{"x":6}');56stream.end();57});585960