test_simple.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../index');
  4. const {streamFromArray, streamToArray} = 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([
  42. async x =>
  43. await new Promise(resolve => {
  44. setTimeout(() => resolve(x + 1), 20);
  45. })
  46. ]),
  47. output = [];
  48. streamFromArray([1, 2, 3]).pipe(chain);
  49. chain.on('data', value => output.push(value));
  50. chain.on('end', () => {
  51. eval(t.TEST('t.unify(output, [2, 3, 4])'));
  52. async.done();
  53. });
  54. },
  55. function test_simpleArray(t) {
  56. const async = t.startAsync('test_simpleArray');
  57. const chain = new Chain([x => [x * x, x * x * x, 2 * x]]),
  58. output = [];
  59. streamFromArray([1, 2, 3]).pipe(chain);
  60. chain.on('data', value => output.push(value));
  61. chain.on('end', () => {
  62. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  63. async.done();
  64. });
  65. },
  66. function test_simpleMany(t) {
  67. const async = t.startAsync('test_simpleMany');
  68. const chain = new Chain([x => Chain.many([x * x, x * x * x, 2 * x])]),
  69. output = [];
  70. streamFromArray([1, 2, 3]).pipe(chain);
  71. chain.on('data', value => output.push(value));
  72. chain.on('end', () => {
  73. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  74. async.done();
  75. });
  76. },
  77. function test_simpleChain(t) {
  78. const async = t.startAsync('test_simpleChain');
  79. const chain = new Chain([x => x * x, x => 2 * x + 1]),
  80. output = [];
  81. streamFromArray([1, 2, 3]).pipe(chain);
  82. chain.on('data', value => output.push(value));
  83. chain.on('end', () => {
  84. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  85. async.done();
  86. });
  87. },
  88. function test_simpleStream(t) {
  89. const async = t.startAsync('test_simpleStream');
  90. const chain = new Chain([
  91. new Transform({
  92. objectMode: true,
  93. transform(x, _, callback) {
  94. callback(null, x * x);
  95. }
  96. }),
  97. x => 2 * x + 1
  98. ]),
  99. output = [];
  100. streamFromArray([1, 2, 3]).pipe(chain);
  101. chain.on('data', value => output.push(value));
  102. chain.on('end', () => {
  103. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  104. async.done();
  105. });
  106. },
  107. function test_simpleFactory(t) {
  108. const async = t.startAsync('test_simpleChain');
  109. const chain = Chain.chain([x => x * x, x => 2 * x + 1]),
  110. output = [];
  111. streamFromArray([1, 2, 3]).pipe(chain);
  112. chain.on('data', value => output.push(value));
  113. chain.on('end', () => {
  114. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  115. async.done();
  116. });
  117. }
  118. ]);