From b870cd1ea6b795e0cb8116478261217c44604310 Mon Sep 17 00:00:00 2001 From: ezubarev Date: Thu, 5 May 2016 21:48:40 +0600 Subject: =?UTF-8?q?=D0=A1onvert=20each/eachOf=20tests=20to=20mocha?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mocha_test/each.js | 283 +++++++++++++++++++++++ mocha_test/eachOf.js | 333 +++++++++++++++++++++++++++ test/test-async.js | 625 --------------------------------------------------- 3 files changed, 616 insertions(+), 625 deletions(-) create mode 100644 mocha_test/each.js create mode 100644 mocha_test/eachOf.js diff --git a/mocha_test/each.js b/mocha_test/each.js new file mode 100644 index 0000000..014e9df --- /dev/null +++ b/mocha_test/each.js @@ -0,0 +1,283 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe("each", function() { + + function eachIteratee(args, x, callback) { + setTimeout(function(){ + args.push(x); + callback(); + }, x*25); + } + + function eachNoCallbackIteratee(done, x, callback) { + expect(x).to.equal(1); + callback(); + done(); + } + + it('each', function(done) { + var args = []; + async.each([1,3,2], eachIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([1,2,3]); + done(); + }); + }); + + it('each extra callback', function(done) { + var count = 0; + async.each([1,3,2], function(val, callback) { + count++; + var done_ = count == 3; + callback(); + assert.throws(callback); + if (done_) { + done(); + } + }); + }); + + it('each empty array', function(done) { + async.each([], function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + + it('each empty array, with other property on the array', function(done) { + var myArray = []; + myArray.myProp = "anything"; + async.each(myArray, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + + it('each error', function(done) { + async.each([1,2,3], function(x, callback){ + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('each no callback', function(done) { + async.each([1], eachNoCallbackIteratee.bind(this, done)); + }); + + it('eachSeries', function(done) { + var args = []; + async.eachSeries([1,3,2], eachIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([1,3,2]); + done(); + }); + }); + + it('eachSeries empty array', function(done) { + async.eachSeries([], function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachSeries array modification', function(done) { + var arr = [1, 2, 3, 4]; + async.eachSeries(arr, function (x, callback) { + async.setImmediate(callback); + }, function () { + assert(true, 'should call callback'); + }); + + arr.pop(); + arr.splice(0, 1); + + setTimeout(done, 50); + }); + + // bug #782. Remove in next major release + it('eachSeries single item', function(done) { + var sync = true; + async.eachSeries([1], function (i, cb) { + cb(null); + }, function () { + assert(sync, "callback not called on same tick"); + }); + sync = false; + done(); + }); + + // bug #782. Remove in next major release + it('eachSeries single item', function(done) { + var sync = true; + async.eachSeries([1], function (i, cb) { + cb(null); + }, function () { + assert(sync, "callback not called on same tick"); + }); + sync = false; + done(); + }); + + it('eachSeries error', function(done) { + var call_order = []; + async.eachSeries([1,2,3], function(x, callback){ + call_order.push(x); + callback('error'); + }, function(err){ + expect(call_order).to.eql([1]); + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('eachSeries no callback', function(done) { + async.eachSeries([1], eachNoCallbackIteratee.bind(this, done)); + }); + + + it('eachLimit', function(done) { + var args = []; + var arr = [0,1,2,3,4,5,6,7,8,9]; + async.eachLimit(arr, 2, function(x,callback){ + setTimeout(function(){ + args.push(x); + callback(); + }, x*5); + }, function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit empty array', function(done) { + async.eachLimit([], 2, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachLimit limit exceeds size', function(done) { + var args = []; + var arr = [0,1,2,3,4,5,6,7,8,9]; + async.eachLimit(arr, 20, eachIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit limit equal size', function(done) { + var args = []; + var arr = [0,1,2,3,4,5,6,7,8,9]; + async.eachLimit(arr, 10, eachIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit zero limit', function(done) { + async.eachLimit([0,1,2,3,4,5], 0, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('eachLimit error', function(done) { + var arr = [0,1,2,3,4,5,6,7,8,9]; + var call_order = []; + + async.eachLimit(arr, 3, function(x, callback){ + call_order.push(x); + if (x === 2) { + callback('error'); + } + }, function(err){ + expect(call_order).to.eql([0,1,2]); + expect(err).to.equal('error'); + }); + setTimeout(done, 25); + }); + + it('eachLimit no callback', function(done) { + async.eachLimit([1], 1, eachNoCallbackIteratee.bind(this, done)); + }); + + it('eachLimit synchronous', function(done) { + var args = []; + var arr = [0,1,2]; + async.eachLimit(arr, 5, function(x,callback){ + args.push(x); + callback(); + }, function(err){ + if (err) throw err; + expect(args).to.eql(arr); + done(); + }); + }); + + it('eachLimit does not continue replenishing after error', function(done) { + var started = 0; + var arr = [0,1,2,3,4,5,6,7,8,9]; + var delay = 10; + var limit = 3; + var maxTime = 10 * arr.length; + + async.eachLimit(arr, limit, function(x, callback) { + started ++; + if (started === 3) { + return callback(new Error ("Test Error")); + } + setTimeout(function(){ + callback(); + }, delay); + }, function(){}); + + setTimeout(function(){ + expect(started).to.equal(3); + done(); + }, maxTime); + }); + + it('forEach alias', function(done) { + assert.strictEqual(async.each, async.forEach); + done(); + }); + + it('forEachSeries alias', function(done) { + assert.strictEqual(async.eachSeries, async.forEachSeries); + done(); + }); + + it('forEachLimit alias', function(done) { + assert.strictEqual(async.eachLimit, async.forEachLimit); + done(); + }); +}); diff --git a/mocha_test/eachOf.js b/mocha_test/eachOf.js new file mode 100644 index 0000000..c705bad --- /dev/null +++ b/mocha_test/eachOf.js @@ -0,0 +1,333 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe("eachOf", function() { + + function forEachOfNoCallbackIteratee(done, x, key, callback) { + expect(x).to.equal(1); + expect(key).to.equal("a"); + callback(); + done(); + } + + function forEachOfIteratee(args, value, key, callback) { + setTimeout(function(){ + args.push(key, value); + callback(); + }, value*25); + } + + it('eachOf alias', function(done) { + expect(async.eachOf).to.equal(async.forEachOf); + done(); + }); + + it('eachOfLimit alias', function(done) { + expect(async.eachOfLimit).to.equal(async.forEachOfLimit); + done(); + }); + + it('eachOfSeries alias', function(done) { + expect(async.eachOfSeries).to.equal(async.forEachOfSeries); + done(); + }); + + it('forEachOf', function(done) { + var args = []; + async.forEachOf({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql(["a", 1, "b", 2]); + done(); + }); + }); + + it('forEachOf - instant resolver', function(done) { + var args = []; + async.forEachOf({ a: 1, b: 2 }, function(x, k, cb) { + args.push(k, x); + cb(); + }, function(){ + // ensures done callback isn't called before all items iterated + expect(args).to.eql(["a", 1, "b", 2]); + done(); + }); + }); + + it('forEachOf empty object', function(done) { + async.forEachOf({}, function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err) { + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOf empty array', function(done) { + async.forEachOf([], function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err) { + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOf error', function(done) { + async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) { + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('forEachOf no callback', function(done) { + async.forEachOf({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + + it('forEachOf with array', function(done) { + var args = []; + async.forEachOf([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOf with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return done(); + + var args = []; + var set = new Set(); + set.add("a"); + set.add("b"); + async.forEachOf(set, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOf with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return done(); + + var args = []; + var map = new Map(); + map.set(1, "a"); + map.set(2, "b"); + async.forEachOf(map, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); + + it('forEachOfSeries', function(done) { + var args = []; + async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([ "a", 1, "b", 2 ]); + done(); + }); + }); + + it('forEachOfSeries empty object', function(done) { + async.forEachOfSeries({}, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfSeries error', function(done) { + var call_order = []; + async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ + call_order.push(value, key); + callback('error'); + }, function(err){ + expect(call_order).to.eql([ 1, "a" ]); + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('forEachOfSeries no callback', function(done) { + async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + it('forEachOfSeries with array', function(done) { + var args = []; + async.forEachOfSeries([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ 0, "a", 1, "b" ]); + done(); + }); + }); + + it('forEachOfSeries with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return done(); + + var args = []; + var set = new Set(); + set.add("a"); + set.add("b"); + async.forEachOfSeries(set, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOfSeries with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return done(); + + var args = []; + var map = new Map(); + map.set(1, "a"); + map.set(2, "b"); + async.forEachOfSeries(map, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); + + it('forEachOfLimit', function(done) { + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4 }; + async.forEachOfLimit(obj, 2, function(value, key, callback){ + setTimeout(function(){ + args.push(value, key); + callback(); + }, value * 5); + }, function(err){ + assert(err === null, err + " passed instead of 'null'"); + expect(args).to.eql([ 1, "a", 2, "b", 3, "c", 4, "d" ]); + done(); + }); + }); + + it('forEachOfLimit empty object', function(done) { + async.forEachOfLimit({}, 2, function(value, key, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit limit exceeds size', function(done) { + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + async.forEachOfLimit(obj, 10, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + done(); + }); + }); + + it('forEachOfLimit limit equal size', function(done) { + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + done(); + }); + }); + + it('forEachOfLimit zero limit', function(done) { + async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){ + assert(false, 'iteratee should not be called'); + callback(); + }, function(err){ + if (err) throw err; + assert(true, 'should call callback'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit error', function(done) { + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + var call_order = []; + + async.forEachOfLimit(obj, 3, function(value, key, callback){ + call_order.push(value, key); + if (value === 2) { + callback('error'); + } + }, function(err){ + expect(call_order).to.eql([ 1, "a", 2, "b" ]); + expect(err).to.equal('error'); + }); + setTimeout(done, 25); + }); + + it('forEachOfLimit no callback', function(done) { + async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIteratee.bind(this, done)); + }); + + it('forEachOfLimit synchronous', function(done) { + var args = []; + var obj = { a: 1, b: 2 }; + async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([ "a", 1, "b", 2 ]); + done(); + }); + }); + + it('forEachOfLimit with array', function(done) { + var args = []; + var arr = [ "a", "b" ]; + async.forEachOfLimit(arr, 1, forEachOfIteratee.bind(this, args), function (err) { + if (err) throw err; + expect(args).to.eql([ 0, "a", 1, "b" ]); + done(); + }); + }); + + it('forEachOfLimit with Set (iterators)', function(done) { + if (typeof Set !== 'function') + return done(); + + var args = []; + var set = new Set(); + set.add("a"); + set.add("b"); + async.forEachOfLimit(set, 1, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, "a", 1, "b"]); + done(); + }); + }); + + it('forEachOfLimit with Map (iterators)', function(done) { + if (typeof Map !== 'function') + return done(); + + var args = []; + var map = new Map(); + map.set(1, "a"); + map.set(2, "b"); + async.forEachOfLimit(map, 1, forEachOfIteratee.bind(this, args), function(err){ + if (err) throw err; + expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]); + done(); + }); + }); +}); diff --git a/test/test-async.js b/test/test-async.js index 53d8cb4..ae3f3c4 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -16,33 +16,6 @@ if (!Function.prototype.bind) { }; } -function eachIteratee(args, x, callback) { - setTimeout(function(){ - args.push(x); - callback(); - }, x*25); -} - -function forEachOfIteratee(args, value, key, callback) { - setTimeout(function(){ - args.push(key, value); - callback(); - }, value*25); -} - -function eachNoCallbackIteratee(test, x, callback) { - test.equal(x, 1); - callback(); - test.done(); -} - -function forEachOfNoCallbackIteratee(test, x, key, callback) { - test.equal(x, 1); - test.equal(key, "a"); - callback(); - test.done(); -} - function getFunctionsObject(call_order) { return { one: function(callback){ @@ -706,604 +679,6 @@ exports['iterator.next'] = function(test){ test.done(); }; -exports['each'] = function(test){ - var args = []; - async.each([1,3,2], eachIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [1,2,3]); - test.done(); - }); -}; - -exports['each extra callback'] = function(test){ - var count = 0; - async.each([1,3,2], function(val, callback) { - count++; - var done = count == 3; - callback(); - test.throws(callback); - if (done) { - test.done(); - } - }); -}; - -exports['each empty array'] = function(test){ - test.expect(1); - async.each([], function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - - -exports['each empty array, with other property on the array'] = function(test){ - test.expect(1); - var myArray = []; - myArray.myProp = "anything"; - async.each(myArray, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - - -exports['each error'] = function(test){ - test.expect(1); - async.each([1,2,3], function(x, callback){ - callback('error'); - }, function(err){ - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['each no callback'] = function(test){ - async.each([1], eachNoCallbackIteratee.bind(this, test)); -}; - -exports['forEach alias'] = function (test) { - test.strictEqual(async.each, async.forEach); - test.done(); -}; - -exports['forEachOf'] = function(test){ - var args = []; - async.forEachOf({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, ["a", 1, "b", 2]); - test.done(); - }); -}; - -exports['forEachOf - instant resolver'] = function(test){ - test.expect(1); - var args = []; - async.forEachOf({ a: 1, b: 2 }, function(x, k, cb) { - args.push(k, x); - cb(); - }, function(){ - // ensures done callback isn't called before all items iterated - test.same(args, ["a", 1, "b", 2]); - test.done(); - }); -}; - -exports['forEachOf empty object'] = function(test){ - test.expect(1); - async.forEachOf({}, function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err) { - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOf empty array'] = function(test){ - test.expect(1); - async.forEachOf([], function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err) { - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOf error'] = function(test){ - test.expect(1); - async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) { - callback('error'); - }, function(err){ - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['forEachOf no callback'] = function(test){ - async.forEachOf({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['eachOf alias'] = function(test){ - test.equals(async.eachOf, async.forEachOf); - test.done(); -}; - -exports['forEachOf with array'] = function(test){ - var args = []; - async.forEachOf([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOf with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOf(set, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOf with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOf(map, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - -exports['eachSeries'] = function(test){ - var args = []; - async.eachSeries([1,3,2], eachIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [1,3,2]); - test.done(); - }); -}; - -exports['eachSeries empty array'] = function(test){ - test.expect(1); - async.eachSeries([], function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachSeries array modification'] = function(test) { - test.expect(1); - var arr = [1, 2, 3, 4]; - async.eachSeries(arr, function (x, callback) { - async.setImmediate(callback); - }, function () { - test.ok(true, 'should call callback'); - }); - - arr.pop(); - arr.splice(0, 1); - - setTimeout(test.done, 50); -}; - -// bug #782. Remove in next major release -exports['eachSeries single item'] = function (test) { - test.expect(1); - var sync = true; - async.eachSeries([1], function (i, cb) { - cb(null); - }, function () { - test.ok(sync, "callback not called on same tick"); - }); - sync = false; - test.done(); -}; - -// bug #782. Remove in next major release -exports['eachSeries single item'] = function (test) { - test.expect(1); - var sync = true; - async.eachSeries([1], function (i, cb) { - cb(null); - }, function () { - test.ok(sync, "callback not called on same tick"); - }); - sync = false; - test.done(); -}; - -exports['eachSeries error'] = function(test){ - test.expect(2); - var call_order = []; - async.eachSeries([1,2,3], function(x, callback){ - call_order.push(x); - callback('error'); - }, function(err){ - test.same(call_order, [1]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['eachSeries no callback'] = function(test){ - async.eachSeries([1], eachNoCallbackIteratee.bind(this, test)); -}; - - -exports['eachLimit'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 2, function(x,callback){ - setTimeout(function(){ - args.push(x); - callback(); - }, x*5); - }, function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit empty array'] = function(test){ - test.expect(1); - async.eachLimit([], 2, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit limit exceeds size'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 20, eachIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit limit equal size'] = function(test){ - var args = []; - var arr = [0,1,2,3,4,5,6,7,8,9]; - async.eachLimit(arr, 10, eachIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - -exports['eachLimit zero limit'] = function(test){ - test.expect(1); - async.eachLimit([0,1,2,3,4,5], 0, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit error'] = function(test){ - test.expect(2); - var arr = [0,1,2,3,4,5,6,7,8,9]; - var call_order = []; - - async.eachLimit(arr, 3, function(x, callback){ - call_order.push(x); - if (x === 2) { - callback('error'); - } - }, function(err){ - test.same(call_order, [0,1,2]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 25); -}; - -exports['eachLimit no callback'] = function(test){ - async.eachLimit([1], 1, eachNoCallbackIteratee.bind(this, test)); -}; - -exports['eachLimit synchronous'] = function(test){ - var args = []; - var arr = [0,1,2]; - async.eachLimit(arr, 5, function(x,callback){ - args.push(x); - callback(); - }, function(err){ - if (err) throw err; - test.same(args, arr); - test.done(); - }); -}; - - -exports['eachLimit does not continue replenishing after error'] = function (test) { - var started = 0; - var arr = [0,1,2,3,4,5,6,7,8,9]; - var delay = 10; - var limit = 3; - var maxTime = 10 * arr.length; - - async.eachLimit(arr, limit, function(x, callback) { - started ++; - if (started === 3) { - return callback(new Error ("Test Error")); - } - setTimeout(function(){ - callback(); - }, delay); - }, function(){}); - - setTimeout(function(){ - test.equal(started, 3); - test.done(); - }, maxTime); -}; - -exports['forEachSeries alias'] = function (test) { - test.strictEqual(async.eachSeries, async.forEachSeries); - test.done(); -}; - -exports['forEachOfSeries'] = function(test){ - var args = []; - async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [ "a", 1, "b", 2 ]); - test.done(); - }); -}; - -exports['forEachOfSeries empty object'] = function(test){ - test.expect(1); - async.forEachOfSeries({}, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfSeries error'] = function(test){ - test.expect(2); - var call_order = []; - async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ - call_order.push(value, key); - callback('error'); - }, function(err){ - test.same(call_order, [ 1, "a" ]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['forEachOfSeries no callback'] = function(test){ - async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['forEachOfSeries with array'] = function(test){ - var args = []; - async.forEachOfSeries([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ 0, "a", 1, "b" ]); - test.done(); - }); -}; - -exports['forEachOfSeries with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOfSeries(set, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOfSeries with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOfSeries(map, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - -exports['eachOfLimit alias'] = function(test){ - test.equals(async.eachOfLimit, async.forEachOfLimit); - test.done(); -}; - - -exports['eachOfSeries alias'] = function(test){ - test.equals(async.eachOfSeries, async.forEachOfSeries); - test.done(); -}; - -exports['forEachLimit alias'] = function (test) { - test.strictEqual(async.eachLimit, async.forEachLimit); - test.done(); -}; - -exports['forEachOfLimit'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4 }; - async.forEachOfLimit(obj, 2, function(value, key, callback){ - setTimeout(function(){ - args.push(value, key); - callback(); - }, value * 5); - }, function(err){ - test.ok(err === null, err + " passed instead of 'null'"); - test.same(args, [ 1, "a", 2, "b", 3, "c", 4, "d" ]); - test.done(); - }); -}; - -exports['forEachOfLimit empty object'] = function(test){ - test.expect(1); - async.forEachOfLimit({}, 2, function(value, key, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit limit exceeds size'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - async.forEachOfLimit(obj, 10, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); - test.done(); - }); -}; - -exports['forEachOfLimit limit equal size'] = function(test){ - var args = []; - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); - test.done(); - }); -}; - -exports['forEachOfLimit zero limit'] = function(test){ - test.expect(1); - async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){ - test.ok(false, 'iteratee should not be called'); - callback(); - }, function(err){ - if (err) throw err; - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit error'] = function(test){ - test.expect(2); - var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; - var call_order = []; - - async.forEachOfLimit(obj, 3, function(value, key, callback){ - call_order.push(value, key); - if (value === 2) { - callback('error'); - } - }, function(err){ - test.same(call_order, [ 1, "a", 2, "b" ]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfLimit no callback'] = function(test){ - async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIteratee.bind(this, test)); -}; - -exports['forEachOfLimit synchronous'] = function(test){ - var args = []; - var obj = { a: 1, b: 2 }; - async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [ "a", 1, "b", 2 ]); - test.done(); - }); -}; - -exports['forEachOfLimit with array'] = function(test){ - var args = []; - var arr = [ "a", "b" ]; - async.forEachOfLimit(arr, 1, forEachOfIteratee.bind(this, args), function (err) { - if (err) throw err; - test.same(args, [ 0, "a", 1, "b" ]); - test.done(); - }); -}; - -exports['forEachOfLimit with Set (iterators)'] = function(test){ - if (typeof Set !== 'function') - return test.done(); - - var args = []; - var set = new Set(); - set.add("a"); - set.add("b"); - async.forEachOfLimit(set, 1, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, "a", 1, "b"]); - test.done(); - }); -}; - -exports['forEachOfLimit with Map (iterators)'] = function(test){ - if (typeof Map !== 'function') - return test.done(); - - var args = []; - var map = new Map(); - map.set(1, "a"); - map.set(2, "b"); - async.forEachOfLimit(map, 1, forEachOfIteratee.bind(this, args), function(err){ - if (err) throw err; - test.same(args, [0, [1, "a"], 1, [2, "b"]]); - test.done(); - }); -}; - exports['reduce'] = function(test){ var call_order = []; async.reduce([1,2,3], 0, function(a, x, callback){ -- cgit v1.2.1 From f4ce893cdec9da946083a32432ac4a3e0d9f16a5 Mon Sep 17 00:00:00 2001 From: ezubarev Date: Thu, 5 May 2016 22:33:06 +0600 Subject: Convert memoize tests to mocha --- mocha_test/memoize.js | 208 +++++++++++++++++++++++++++++++++++++++++++++++ test/test-async.js | 220 -------------------------------------------------- 2 files changed, 208 insertions(+), 220 deletions(-) create mode 100644 mocha_test/memoize.js diff --git a/mocha_test/memoize.js b/mocha_test/memoize.js new file mode 100644 index 0000000..1a6e886 --- /dev/null +++ b/mocha_test/memoize.js @@ -0,0 +1,208 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe("memoize", function() { + + it('memoize', function(done) { + var call_order = []; + + var fn = function (arg1, arg2, callback) { + async.setImmediate(function () { + call_order.push(['fn', arg1, arg2]); + callback(null, arg1 + arg2); + }); + }; + + var fn2 = async.memoize(fn); + fn2(1, 2, function (err, result) { + assert(err === null, err + " passed instead of 'null'"); + expect(result).to.equal(3); + fn2(1, 2, function (err, result) { + expect(result).to.equal(3); + fn2(2, 2, function (err, result) { + expect(result).to.equal(4); + expect(call_order).to.eql([['fn',1,2], ['fn',2,2]]); + done(); + }); + }); + }); + }); + + it('maintains asynchrony', function(done) { + var call_order = []; + + var fn = function (arg1, arg2, callback) { + call_order.push(['fn', arg1, arg2]); + async.setImmediate(function () { + call_order.push(['cb', arg1, arg2]); + callback(null, arg1 + arg2); + }); + }; + + var fn2 = async.memoize(fn); + fn2(1, 2, function (err, result) { + expect(result).to.equal(3); + fn2(1, 2, function (err, result) { + expect(result).to.equal(3); + async.nextTick(memoize_done); + call_order.push('tick3'); + }); + call_order.push('tick2'); + }); + call_order.push('tick1'); + + function memoize_done() { + var async_call_order = [ + ['fn',1,2], // initial async call + 'tick1', // async caller + ['cb',1,2], // async callback + // ['fn',1,2], // memoized // memoized async body + 'tick2', // handler for first async call + // ['cb',1,2], // memoized // memoized async response body + 'tick3' // handler for memoized async call + ]; + expect(call_order).to.eql(async_call_order); + done(); + } + }); + + it('unmemoize', function(done) { + var call_order = []; + + var fn = function (arg1, arg2, callback) { + call_order.push(['fn', arg1, arg2]); + async.setImmediate(function () { + callback(null, arg1 + arg2); + }); + }; + + var fn2 = async.memoize(fn); + var fn3 = async.unmemoize(fn2); + fn3(1, 2, function (err, result) { + expect(result).to.equal(3); + fn3(1, 2, function (err, result) { + expect(result).to.equal(3); + fn3(2, 2, function (err, result) { + expect(result).to.equal(4); + expect(call_order).to.eql([['fn',1,2], ['fn',1,2], ['fn',2,2]]); + done(); + }); + }); + }); + }); + + it('unmemoize a not memoized function', function(done) { + var fn = function (arg1, arg2, callback) { + callback(null, arg1 + arg2); + }; + + var fn2 = async.unmemoize(fn); + fn2(1, 2, function(err, result) { + expect(result).to.equal(3); + }); + + done(); + }); + + it('error', function(done) { + var testerr = new Error('test'); + var fn = function (arg1, arg2, callback) { + callback(testerr, arg1 + arg2); + }; + async.memoize(fn)(1, 2, function (err) { + expect(err).to.equal(testerr); + }); + done(); + }); + + it('multiple calls', function(done) { + var fn = function (arg1, arg2, callback) { + assert(true); + setTimeout(function(){ + callback(null, arg1, arg2); + }, 10); + }; + var fn2 = async.memoize(fn); + fn2(1, 2, function(err, result) { + expect(result).to.equal(1, 2); + }); + fn2(1, 2, function(err, result) { + expect(result).to.equal(1, 2); + done(); + }); + }); + + it('custom hash function', function(done) { + var testerr = new Error('test'); + + var fn = function (arg1, arg2, callback) { + callback(testerr, arg1 + arg2); + }; + var fn2 = async.memoize(fn, function () { + return 'custom hash'; + }); + fn2(1, 2, function (err, result) { + expect(result).to.equal(3); + fn2(2, 2, function (err, result) { + expect(result).to.equal(3); + done(); + }); + }); + }); + + it('manually added memo value', function(done) { + var fn = async.memoize(function() { + test(false, "Function should never be called"); + }); + fn.memo.foo = ["bar"]; + fn("foo", function(val) { + expect(val).to.equal("bar"); + done(); + }); + }); + + it('avoid constructor key return undefined', function(done) { + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('constructor', function(error, results) { + expect(results).to.equal('constructor'); + done(); + }); + }); + + it('avoid __proto__ key return undefined', function(done) { + // Skip test if there is a Object.create bug (node 0.10 and some Chrome 30x versions) + var x = Object.create(null); + /* jshint proto: true */ + x.__proto__ = 'foo'; + if (x.__proto__ !== 'foo') { + return done(); + } + + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('__proto__', function(error, results) { + expect(results).to.equal('__proto__'); + done(); + }); + }); + + it('allow hasOwnProperty as key', function(done) { + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('hasOwnProperty', function(error, results) { + expect(results).to.equal('hasOwnProperty'); + done(); + }); + }); +}); diff --git a/test/test-async.js b/test/test-async.js index ae3f3c4..e35bdcf 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1377,226 +1377,6 @@ exports['whilst optional callback'] = function (test) { test.done(); }; - -exports['memoize'] = { - - 'memoize': function (test) { - test.expect(5); - var call_order = []; - - var fn = function (arg1, arg2, callback) { - async.setImmediate(function () { - call_order.push(['fn', arg1, arg2]); - callback(null, arg1 + arg2); - }); - }; - - var fn2 = async.memoize(fn); - fn2(1, 2, function (err, result) { - test.ok(err === null, err + " passed instead of 'null'"); - test.equal(result, 3); - fn2(1, 2, function (err, result) { - test.equal(result, 3); - fn2(2, 2, function (err, result) { - test.equal(result, 4); - test.same(call_order, [['fn',1,2], ['fn',2,2]]); - test.done(); - }); - }); - }); -}, - - 'maintains asynchrony': function (test) { - test.expect(3); - var call_order = []; - - var fn = function (arg1, arg2, callback) { - call_order.push(['fn', arg1, arg2]); - async.setImmediate(function () { - call_order.push(['cb', arg1, arg2]); - callback(null, arg1 + arg2); - }); - }; - - var fn2 = async.memoize(fn); - fn2(1, 2, function (err, result) { - test.equal(result, 3); - fn2(1, 2, function (err, result) { - test.equal(result, 3); - async.nextTick(memoize_done); - call_order.push('tick3'); - }); - call_order.push('tick2'); - }); - call_order.push('tick1'); - - function memoize_done() { - var async_call_order = [ - ['fn',1,2], // initial async call - 'tick1', // async caller - ['cb',1,2], // async callback - // ['fn',1,2], // memoized // memoized async body - 'tick2', // handler for first async call - // ['cb',1,2], // memoized // memoized async response body - 'tick3' // handler for memoized async call - ]; - test.same(call_order, async_call_order); - test.done(); - } -}, - - 'unmemoize': function(test) { - test.expect(4); - var call_order = []; - - var fn = function (arg1, arg2, callback) { - call_order.push(['fn', arg1, arg2]); - async.setImmediate(function () { - callback(null, arg1 + arg2); - }); - }; - - var fn2 = async.memoize(fn); - var fn3 = async.unmemoize(fn2); - fn3(1, 2, function (err, result) { - test.equal(result, 3); - fn3(1, 2, function (err, result) { - test.equal(result, 3); - fn3(2, 2, function (err, result) { - test.equal(result, 4); - test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]); - test.done(); - }); - }); - }); -}, - - 'unmemoize a not memoized function': function(test) { - test.expect(1); - - var fn = function (arg1, arg2, callback) { - callback(null, arg1 + arg2); - }; - - var fn2 = async.unmemoize(fn); - fn2(1, 2, function(err, result) { - test.equal(result, 3); - }); - - test.done(); -}, - - 'error': function (test) { - test.expect(1); - var testerr = new Error('test'); - var fn = function (arg1, arg2, callback) { - callback(testerr, arg1 + arg2); - }; - async.memoize(fn)(1, 2, function (err) { - test.equal(err, testerr); - }); - test.done(); -}, - - 'multiple calls': function (test) { - test.expect(3); - var fn = function (arg1, arg2, callback) { - test.ok(true); - setTimeout(function(){ - callback(null, arg1, arg2); - }, 10); - }; - var fn2 = async.memoize(fn); - fn2(1, 2, function(err, result) { - test.equal(result, 1, 2); - }); - fn2(1, 2, function(err, result) { - test.equal(result, 1, 2); - test.done(); - }); -}, - - 'custom hash function': function (test) { - test.expect(2); - var testerr = new Error('test'); - - var fn = function (arg1, arg2, callback) { - callback(testerr, arg1 + arg2); - }; - var fn2 = async.memoize(fn, function () { - return 'custom hash'; - }); - fn2(1, 2, function (err, result) { - test.equal(result, 3); - fn2(2, 2, function (err, result) { - test.equal(result, 3); - test.done(); - }); - }); -}, - - 'manually added memo value': function (test) { - test.expect(1); - var fn = async.memoize(function() { - test(false, "Function should never be called"); - }); - fn.memo["foo"] = ["bar"]; - fn("foo", function(val) { - test.equal(val, "bar"); - test.done(); - }); -}, - - 'avoid constructor key return undefined': function (test) { - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('constructor', function(error, results) { - test.equal(results, 'constructor'); - test.done(); - }); -}, - - 'avoid __proto__ key return undefined': function (test) { - // Skip test if there is a Object.create bug (node 0.10 and some Chrome 30x versions) - var x = Object.create(null); - /* jshint proto: true */ - x.__proto__ = 'foo'; - if (x.__proto__ !== 'foo') { - return test.done(); - } - - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('__proto__', function(error, results) { - test.equal(results, '__proto__'); - test.done(); - }); -}, - - 'allow hasOwnProperty as key': function (test) { - test.expect(1); - var fn = async.memoize(function(name, callback) { - setTimeout(function(){ - callback(null, name); - }, 100); - }); - fn('hasOwnProperty', function(error, results) { - test.equal(results, 'hasOwnProperty'); - test.done(); - }); -} - -}; - - exports['ensureAsync'] = { 'defer sync functions': function (test) { test.expect(6); -- cgit v1.2.1