From e17f3d7d534dc6bf9aad530c3a3e77924a9070fe Mon Sep 17 00:00:00 2001 From: ezubarev Date: Fri, 6 May 2016 13:31:54 +0600 Subject: Convert during/ensureAsync tests to mocha --- mocha_test/during.js | 97 +++++++++++++++++++++++++++ mocha_test/ensureAsync.js | 53 +++++++++++++++ test/test-async.js | 162 ---------------------------------------------- 3 files changed, 150 insertions(+), 162 deletions(-) create mode 100644 mocha_test/during.js diff --git a/mocha_test/during.js b/mocha_test/during.js new file mode 100644 index 0000000..851acf1 --- /dev/null +++ b/mocha_test/during.js @@ -0,0 +1,97 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe('during', function() { + + it('during', function(done) { + var call_order = []; + + var count = 0; + async.during( + function (cb) { + call_order.push(['test', count]); + cb(null, count < 5); + }, + function (cb) { + call_order.push(['iteratee', count]); + count++; + cb(); + }, + function (err) { + assert(err === null, err + " passed instead of 'null'"); + expect(call_order).to.eql([ + ['test', 0], + ['iteratee', 0], ['test', 1], + ['iteratee', 1], ['test', 2], + ['iteratee', 2], ['test', 3], + ['iteratee', 3], ['test', 4], + ['iteratee', 4], ['test', 5], + ]); + expect(count).to.equal(5); + done(); + } + ); + }); + + it('doDuring', function(done) { + var call_order = []; + + var count = 0; + async.doDuring( + function (cb) { + call_order.push(['iteratee', count]); + count++; + cb(); + }, + function (cb) { + call_order.push(['test', count]); + cb(null, count < 5); + }, + function (err) { + assert(err === null, err + " passed instead of 'null'"); + expect(call_order).to.eql([ + ['iteratee', 0], ['test', 1], + ['iteratee', 1], ['test', 2], + ['iteratee', 2], ['test', 3], + ['iteratee', 3], ['test', 4], + ['iteratee', 4], ['test', 5], + ]); + expect(count).to.equal(5); + done(); + } + ); + }); + + it('doDuring - error test', function(done) { + var error = new Error('asdas'); + + async.doDuring( + function (cb) { + cb(error); + }, + function () {}, + function (err) { + expect(err).to.equal(error); + done(); + } + ); + }); + + it('doDuring - error iteratee', function(done) { + var error = new Error('asdas'); + + async.doDuring( + function (cb) { + cb(null); + }, + function (cb) { + cb(error); + }, + function (err) { + expect(err).to.equal(error); + done(); + } + ); + }); +}); diff --git a/mocha_test/ensureAsync.js b/mocha_test/ensureAsync.js index c33e344..fd8cc3f 100644 --- a/mocha_test/ensureAsync.js +++ b/mocha_test/ensureAsync.js @@ -1,11 +1,64 @@ var async = require('../lib'); var expect = require('chai').expect; +var assert = require('assert'); describe('ensureAsync', function() { var passContext = function(cb) { cb(this); }; + it('defer sync functions', function(done) { + var sync = true; + async.ensureAsync(function (arg1, arg2, cb) { + expect(arg1).to.equal(1); + expect(arg2).to.equal(2); + cb(null, 4, 5); + })(1, 2, function (err, arg4, arg5) { + expect(err).to.equal(null); + expect(arg4).to.equal(4); + expect(arg5).to.equal(5); + assert(!sync, 'callback called on same tick'); + done(); + }); + sync = false; + }); + + it('do not defer async functions', function(done) { + var sync = false; + async.ensureAsync(function (arg1, arg2, cb) { + expect(arg1).to.equal(1); + expect(arg2).to.equal(2); + async.setImmediate(function () { + sync = true; + cb(null, 4, 5); + sync = false; + }); + })(1, 2, function (err, arg4, arg5) { + expect(err).to.equal(null); + expect(arg4).to.equal(4); + expect(arg5).to.equal(5); + assert(sync, 'callback called on next tick'); + done(); + }); + }); + + it('double wrapping', function(done) { + var sync = true; + async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { + expect(arg1).to.equal(1); + expect(arg2).to.equal(2); + cb(null, 4, 5); + }))(1, 2, function (err, arg4, arg5) { + expect(err).to.equal(null); + expect(arg4).to.equal(4); + expect(arg5).to.equal(5); + assert(!sync, 'callback called on same tick'); + done(); + }); + sync = false; + }); + + it('should propely bind context to the wrapped function', function(done) { // call bind after wrapping with ensureAsync diff --git a/test/test-async.js b/test/test-async.js index 45d916a..ba1a6f3 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -156,165 +156,3 @@ exports['concatSeries'] = function(test){ test.done(); }); }; - -exports['during'] = function (test) { - var call_order = []; - - var count = 0; - async.during( - function (cb) { - call_order.push(['test', count]); - cb(null, count < 5); - }, - function (cb) { - call_order.push(['iteratee', count]); - count++; - cb(); - }, - function (err) { - test.ok(err === null, err + " passed instead of 'null'"); - test.same(call_order, [ - ['test', 0], - ['iteratee', 0], ['test', 1], - ['iteratee', 1], ['test', 2], - ['iteratee', 2], ['test', 3], - ['iteratee', 3], ['test', 4], - ['iteratee', 4], ['test', 5], - ]); - test.equals(count, 5); - test.done(); - } - ); -}; - -exports['doDuring'] = function (test) { - var call_order = []; - - var count = 0; - async.doDuring( - function (cb) { - call_order.push(['iteratee', count]); - count++; - cb(); - }, - function (cb) { - call_order.push(['test', count]); - cb(null, count < 5); - }, - function (err) { - test.ok(err === null, err + " passed instead of 'null'"); - test.same(call_order, [ - ['iteratee', 0], ['test', 1], - ['iteratee', 1], ['test', 2], - ['iteratee', 2], ['test', 3], - ['iteratee', 3], ['test', 4], - ['iteratee', 4], ['test', 5], - ]); - test.equals(count, 5); - test.done(); - } - ); -}; - -exports['doDuring - error test'] = function (test) { - test.expect(1); - var error = new Error('asdas'); - - async.doDuring( - function (cb) { - cb(error); - }, - function () {}, - function (err) { - test.equal(err, error); - test.done(); - } - ); -}; - -exports['doDuring - error iteratee'] = function (test) { - test.expect(1); - var error = new Error('asdas'); - - async.doDuring( - function (cb) { - cb(null); - }, - function (cb) { - cb(error); - }, - function (err) { - test.equal(err, error); - test.done(); - } - ); -}; - -exports['ensureAsync'] = { - 'defer sync functions': function (test) { - test.expect(6); - var sync = true; - async.ensureAsync(function (arg1, arg2, cb) { - test.equal(arg1, 1); - test.equal(arg2, 2); - cb(null, 4, 5); - })(1, 2, function (err, arg4, arg5) { - test.equal(err, null); - test.equal(arg4, 4); - test.equal(arg5, 5); - test.ok(!sync, 'callback called on same tick'); - test.done(); - }); - sync = false; - }, - - 'do not defer async functions': function (test) { - test.expect(6); - var sync = false; - async.ensureAsync(function (arg1, arg2, cb) { - test.equal(arg1, 1); - test.equal(arg2, 2); - async.setImmediate(function () { - sync = true; - cb(null, 4, 5); - sync = false; - }); - })(1, 2, function (err, arg4, arg5) { - test.equal(err, null); - test.equal(arg4, 4); - test.equal(arg5, 5); - test.ok(sync, 'callback called on next tick'); - test.done(); - }); - }, - - 'double wrapping': function (test) { - test.expect(6); - var sync = true; - async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { - test.equal(arg1, 1); - test.equal(arg2, 2); - cb(null, 4, 5); - }))(1, 2, function (err, arg4, arg5) { - test.equal(err, null); - test.equal(arg4, 4); - test.equal(arg5, 5); - test.ok(!sync, 'callback called on same tick'); - test.done(); - }); - sync = false; - } -}; - -exports['constant'] = function (test) { - test.expect(5); - var f = async.constant(42, 1, 2, 3); - f(function (err, value, a, b, c) { - test.ok(!err); - test.ok(value === 42); - test.ok(a === 1); - test.ok(b === 2); - test.ok(c === 3); - test.done(); - }); -}; -- cgit v1.2.1