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

describe('whilst', function(){
    it('whilst', function(done) {
        var call_order = [];

        var count = 0;
        async.whilst(
            function (c) {
                expect(c).to.equal(undefined);
                call_order.push(['test', count]);
                return (count < 5);
            },
            function (cb) {
                call_order.push(['iteratee', count]);
                count++;
                cb(null, count);
            },
            function (err, result) {
                assert(err === null, err + " passed instead of 'null'");
                expect(result).to.equal(5, 'last result passed through');
                expect(call_order).to.eql([
                    ['test', 0],
                    ['iteratee', 0], ['test', 1],
                    ['iteratee', 1], ['test', 2],
                    ['iteratee', 2], ['test', 3],
                    ['iteratee', 3], ['test', 4],
                    ['iteratee', 4], ['test', 5],
                ]);
                expect(count).to.equal(5);
                done();
            }
        );
    });

    it('whilst optional callback', function(done) {
        var counter = 0;
        async.whilst(
            function () { return counter < 2; },
            function (cb) {
                counter++;
                cb();
            }
        );
        expect(counter).to.equal(2);
        done();
    });

    it('doWhilst', function(done) {
        var call_order = [];

        var count = 0;
        async.doWhilst(
            function (cb) {
                call_order.push(['iteratee', count]);
                count++;
                cb(null, count);
            },
            function (c) {
                expect(c).to.equal(count);
                call_order.push(['test', count]);
                return (count < 5);
            },
            function (err, result) {
                assert(err === null, err + " passed instead of 'null'");
                expect(result).to.equal(5, 'last result passed through');
                expect(call_order).to.eql([
                    ['iteratee', 0], ['test', 1],
                    ['iteratee', 1], ['test', 2],
                    ['iteratee', 2], ['test', 3],
                    ['iteratee', 3], ['test', 4],
                    ['iteratee', 4], ['test', 5]
                ]);
                expect(count).to.equal(5);
                done();
            }
        );
    });

    it('doWhilst callback params', function(done) {
        var call_order = [];
        var count = 0;
        async.doWhilst(
            function (cb) {
                call_order.push(['iteratee', count]);
                count++;
                cb(null, count);
            },
            function (c) {
                call_order.push(['test', c]);
                return (c < 5);
            },
            function (err, result) {
                if (err) throw err;
                expect(result).to.equal(5, 'last result passed through');
                expect(call_order).to.eql([
                    ['iteratee', 0], ['test', 1],
                    ['iteratee', 1], ['test', 2],
                    ['iteratee', 2], ['test', 3],
                    ['iteratee', 3], ['test', 4],
                    ['iteratee', 4], ['test', 5]
                ]);
                expect(count).to.equal(5);
                done();
            }
        );
    });

    it('doWhilst - error', function(done) {
        var error = new Error('asdas');

        async.doWhilst(
            function (cb) {
                cb(error);
            },
            function () {},
            function (err) {
                expect(err).to.equal(error);
                done();
            }
        );
    });
});