test_simple.js 3.3 KB

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