test_gen.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const Chain = require('../index');
  4. const {streamToArray, delay} = require('./helpers');
  5. const {fromIterable} = require('../utils/FromIterable');
  6. const gen = require('../utils/gen');
  7. const asGen = require('../utils/asGen');
  8. const {none, finalValue, many} = Chain;
  9. unit.add(module, [
  10. function test_gen(t) {
  11. const async = t.startAsync('test_gen');
  12. const output = [],
  13. chain = new Chain([fromIterable([1, 2, 3]), gen(x => x * x, x => 2 * x + 1), streamToArray(output)]);
  14. chain.on('end', () => {
  15. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  16. async.done();
  17. });
  18. },
  19. function test_genFinal(t) {
  20. const async = t.startAsync('test_genFinal');
  21. const output = [],
  22. chain = new Chain([
  23. fromIterable([1, 2, 3]),
  24. gen(x => x * x, x => finalValue(x), x => 2 * x + 1),
  25. streamToArray(output)
  26. ]);
  27. chain.on('end', () => {
  28. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  29. async.done();
  30. });
  31. },
  32. function test_compNothing(t) {
  33. const async = t.startAsync('test_compNothing');
  34. const output = [],
  35. chain = new Chain([fromIterable([1, 2, 3]), gen(x => x * x, () => none, x => 2 * x + 1), streamToArray(output)]);
  36. chain.on('end', () => {
  37. eval(t.TEST('t.unify(output, [])'));
  38. async.done();
  39. });
  40. },
  41. function test_genEmpty(t) {
  42. const async = t.startAsync('test_genEmpty');
  43. const output = [],
  44. chain = new Chain([fromIterable([1, 2, 3]), x => x * x, gen(), streamToArray(output)]);
  45. chain.on('end', () => {
  46. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  47. async.done();
  48. });
  49. },
  50. function test_genAsync(t) {
  51. const async = t.startAsync('test_genAsync');
  52. const output = [],
  53. chain = new Chain([fromIterable([1, 2, 3]), gen(delay(x => x * x), x => 2 * x + 1), streamToArray(output)]);
  54. chain.on('end', () => {
  55. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  56. async.done();
  57. });
  58. },
  59. function test_genGenerator(t) {
  60. const async = t.startAsync('test_genGenerator');
  61. const output = [],
  62. chain = new Chain([
  63. fromIterable([1, 2, 3]),
  64. gen(
  65. x => x * x,
  66. function*(x) {
  67. yield x;
  68. yield x + 1;
  69. yield x + 2;
  70. },
  71. x => 2 * x + 1
  72. ),
  73. streamToArray(output)
  74. ]);
  75. chain.on('end', () => {
  76. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  77. async.done();
  78. });
  79. },
  80. function test_genMany(t) {
  81. const async = t.startAsync('test_genMany');
  82. const output = [],
  83. chain = new Chain([
  84. fromIterable([1, 2, 3]),
  85. gen(x => x * x, x => many([x, x + 1, x + 2]), x => 2 * x + 1),
  86. streamToArray(output)
  87. ]);
  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_genCombined(t) {
  94. const async = t.startAsync('test_genCombined');
  95. const output = [],
  96. chain = new Chain([
  97. fromIterable([1, 2]),
  98. gen(
  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. streamToArray(output)
  108. ]);
  109. chain.on('end', () => {
  110. eval(t.TEST('t.unify(output, [1, 2, 10, 11, 2, 3, 20, 21])'));
  111. async.done();
  112. });
  113. },
  114. function test_genCombinedFinal(t) {
  115. const async = t.startAsync('test_genCombinedFinal');
  116. const output = [],
  117. chain = new Chain([
  118. fromIterable([1, 2]),
  119. gen(
  120. delay(x => -x),
  121. x => many([x, x * 10]),
  122. function*(x) {
  123. yield x;
  124. yield finalValue(x - 1);
  125. },
  126. x => -x
  127. ),
  128. streamToArray(output)
  129. ]);
  130. chain.on('end', () => {
  131. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  132. async.done();
  133. });
  134. },
  135. function test_genAsGen(t) {
  136. const async = t.startAsync('test_genAsGen');
  137. const output = [],
  138. chain = new Chain([
  139. fromIterable([1, 2]),
  140. asGen(
  141. delay(x => -x),
  142. x => many([x, x * 10]),
  143. function*(x) {
  144. yield x;
  145. yield finalValue(x - 1);
  146. },
  147. x => -x
  148. ),
  149. streamToArray(output)
  150. ]);
  151. chain.on('end', () => {
  152. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  153. async.done();
  154. });
  155. },
  156. function test_genAsAsyncGen(t) {
  157. const async = t.startAsync('test_genAsAsyncGen');
  158. const output = [],
  159. chain = new Chain([
  160. fromIterable([1, 2]),
  161. asGen(
  162. delay(x => -x),
  163. x => many([x, x * 10]),
  164. async function*(x) {
  165. yield delay(x => x)(x);
  166. yield delay(x => finalValue(x - 1))(x);
  167. },
  168. x => -x
  169. ),
  170. streamToArray(output)
  171. ]);
  172. chain.on('end', () => {
  173. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  174. async.done();
  175. });
  176. }
  177. ]);