summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorezubarev <zubarev.eugene@gmail.com>2016-05-06 11:46:04 +0600
committerezubarev <zubarev.eugene@gmail.com>2016-05-06 11:47:14 +0600
commit5dac23157a21f471993ce263ee73a517f32c8e61 (patch)
tree3676b1a0341a0e360804f0f9db8338319a9c893a
parent4f58e263911a6aececf9c6c37ce057edaf435236 (diff)
downloadasync-5dac23157a21f471993ce263ee73a517f32c8e61.tar.gz
Convert series tests to mocha
-rw-r--r--mocha_test/series.js200
-rwxr-xr-xtest/test-async.js224
2 files changed, 200 insertions, 224 deletions
diff --git a/mocha_test/series.js b/mocha_test/series.js
new file mode 100644
index 0000000..0237582
--- /dev/null
+++ b/mocha_test/series.js
@@ -0,0 +1,200 @@
+var async = require('../lib');
+var expect = require('chai').expect;
+var assert = require('assert');
+var isBrowser = require('./support/is_browser');
+var getFunctionsObject = require('./support/get_function_object');
+
+describe('series', function() {
+ it('series', function(done) {
+ var call_order = [];
+ async.series([
+ function(callback){
+ setTimeout(function(){
+ call_order.push(1);
+ callback(null, 1);
+ }, 25);
+ },
+ function(callback){
+ setTimeout(function(){
+ call_order.push(2);
+ callback(null, 2);
+ }, 50);
+ },
+ function(callback){
+ setTimeout(function(){
+ call_order.push(3);
+ callback(null, 3,3);
+ }, 15);
+ }
+ ],
+ function(err, results){
+ assert(err === null, err + " passed instead of 'null'");
+ expect(results).to.eql([1,2,[3,3]]);
+ expect(call_order).to.eql([1,2,3]);
+ done();
+ });
+ });
+
+ it('with reflect', function(done) {
+ var call_order = [];
+ async.series([
+ async.reflect(function(callback){
+ setTimeout(function(){
+ call_order.push(1);
+ callback(null, 1);
+ }, 25);
+ }),
+ async.reflect(function(callback){
+ setTimeout(function(){
+ call_order.push(2);
+ callback(null, 2);
+ }, 50);
+ }),
+ async.reflect(function(callback){
+ setTimeout(function(){
+ call_order.push(3);
+ callback(null, 3,3);
+ }, 15);
+ })
+ ],
+ function(err, results){
+ assert(err === null, err + " passed instead of 'null'");
+ expect(results).to.eql([
+ { value: 1 },
+ { value: 2 },
+ { value: [3,3] }
+ ]);
+ expect(call_order).to.eql([1,2,3]);
+ done();
+ });
+ });
+
+ it('empty array', function(done) {
+ async.series([], function(err, results){
+ expect(err).to.equal(null);
+ expect(results).to.eql([]);
+ done();
+ });
+ });
+
+ it('error', function(done) {
+ async.series([
+ function(callback){
+ callback('error', 1);
+ },
+ function(callback){
+ assert(false, 'should not be called');
+ callback('error2', 2);
+ }
+ ],
+ function(err){
+ expect(err).to.equal('error');
+ });
+ setTimeout(done, 100);
+ });
+
+ it('error with reflect', function(done) {
+ async.series([
+ async.reflect(function(callback){
+ callback('error', 1);
+ }),
+ async.reflect(function(callback){
+ callback('error2', 2);
+ }),
+ async.reflect(function(callback){
+ callback(null, 1);
+ })
+ ],
+ function(err, results){
+ assert(err === null, err + " passed instead of 'null'");
+ expect(results).to.eql([
+ { error: 'error' },
+ { error: 'error2' },
+ { value: 1 }
+ ]);
+ done();
+ });
+ });
+
+ it('no callback', function(done) {
+ async.series([
+ function(callback){callback();},
+ function(callback){callback(); done();},
+ ]);
+ });
+
+ it('object', function(done) {
+ var call_order = [];
+ async.series(getFunctionsObject(call_order), function(err, results){
+ expect(err).to.equal(null);
+ expect(results).to.eql({
+ one: 1,
+ two: 2,
+ three: [3,3]
+ });
+ expect(call_order).to.eql([1,2,3]);
+ done();
+ });
+ });
+
+ it('call in another context', function(done) {
+ if (isBrowser()) {
+ // node only test
+ done();
+ return;
+ }
+ var vm = require('vm');
+ var sandbox = {
+ async: async,
+ done: done
+ };
+
+ var fn = "(" + (function () {
+ async.series([function (callback) {
+ callback();
+ }], function (err) {
+ if (err) {
+ return done(err);
+ }
+ done();
+ });
+ }).toString() + "())";
+
+ vm.runInNewContext(fn, sandbox);
+ });
+
+ // Issue 10 on github: https://github.com/caolan/async/issues#issue/10
+ it('falsy return values', function(done) {
+ function taskFalse(callback) {
+ async.nextTick(function() {
+ callback(null, false);
+ });
+ }
+ function taskUndefined(callback) {
+ async.nextTick(function() {
+ callback(null, undefined);
+ });
+ }
+ function taskEmpty(callback) {
+ async.nextTick(function() {
+ callback(null);
+ });
+ }
+ function taskNull(callback) {
+ async.nextTick(function() {
+ callback(null, null);
+ });
+ }
+ async.series(
+ [taskFalse, taskUndefined, taskEmpty, taskNull],
+ function(err, results) {
+ expect(results.length).to.equal(4);
+ assert.strictEqual(results[0], false);
+ assert.strictEqual(results[1], undefined);
+ assert.strictEqual(results[2], undefined);
+ assert.strictEqual(results[3], null);
+ done();
+ }
+ );
+ });
+});
diff --git a/test/test-async.js b/test/test-async.js
index e02c0df..c7613d0 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -16,35 +16,11 @@ if (!Function.prototype.bind) {
};
}
-function getFunctionsObject(call_order) {
- return {
- one: function(callback){
- setTimeout(function(){
- call_order.push(1);
- callback(null, 1);
- }, 125);
- },
- two: function(callback){
- setTimeout(function(){
- call_order.push(2);
- callback(null, 2);
- }, 200);
- },
- three: function(callback){
- setTimeout(function(){
- call_order.push(3);
- callback(null, 3,3);
- }, 50);
- }
- };
-}
-
function isBrowser() {
return (typeof process === "undefined") ||
(process + "" !== "[object process]"); // browserify
}
-
exports['seq'] = function (test) {
test.expect(5);
var add2 = function (n, cb) {
@@ -152,206 +128,6 @@ exports['seq without callback'] = function (test) {
add2mul3.call(testcontext, 3);
};
-exports['series'] = {
-
- 'series': function(test){
- var call_order = [];
- async.series([
- function(callback){
- setTimeout(function(){
- call_order.push(1);
- callback(null, 1);
- }, 25);
- },
- function(callback){
- setTimeout(function(){
- call_order.push(2);
- callback(null, 2);
- }, 50);
- },
- function(callback){
- setTimeout(function(){
- call_order.push(3);
- callback(null, 3,3);
- }, 15);
- }
- ],
- function(err, results){
- test.ok(err === null, err + " passed instead of 'null'");
- test.same(results, [1,2,[3,3]]);
- test.same(call_order, [1,2,3]);
- test.done();
- });
-},
-
- 'with reflect': function(test){
- var call_order = [];
- async.series([
- async.reflect(function(callback){
- setTimeout(function(){
- call_order.push(1);
- callback(null, 1);
- }, 25);
- }),
- async.reflect(function(callback){
- setTimeout(function(){
- call_order.push(2);
- callback(null, 2);
- }, 50);
- }),
- async.reflect(function(callback){
- setTimeout(function(){
- call_order.push(3);
- callback(null, 3,3);
- }, 15);
- })
- ],
- function(err, results){
- test.ok(err === null, err + " passed instead of 'null'");
- test.deepEqual(results, [
- { value: 1 },
- { value: 2 },
- { value: [3,3] }
- ]);
- test.same(call_order, [1,2,3]);
- test.done();
- });
-},
-
- 'empty array': function(test){
- async.series([], function(err, results){
- test.equals(err, null);
- test.same(results, []);
- test.done();
- });
-},
-
- 'error': function(test){
- test.expect(1);
- async.series([
- function(callback){
- callback('error', 1);
- },
- function(callback){
- test.ok(false, 'should not be called');
- callback('error2', 2);
- }
- ],
- function(err){
- test.equals(err, 'error');
- });
- setTimeout(test.done, 100);
-},
-
- 'error with reflect': function(test){
- test.expect(2);
- async.series([
- async.reflect(function(callback){
- callback('error', 1);
- }),
- async.reflect(function(callback){
- callback('error2', 2);
- }),
- async.reflect(function(callback){
- callback(null, 1);
- })
- ],
- function(err, results){
- test.ok(err === null, err + " passed instead of 'null'");
- test.deepEqual(results, [
- { error: 'error' },
- { error: 'error2' },
- { value: 1 }
- ]);
- test.done();
- });
-},
-
- 'no callback': function(test){
- async.series([
- function(callback){callback();},
- function(callback){callback(); test.done();},
- ]);
-},
-
- 'object': function(test){
- var call_order = [];
- async.series(getFunctionsObject(call_order), function(err, results){
- test.equals(err, null);
- test.same(results, {
- one: 1,
- two: 2,
- three: [3,3]
- });
- test.same(call_order, [1,2,3]);
- test.done();
- });
-},
-
- 'call in another context': function(test) {
- if (isBrowser()) {
- // node only test
- test.done();
- return;
- }
- var vm = require('vm');
- var sandbox = {
- async: async,
- test: test
- };
-
- var fn = "(" + (function () {
- async.series([function (callback) {
- callback();
- }], function (err) {
- if (err) {
- return test.done(err);
- }
- test.done();
- });
- }).toString() + "())";
-
- vm.runInNewContext(fn, sandbox);
-},
-
- // Issue 10 on github: https://github.com/caolan/async/issues#issue/10
- 'falsy return values': function (test) {
- function taskFalse(callback) {
- async.nextTick(function() {
- callback(null, false);
- });
- }
- function taskUndefined(callback) {
- async.nextTick(function() {
- callback(null, undefined);
- });
- }
- function taskEmpty(callback) {
- async.nextTick(function() {
- callback(null);
- });
- }
- function taskNull(callback) {
- async.nextTick(function() {
- callback(null, null);
- });
- }
- async.series(
- [taskFalse, taskUndefined, taskEmpty, taskNull],
- function(err, results) {
- test.equal(results.length, 4);
- test.strictEqual(results[0], false);
- test.strictEqual(results[1], undefined);
- test.strictEqual(results[2], undefined);
- test.strictEqual(results[3], null);
- test.done();
- }
- );
-}
-
-};
-
-
exports['iterator'] = function(test){
var call_order = [];
var iterator = async.iterator([