test_simple.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../index');
  4. const {streamFromArray, streamToArray, delay} = require('./helpers');
  5. const {Transform} = require('stream');
  6. unit.add(module, [
  7. function test_simpleGeneric(t) {
  8. const async = t.startAsync('test_simpleGeneric');
  9. const chain = new Chain([x => x * x]),
  10. output1 = [],
  11. output2 = [];
  12. streamFromArray([1, 2, 3])
  13. .pipe(chain)
  14. .pipe(streamToArray(output1));
  15. chain.on('data', value => output2.push(value));
  16. chain.on('end', () => {
  17. eval(t.TEST('t.unify(output1, [1, 4, 9])'));
  18. eval(t.TEST('t.unify(output2, [1, 4, 9])'));
  19. async.done();
  20. });
  21. },
  22. function test_simpleGenerator(t) {
  23. const async = t.startAsync('test_simpleGenerator');
  24. const chain = new Chain([
  25. function*(x) {
  26. yield x * x;
  27. yield x * x * x;
  28. yield 2 * x;
  29. }
  30. ]),
  31. output = [];
  32. streamFromArray([1, 2, 3]).pipe(chain);
  33. chain.on('data', value => output.push(value));
  34. chain.on('end', () => {
  35. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  36. async.done();
  37. });
  38. },
  39. function test_simpleAsync(t) {
  40. const async = t.startAsync('test_simpleAsync');
  41. const chain = new Chain([delay(x => x + 1)]),
  42. output = [];
  43. streamFromArray([1, 2, 3]).pipe(chain);
  44. chain.on('data', value => output.push(value));
  45. chain.on('end', () => {
  46. eval(t.TEST('t.unify(output, [2, 3, 4])'));
  47. async.done();
  48. });
  49. },
  50. function test_simpleArray(t) {
  51. const async = t.startAsync('test_simpleArray');
  52. const chain = new Chain([x => [x * x, x * x * x, 2 * x]]),
  53. output = [];
  54. streamFromArray([1, 2, 3]).pipe(chain);
  55. chain.on('data', value => output.push(value));
  56. chain.on('end', () => {
  57. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  58. async.done();
  59. });
  60. },
  61. function test_simpleMany(t) {
  62. const async = t.startAsync('test_simpleMany');
  63. const chain = new Chain([x => Chain.many([x * x, x * x * x, 2 * x])]),
  64. output = [];
  65. streamFromArray([1, 2, 3]).pipe(chain);
  66. chain.on('data', value => output.push(value));
  67. chain.on('end', () => {
  68. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  69. async.done();
  70. });
  71. },
  72. function test_simpleChain(t) {
  73. const async = t.startAsync('test_simpleChain');
  74. const chain = new Chain([x => x * x, x => 2 * x + 1]),
  75. output = [];
  76. streamFromArray([1, 2, 3]).pipe(chain);
  77. chain.on('data', value => output.push(value));
  78. chain.on('end', () => {
  79. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  80. async.done();
  81. });
  82. },
  83. function test_simpleStream(t) {
  84. const async = t.startAsync('test_simpleStream');
  85. const chain = new Chain([
  86. new Transform({
  87. objectMode: true,
  88. transform(x, _, callback) {
  89. callback(null, x * x);
  90. }
  91. }),
  92. x => 2 * x + 1
  93. ]),
  94. output = [];
  95. streamFromArray([1, 2, 3]).pipe(chain);
  96. chain.on('data', value => output.push(value));
  97. chain.on('end', () => {
  98. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  99. async.done();
  100. });
  101. },
  102. function test_simpleFactory(t) {
  103. const async = t.startAsync('test_simpleChain');
  104. const chain = Chain.chain([x => x * x, x => 2 * x + 1]),
  105. output = [];
  106. streamFromArray([1, 2, 3]).pipe(chain);
  107. chain.on('data', value => output.push(value));
  108. chain.on('end', () => {
  109. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  110. async.done();
  111. });
  112. }
  113. ]);