Browse Source

Updated tests.

Eugene Lazutkin 3 years ago
parent
commit
0c382fccb6
5 changed files with 117 additions and 48 deletions
  1. 2 2
      tests/test_demo.js
  2. 47 19
      tests/test_gen.js
  3. 40 18
      tests/test_simple.js
  4. 26 5
      tests/test_transducers.js
  5. 2 4
      tests/tests.js

+ 2 - 2
tests/test_demo.js

@@ -21,11 +21,11 @@ unit.add(module, [
         // transforms a value
         x => x * x,
         // returns several values
-        x => [x - 1, x, x + 1],
+        x => Chain.many([x - 1, x, x + 1]),
         // waits for an asynchronous operation
         async x => await getTotalFromDatabaseByKey(x),
         // returns multiple values with a generator
-        function*(x) {
+        function* (x) {
           for (let i = x; i > 0; --i) {
             yield i;
           }

+ 47 - 19
tests/test_gen.js

@@ -6,17 +6,22 @@ const Chain = require('../index');
 const {streamToArray, delay} = require('./helpers');
 
 const {fromIterable} = require('../utils/FromIterable');
-const gen = require('../utils/gen');
-const asGen = require('../utils/asGen');
 
-const {none, finalValue, many} = Chain;
+const {none, finalValue, many, gen} = Chain;
 
 unit.add(module, [
   function test_gen(t) {
     const async = t.startAsync('test_gen');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), gen(x => x * x, x => 2 * x + 1), streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        gen(
+          x => x * x,
+          x => 2 * x + 1
+        ),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));
@@ -29,7 +34,11 @@ unit.add(module, [
     const output = [],
       chain = new Chain([
         fromIterable([1, 2, 3]),
-        gen(x => x * x, x => finalValue(x), x => 2 * x + 1),
+        gen(
+          x => x * x,
+          x => finalValue(x),
+          x => 2 * x + 1
+        ),
         streamToArray(output)
       ]);
 
@@ -42,7 +51,15 @@ unit.add(module, [
     const async = t.startAsync('test_compNothing');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), gen(x => x * x, () => none, x => 2 * x + 1), streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        gen(
+          x => x * x,
+          () => none,
+          x => 2 * x + 1
+        ),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [])'));
@@ -64,7 +81,14 @@ unit.add(module, [
     const async = t.startAsync('test_genAsync');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), gen(delay(x => x * x), x => 2 * x + 1), streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        gen(
+          delay(x => x * x),
+          x => 2 * x + 1
+        ),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));
@@ -79,7 +103,7 @@ unit.add(module, [
         fromIterable([1, 2, 3]),
         gen(
           x => x * x,
-          function*(x) {
+          function* (x) {
             yield x;
             yield x + 1;
             yield x + 2;
@@ -100,7 +124,11 @@ unit.add(module, [
     const output = [],
       chain = new Chain([
         fromIterable([1, 2, 3]),
-        gen(x => x * x, x => many([x, x + 1, x + 2]), x => 2 * x + 1),
+        gen(
+          x => x * x,
+          x => many([x, x + 1, x + 2]),
+          x => 2 * x + 1
+        ),
         streamToArray(output)
       ]);
 
@@ -118,7 +146,7 @@ unit.add(module, [
         gen(
           delay(x => -x),
           x => many([x, x * 10]),
-          function*(x) {
+          function* (x) {
             yield x;
             yield x - 1;
           },
@@ -141,7 +169,7 @@ unit.add(module, [
         gen(
           delay(x => -x),
           x => many([x, x * 10]),
-          function*(x) {
+          function* (x) {
             yield x;
             yield finalValue(x - 1);
           },
@@ -155,16 +183,16 @@ unit.add(module, [
       async.done();
     });
   },
-  function test_genAsGen(t) {
-    const async = t.startAsync('test_genAsGen');
+  function test_genSyncIterator(t) {
+    const async = t.startAsync('test_genSyncIterator');
 
     const output = [],
       chain = new Chain([
         fromIterable([1, 2]),
-        asGen(
+        gen(
           delay(x => -x),
           x => many([x, x * 10]),
-          function*(x) {
+          function* (x) {
             yield x;
             yield finalValue(x - 1);
           },
@@ -178,16 +206,16 @@ unit.add(module, [
       async.done();
     });
   },
-  function test_genAsAsyncGen(t) {
-    const async = t.startAsync('test_genAsAsyncGen');
+  function test_genAsyncIterator(t) {
+    const async = t.startAsync('test_genAsyncIterator');
 
     const output = [],
       chain = new Chain([
         fromIterable([1, 2]),
-        asGen(
+        gen(
           delay(x => -x),
           x => many([x, x * 10]),
-          async function*(x) {
+          async function* (x) {
             yield delay(x => x)(x);
             yield delay(x => finalValue(x - 1))(x);
           },

+ 40 - 18
tests/test_simple.js

@@ -15,9 +15,7 @@ unit.add(module, [
       output1 = [],
       output2 = [];
 
-    fromIterable([1, 2, 3])
-      .pipe(chain)
-      .pipe(streamToArray(output1));
+    fromIterable([1, 2, 3]).pipe(chain).pipe(streamToArray(output1));
 
     chain.on('data', value => output2.push(value));
     chain.on('end', () => {
@@ -32,7 +30,7 @@ unit.add(module, [
     const output = [],
       chain = new Chain([
         fromIterable([1, 2, 3]),
-        function*(x) {
+        function* (x) {
           yield x * x;
           yield x * x * x;
           yield 2 * x;
@@ -56,22 +54,15 @@ unit.add(module, [
       async.done();
     });
   },
-  function test_simpleArray(t) {
-    const async = t.startAsync('test_simpleArray');
-
-    const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), x => [x * x, x * x * x, 2 * x], streamToArray(output)]);
-
-    chain.on('end', () => {
-      eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
-      async.done();
-    });
-  },
   function test_simpleMany(t) {
     const async = t.startAsync('test_simpleMany');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), x => Chain.many([x * x, x * x * x, 2 * x]), streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        x => Chain.many([x * x, x * x * x, 2 * x]),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
@@ -82,7 +73,12 @@ unit.add(module, [
     const async = t.startAsync('test_simpleChain');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), x => x * x, x => 2 * x + 1, streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        x => x * x,
+        x => 2 * x + 1,
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));
@@ -114,11 +110,37 @@ unit.add(module, [
     const async = t.startAsync('test_simpleChain');
 
     const output = [],
-      chain = Chain.chain([fromIterable([1, 2, 3]), x => x * x, x => 2 * x + 1, streamToArray(output)]);
+      chain = Chain.chain([
+        fromIterable([1, 2, 3]),
+        x => x * x,
+        x => 2 * x + 1,
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));
       async.done();
     });
+  },
+  function test_simpleIterable(t) {
+    const async = t.startAsync('test_simpleIterable');
+
+    const output = [],
+      chain = new Chain([
+        [1, 2, 3],
+        function* (x) {
+          yield x * x;
+          yield x * x * x;
+          yield 2 * x;
+        },
+        streamToArray(output)
+      ]);
+
+    chain.on('end', () => {
+      eval(t.TEST('t.unify(output, [1, 1, 2, 4, 8, 4, 9, 27, 6])'));
+      async.done();
+    });
+
+    chain.end(0); // start the chain
   }
 ]);

+ 26 - 5
tests/test_transducers.js

@@ -3,6 +3,7 @@
 const unit = require('heya-unit');
 
 const Chain = require('../index');
+const gen = require('../gen');
 const {streamToArray} = require('./helpers');
 const {fromIterable} = require('../utils/FromIterable');
 
@@ -11,7 +12,14 @@ unit.add(module, [
     const async = t.startAsync('test_transducers');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), [x => x * x, x => 2 * x + 1], streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        gen(
+          x => x * x,
+          x => 2 * x + 1
+        ),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));
@@ -24,7 +32,11 @@ unit.add(module, [
     const output = [],
       chain = new Chain([
         fromIterable([1, 2, 3]),
-        [x => x * x, x => Chain.finalValue(x), x => 2 * x + 1],
+        gen(
+          x => x * x,
+          x => Chain.finalValue(x),
+          x => 2 * x + 1
+        ),
         streamToArray(output)
       ]);
 
@@ -39,7 +51,11 @@ unit.add(module, [
     const output = [],
       chain = new Chain([
         fromIterable([1, 2, 3]),
-        [x => x * x, () => Chain.none, x => 2 * x + 1],
+        gen(
+          x => x * x,
+          () => Chain.none,
+          x => 2 * x + 1
+        ),
         streamToArray(output)
       ]);
 
@@ -52,7 +68,7 @@ unit.add(module, [
     const async = t.startAsync('test_transducersEmpty');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), x => x * x, [], streamToArray(output)]);
+      chain = new Chain([fromIterable([1, 2, 3]), x => x * x, gen(), streamToArray(output)]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [1, 4, 9])'));
@@ -63,7 +79,12 @@ unit.add(module, [
     const async = t.startAsync('test_transducersOne');
 
     const output = [],
-      chain = new Chain([fromIterable([1, 2, 3]), x => x * x, [x => 2 * x + 1], streamToArray(output)]);
+      chain = new Chain([
+        fromIterable([1, 2, 3]),
+        x => x * x,
+        gen(x => 2 * x + 1),
+        streamToArray(output)
+      ]);
 
     chain.on('end', () => {
       eval(t.TEST('t.unify(output, [3, 9, 19])'));

+ 2 - 4
tests/tests.js

@@ -1,7 +1,5 @@
 'use strict';
 
-const nodeVersion = /^v?(\d+)\./.exec(process.version);
-
 const unit = require('heya-unit');
 
 require('./test_FromIterable');
@@ -11,8 +9,8 @@ require('./test_readWrite');
 require('./test_errors');
 
 require('./test_transducers');
-require('./test_comp');
-nodeVersion && +nodeVersion[1] >= 10 && require('./test_gen');
+// require('./test_comp');
+require('./test_gen');
 
 require('./test_take');
 require('./test_skip');