test_comp.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 comp = require('../utils/comp');
  7. const asFun = require('../utils/asFun');
  8. const {none, final, many} = Chain;
  9. unit.add(module, [
  10. function test_comp(t) {
  11. const async = t.startAsync('test_comp');
  12. const output = [],
  13. chain = new Chain([fromIterable([1, 2, 3]), comp(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_compFinal(t) {
  20. const async = t.startAsync('test_compFinal');
  21. const output = [],
  22. chain = new Chain([
  23. fromIterable([1, 2, 3]),
  24. comp(x => x * x, x => final(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]), comp(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_compEmpty(t) {
  42. const async = t.startAsync('test_compEmpty');
  43. const output = [],
  44. chain = new Chain([fromIterable([1, 2, 3]), x => x * x, comp(), streamToArray(output)]);
  45. chain.on('end', () => {
  46. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  47. async.done();
  48. });
  49. },
  50. function test_compAsync(t) {
  51. const async = t.startAsync('test_compAsync');
  52. const output = [],
  53. chain = new Chain([fromIterable([1, 2, 3]), comp(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_compGenerator(t) {
  60. const async = t.startAsync('test_compGenerator');
  61. const output = [],
  62. chain = new Chain([
  63. fromIterable([1, 2, 3]),
  64. comp(
  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_compMany(t) {
  81. const async = t.startAsync('test_compMany');
  82. const output = [],
  83. chain = new Chain([
  84. fromIterable([1, 2, 3]),
  85. comp(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_compCombined(t) {
  94. const async = t.startAsync('test_compCombined');
  95. const output = [],
  96. chain = new Chain([
  97. fromIterable([1, 2]),
  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. 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_compCombinedFinal(t) {
  115. const async = t.startAsync('test_compCombinedFinal');
  116. const output = [],
  117. chain = new Chain([
  118. fromIterable([1, 2]),
  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. 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_compAsFun(t) {
  136. const async = t.startAsync('test_compAsFun');
  137. const output = [],
  138. chain = new Chain([
  139. fromIterable([1, 2]),
  140. 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. 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. ]);