test_comp.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../index');
  4. const {streamFromArray, delay} = 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([comp(delay(x => x * x), x => 2 * x + 1)]),
  55. output = [];
  56. streamFromArray([1, 2, 3]).pipe(chain);
  57. chain.on('data', value => output.push(value));
  58. chain.on('end', () => {
  59. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  60. async.done();
  61. });
  62. },
  63. function test_compGenerator(t) {
  64. const async = t.startAsync('test_compGenerator');
  65. const chain = new Chain([
  66. comp(
  67. x => x * x,
  68. function*(x) {
  69. yield x;
  70. yield x + 1;
  71. yield x + 2;
  72. },
  73. x => 2 * x + 1
  74. )
  75. ]),
  76. output = [];
  77. streamFromArray([1, 2, 3]).pipe(chain);
  78. chain.on('data', value => output.push(value));
  79. chain.on('end', () => {
  80. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  81. async.done();
  82. });
  83. },
  84. function test_compMany(t) {
  85. const async = t.startAsync('test_compMany');
  86. const chain = new Chain([comp(x => x * x, x => many([x, x + 1, x + 2]), x => 2 * x + 1)]),
  87. output = [];
  88. streamFromArray([1, 2, 3]).pipe(chain);
  89. chain.on('data', value => output.push(value));
  90. chain.on('end', () => {
  91. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  92. async.done();
  93. });
  94. },
  95. function test_compCombined(t) {
  96. const async = t.startAsync('test_compCombined');
  97. const chain = new Chain([
  98. comp(
  99. delay(x => -x),
  100. x => many([x, x * 10]),
  101. function*(x) {
  102. yield x;
  103. yield x - 1;
  104. },
  105. x => -x
  106. )
  107. ]),
  108. output = [];
  109. streamFromArray([1, 2]).pipe(chain);
  110. chain.on('data', value => output.push(value));
  111. chain.on('end', () => {
  112. eval(t.TEST('t.unify(output, [1, 2, 10, 11, 2, 3, 20, 21])'));
  113. async.done();
  114. });
  115. },
  116. function test_compCombinedFinal(t) {
  117. const async = t.startAsync('test_compCombinedFinal');
  118. const chain = new Chain([
  119. comp(
  120. delay(x => -x),
  121. x => many([x, x * 10]),
  122. function*(x) {
  123. yield x;
  124. yield final(x - 1);
  125. },
  126. x => -x
  127. )
  128. ]),
  129. output = [];
  130. streamFromArray([1, 2]).pipe(chain);
  131. chain.on('data', value => output.push(value));
  132. chain.on('end', () => {
  133. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  134. async.done();
  135. });
  136. },
  137. function test_compAsFun(t) {
  138. const async = t.startAsync('test_compAsFun');
  139. const chain = new Chain([
  140. comp.asFun(
  141. delay(x => -x),
  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. ]);