summaryrefslogtreecommitdiff
path: root/mocha_test/eachOf.js
blob: 925f9958421a43b53549fd4fe998b2b6824e8dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');
var _ = require('lodash');

describe("eachOf", function() {

    function forEachOfNoCallbackIteratee(done, x, key, callback) {
        expect(x).to.equal(1);
        expect(key).to.equal("a");
        callback();
        done();
    }

    function forEachOfIteratee(args, value, key, callback) {
        setTimeout(function(){
            args.push(key, value);
            callback();
        }, value*25);
    }

    it('eachOf alias', function(done) {
        expect(async.eachOf).to.equal(async.forEachOf);
        done();
    });

    it('eachOfLimit alias', function(done) {
        expect(async.eachOfLimit).to.equal(async.forEachOfLimit);
        done();
    });

    it('eachOfSeries alias', function(done) {
        expect(async.eachOfSeries).to.equal(async.forEachOfSeries);
        done();
    });

    it('forEachOf', function(done) {
        var args = [];
        async.forEachOf({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){
            assert(err === null, err + " passed instead of 'null'");
            expect(args).to.eql(["a", 1, "b", 2]);
            done();
        });
    });

    it('forEachOf - instant resolver', function(done) {
        var args = [];
        async.forEachOf({ a: 1, b: 2 }, function(x, k, cb) {
            args.push(k, x);
            cb();
        }, function(){
            // ensures done callback isn't called before all items iterated
            expect(args).to.eql(["a", 1, "b", 2]);
            done();
        });
    });

    it('forEachOf empty object', function(done) {
        async.forEachOf({}, function(value, key, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err) {
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('forEachOf empty array', function(done) {
        async.forEachOf([], function(value, key, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err) {
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('forEachOf error', function(done) {
        async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) {
            callback('error');
        }, function(err){
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('forEachOf no callback', function(done) {
        async.forEachOf({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done));
    });


    it('forEachOf with array', function(done) {
        var args = [];
        async.forEachOf([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, "a", 1, "b"]);
            done();
        });
    });

    it('forEachOf with Set (iterators)', function(done) {
        if (typeof Set !== 'function')
            return done();

        var args = [];
        var set = new Set();
        set.add("a");
        set.add("b");
        async.forEachOf(set, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, "a", 1, "b"]);
            done();
        });
    });

    it('forEachOf with Map (iterators)', function(done) {
        if (typeof Map !== 'function')
            return done();

        var args = [];
        var map = new Map();
        map.set(1, "a");
        map.set(2, "b");
        async.forEachOf(map, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]);
            done();
        });
    });

    it('forEachOfSeries', function(done) {
        var args = [];
        async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIteratee.bind(this, args), function(err){
            assert(err === null, err + " passed instead of 'null'");
            expect(args).to.eql([ "a", 1, "b", 2 ]);
            done();
        });
    });

    it('forEachOfSeries empty object', function(done) {
        async.forEachOfSeries({}, function(x, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err){
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('forEachOfSeries error', function(done) {
        var call_order = [];
        async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){
            call_order.push(value, key);
            callback('error');
        }, function(err){
            expect(call_order).to.eql([ 1, "a" ]);
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('forEachOfSeries no callback', function(done) {
        async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIteratee.bind(this, done));
    });

    it('forEachOfSeries with array', function(done) {
        var args = [];
        async.forEachOfSeries([ "a", "b" ], forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([ 0, "a", 1, "b" ]);
            done();
        });
    });

    it('forEachOfSeries with Set (iterators)', function(done) {
        if (typeof Set !== 'function')
            return done();

        var args = [];
        var set = new Set();
        set.add("a");
        set.add("b");
        async.forEachOfSeries(set, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, "a", 1, "b"]);
            done();
        });
    });

    it('forEachOfSeries with Map (iterators)', function(done) {
        if (typeof Map !== 'function')
            return done();

        var args = [];
        var map = new Map();
        map.set(1, "a");
        map.set(2, "b");
        async.forEachOfSeries(map, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]);
            done();
        });
    });

    it('forEachOfLimit', function(done) {
        var args = [];
        var obj = { a: 1, b: 2, c: 3, d: 4 };
        async.forEachOfLimit(obj, 2, function(value, key, callback){
            setTimeout(function(){
                args.push(value, key);
                callback();
            }, value * 5);
        }, function(err){
            assert(err === null, err + " passed instead of 'null'");
            expect(args).to.eql([ 1, "a", 2, "b", 3, "c", 4, "d" ]);
            done();
        });
    });

    it('forEachOfLimit empty object', function(done) {
        async.forEachOfLimit({}, 2, function(value, key, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err){
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('forEachOfLimit limit exceeds size', function(done) {
        var args = [];
        var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
        async.forEachOfLimit(obj, 10, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]);
            done();
        });
    });

    it('forEachOfLimit limit equal size', function(done) {
        var args = [];
        var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
        async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]);
            done();
        });
    });

    it('forEachOfLimit zero limit', function(done) {
        async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err){
            if (err) throw err;
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('forEachOfLimit no limit', function(done) {
        var count = 0;
        async.forEachOfLimit(_.range(100), Infinity, function(x, i, callback){
            count++;
            callback();
        }, function(err){
            if (err) throw err;
            expect(count).to.equal(100);
        });
        setTimeout(done, 25);
    });

    it('forEachOfLimit error', function(done) {
        var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 };
        var call_order = [];

        async.forEachOfLimit(obj, 3, function(value, key, callback){
            call_order.push(value, key);
            if (value === 2) {
                callback('error');
            }
        }, function(err){
            expect(call_order).to.eql([ 1, "a", 2, "b" ]);
            expect(err).to.equal('error');
        });
        setTimeout(done, 25);
    });

    it('forEachOfLimit no callback', function(done) {
        async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIteratee.bind(this, done));
    });

    it('forEachOfLimit synchronous', function(done) {
        var args = [];
        var obj = { a: 1, b: 2 };
        async.forEachOfLimit(obj, 5, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([ "a", 1, "b", 2 ]);
            done();
        });
    });

    it('forEachOfLimit with array', function(done) {
        var args = [];
        var arr = [ "a", "b" ];
        async.forEachOfLimit(arr, 1, forEachOfIteratee.bind(this, args), function (err) {
            if (err) throw err;
            expect(args).to.eql([ 0, "a", 1, "b" ]);
            done();
        });
    });

    it('forEachOfLimit with Set (iterators)', function(done) {
        if (typeof Set !== 'function')
            return done();

        var args = [];
        var set = new Set();
        set.add("a");
        set.add("b");
        async.forEachOfLimit(set, 1, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, "a", 1, "b"]);
            done();
        });
    });

    it('forEachOfLimit with Map (iterators)', function(done) {
        if (typeof Map !== 'function')
            return done();

        var args = [];
        var map = new Map();
        map.set(1, "a");
        map.set(2, "b");
        async.forEachOfLimit(map, 1, forEachOfIteratee.bind(this, args), function(err){
            if (err) throw err;
            expect(args).to.eql([0, [1, "a"], 1, [2, "b"]]);
            done();
        });
    });
});