test_transducers.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const unit = require('heya-unit');
  3. const {streamToArray} = require('./helpers');
  4. const Chain = require('../src/index');
  5. const {fromIterable} = require('../src/utils/FromIterable');
  6. const {gen} = Chain;
  7. unit.add(module, [
  8. function test_transducers(t) {
  9. const async = t.startAsync('test_transducers');
  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_transducersFinal(t) {
  25. const async = t.startAsync('test_transducersFinal');
  26. const output = [],
  27. chain = new Chain([
  28. fromIterable([1, 2, 3]),
  29. gen(
  30. x => x * x,
  31. x => Chain.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_transducersNothing(t) {
  42. const async = t.startAsync('test_transducersNothing');
  43. const output = [],
  44. chain = new Chain([
  45. fromIterable([1, 2, 3]),
  46. gen(
  47. x => x * x,
  48. () => Chain.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_transducersEmpty(t) {
  59. const async = t.startAsync('test_transducersEmpty');
  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_transducersOne(t) {
  68. const async = t.startAsync('test_transducersOne');
  69. const output = [],
  70. chain = new Chain([
  71. fromIterable([1, 2, 3]),
  72. x => x * x,
  73. gen(x => 2 * x + 1),
  74. streamToArray(output)
  75. ]);
  76. chain.on('end', () => {
  77. eval(t.TEST('t.unify(output, [3, 9, 19])'));
  78. async.done();
  79. });
  80. }
  81. ]);