Skip to content

Commit

Permalink
Add tests for iterable support
Browse files Browse the repository at this point in the history
PR-URL: #415
  • Loading branch information
o-rumiantsev committed Jul 1, 2019
1 parent 6019af6 commit d713492
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 104 deletions.
38 changes: 33 additions & 5 deletions test/array.asyncMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@
const metasync = require('..');
const metatests = require('metatests');

metatests.test('succesfull map', test => {
test.plan(2);

metatests.test('successful asyncMap with array', test => {
const arr = [1, 2, 3];
const expectedArr = [2, 4, 6];

metasync.asyncMap(
arr,
item => item * 2,
(item, callback) => process.nextTick(() => callback(null, item * 2)),
(err, newArr) => {
test.error(err);
test.strictSame(newArr, expectedArr);
test.end();
}
);
});

metatests.test('successful asyncMap with another iterable', test => {
const set = new Set([1, 2, 3]);
const expectedSet = new Set([2, 4, 6]);

metasync.asyncMap(
set,
(item, callback) => process.nextTick(() => callback(null, item * 2)),
(err, newSet) => {
test.error(err);
test.strictSame([...newSet], [...expectedSet]);
test.end();
}
);
});
Expand All @@ -38,7 +52,10 @@ metatests.test('Non-blocking', test => {
const begin = Date.now();
metasync.asyncMap(
arr,
() => doSmth(ITEM_TIME),
(x, callback) => {
doSmth(ITEM_TIME);
callback();
},
{ percent: EXPECTED_PERCENT },
() => {
clearInterval(timer);
Expand All @@ -52,3 +69,14 @@ metatests.test('Non-blocking', test => {
}
);
});

metatests.test('asyncMap with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };

test.throws(
() => metasync.asyncMap(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});
38 changes: 22 additions & 16 deletions test/array.each.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,35 @@ metatests.test('each with empty array', test => {
);
});

metatests.test('successful each with another iterable', test => {
const map = new Map([[1, 'a'], [2, 'b'], [3, 'c']]);
const mapCopy = new Map();

metasync.each(
map,
(entry, callback) =>
process.nextTick(() => {
mapCopy.set(...entry);
callback(null);
}),
err => {
test.error(err);
test.strictSame([...mapCopy], [...map]);
test.end();
}
);
});

metatests.test('each with error', test => {
const arr = [1, 2, 3, 4];
let count = 0;

const elementsSet = new Set();
const expectedElementsCount = 2;
const eachError = new Error('Each error');

metasync.each(
arr,
(el, callback) =>
process.nextTick(() => {
elementsSet.add(el);
count++;
if (count === expectedElementsCount) {
callback(eachError);
} else {
callback(null);
}
}),
(item, callback) =>
process.nextTick(() => callback(item === 2 ? eachError : null)),
err => {
test.strictSame(err, eachError);
test.strictSame(elementsSet.size, expectedElementsCount);
test.isError(err, eachError);
test.end();
}
);
Expand Down
22 changes: 17 additions & 5 deletions test/array.every.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ metatests.test('every with error', test => {
};

metasync.every(data, predicate, err => {
test.strictSame(err, everyErr);
test.isError(err, everyErr);
test.end();
});
});
Expand All @@ -60,14 +60,26 @@ metatests.test('every with two-element arrays', test =>
)
);

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

metatests.test('every', test => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
const predicate = (item, callback) =>
process.nextTick(() => callback(null, item > 0));

const predicate = (item, callback) => {
metasync.every(arr, predicate, (err, result) => {
test.error(err);
test.strictSame(result, true);
test.end();
});
});

metatests.test('every with another iterable', test => {
const set = new Set(arr);

const predicate = (item, callback) =>
process.nextTick(() => callback(null, item > 0));
};

metasync.every(data, predicate, (err, result) => {
metasync.every(set, predicate, (err, result) => {
test.error(err);
test.strictSame(result, true);
test.end();
Expand Down
76 changes: 33 additions & 43 deletions test/array.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ metatests.test('successful filter', test => {
);
});

metatests.test('filter with empty array', test => {
metatests.test('filter with empty array ', test => {
const arr = [];
const expectedArr = [];

Expand All @@ -65,54 +65,44 @@ metatests.test('filter with empty array', test => {
);
});

metatests.test('successful filter', test => {
const arr = [
'Lorem',
'ipsum',
'dolor',
'sit',
'amet',
'consectetur',
'adipiscing',
'elit',
'sed',
'do',
'eiusmod',
'tempor',
'incididunt',
'ut',
'labore',
'et',
'dolore',
'magna',
'aliqua',
];
metatests.test('successful filter with another iterable', test => {
const set = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const expectedSet = new Set([2, 4, 6, 8, 10]);

metasync.filter(
set,
(x, callback) => process.nextTick(() => callback(null, x % 2 === 0)),
(err, res) => {
test.error(err);
test.strictSame([...res], [...expectedSet]);
test.end();
}
);
});

metatests.test('filter with error', test => {
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const filterError = new Error('Filter error');
const expectedArr = [
'Lorem',
'ipsum',
'dolor',
'sit',
'amet',
'elit',
'sed',
'magna',
];

metasync.filter(
arr,
(str, callback) =>
process.nextTick(() => {
if (str.length === 2) {
callback(filterError);
return;
}
callback(null, str.length < 6);
}),
(x, callback) =>
process.nextTick(() => callback(x === 5 ? filterError : null, x % 2)),
(err, res) => {
test.error(err);
test.same(res.join(), expectedArr.join());
test.isError(err, filterError);
test.assertNot(res);
test.end();
}
);
});

metatests.test('filter with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };

test.throws(
() => metasync.filter(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});
21 changes: 17 additions & 4 deletions test/array.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const metasync = require('..');
const metatests = require('metatests');

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

metatests.test('find with error', test => {
const data = [1, 2, 3];
const expectedErrorMessage = 'Intentional error';
Expand All @@ -23,12 +25,11 @@ metatests.test('find with error', test => {
});

metatests.test('find', test => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
const expected = 15;
const predicate = (item, callback) =>
process.nextTick(() => callback(null, item % 3 === 0 && item % 5 === 0));

metasync.find(data, predicate, (err, result) => {
metasync.find(arr, predicate, (err, result) => {
test.error(err, 'must not return an error');
test.strictSame(result, expected, `result should be: ${expected}`);
test.end();
Expand All @@ -48,9 +49,8 @@ metatests.test('with empty array', test => {
});

metatests.test('with array without element which is searching', test => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
metasync.find(
data,
arr,
(el, callback) => process.nextTick(() => callback(null, el === 20)),
(err, result) => {
test.error(err);
Expand All @@ -59,3 +59,16 @@ metatests.test('with array without element which is searching', test => {
}
);
});

metatests.test('find with another iterable', test => {
const map = new Map([[1, 'a'], [2, 'b'], [3, 'c']]);
const expected = [3, 'c'];
const predicate = (item, callback) =>
process.nextTick(() => callback(null, item[1] === 'c'));

metasync.find(map, predicate, (err, result) => {
test.error(err);
test.strictSame(result, expected);
test.end();
});
});
30 changes: 28 additions & 2 deletions test/array.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const metasync = require('..');
const metatests = require('metatests');

metatests.test('succesfull map', test => {
metatests.test('successful map', test => {
const arr = [1, 2, 3];
const expectedArr = [1, 4, 9];

Expand Down Expand Up @@ -33,6 +33,21 @@ metatests.test('map with empty array', test => {
);
});

metatests.test('successful map with another iterable', test => {
const set = new Set([1, 2, 3]);
const expectedSet = new Set([1, 4, 9]);

metasync.map(
set,
(x, callback) => process.nextTick(() => callback(null, x * x)),
(err, res) => {
test.error(err);
test.strictSame([...res], [...expectedSet]);
test.end();
}
);
});

metatests.test('map with error', test => {
const arr = [1, 2, 3];
const mapError = new Error('Map error');
Expand All @@ -50,9 +65,20 @@ metatests.test('map with error', test => {
callback(null, x * x);
}),
(err, res) => {
test.strictSame(err, mapError);
test.isError(err, mapError);
test.strictSame(res, undefined);
test.end();
}
);
});

metatests.test('map with not iterable', test => {
const obj = { a: '1', b: '2', c: '3' };

test.throws(
() => metasync.map(obj, test.mustNotCall(), test.mustNotCall()),
new TypeError('Base is not Iterable')
);

test.end();
});

0 comments on commit d713492

Please sign in to comment.