Permalink
Browse files

Renaming forEachLimit to _forEachLimit, adding mapLimit tests

  • Loading branch information...
calvinfo committed Nov 24, 2012
1 parent 49c7fd7 commit eaaad5f07bb07bad276058e15a607b137dee2003
Showing with 73 additions and 4 deletions.
  1. +3 −3 lib/async.js
  2. +70 −1 test/test-async.js
View
@@ -118,11 +118,11 @@
};
async.forEachLimit = function (arr, limit, iterator, callback) {
- var fn = forEachLimit(limit);
+ var fn = _forEachLimit(limit);
fn.apply(null, [arr, iterator, callback]);
};
- var forEachLimit = function (limit) {
+ var _forEachLimit = function (limit) {
return function (arr, iterator, callback) {
callback = callback || function () {};
@@ -195,7 +195,7 @@
async.mapSeries = doSeries(_asyncMap);
async.mapLimit = function (arr, limit, iterator, callback) {
- var fn = forEachLimit(limit);
+ var fn = _forEachLimit(limit);
return _asyncMap.apply(null, [fn, arr, iterator, callback]);
};
View
@@ -652,7 +652,7 @@ exports['forEachLimit error'] = function(test){
test.expect(2);
var arr = [0,1,2,3,4,5,6,7,8,9];
var call_order = [];
-
+
async.forEachLimit(arr, 3, function(x, callback){
call_order.push(x);
if (x === 2) {
@@ -730,6 +730,75 @@ exports['mapSeries error'] = function(test){
setTimeout(test.done, 50);
};
+
+exports['mapLimit'] = function(test){
+ var call_order = [];
+ async.mapLimit([1,3,2], 2, mapIterator.bind(this, call_order), function(err, results){
+ test.same(call_order, [1,3,2]);
+ test.same(results, [2,6,4]);
+ test.done();
+ });
+};
+
+exports['mapLimit empty array'] = function(test){
+ test.expect(1);
+ async.mapLimit([], 2, function(x, callback){
+ test.ok(false, 'iterator should not be called');
+ callback();
+ }, function(err){
+ test.ok(true, 'should call callback');
+ });
+ setTimeout(test.done, 25);
+};
+
+exports['mapLimit limit exceeds size'] = function(test){
+ var call_order = [];
+ async.mapLimit([0,1,2,3,4,5,6,7,8,9], 20, mapIterator.bind(this, call_order), function(err, results){
+ test.same(call_order, [0,1,2,3,4,5,6,7,8,9]);
+ test.same(results, [0,2,4,6,8,10,12,14,16,18]);
+ test.done();
+ });
+};
+
+exports['mapLimit limit equal size'] = function(test){
+ var call_order = [];
+ async.mapLimit([0,1,2,3,4,5,6,7,8,9], 10, mapIterator.bind(this, call_order), function(err, results){
+ test.same(call_order, [0,1,2,3,4,5,6,7,8,9]);
+ test.same(results, [0,2,4,6,8,10,12,14,16,18]);
+ test.done();
+ });
+};
+
+exports['mapLimit zero limit'] = function(test){
+ test.expect(2);
+ async.mapLimit([0,1,2,3,4,5], 0, function(x, callback){
+ test.ok(false, 'iterator should not be called');
+ callback();
+ }, function(err, results){
+ test.same(results, []);
+ test.ok(true, 'should call callback');
+ });
+ setTimeout(test.done, 25);
+};
+
+exports['mapLimit error'] = function(test){
+ test.expect(2);
+ var arr = [0,1,2,3,4,5,6,7,8,9];
+ var call_order = [];
+
+ async.mapLimit(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['reduce'] = function(test){
var call_order = [];
async.reduce([1,2,3], 0, function(a, x, callback){

0 comments on commit eaaad5f

Please sign in to comment.