diff options
author | ezubarev <zubarev.eugene@gmail.com> | 2016-05-05 22:33:06 +0600 |
---|---|---|
committer | ezubarev <zubarev.eugene@gmail.com> | 2016-05-05 22:50:32 +0600 |
commit | f4ce893cdec9da946083a32432ac4a3e0d9f16a5 (patch) | |
tree | 5a1d592f648e97910d509dbb5bc7884bb37a72d7 | |
parent | b870cd1ea6b795e0cb8116478261217c44604310 (diff) | |
download | async-f4ce893cdec9da946083a32432ac4a3e0d9f16a5.tar.gz |
Convert memoize tests to mocha
-rw-r--r-- | mocha_test/memoize.js | 208 | ||||
-rwxr-xr-x | test/test-async.js | 220 |
2 files changed, 208 insertions, 220 deletions
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); |