From d7134925c334e89cd35d1d06e8e053c5310f5ebc Mon Sep 17 00:00:00 2001 From: Rumiantsev Oleksii Date: Mon, 1 Jul 2019 18:55:46 +0300 Subject: [PATCH] Add tests for iterable support PR-URL: https://github.com/metarhia/metasync/pull/415 --- test/array.asyncMap.js | 38 +++++++++++++++++--- test/array.each.js | 38 +++++++++++--------- test/array.every.js | 22 +++++++++--- test/array.filter.js | 76 +++++++++++++++++---------------------- test/array.find.js | 21 ++++++++--- test/array.map.js | 30 ++++++++++++++-- test/array.reduce.js | 37 +++++++++++++------ test/array.reduceRight.js | 38 ++++++++++++++------ test/array.series.js | 23 +++++++++++- test/array.some.js | 14 +++++++- test/control.js | 11 +++--- test/firstOf.js | 6 ++-- 12 files changed, 250 insertions(+), 104 deletions(-) diff --git a/test/array.asyncMap.js b/test/array.asyncMap.js index a2deabc3..1dd3f294 100644 --- a/test/array.asyncMap.js +++ b/test/array.asyncMap.js @@ -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(); } ); }); @@ -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); @@ -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(); +}); diff --git a/test/array.each.js b/test/array.each.js index 22b2c4a2..e3838070 100644 --- a/test/array.each.js +++ b/test/array.each.js @@ -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(); } ); diff --git a/test/array.every.js b/test/array.every.js index fb36a173..21e6d638 100644 --- a/test/array.every.js +++ b/test/array.every.js @@ -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(); }); }); @@ -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(); diff --git a/test/array.filter.js b/test/array.filter.js index 55528e85..f904d38e 100644 --- a/test/array.filter.js +++ b/test/array.filter.js @@ -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 = []; @@ -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(); +}); diff --git a/test/array.find.js b/test/array.find.js index fa26a458..098a0875 100644 --- a/test/array.find.js +++ b/test/array.find.js @@ -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'; @@ -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(); @@ -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); @@ -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(); + }); +}); diff --git a/test/array.map.js b/test/array.map.js index f059774a..8829c56d 100644 --- a/test/array.map.js +++ b/test/array.map.js @@ -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]; @@ -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'); @@ -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(); +}); diff --git a/test/array.reduce.js b/test/array.reduce.js index 21028eac..8a4aed3b 100644 --- a/test/array.reduce.js +++ b/test/array.reduce.js @@ -20,6 +20,23 @@ metatests.test('reduce with initial', test => { ); }); +metatests.test('reduce with initial and another iterable', test => { + const set = new Set([1, 2, 3, 4, 5]); + const initial = 10; + const expectedRes = 25; + + metasync.reduce( + set, + (prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)), + (err, res) => { + test.error(err); + test.strictSame(res, expectedRes); + test.end(); + }, + initial + ); +}); + metatests.test('reduce with initial and empty array', test => { const arr = []; const initial = 10; @@ -40,28 +57,28 @@ metatests.test('reduce with initial and empty array', test => { metatests.test('reduce without initial and with empty array', test => { const arr = []; const expectedError = new TypeError( - 'Reduce of empty array with no initial value' + 'Reduce of consumed async iterator with no initial value' ); metasync.reduce( arr, (prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)), (err, res) => { - test.strictSame(err, expectedError); + test.isError(err, expectedError); test.strictSame(res, undefined); test.end(); } ); }); -metatests.test('reduce without initial and with single-element array', test => { +metatests.test('reduce single-element array without initial', test => { const arr = [2]; metasync.reduce( arr, (prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)), (err, res) => { - test.strictSame(err, null); + test.error(err); test.strictSame(res, 2); test.end(); } @@ -83,12 +100,12 @@ metatests.test('reduce without initial', test => { ); }); -metatests.test('reduce with asymetric function', test => { - const arr = '10110011'; +metatests.test('reduce with asymmetric function', test => { + const str = '10110011'; const expectedRes = 179; metasync.reduce( - arr, + str, (prev, cur, callback) => process.nextTick(() => callback(null, prev * 2 + +cur)), (err, res) => { @@ -100,11 +117,11 @@ metatests.test('reduce with asymetric function', test => { }); metatests.test('reduce with error', test => { - const arr = '10120011'; + const str = '10120011'; const reduceError = new Error('Reduce error'); metasync.reduce( - arr, + str, (prev, cur, callback) => process.nextTick(() => { const digit = +cur; @@ -115,7 +132,7 @@ metatests.test('reduce with error', test => { callback(null, prev * 2 + digit); }), (err, res) => { - test.strictSame(err, reduceError); + test.isError(err, reduceError); test.strictSame(res, undefined); test.end(); } diff --git a/test/array.reduceRight.js b/test/array.reduceRight.js index a3fe9da9..09932bb0 100644 --- a/test/array.reduceRight.js +++ b/test/array.reduceRight.js @@ -37,17 +37,35 @@ metatests.test('reduceRight with initial and empty array', test => { ); }); +metatests.test('reduceRight with initial and another iterable', test => { + const map = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]]); + const initial = 10; + const expectedRes = 25; + + metasync.reduceRight( + map, + (prev, cur, callback) => + process.nextTick(() => callback(null, prev + cur[1])), + (err, res) => { + test.error(err); + test.strictSame(res, expectedRes); + test.end(); + }, + initial + ); +}); + metatests.test('reduceRight without initial and with empty array', test => { const arr = []; const expectedError = new TypeError( - 'Reduce of empty array with no initial value' + 'Reduce of consumed async iterator with no initial value' ); metasync.reduceRight( arr, (prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)), (err, res) => { - test.strictSame(err, expectedError); + test.isError(err, expectedError); test.strictSame(res, undefined); test.end(); } @@ -64,7 +82,7 @@ metatests.test( (prev, cur, callback) => process.nextTick(() => callback(null, prev + cur)), (err, res) => { - test.strictSame(err, null); + test.error(err); test.strictSame(res, 2); test.end(); } @@ -87,12 +105,12 @@ metatests.test('reduceRight without initial', test => { ); }); -metatests.test('reduceRight with asymetric function', test => { - const arr = '10110011'; +metatests.test('reduceRight with asymmetric function', test => { + const str = '10110011'; const expectedRes = 205; metasync.reduceRight( - arr, + str, (prev, cur, callback) => process.nextTick(() => callback(null, prev * 2 + +cur)), (err, res) => { @@ -104,11 +122,11 @@ metatests.test('reduceRight with asymetric function', test => { }); metatests.test('reduceRight with error', test => { - const arr = '10120011'; + const str = '10120011'; const reduceError = new Error('Reduce error'); - metasync.reduce( - arr, + metasync.reduceRight( + str, (prev, cur, callback) => process.nextTick(() => { const digit = +cur; @@ -119,7 +137,7 @@ metatests.test('reduceRight with error', test => { callback(null, prev * 2 + digit); }), (err, res) => { - test.strictSame(err, reduceError); + test.isError(err, reduceError); test.strictSame(res, undefined); test.end(); } diff --git a/test/array.series.js b/test/array.series.js index 5171d893..987950be 100644 --- a/test/array.series.js +++ b/test/array.series.js @@ -7,6 +7,7 @@ metatests.test('successful series', test => { const arr = [1, 2, 3, 4]; const expectedElements = arr; const elements = []; + metasync.series( arr, (el, callback) => { @@ -21,6 +22,25 @@ metatests.test('successful series', test => { ); }); +metatests.test('successful series with another iterable', test => { + const set = new Set([1, 2, 3, 4]); + const expectedElements = set; + const elements = []; + + metasync.series( + set, + (el, callback) => { + elements.push(el); + callback(null); + }, + err => { + test.error(err); + test.strictSame(elements, [...expectedElements]); + test.end(); + } + ); +}); + metatests.test('series with error', test => { const arr = [1, 2, 3, 4]; const expectedElements = [1, 2]; @@ -42,8 +62,9 @@ metatests.test('series with error', test => { } }, err => { - test.strictSame(err, seriesError); + test.isError(err, seriesError); test.strictSame(elements, expectedElements); + test.strictSame(count, expectedElementsCount); test.end(); } ); diff --git a/test/array.some.js b/test/array.some.js index bb12bcea..3b44f730 100644 --- a/test/array.some.js +++ b/test/array.some.js @@ -7,6 +7,7 @@ metatests.test('successful some', test => { const arr = [1, 2, 3]; const predicate = (x, callback) => callback(null, x % 2 === 0); + metasync.some(arr, predicate, (err, accepted) => { test.error(err); test.strictSame(accepted, true); @@ -14,6 +15,17 @@ metatests.test('successful some', test => { }); }); +metatests.test('successful some with another iterable', test => { + const set = new Set([1, 2, 3]); + const predicate = (x, callback) => callback(null, x % 2 === 0); + + metasync.some(set, predicate, (err, accepted) => { + test.error(err); + test.strictSame(accepted, true); + test.end(); + }); +}); + metatests.test('failing some', test => { const arr = [1, 2, 3]; @@ -32,7 +44,7 @@ metatests.test('erroneous some', test => { const predicate = (x, callback) => x % 2 === 0 ? callback(someError) : callback(null, false); metasync.some(arr, predicate, (err, accepted) => { - test.strictSame(err, someError); + test.isError(err, someError); test.strictSame(accepted, undefined); test.end(); }); diff --git a/test/control.js b/test/control.js index a56761d6..931e79e9 100644 --- a/test/control.js +++ b/test/control.js @@ -7,11 +7,11 @@ metatests.test('firstOf', test => { const returningFnIndex = 2; let dataReturned = false; - const execUnlessDataReturned = data => callback => { + const execUnlessDataReturned = (data, callback) => { if (dataReturned) { callback(null, data); } else { - process.nextTick(execUnlessDataReturned); + process.nextTick(() => execUnlessDataReturned(data, callback)); } }; const makeIFn = i => callback => @@ -21,7 +21,7 @@ metatests.test('firstOf', test => { dataReturned = true; callback(null, iData); } else { - execUnlessDataReturned(iData); + execUnlessDataReturned(iData, callback); } }); @@ -136,7 +136,6 @@ metatests.test('runIf forward an error', test => { }); metatests.test('runIfFn', test => { - test.plan(5); const value = 42; const asyncFn = cb => { cb(null, value); @@ -145,10 +144,14 @@ metatests.test('runIfFn', test => { metasync.runIfFn(test.mustCall(asyncFn), (err, res) => { test.error(err); test.strictSame(res, value); + test.end(); }); +}); +metatests.test('runIfFn without fn', test => { metasync.runIfFn(null, (err, res) => { test.error(err); test.assertNot(res); + test.end(); }); }); diff --git a/test/firstOf.js b/test/firstOf.js index 25fb3864..a4a99aeb 100644 --- a/test/firstOf.js +++ b/test/firstOf.js @@ -7,11 +7,11 @@ metatests.test('firstOf', test => { const returningFnIndex = 2; let dataReturned = false; - const execUnlessDataReturned = data => callback => { + const execUnlessDataReturned = (data, callback) => { if (dataReturned) { callback(null, data); } else { - process.nextTick(execUnlessDataReturned); + process.nextTick(() => execUnlessDataReturned(data, callback)); } }; const makeIFn = i => callback => @@ -21,7 +21,7 @@ metatests.test('firstOf', test => { dataReturned = true; callback(null, iData); } else { - execUnlessDataReturned(iData); + execUnlessDataReturned(iData, callback); } });