diff options
-rw-r--r-- | mocha_test/iterator.js | 61 | ||||
-rw-r--r-- | mocha_test/map.js | 4 | ||||
-rw-r--r-- | mocha_test/reduce.js | 66 | ||||
-rw-r--r-- | mocha_test/seq.js | 109 | ||||
-rw-r--r-- | mocha_test/transform.js | 54 | ||||
-rw-r--r-- | mocha_test/waterfall.js | 3 | ||||
-rwxr-xr-x | test/test-async.js | 277 |
7 files changed, 294 insertions, 280 deletions
diff --git a/mocha_test/iterator.js b/mocha_test/iterator.js new file mode 100644 index 0000000..71c477c --- /dev/null +++ b/mocha_test/iterator.js @@ -0,0 +1,61 @@ +var async = require('../lib'); +var expect = require('chai').expect; + +describe('iterator', function() { + + it('iterator', function(done) { + var call_order = []; + var iterator = async.iterator([ + function(){call_order.push(1);}, + function(arg1){ + expect(arg1).to.equal('arg1'); + call_order.push(2); + }, + function(arg1, arg2){ + expect(arg1).to.equal('arg1'); + expect(arg2).to.equal('arg2'); + call_order.push(3); + } + ]); + iterator(); + expect(call_order).to.eql([1]); + var iterator2 = iterator(); + expect(call_order).to.eql([1,1]); + var iterator3 = iterator2('arg1'); + expect(call_order).to.eql([1,1,2]); + var iterator4 = iterator3('arg1', 'arg2'); + expect(call_order).to.eql([1,1,2,3]); + expect(iterator4).to.equal(null); + done(); + }); + + it('iterator empty array', function(done) { + var iterator = async.iterator([]); + expect(iterator()).to.equal(null); + expect(iterator.next()).to.equal(null); + done(); + }); + + it('iterator.next', function(done) { + var call_order = []; + var iterator = async.iterator([ + function(){call_order.push(1);}, + function(arg1){ + expect(arg1).to.equal('arg1'); + call_order.push(2); + }, + function(arg1, arg2){ + expect(arg1).to.equal('arg1'); + expect(arg2).to.equal('arg2'); + call_order.push(3); + } + ]); + var fn = iterator.next(); + var iterator2 = fn('arg1'); + expect(call_order).to.eql([2]); + iterator2('arg1','arg2'); + expect(call_order).to.eql([2,3]); + expect(iterator2.next()).to.equal(null); + done(); + }); +}); diff --git a/mocha_test/map.js b/mocha_test/map.js index 5161765..a537427 100644 --- a/mocha_test/map.js +++ b/mocha_test/map.js @@ -191,7 +191,7 @@ describe("map", function() { it('mapLimit empty array', function(done) { async.mapLimit([], 2, function(x, callback) { - test.ok(false, 'iteratee should not be called'); + assert(false, 'iteratee should not be called'); callback(); }, function(err) { if (err) throw err; @@ -230,7 +230,7 @@ describe("map", function() { it('mapLimit zero limit', function(done) { async.mapLimit([0, 1, 2, 3, 4, 5], 0, function(x, callback) { - test.ok(false, 'iteratee should not be called'); + assert(false, 'iteratee should not be called'); callback(); }, function(err, results) { expect(results).to.eql([]); diff --git a/mocha_test/reduce.js b/mocha_test/reduce.js new file mode 100644 index 0000000..f5dce59 --- /dev/null +++ b/mocha_test/reduce.js @@ -0,0 +1,66 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe('reduce', function() { + + it('reduce', function(done) { + var call_order = []; + async.reduce([1,2,3], 0, function(a, x, callback){ + call_order.push(x); + callback(null, a + x); + }, function(err, result){ + assert(err === null, err + " passed instead of 'null'"); + expect(result).to.equal(6); + expect(call_order).to.eql([1,2,3]); + done(); + }); + }); + + it('reduce async with non-reference memo', function(done) { + async.reduce([1,3,2], 0, function(a, x, callback){ + setTimeout(function(){callback(null, a + x);}, Math.random()*100); + }, function(err, result){ + expect(result).to.equal(6); + done(); + }); + }); + + it('reduce error', function(done) { + async.reduce([1,2,3], 0, function(a, x, callback){ + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + }); + setTimeout(done, 50); + }); + + it('inject alias', function(done) { + expect(async.inject).to.equal(async.reduce); + done(); + }); + + it('foldl alias', function(done) { + expect(async.foldl).to.equal(async.reduce); + done(); + }); + + it('reduceRight', function(done) { + var call_order = []; + var a = [1,2,3]; + async.reduceRight(a, 0, function(a, x, callback){ + call_order.push(x); + callback(null, a + x); + }, function(err, result){ + expect(result).to.equal(6); + expect(call_order).to.eql([3,2,1]); + expect(a).to.eql([1,2,3]); + done(); + }); + }); + + it('foldr alias', function(done) { + expect(async.foldr).to.equal(async.reduceRight); + done(); + }); +}); diff --git a/mocha_test/seq.js b/mocha_test/seq.js new file mode 100644 index 0000000..1f7cbfc --- /dev/null +++ b/mocha_test/seq.js @@ -0,0 +1,109 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe('seq', function() { + + it('seq', function(done) { + var add2 = function (n, cb) { + expect(n).to.equal(3); + setTimeout(function () { + cb(null, n + 2); + }, 50); + }; + var mul3 = function (n, cb) { + expect(n).to.equal(5); + setTimeout(function () { + cb(null, n * 3); + }, 15); + }; + var add1 = function (n, cb) { + expect(n).to.equal(15); + setTimeout(function () { + cb(null, n + 1); + }, 100); + }; + var add2mul3add1 = async.seq(add2, mul3, add1); + add2mul3add1(3, function (err, result) { + if (err) { + return done(err); + } + assert(err === null, err + " passed instead of 'null'"); + expect(result).to.equal(16); + done(); + }); + }); + + it('seq error', function(done) { + var testerr = new Error('test'); + + var add2 = function (n, cb) { + expect(n).to.equal(3); + setTimeout(function () { + cb(null, n + 2); + }, 50); + }; + var mul3 = function (n, cb) { + expect(n).to.equal(5); + setTimeout(function () { + cb(testerr); + }, 15); + }; + var add1 = function (n, cb) { + assert(false, 'add1 should not get called'); + setTimeout(function () { + cb(null, n + 1); + }, 100); + }; + var add2mul3add1 = async.seq(add2, mul3, add1); + add2mul3add1(3, function (err) { + expect(err).to.equal(testerr); + done(); + }); + }); + + it('seq binding', function(done) { + var testcontext = {name: 'foo'}; + + var add2 = function (n, cb) { + expect(this).to.equal(testcontext); + setTimeout(function () { + cb(null, n + 2); + }, 50); + }; + var mul3 = function (n, cb) { + expect(this).to.equal(testcontext); + setTimeout(function () { + cb(null, n * 3); + }, 15); + }; + var add2mul3 = async.seq(add2, mul3); + add2mul3.call(testcontext, 3, function (err, result) { + if (err) { + return done(err); + } + expect(this).to.equal(testcontext); + expect(result).to.equal(15); + done(); + }); + }); + + it('seq without callback', function(done) { + var testcontext = {name: 'foo'}; + + var add2 = function (n, cb) { + expect(this).to.equal(testcontext); + setTimeout(function () { + cb(null, n + 2); + }, 50); + }; + var mul3 = function () { + expect(this).to.equal(testcontext); + setTimeout(function () { + done(); + }, 15); + }; + var add2mul3 = async.seq(add2, mul3); + add2mul3.call(testcontext, 3); + }); +}); diff --git a/mocha_test/transform.js b/mocha_test/transform.js new file mode 100644 index 0000000..5c04f56 --- /dev/null +++ b/mocha_test/transform.js @@ -0,0 +1,54 @@ +var async = require('../lib'); +var expect = require('chai').expect; + +describe('transform', function() { + + it('transform implictly determines memo if not provided', function(done) { + async.transform([1,2,3], function(memo, x, v, callback){ + memo.push(x + 1); + callback(); + }, function(err, result){ + expect(result).to.eql([2, 3, 4]); + done(); + }); + }); + + it('transform async with object memo', function(done) { + async.transform([1,3,2], {}, function(memo, v, k, callback){ + setTimeout(function() { + memo[k] = v; + callback(); + }); + }, function(err, result) { + expect(err).to.equal(null); + expect(result).to.eql({ + 0: 1, + 1: 3, + 2: 2 + }); + done(); + }); + }); + + it('transform iterating object', function(done) { + async.transform({a: 1, b: 3, c: 2}, function(memo, v, k, callback){ + setTimeout(function() { + memo[k] = v + 1; + callback(); + }); + }, function(err, result) { + expect(err).to.equal(null); + expect(result).to.eql({a: 2, b: 4, c: 3}); + done(); + }); + }); + + it('transform error', function(done) { + async.transform([1,2,3], function(a, v, k, callback){ + callback('error'); + }, function(err){ + expect(err).to.equal('error'); + done(); + }); + }); +}); diff --git a/mocha_test/waterfall.js b/mocha_test/waterfall.js index b1d1561..7053b2c 100644 --- a/mocha_test/waterfall.js +++ b/mocha_test/waterfall.js @@ -1,5 +1,6 @@ var async = require('../lib'); var expect = require('chai').expect; +var assert = require('assert'); describe("waterfall", function () { @@ -80,7 +81,7 @@ describe("waterfall", function () { callback('error'); }, function(callback){ - test.ok(false, 'next function should not be called'); + assert(false, 'next function should not be called'); callback(); } ], function(err){ diff --git a/test/test-async.js b/test/test-async.js index d5cb5e9..c91648c 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -21,283 +21,6 @@ function isBrowser() { (process + "" !== "[object process]"); // browserify } -exports['seq'] = function (test) { - test.expect(5); - var add2 = function (n, cb) { - test.equal(n, 3); - setTimeout(function () { - cb(null, n + 2); - }, 50); - }; - var mul3 = function (n, cb) { - test.equal(n, 5); - setTimeout(function () { - cb(null, n * 3); - }, 15); - }; - var add1 = function (n, cb) { - test.equal(n, 15); - setTimeout(function () { - cb(null, n + 1); - }, 100); - }; - var add2mul3add1 = async.seq(add2, mul3, add1); - add2mul3add1(3, function (err, result) { - if (err) { - return test.done(err); - } - test.ok(err === null, err + " passed instead of 'null'"); - test.equal(result, 16); - test.done(); - }); -}; - -exports['seq error'] = function (test) { - test.expect(3); - var testerr = new Error('test'); - - var add2 = function (n, cb) { - test.equal(n, 3); - setTimeout(function () { - cb(null, n + 2); - }, 50); - }; - var mul3 = function (n, cb) { - test.equal(n, 5); - setTimeout(function () { - cb(testerr); - }, 15); - }; - var add1 = function (n, cb) { - test.ok(false, 'add1 should not get called'); - setTimeout(function () { - cb(null, n + 1); - }, 100); - }; - var add2mul3add1 = async.seq(add2, mul3, add1); - add2mul3add1(3, function (err) { - test.equal(err, testerr); - test.done(); - }); -}; - -exports['seq binding'] = function (test) { - test.expect(4); - var testcontext = {name: 'foo'}; - - var add2 = function (n, cb) { - test.equal(this, testcontext); - setTimeout(function () { - cb(null, n + 2); - }, 50); - }; - var mul3 = function (n, cb) { - test.equal(this, testcontext); - setTimeout(function () { - cb(null, n * 3); - }, 15); - }; - var add2mul3 = async.seq(add2, mul3); - add2mul3.call(testcontext, 3, function (err, result) { - if (err) { - return test.done(err); - } - test.equal(this, testcontext); - test.equal(result, 15); - test.done(); - }); -}; - -exports['seq without callback'] = function (test) { - test.expect(2); - var testcontext = {name: 'foo'}; - - var add2 = function (n, cb) { - test.equal(this, testcontext); - setTimeout(function () { - cb(null, n + 2); - }, 50); - }; - var mul3 = function () { - test.equal(this, testcontext); - setTimeout(function () { - test.done(); - }, 15); - }; - var add2mul3 = async.seq(add2, mul3); - add2mul3.call(testcontext, 3); -}; - -exports['iterator'] = function(test){ - var call_order = []; - var iterator = async.iterator([ - function(){call_order.push(1);}, - function(arg1){ - test.equals(arg1, 'arg1'); - call_order.push(2); - }, - function(arg1, arg2){ - test.equals(arg1, 'arg1'); - test.equals(arg2, 'arg2'); - call_order.push(3); - } - ]); - iterator(); - test.same(call_order, [1]); - var iterator2 = iterator(); - test.same(call_order, [1,1]); - var iterator3 = iterator2('arg1'); - test.same(call_order, [1,1,2]); - var iterator4 = iterator3('arg1', 'arg2'); - test.same(call_order, [1,1,2,3]); - test.equals(iterator4, undefined); - test.done(); -}; - -exports['iterator empty array'] = function(test){ - var iterator = async.iterator([]); - test.equals(iterator(), undefined); - test.equals(iterator.next(), undefined); - test.done(); -}; - -exports['iterator.next'] = function(test){ - var call_order = []; - var iterator = async.iterator([ - function(){call_order.push(1);}, - function(arg1){ - test.equals(arg1, 'arg1'); - call_order.push(2); - }, - function(arg1, arg2){ - test.equals(arg1, 'arg1'); - test.equals(arg2, 'arg2'); - call_order.push(3); - } - ]); - var fn = iterator.next(); - var iterator2 = fn('arg1'); - test.same(call_order, [2]); - iterator2('arg1','arg2'); - test.same(call_order, [2,3]); - test.equals(iterator2.next(), undefined); - test.done(); -}; - -exports['reduce'] = function(test){ - var call_order = []; - async.reduce([1,2,3], 0, function(a, x, callback){ - call_order.push(x); - callback(null, a + x); - }, function(err, result){ - test.ok(err === null, err + " passed instead of 'null'"); - test.equals(result, 6); - test.same(call_order, [1,2,3]); - test.done(); - }); -}; - -exports['reduce async with non-reference memo'] = function(test){ - async.reduce([1,3,2], 0, function(a, x, callback){ - setTimeout(function(){callback(null, a + x);}, Math.random()*100); - }, function(err, result){ - test.equals(result, 6); - test.done(); - }); -}; - -exports['reduce error'] = function(test){ - test.expect(1); - async.reduce([1,2,3], 0, function(a, x, callback){ - callback('error'); - }, function(err){ - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['inject alias'] = function(test){ - test.equals(async.inject, async.reduce); - test.done(); -}; - -exports['foldl alias'] = function(test){ - test.equals(async.foldl, async.reduce); - test.done(); -}; - -exports['reduceRight'] = function(test){ - var call_order = []; - var a = [1,2,3]; - async.reduceRight(a, 0, function(a, x, callback){ - call_order.push(x); - callback(null, a + x); - }, function(err, result){ - test.equals(result, 6); - test.same(call_order, [3,2,1]); - test.same(a, [1,2,3]); - test.done(); - }); -}; - -exports['foldr alias'] = function(test){ - test.equals(async.foldr, async.reduceRight); - test.done(); -}; - -exports['transform implictly determines memo if not provided'] = function(test){ - async.transform([1,2,3], function(memo, x, v, callback){ - memo.push(x + 1); - callback(); - }, function(err, result){ - test.same(result, [2, 3, 4]); - test.done(); - }); -}; - -exports['transform async with object memo'] = function(test){ - test.expect(2); - - async.transform([1,3,2], {}, function(memo, v, k, callback){ - setTimeout(function() { - memo[k] = v; - callback(); - }); - }, function(err, result) { - test.equals(err, null); - test.same(result, { - 0: 1, - 1: 3, - 2: 2 - }); - test.done(); - }); -}; - -exports['transform iterating object'] = function(test){ - test.expect(2); - - async.transform({a: 1, b: 3, c: 2}, function(memo, v, k, callback){ - setTimeout(function() { - memo[k] = v + 1; - callback(); - }); - }, function(err, result) { - test.equals(err, null); - test.same(result, {a: 2, b: 4, c: 3}); - test.done(); - }); -}; - -exports['transform error'] = function(test){ - async.transform([1,2,3], function(a, v, k, callback){ - callback('error'); - }, function(err){ - test.equals(err, 'error'); - test.done(); - }); -}; - exports['sortBy'] = function(test){ test.expect(2); |