Added some tests

Also fixed xProd. Tests for xProd, unzipWith, unzip and is.
This commit is contained in:
Angelos Chalaris
2018-01-24 17:17:51 +02:00
parent 16b0701084
commit 449dfdea1c
7 changed files with 51 additions and 12 deletions

View File

@ -2,10 +2,10 @@
Creates a new array out of the two supplied by creating each possible pair from the arrays.
Use `Array.map()` to produce every possible pair from the elements of the two arrays.
Use `Array.reduce()`, `Array.map()` and `Array.concat()` to produce every possible pair from the elements of the two arrays and save them in an array.
```js
const xProd = (a, b) => a.map(x => b.map(y => [x, y]));
const xProd = (a, b) => a.reduce((acc,x) => acc.concat(b.map(y => [x, y])),[]);
```
```js

View File

@ -5,6 +5,22 @@ test('Testing is', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof is === 'function', 'is is a Function');
t.true(is(Array, [1]), `Works for arrays with data`);
t.true(is(Array, []), `Works for empty arrays`);
t.false(is(Array, {}), `Works for arrays, not objects`);
t.true(is(Object, {}), `Works for objects`);
t.true(is(Map, new Map()), `Works for maps`);
t.true(is(RegExp, /./g), `Works for regular expressions`);
t.true(is(Set, new Set()), `Works for sets`);
t.true(is(WeakMap, new WeakMap()), `Works for weak maps`);
t.true(is(WeakSet, new WeakSet()), `Works for weak sets`);
t.false(is(String, ''), `Works for strings - returns false for primitive`);
t.true(is(String, new String('')), `Works for strings - returns true when using constructor`);
t.false(is(Number, 1), `Works for numbers - returns false for primitive`);
t.true(is(Number, new Number('10')), `Works for numbers - returns true when using constructor`);
t.false(is(Boolean, false), `Works for booleans - returns false for primitive`);
t.true(is(Boolean, new Boolean(false)), `Works for booleans - returns true when using constructor`);
t.true(is(Function, () => null), `Works for functions`);
//t.deepEqual(is(args..), 'Expected');
//t.equal(is(args..), 'Expected');
//t.false(is(args..), 'Expected');

View File

@ -1,4 +1,4 @@
Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time)
Test log for: Wed Jan 24 2018 17:16:12 GMT+0200 (GTB Standard Time)
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
> tape test/**/*.test.js | tap-spec
@ -555,6 +555,22 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time)
Testing is
√ is is a Function
√ Works for arrays with data
√ Works for empty arrays
√ Works for arrays, not objects
√ Works for objects
√ Works for maps
√ Works for regular expressions
√ Works for sets
√ Works for weak maps
√ Works for weak sets
√ Works for strings - returns false for primitive
√ Works for strings - returns true when using constructor
√ Works for numbers - returns false for primitive
√ Works for numbers - returns true when using constructor
√ Works for booleans - returns false for primitive
√ Works for booleans - returns true when using constructor
√ Works for functions
Testing isAbsoluteURL
@ -1323,10 +1339,13 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time)
Testing unzip
√ unzip is a Function
√ unzip([['a', 1, true], ['b', 2, false]]) equals [['a', 'b'], [1, 2], [true, false]]
√ unzip([['a', 1, true], ['b', 2]]) equals [['a', 'b'], [1, 2], [true]]
Testing unzipWith
√ unzipWith is a Function
√ unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)) equals [3, 30, 300]
Testing URLJoin
@ -1387,6 +1406,7 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time)
Testing xProd
√ xProd is a Function
√ xProd([1, 2], ['a', 'b']) returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
Testing yesNo
@ -1434,8 +1454,8 @@ Test log for: Wed Jan 24 2018 16:47:50 GMT+0200 (GTB Standard Time)
√ zipWith is a Function
total: 598
passing: 598
duration: 1.3s
total: 618
passing: 618
duration: 412ms

View File

@ -5,6 +5,8 @@ test('Testing unzip', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof unzip === 'function', 'unzip is a Function');
t.deepEqual(unzip([['a', 1, true], ['b', 2, false]]), [['a', 'b'], [1, 2], [true, false]], `unzip([['a', 1, true], ['b', 2, false]]) equals [['a', 'b'], [1, 2], [true, false]]`);
t.deepEqual(unzip([['a', 1, true], ['b', 2]]), [['a', 'b'], [1, 2], [true]], `unzip([['a', 1, true], ['b', 2]]) equals [['a', 'b'], [1, 2], [true]]`);
//t.deepEqual(unzip(args..), 'Expected');
//t.equal(unzip(args..), 'Expected');
//t.false(unzip(args..), 'Expected');

View File

@ -5,7 +5,7 @@ test('Testing unzipWith', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof unzipWith === 'function', 'unzipWith is a Function');
//t.deepEqual(unzipWith(args..), 'Expected');
t.deepEqual(unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)), [3, 30, 300], `unzipWith([[1, 10, 100], [2, 20, 200]], (...args) => args.reduce((acc, v) => acc + v, 0)) equals [3, 30, 300]`);
//t.equal(unzipWith(args..), 'Expected');
//t.false(unzipWith(args..), 'Expected');
//t.throws(unzipWith(args..), 'Expected');

View File

@ -1,2 +1,2 @@
const xProd = (a, b) => a.map(x => b.map(y => [x, y]));
const xProd = (a, b) => a.reduce((acc,x) => acc.concat(b.map(y => [x, y])),[]);
module.exports = xProd

View File

@ -5,6 +5,7 @@ test('Testing xProd', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof xProd === 'function', 'xProd is a Function');
t.deepEqual(xProd([1, 2], ['a', 'b']), [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']], `xProd([1, 2], ['a', 'b']) returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]`);
//t.deepEqual(xProd(args..), 'Expected');
//t.equal(xProd(args..), 'Expected');
//t.false(xProd(args..), 'Expected');