test_simple.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../main');
  4. const {streamFromArray, streamToArray} = require('./helper');
  5. const {Transform} = require('stream');
  6. unit.add(module, [
  7. function test_simple(t) {
  8. const async = t.startAsync('test_simple');
  9. const chain = new Chain([x => x * x]),
  10. output1 = [],
  11. output2 = [];
  12. streamFromArray([1, 2, 3]).pipe(chain.input);
  13. chain.output.pipe(streamToArray(output1));
  14. chain.on('data', value => output2.push(value));
  15. chain.on('end', () => {
  16. eval(t.TEST('t.unify(output1, [1, 4, 9])'));
  17. eval(t.TEST('t.unify(output2, [1, 4, 9])'));
  18. async.done();
  19. });
  20. },
  21. function test_gen(t) {
  22. const async = t.startAsync('test_gen');
  23. const chain = new Chain([
  24. function*(x) {
  25. yield x * x;
  26. yield x * x * x;
  27. return 2 * x;
  28. }
  29. ]),
  30. output = [];
  31. streamFromArray([1, 2, 3]).pipe(chain.input);
  32. chain.on('data', value => output.push(value));
  33. chain.on('end', () => {
  34. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  35. async.done();
  36. });
  37. },
  38. function test_async(t) {
  39. const async = t.startAsync('test_async');
  40. const chain = new Chain([
  41. async x =>
  42. new Promise(resolve => {
  43. setTimeout(() => resolve(x + 1), 20);
  44. })
  45. ]),
  46. output = [];
  47. streamFromArray([1, 2, 3]).pipe(chain.input);
  48. chain.on('data', value => output.push(value));
  49. chain.on('end', () => {
  50. eval(t.TEST('t.unify(output, [2, 3, 4])'));
  51. async.done();
  52. });
  53. },
  54. function test_array(t) {
  55. const async = t.startAsync('test_array');
  56. const chain = new Chain([x => [x * x, x * x * x, 2 * x]]),
  57. output = [];
  58. streamFromArray([1, 2, 3]).pipe(chain.input);
  59. chain.on('data', value => output.push(value));
  60. chain.on('end', () => {
  61. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  62. async.done();
  63. });
  64. },
  65. function test_chain(t) {
  66. const async = t.startAsync('test_chain');
  67. const chain = new Chain([x => x * x, x => 2 * x + 1]),
  68. output = [];
  69. streamFromArray([1, 2, 3]).pipe(chain.input);
  70. chain.on('data', value => output.push(value));
  71. chain.on('end', () => {
  72. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  73. async.done();
  74. });
  75. },
  76. function test_stream(t) {
  77. const async = t.startAsync('test_stream');
  78. const chain = new Chain([
  79. new Transform({
  80. objectMode: true,
  81. transform(x, _, callback) {
  82. callback(null, x * x);
  83. }
  84. }),
  85. x => 2 * x + 1
  86. ]),
  87. output = [];
  88. streamFromArray([1, 2, 3]).pipe(chain.input);
  89. chain.on('data', value => output.push(value));
  90. chain.on('end', () => {
  91. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  92. async.done();
  93. });
  94. }
  95. ]);