summaryrefslogtreecommitdiff
path: root/mocha_test/map.js
blob: e7d4fa3105e5d38143d29bef3b0c952b86c0a4dc (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
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');

describe("map", function() {

    function mapIteratee(call_order, x, callback) {
        setTimeout(function() {
            call_order.push(x);
            callback(null, x * 2);
        }, x * 25);
    }

    it('basic', function(done) {
        var call_order = [];
        async.map([1, 3, 2], mapIteratee.bind(this, call_order), function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql([1, 2, 3]);
            expect(results).to.eql([2, 6, 4]);
            done();
        });
    });

    it('with reflect', function(done) {
        var call_order = [];
        async.map([1, 3, 2], async.reflect(function(item, cb) {
            setTimeout(function() {
                call_order.push(item);
                cb(null, item * 2);
            }, item * 25);
        }), function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql([1, 2, 3]);
            expect(results).to.eql([{
                value: 2
            }, {
                value: 6
            }, {
                value: 4
            }]);
            done();
        });
    });

    it('error with reflect', function(done) {
        var call_order = [];
        async.map([-1, 1, 3, 2], async.reflect(function(item, cb) {
            setTimeout(function() {
                call_order.push(item);
                if (item < 0) {
                    cb('number less then zero');
                } else {
                    cb(null, item * 2);
                }
            }, item * 25);
        }), function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql([-1, 1, 2, 3]);
            expect(results).to.eql([{
                error: 'number less then zero'
            }, {
                value: 2
            }, {
                value: 6
            }, {
                value: 4
            }]);
            done();
        });
    });

    it('map original untouched', function(done) {
        var a = [1, 2, 3];
        async.map(a, function(x, callback) {
            callback(null, x * 2);
        }, function(err, results) {
            expect(results).to.eql([2, 4, 6]);
            expect(a).to.eql([1, 2, 3]);
            done();
        });
    });

    it('map without main callback', function(done) {
        var a = [1, 2, 3];
        var r = [];
        async.map(a, function(x, callback) {
            r.push(x);
            var done_ = r.length == a.length;
            callback(null);
            if (done_) {
                expect(r).to.eql(a);
                done();
            }
        });
    });

    it('map error', function(done) {
        async.map([1, 2, 3], function(x, callback) {
            callback('error');
        }, function(err) {
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('map undefined array', function(done) {
        async.map(undefined, function(x, callback) {
            callback();
        }, function(err, result) {
            expect(err).to.equal(null);
            expect(result).to.eql([]);
        });
        setTimeout(done, 50);
    });

    it('map object', function(done) {
        async.map({
            a: 1,
            b: 2,
            c: 3
        }, function(val, callback) {
            callback(null, val * 2);
        }, function(err, result) {
            if (err) throw err;
            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();
        });
    });

    it('mapSeries', function(done) {
        var call_order = [];
        async.mapSeries([1, 3, 2], mapIteratee.bind(this, call_order), function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql([1, 3, 2]);
            expect(results).to.eql([2, 6, 4]);
            done();
        });
    });

    it('mapSeries error', function(done) {
        async.mapSeries([1, 2, 3], function(x, callback) {
            callback('error');
        }, function(err) {
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('mapSeries undefined array', function(done) {
        async.mapSeries(undefined, function(x, callback) {
            callback();
        }, function(err, result) {
            expect(err).to.equal(null);
            expect(result).to.eql([]);
        });
        setTimeout(done, 50);
    });

    it('mapSeries object', function(done) {
        async.mapSeries({
            a: 1,
            b: 2,
            c: 3
        }, function(val, callback) {
            callback(null, val * 2);
        }, function(err, result) {
            if (err) throw err;
            expect(result).to.contain(2);
            expect(result).to.contain(4);
            expect(result).to.contain(6);
            done();
        });
    });

    it('mapLimit', function(done) {
        var call_order = [];
        async.mapLimit([2, 4, 3], 2, mapIteratee.bind(this, call_order), function(err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql([2, 4, 3]);
            expect(results).to.eql([4, 8, 6]);
            done();
        });
    });

    it('mapLimit empty array', function(done) {
        async.mapLimit([], 2, 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('mapLimit undefined array', function(done) {
        async.mapLimit(undefined, 2, function(x, callback) {
            callback();
        }, function(err, result) {
            expect(err).to.equal(null);
            expect(result).to.eql([]);
        });
        setTimeout(done, 50);
    });

    it('mapLimit limit exceeds size', function(done) {
        var call_order = [];
        async.mapLimit([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 20, mapIteratee.bind(this, call_order), function(err, results) {
            expect(call_order).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
            expect(results).to.eql([0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
            done();
        });
    });

    it('mapLimit limit equal size', function(done) {
        var call_order = [];
        async.mapLimit([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10, mapIteratee.bind(this, call_order), function(err, results) {
            expect(call_order).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
            expect(results).to.eql([0, 2, 4, 6, 8, 10, 12, 14, 16, 18]);
            done();
        });
    });

    it('mapLimit zero limit', function(done) {
        async.mapLimit([0, 1, 2, 3, 4, 5], 0, function(x, callback) {
            assert(false, 'iteratee should not be called');
            callback();
        }, function(err, results) {
            expect(results).to.eql([]);
            assert(true, 'should call callback');
        });
        setTimeout(done, 25);
    });

    it('mapLimit error', function(done) {
        var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        var call_order = [];

        async.mapLimit(arr, 3, function(x, callback) {
            call_order.push(x);
            if (x === 2) {
                callback('error');
            }
        }, function(err) {
            expect(call_order).to.eql([0, 1, 2]);
            expect(err).to.equal('error');
        });
        setTimeout(done, 25);
    });

    it('mapLimit does not continue replenishing after error', function(done) {
        var started = 0;
        var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        var delay = 10;
        var limit = 3;
        var maxTime = 10 * arr.length;

        async.mapLimit(arr, limit, function(x, callback) {
            started++;
            if (started === 3) {
                return callback(new Error("Test Error"));
            }
            setTimeout(function() {
                callback();
            }, delay);
        }, function() {});

        setTimeout(function() {
            expect(started).to.equal(3);
            done();
        }, maxTime);
    });

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

        var map = new Map();
        map.set(1, "a");
        map.set(2, "b");
        async.map(map, function(val, cb) {
            cb(null, val);
        }, function(err, result) {
            assert(Array.isArray(result), "map should return an array for an iterable");
            done();
        });
    });

    // Issue 1106 on github: https://github.com/caolan/async/issues/1106
    it('map main callback is called only once', function(done) {
        async.map([1, 2], function(item, callback) {
            try {
                callback(item);
            } catch (exception) {
                expect(function() {
                    callback(exception);
                }).to.throw(/already called/);
                done();
            }
        }, function(err) {
            no_such_function();
        });
    });
});