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

describe("some", function () {

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

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

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

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

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

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

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

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

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


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

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

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

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


});