test_gen.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 {none, finalValue, many, gen} = Chain;
  7. unit.add(module, [
  8. function test_gen(t) {
  9. const async = t.startAsync('test_gen');
  10. const output = [],
  11. chain = new Chain([
  12. fromIterable([1, 2, 3]),
  13. gen(
  14. x => x * x,
  15. x => 2 * x + 1
  16. ),
  17. streamToArray(output)
  18. ]);
  19. chain.on('end', () => {
  20. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  21. async.done();
  22. });
  23. },
  24. function test_genFinal(t) {
  25. const async = t.startAsync('test_genFinal');
  26. const output = [],
  27. chain = new Chain([
  28. fromIterable([1, 2, 3]),
  29. gen(
  30. x => x * x,
  31. x => finalValue(x),
  32. x => 2 * x + 1
  33. ),
  34. streamToArray(output)
  35. ]);
  36. chain.on('end', () => {
  37. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  38. async.done();
  39. });
  40. },
  41. function test_compNothing(t) {
  42. const async = t.startAsync('test_compNothing');
  43. const output = [],
  44. chain = new Chain([
  45. fromIterable([1, 2, 3]),
  46. gen(
  47. x => x * x,
  48. () => none,
  49. x => 2 * x + 1
  50. ),
  51. streamToArray(output)
  52. ]);
  53. chain.on('end', () => {
  54. eval(t.TEST('t.unify(output, [])'));
  55. async.done();
  56. });
  57. },
  58. function test_genEmpty(t) {
  59. const async = t.startAsync('test_genEmpty');
  60. const output = [],
  61. chain = new Chain([fromIterable([1, 2, 3]), x => x * x, gen(), streamToArray(output)]);
  62. chain.on('end', () => {
  63. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  64. async.done();
  65. });
  66. },
  67. function test_genAsync(t) {
  68. const async = t.startAsync('test_genAsync');
  69. const output = [],
  70. chain = new Chain([
  71. fromIterable([1, 2, 3]),
  72. gen(
  73. delay(x => x * x),
  74. x => 2 * x + 1
  75. ),
  76. streamToArray(output)
  77. ]);
  78. chain.on('end', () => {
  79. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  80. async.done();
  81. });
  82. },
  83. function test_genGenerator(t) {
  84. const async = t.startAsync('test_genGenerator');
  85. const output = [],
  86. chain = new Chain([
  87. fromIterable([1, 2, 3]),
  88. gen(
  89. x => x * x,
  90. function* (x) {
  91. yield x;
  92. yield x + 1;
  93. yield x + 2;
  94. },
  95. x => 2 * x + 1
  96. ),
  97. streamToArray(output)
  98. ]);
  99. chain.on('end', () => {
  100. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  101. async.done();
  102. });
  103. },
  104. function test_genMany(t) {
  105. const async = t.startAsync('test_genMany');
  106. const output = [],
  107. chain = new Chain([
  108. fromIterable([1, 2, 3]),
  109. gen(
  110. x => x * x,
  111. x => many([x, x + 1, x + 2]),
  112. x => 2 * x + 1
  113. ),
  114. streamToArray(output)
  115. ]);
  116. chain.on('end', () => {
  117. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  118. async.done();
  119. });
  120. },
  121. function test_genCombined(t) {
  122. const async = t.startAsync('test_genCombined');
  123. const output = [],
  124. chain = new Chain([
  125. fromIterable([1, 2]),
  126. gen(
  127. delay(x => -x),
  128. x => many([x, x * 10]),
  129. function* (x) {
  130. yield x;
  131. yield x - 1;
  132. },
  133. x => -x
  134. ),
  135. streamToArray(output)
  136. ]);
  137. chain.on('end', () => {
  138. eval(t.TEST('t.unify(output, [1, 2, 10, 11, 2, 3, 20, 21])'));
  139. async.done();
  140. });
  141. },
  142. function test_genCombinedFinal(t) {
  143. const async = t.startAsync('test_genCombinedFinal');
  144. const output = [],
  145. chain = new Chain([
  146. fromIterable([1, 2]),
  147. gen(
  148. delay(x => -x),
  149. x => many([x, x * 10]),
  150. function* (x) {
  151. yield x;
  152. yield finalValue(x - 1);
  153. },
  154. x => -x
  155. ),
  156. streamToArray(output)
  157. ]);
  158. chain.on('end', () => {
  159. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  160. async.done();
  161. });
  162. },
  163. function test_genSyncIterator(t) {
  164. const async = t.startAsync('test_genSyncIterator');
  165. const output = [],
  166. chain = new Chain([
  167. fromIterable([1, 2]),
  168. gen(
  169. delay(x => -x),
  170. x => many([x, x * 10]),
  171. function* (x) {
  172. yield x;
  173. yield finalValue(x - 1);
  174. },
  175. x => -x
  176. ),
  177. streamToArray(output)
  178. ]);
  179. chain.on('end', () => {
  180. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  181. async.done();
  182. });
  183. },
  184. function test_genAsyncIterator(t) {
  185. const async = t.startAsync('test_genAsyncIterator');
  186. const output = [],
  187. chain = new Chain([
  188. fromIterable([1, 2]),
  189. gen(
  190. delay(x => -x),
  191. x => many([x, x * 10]),
  192. async function* (x) {
  193. yield delay(x => x)(x);
  194. yield delay(x => finalValue(x - 1))(x);
  195. },
  196. x => -x
  197. ),
  198. streamToArray(output)
  199. ]);
  200. chain.on('end', () => {
  201. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  202. async.done();
  203. });
  204. }
  205. ]);