push.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var pipeline = require('../');
  2. var through = require('through2');
  3. var split = require('split');
  4. var test = require('tape');
  5. test('push', function (t) {
  6. var expected = {};
  7. expected.first = [ 333, 444, 555, 666, 777 ];
  8. expected.second = [ 6.66, 7.77 ];
  9. expected.output = [ 3.33, 4.44, 5.55, 3, 2 ];
  10. t.plan(5 + 2 + 5 + 3);
  11. var a = split();
  12. var b = through.obj(function (row, enc, next) {
  13. this.push(JSON.parse(row));
  14. next();
  15. });
  16. var c = through.obj(function (row, enc, next) { this.push(row.x); next() });
  17. var d = through.obj(function (x, enc, next) { this.push(x * 111); next() });
  18. var first = through.obj(function (row, enc, next) {
  19. if (expected.first.length === 2) {
  20. t.equal(p.length, 5);
  21. p.push(second);
  22. t.equal(p.length, 6);
  23. }
  24. var ex = expected.first.shift();
  25. t.deepEqual(row, ex);
  26. this.push(row / 100);
  27. next();
  28. });
  29. var second = through.obj(function (row, enc, next) {
  30. var ex = expected.second.shift();
  31. t.deepEqual(row, ex);
  32. this.push(Math.floor(10 - row));
  33. next();
  34. });
  35. var p = pipeline.obj([ a, b, c, d, first ]);
  36. t.equal(p.length, 5);
  37. p.pipe(through.obj(function (row, enc, next) {
  38. var ex = expected.output.shift();
  39. t.deepEqual(row, ex);
  40. next();
  41. }));
  42. p.write('{"x":3}\n');
  43. p.write('{"x":4}\n');
  44. p.write('{"x":5}\n');
  45. p.write('{"x":6}\n');
  46. p.write('{"x":7}');
  47. p.end();
  48. });