test_comp.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../index');
  4. const {streamFromArray} = require('./helpers');
  5. const comp = require('../utils/comp');
  6. const {final, many} = Chain;
  7. const none = final();
  8. unit.add(module, [
  9. function test_comp(t) {
  10. const async = t.startAsync('test_comp');
  11. const chain = new Chain([comp(x => x * x, x => 2 * x + 1)]),
  12. output = [];
  13. streamFromArray([1, 2, 3]).pipe(chain);
  14. chain.on('data', value => output.push(value));
  15. chain.on('end', () => {
  16. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  17. async.done();
  18. });
  19. },
  20. function test_compFinal(t) {
  21. const async = t.startAsync('test_compFinal');
  22. const chain = new Chain([comp(x => x * x, x => final(x), x => 2 * x + 1)]),
  23. output = [];
  24. streamFromArray([1, 2, 3]).pipe(chain);
  25. chain.on('data', value => output.push(value));
  26. chain.on('end', () => {
  27. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  28. async.done();
  29. });
  30. },
  31. function test_compNothing(t) {
  32. const async = t.startAsync('test_compNothing');
  33. const chain = new Chain([comp(x => x * x, () => final(), x => 2 * x + 1)]),
  34. output = [];
  35. streamFromArray([1, 2, 3]).pipe(chain);
  36. chain.on('data', value => output.push(value));
  37. chain.on('end', () => {
  38. eval(t.TEST('t.unify(output, [])'));
  39. async.done();
  40. });
  41. },
  42. function test_compEmpty(t) {
  43. const async = t.startAsync('test_compEmpty');
  44. const chain = new Chain([x => x * x, comp()]),
  45. output = [];
  46. streamFromArray([1, 2, 3]).pipe(chain);
  47. chain.on('data', value => output.push(value));
  48. chain.on('end', () => {
  49. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  50. async.done();
  51. });
  52. },
  53. function test_compAsync(t) {
  54. const async = t.startAsync('test_compAsync');
  55. const chain = new Chain([
  56. comp(
  57. async x =>
  58. await new Promise(resolve => {
  59. setTimeout(() => resolve(x * x), 20);
  60. }),
  61. x => 2 * x + 1
  62. )
  63. ]),
  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, [3, 9, 19])'));
  69. async.done();
  70. });
  71. },
  72. function test_compGenerator(t) {
  73. const async = t.startAsync('test_compGenerator');
  74. const chain = new Chain([
  75. comp(
  76. x => x * x,
  77. function*(x) {
  78. yield x;
  79. yield x + 1;
  80. yield x + 2;
  81. },
  82. x => 2 * x + 1
  83. )
  84. ]),
  85. output = [];
  86. streamFromArray([1, 2, 3]).pipe(chain);
  87. chain.on('data', value => output.push(value));
  88. chain.on('end', () => {
  89. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  90. async.done();
  91. });
  92. },
  93. function test_compMany(t) {
  94. const async = t.startAsync('test_compMany');
  95. const chain = new Chain([
  96. comp(
  97. x => x * x,
  98. x => many([x, x + 1, x + 2]),
  99. x => 2 * x + 1
  100. )
  101. ]),
  102. output = [];
  103. streamFromArray([1, 2, 3]).pipe(chain);
  104. chain.on('data', value => output.push(value));
  105. chain.on('end', () => {
  106. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  107. async.done();
  108. });
  109. },
  110. function test_compCombined(t) {
  111. const async = t.startAsync('test_compCombined');
  112. const chain = new Chain([
  113. comp(
  114. async x =>
  115. await new Promise(resolve => {
  116. setTimeout(() => resolve(-x), 20);
  117. }),
  118. x => many([x, x * 10]),
  119. function*(x) {
  120. yield x;
  121. yield x - 1;
  122. },
  123. x => -x
  124. )
  125. ]),
  126. output = [];
  127. streamFromArray([1, 2]).pipe(chain);
  128. chain.on('data', value => output.push(value));
  129. chain.on('end', () => {
  130. eval(t.TEST('t.unify(output, [1, 2, 10, 11, 2, 3, 20, 21])'));
  131. async.done();
  132. });
  133. },
  134. function test_compCombinedFinal(t) {
  135. const async = t.startAsync('test_compCombinedFinal');
  136. const chain = new Chain([
  137. comp(
  138. async x =>
  139. await new Promise(resolve => {
  140. setTimeout(() => resolve(-x), 20);
  141. }),
  142. x => many([x, x * 10]),
  143. function*(x) {
  144. yield x;
  145. yield final(x - 1);
  146. },
  147. x => -x
  148. )
  149. ]),
  150. output = [];
  151. streamFromArray([1, 2]).pipe(chain);
  152. chain.on('data', value => output.push(value));
  153. chain.on('end', () => {
  154. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  155. async.done();
  156. });
  157. }
  158. ]);