test-gen.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. 'use strict';
  2. import test from 'tape-six';
  3. import {streamToArray, delay} from './helpers.mjs';
  4. import chain, {none, finalValue, many, gen} from '../src/index.js';
  5. import fromIterable from '../src/utils/fromIterable.js';
  6. test.asPromise('gen: smoke test', (t, resolve) => {
  7. const output = [],
  8. c = chain([
  9. fromIterable([1, 2, 3]),
  10. gen(
  11. x => x * x,
  12. x => 2 * x + 1
  13. ),
  14. streamToArray(output)
  15. ]);
  16. c.on('end', () => {
  17. t.deepEqual(output, [3, 9, 19]);
  18. resolve();
  19. });
  20. });
  21. test.asPromise('gen: final', (t, resolve) => {
  22. const output = [],
  23. c = chain([
  24. fromIterable([1, 2, 3]),
  25. gen(
  26. x => x * x,
  27. x => finalValue(x),
  28. x => 2 * x + 1
  29. ),
  30. streamToArray(output)
  31. ]);
  32. c.on('end', () => {
  33. t.deepEqual(output, [1, 4, 9]);
  34. resolve();
  35. });
  36. });
  37. test.asPromise('gen: nothing', (t, resolve) => {
  38. const output = [],
  39. c = chain([
  40. fromIterable([1, 2, 3]),
  41. gen(
  42. x => x * x,
  43. () => none,
  44. x => 2 * x + 1
  45. ),
  46. streamToArray(output)
  47. ]);
  48. c.on('end', () => {
  49. t.deepEqual(output, []);
  50. resolve();
  51. });
  52. });
  53. test.asPromise('gen: empty', (t, resolve) => {
  54. const output = [],
  55. c = chain([fromIterable([1, 2, 3]), x => x * x, gen(), streamToArray(output)]);
  56. c.on('end', () => {
  57. t.deepEqual(output, [1, 4, 9]);
  58. resolve();
  59. });
  60. });
  61. test.asPromise('gen: async', (t, resolve) => {
  62. const output = [],
  63. c = chain([
  64. fromIterable([1, 2, 3]),
  65. gen(
  66. delay(x => x * x),
  67. x => 2 * x + 1
  68. ),
  69. streamToArray(output)
  70. ]);
  71. c.on('end', () => {
  72. t.deepEqual(output, [3, 9, 19]);
  73. resolve();
  74. });
  75. });
  76. test.asPromise('gen: generator', (t, resolve) => {
  77. const output = [],
  78. c = chain([
  79. fromIterable([1, 2, 3]),
  80. gen(
  81. x => x * x,
  82. function* (x) {
  83. yield x;
  84. yield x + 1;
  85. yield x + 2;
  86. },
  87. x => 2 * x + 1
  88. ),
  89. streamToArray(output)
  90. ]);
  91. c.on('end', () => {
  92. t.deepEqual(output, [3, 5, 7, 9, 11, 13, 19, 21, 23]);
  93. resolve();
  94. });
  95. });
  96. test.asPromise('gen: many', (t, resolve) => {
  97. const output = [],
  98. c = chain([
  99. fromIterable([1, 2, 3]),
  100. gen(
  101. x => x * x,
  102. x => many([x, x + 1, x + 2]),
  103. x => 2 * x + 1
  104. ),
  105. streamToArray(output)
  106. ]);
  107. c.on('end', () => {
  108. t.deepEqual(output, [3, 5, 7, 9, 11, 13, 19, 21, 23]);
  109. resolve();
  110. });
  111. });
  112. test.asPromise('gen: combined', (t, resolve) => {
  113. const output = [],
  114. c = chain([
  115. fromIterable([1, 2]),
  116. gen(
  117. delay(x => -x),
  118. x => many([x, x * 10]),
  119. function* (x) {
  120. yield x;
  121. yield x - 1;
  122. },
  123. x => -x
  124. ),
  125. streamToArray(output)
  126. ]);
  127. c.on('end', () => {
  128. t.deepEqual(output, [1, 2, 10, 11, 2, 3, 20, 21]);
  129. resolve();
  130. });
  131. });
  132. test.asPromise('gen: combined final', (t, resolve) => {
  133. const output = [],
  134. c = chain([
  135. fromIterable([1, 2]),
  136. gen(
  137. delay(x => -x),
  138. x => many([x, x * 10]),
  139. function* (x) {
  140. yield x;
  141. yield finalValue(x - 1);
  142. },
  143. x => -x
  144. ),
  145. streamToArray(output)
  146. ]);
  147. c.on('end', () => {
  148. t.deepEqual(output, [1, -2, 10, -11, 2, -3, 20, -21]);
  149. resolve();
  150. });
  151. });
  152. test.asPromise('gen: iterator', (t, resolve) => {
  153. const output = [],
  154. c = chain([
  155. fromIterable([1, 2]),
  156. gen(
  157. delay(x => -x),
  158. x => many([x, x * 10]),
  159. function* (x) {
  160. yield x;
  161. yield finalValue(x - 1);
  162. },
  163. x => -x
  164. ),
  165. streamToArray(output)
  166. ]);
  167. c.on('end', () => {
  168. t.deepEqual(output, [1, -2, 10, -11, 2, -3, 20, -21]);
  169. resolve();
  170. });
  171. });
  172. test.asPromise('gen: async iterator', (t, resolve) => {
  173. const output = [],
  174. c = chain([
  175. fromIterable([1, 2]),
  176. gen(
  177. delay(x => -x),
  178. x => many([x, x * 10]),
  179. async function* (x) {
  180. yield delay(x => x)(x);
  181. yield delay(x => finalValue(x - 1))(x);
  182. },
  183. x => -x
  184. ),
  185. streamToArray(output)
  186. ]);
  187. c.on('end', () => {
  188. t.deepEqual(output, [1, -2, 10, -11, 2, -3, 20, -21]);
  189. resolve();
  190. });
  191. });