summaryrefslogtreecommitdiff
path: root/test/test-async.js
diff options
context:
space:
mode:
authorCaolan McMahon <caolan@caolanmcmahon.com>2010-11-24 20:06:16 +0000
committerCaolan McMahon <caolan@caolanmcmahon.com>2010-11-24 20:06:16 +0000
commit2bc2ec32f2b49048ab975bc25c09cc46b0273c9c (patch)
tree3de5c5455fc816c4fb158c4c52739a1e9c32591e /test/test-async.js
parent051e1b2608e129d661d4665671e28edf7c89285c (diff)
downloadasync-2bc2ec32f2b49048ab975bc25c09cc46b0273c9c.tar.gz
added memoize
Diffstat (limited to 'test/test-async.js')
-rw-r--r--test/test-async.js59
1 files changed, 58 insertions, 1 deletions
diff --git a/test/test-async.js b/test/test-async.js
index b1c1f56..60b1d81 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -862,7 +862,9 @@ var console_fn_tests = function(name){
}
exports[name + ' without console.' + name] = function(test){
- if (typeof global === 'undefined') var global = window;
+ if (typeof global === 'undefined') {
+ global = window;
+ }
var _console = global.console;
global.console = undefined;
var fn = function(callback){callback(null, 'val');};
@@ -1214,3 +1216,58 @@ exports['queue push without callback'] = function (test) {
test.done();
}, 200);
};
+
+exports['memoize'] = function (test) {
+ test.expect(4);
+ var call_order = [];
+
+ var fn = function (arg1, arg2, callback) {
+ call_order.push(['fn', 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);
+ });
+ fn2(2, 2, function (err, result) {
+ test.equal(result, 4);
+ });
+
+ test.same(call_order, [['fn',1,2], ['fn',2,2]]);
+ test.done();
+};
+
+exports['memoize 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, result) {
+ test.equal(err, testerr);
+ });
+ test.done();
+};
+
+exports['memoize 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();
+};