summaryrefslogtreecommitdiff
path: root/test/some.js
blob: 676797a9928d4b38ca689040efef41e36bb63010 (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
var async = require('../lib');
var {expect} = require('chai');
var _ = require('lodash');

describe("some", () => {

    it('some true', (done) => {
        async.some([3,1,2], (x, callback) => {
            setTimeout(() => {callback(null, x === 1);}, 0);
        }, (err, result) => {
            expect(err).to.equal(null);
            expect(result).to.equal(true);
            done();
        });
    });

    it('some false', (done) => {
        async.some([3,1,2], (x, callback) => {
            setTimeout(() => {callback(null, x === 10);}, 0);
        }, (err, result) => {
            expect(err).to.equal(null);
            expect(result).to.equal(false);
            done();
        });
    });

    it('some early return', (done) => {
        var call_order = [];
        async.some([1,2,3], (x, callback) => {
            setTimeout(() => {
                call_order.push(x);
                callback(null, x === 1);
            }, x*5);
        }, () => {
            call_order.push('callback');
        });
        setTimeout(() => {
            expect(call_order).to.eql([1,'callback',2,3]);
            done();
        }, 25);
    });

    it('some error', (done) => {
        async.some([3,1,2], (x, callback) => {
            setTimeout(() => {callback('error');}, 0);
        }, (err, result) => {
            expect(err).to.equal('error');
            expect(result).to.not.exist;
            done();
        });
    });

    it('some no callback', (done) => {
        var calls = [];

        async.some([1, 2, 3], (val, cb) => {
            calls.push(val);
            cb();
        });

        setTimeout(() => {
            expect(calls).to.eql([1, 2, 3]);
            done();
        }, 10);
    });

    it('someLimit true', (done) => {
        async.someLimit([3,1,2], 2, (x, callback) => {
            setTimeout(() => {callback(null, x === 2);}, 0);
        }, (err, result) => {
            expect(err).to.equal(null);
            expect(result).to.equal(true);
            done();
        });
    });

    it('someLimit false', (done) => {
        async.someLimit([3,1,2], 2, (x, callback) => {
            setTimeout(() => {callback(null, x === 10);}, 0);
        }, (err, result) => {
            expect(err).to.equal(null);
            expect(result).to.equal(false);
            done();
        });
    });

    it('someLimit short-circuit', (done) => {
        var calls = 0;
        async.someLimit([3,1,2], 1, (x, callback) => {
            calls++;
            callback(null, x === 1);
        }, (err, result) => {
            expect(err).to.equal(null);
            expect(result).to.equal(true);
            expect(calls).to.equal(2);
            done();
        });
    });


    it('someSeries doesn\'t cause stack overflow (#1293)', (done) => {
        var arr = _.range(10000);
        let calls = 0;
        async.someSeries(arr, (data, cb) => {
            calls += 1;
            async.setImmediate(_.partial(cb, null, true));
        }, (err) => {
            expect(err).to.equal(null);
            expect(calls).to.equal(1);
            done();
        });
    });

    it('someLimit doesn\'t cause stack overflow (#1293)', (done) => {
        var arr = _.range(10000);
        let calls = 0;
        async.someLimit(arr, 100, (data, cb) => {
            calls += 1;
            async.setImmediate(_.partial(cb, null, true));
        }, (err) => {
            expect(err).to.equal(null);
            expect(calls).to.equal(100);
            done();
        });
    });

    it('any alias', () => {
        expect(async.any).to.equal(async.some);
    });

    it('anyLimit alias', () => {
        expect(async.anyLimit).to.equal(async.someLimit);
    });

    it('anySeries alias', () => {
        expect(async.anySeries).to.be.a('function');
        expect(async.anySeries).to.equal(async.someSeries);
    });


});