diff options
author | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-06-05 18:10:12 -0400 |
---|---|---|
committer | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-06-05 18:10:12 -0400 |
commit | 872cb9133882302627e989432f3aa7a028ccfa3f (patch) | |
tree | 01e102d023ec5ecf5e4efed3c560658d660e038a /mocha_test | |
parent | b49787267c8c8d606debf0fa4f4be6ef793270f5 (diff) | |
parent | 611a4424b4490592fd037279f65e22630e66ffd4 (diff) | |
download | async-872cb9133882302627e989432f3aa7a028ccfa3f.tar.gz |
Merge pull request #1177 from caolan/map-objects
Mapping over Objects
Diffstat (limited to 'mocha_test')
-rw-r--r-- | mocha_test/map.js | 18 | ||||
-rw-r--r-- | mocha_test/mapValues.js | 92 |
2 files changed, 99 insertions, 11 deletions
diff --git a/mocha_test/map.js b/mocha_test/map.js index a537427..e7d4fa3 100644 --- a/mocha_test/map.js +++ b/mocha_test/map.js @@ -122,12 +122,10 @@ describe("map", function() { callback(null, val * 2); }, function(err, result) { if (err) throw err; - expect(Object.prototype.toString.call(result)).to.equal('[object Object]'); - expect(result).to.eql({ - a: 2, - b: 4, - c: 6 - }); + expect(Object.prototype.toString.call(result)).to.equal('[object Array]'); + expect(result).to.contain(2); + expect(result).to.contain(4); + expect(result).to.contain(6); done(); }); }); @@ -170,11 +168,9 @@ describe("map", function() { callback(null, val * 2); }, function(err, result) { if (err) throw err; - expect(result).to.eql({ - a: 2, - b: 4, - c: 6 - }); + expect(result).to.contain(2); + expect(result).to.contain(4); + expect(result).to.contain(6); done(); }); }); diff --git a/mocha_test/mapValues.js b/mocha_test/mapValues.js new file mode 100644 index 0000000..44eb27d --- /dev/null +++ b/mocha_test/mapValues.js @@ -0,0 +1,92 @@ +var async = require('../lib'); +var expect = require('chai').expect; +var assert = require('assert'); + +describe('mapValues', function () { + var obj = {a: 1, b: 2, c: 3}; + + context('mapValuesLimit', function () { + it('basics', function (done) { + var running = 0; + var concurrency = { + a: 2, + b: 2, + c: 1 + }; + async.mapValuesLimit(obj, 2, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0); + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}); + done(); + }); + }); + + it('error', function (done) { + async.mapValuesLimit(obj, 1, function(val, key, next) { + if (key === 'b') { + return next(new Error("fail")); + } + next(null, val); + }, function (err, result) { + expect(err).to.not.eql(null); + expect(result).to.eql({a: 1}); + done(); + }); + }); + }); + + context('mapValues', function () { + it('basics', function (done) { + var running = 0; + var concurrency = { + a: 3, + b: 2, + c: 1 + }; + async.mapValues(obj, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0); + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}); + done(); + }); + }); + }); + + context('mapValuesSeries', function () { + it('basics', function (done) { + var running = 0; + var concurrency = { + a: 1, + b: 1, + c: 1 + }; + async.mapValuesSeries(obj, function (val, key, next) { + running++; + async.setImmediate(function () { + expect(running).to.equal(concurrency[key]); + running--; + next(null, key + val); + }); + }, function (err, result) { + expect(running).to.equal(0); + expect(err).to.eql(null); + expect(result).to.eql({a: 'a1', b: 'b2', c: 'c3'}); + done(); + }); + }); + }); +}); |