test_fun.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const {streamToArray, delay} = require('./helpers');
  4. const chain = require('../src/index');
  5. const fromIterable = require('../src/utils/FromIterable');
  6. const fun = require('../src/fun');
  7. const {none, finalValue, many} = chain;
  8. unit.add(module, [
  9. function test_fun(t) {
  10. const async = t.startAsync('test_fun');
  11. const output = [],
  12. c = chain([fromIterable([1, 2, 3]), fun(x => x * x, x => 2 * x + 1), streamToArray(output)]);
  13. c.on('end', () => {
  14. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  15. async.done();
  16. });
  17. },
  18. function test_funFinal(t) {
  19. const async = t.startAsync('test_funFinal');
  20. const output = [],
  21. c = chain([
  22. fromIterable([1, 2, 3]),
  23. fun(x => x * x, x => finalValue(x), x => 2 * x + 1),
  24. streamToArray(output)
  25. ]);
  26. c.on('end', () => {
  27. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  28. async.done();
  29. });
  30. },
  31. function test_funNothing(t) {
  32. const async = t.startAsync('test_funNothing');
  33. const output = [],
  34. c = chain([fromIterable([1, 2, 3]), fun(x => x * x, () => none, x => 2 * x + 1), streamToArray(output)]);
  35. c.on('end', () => {
  36. eval(t.TEST('t.unify(output, [])'));
  37. async.done();
  38. });
  39. },
  40. function test_funEmpty(t) {
  41. const async = t.startAsync('test_funEmpty');
  42. const output = [],
  43. c = chain([fromIterable([1, 2, 3]), x => x * x, fun(), streamToArray(output)]);
  44. c.on('end', () => {
  45. eval(t.TEST('t.unify(output, [1, 4, 9])'));
  46. async.done();
  47. });
  48. },
  49. function test_funAsync(t) {
  50. const async = t.startAsync('test_funAsync');
  51. const output = [],
  52. c = chain([fromIterable([1, 2, 3]), fun(delay(x => x * x), x => 2 * x + 1), streamToArray(output)]);
  53. c.on('end', () => {
  54. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  55. async.done();
  56. });
  57. },
  58. function test_funGenerator(t) {
  59. const async = t.startAsync('test_funGenerator');
  60. const output = [],
  61. c = chain([
  62. fromIterable([1, 2, 3]),
  63. fun(
  64. x => x * x,
  65. function*(x) {
  66. yield x;
  67. yield x + 1;
  68. yield x + 2;
  69. },
  70. x => 2 * x + 1
  71. ),
  72. streamToArray(output)
  73. ]);
  74. c.on('end', () => {
  75. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  76. async.done();
  77. });
  78. },
  79. function test_funMany(t) {
  80. const async = t.startAsync('test_funMany');
  81. const output = [],
  82. c = chain([
  83. fromIterable([1, 2, 3]),
  84. fun(x => x * x, x => many([x, x + 1, x + 2]), x => 2 * x + 1),
  85. streamToArray(output)
  86. ]);
  87. c.on('end', () => {
  88. eval(t.TEST('t.unify(output, [3, 5, 7, 9, 11, 13, 19, 21, 23])'));
  89. async.done();
  90. });
  91. },
  92. function test_funCombined(t) {
  93. const async = t.startAsync('test_funCombined');
  94. const output = [],
  95. c = chain([
  96. fromIterable([1, 2]),
  97. fun(
  98. delay(x => -x),
  99. x => many([x, x * 10]),
  100. function*(x) {
  101. yield x;
  102. yield x - 1;
  103. },
  104. x => -x
  105. ),
  106. streamToArray(output)
  107. ]);
  108. c.on('end', () => {
  109. eval(t.TEST('t.unify(output, [1, 2, 10, 11, 2, 3, 20, 21])'));
  110. async.done();
  111. });
  112. },
  113. function test_funCombinedFinal(t) {
  114. const async = t.startAsync('test_funCombinedFinal');
  115. const output = [],
  116. c = chain([
  117. fromIterable([1, 2]),
  118. fun(
  119. delay(x => -x),
  120. x => many([x, x * 10]),
  121. function*(x) {
  122. yield x;
  123. yield finalValue(x - 1);
  124. },
  125. x => -x
  126. ),
  127. streamToArray(output)
  128. ]);
  129. c.on('end', () => {
  130. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  131. async.done();
  132. });
  133. },
  134. function test_funAsFun(t) {
  135. const async = t.startAsync('test_funAsFun');
  136. const output = [],
  137. c = chain([
  138. fromIterable([1, 2]),
  139. fun(
  140. delay(x => -x),
  141. x => many([x, x * 10]),
  142. function*(x) {
  143. yield x;
  144. yield finalValue(x - 1);
  145. },
  146. x => -x
  147. ),
  148. streamToArray(output)
  149. ]);
  150. c.on('end', () => {
  151. eval(t.TEST('t.unify(output, [1, -2, 10, -11, 2, -3, 20, -21])'));
  152. async.done();
  153. });
  154. }
  155. ]);