test_simple.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const {Transform} = require('stream');
  4. const {streamToArray, delay} = require('./helpers');
  5. const chain = require('../src/index');
  6. const fromIterable = require('../src/utils/FromIterable');
  7. unit.add(module, [
  8. function test_simpleGeneric(t) {
  9. const async = t.startAsync('test_simpleGeneric');
  10. const c = chain([x => x * x]),
  11. output1 = [],
  12. output2 = [];
  13. fromIterable([1, 2, 3]).pipe(c).pipe(streamToArray(output1));
  14. c.on('data', value => output2.push(value));
  15. c.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_simpleGenerator(t) {
  22. const async = t.startAsync('test_simpleGenerator');
  23. const output = [],
  24. c = chain([
  25. fromIterable([1, 2, 3]),
  26. function* (x) {
  27. yield x * x;
  28. yield x * x * x;
  29. yield 2 * x;
  30. },
  31. streamToArray(output)
  32. ]);
  33. c.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_simpleAsync(t) {
  39. const async = t.startAsync('test_simpleAsync');
  40. const output = [],
  41. c = chain([fromIterable([1, 2, 3]), delay(x => x + 1), streamToArray(output)]);
  42. c.on('end', () => {
  43. eval(t.TEST('t.unify(output, [2, 3, 4])'));
  44. async.done();
  45. });
  46. },
  47. function test_simpleMany(t) {
  48. const async = t.startAsync('test_simpleMany');
  49. const output = [],
  50. c = chain([
  51. fromIterable([1, 2, 3]),
  52. x => chain.many([x * x, x * x * x, 2 * x]),
  53. streamToArray(output)
  54. ]);
  55. c.on('end', () => {
  56. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  57. async.done();
  58. });
  59. },
  60. function test_simpleChain(t) {
  61. const async = t.startAsync('test_simpleChain');
  62. const output = [],
  63. c = chain([
  64. fromIterable([1, 2, 3]),
  65. x => x * x,
  66. x => 2 * x + 1,
  67. streamToArray(output)
  68. ]);
  69. c.on('end', () => {
  70. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  71. async.done();
  72. });
  73. },
  74. function test_simpleStream(t) {
  75. const async = t.startAsync('test_simpleStream');
  76. const output = [],
  77. c = chain([
  78. fromIterable([1, 2, 3]),
  79. new Transform({
  80. objectMode: true,
  81. transform(x, _, callback) {
  82. callback(null, x * x);
  83. }
  84. }),
  85. x => 2 * x + 1,
  86. streamToArray(output)
  87. ]);
  88. c.on('end', () => {
  89. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  90. async.done();
  91. });
  92. },
  93. function test_simpleFactory(t) {
  94. const async = t.startAsync('test_simpleChain');
  95. const output = [],
  96. c = chain([
  97. fromIterable([1, 2, 3]),
  98. x => x * x,
  99. x => 2 * x + 1,
  100. streamToArray(output)
  101. ]);
  102. c.on('end', () => {
  103. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  104. async.done();
  105. });
  106. },
  107. function test_simpleIterable(t) {
  108. const async = t.startAsync('test_simpleIterable');
  109. const output = [],
  110. c = chain([
  111. [1, 2, 3],
  112. function* (x) {
  113. yield x * x;
  114. yield x * x * x;
  115. yield 2 * x;
  116. },
  117. streamToArray(output)
  118. ]);
  119. c.on('end', () => {
  120. eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
  121. async.done();
  122. });
  123. c.end(0); // start the chain
  124. }
  125. ]);