test_comp.js 5.2 KB

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