wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
/*
 * Tests the "whilst" function
 */
 
var mod_util = require('util');
 
var mod_tap = require('tap');
var mod_vasync = require('..');
 
mod_tap.test('basic whilst', function (test) {
    var n = 0;
 
    mod_vasync.whilst(
        function condition() {
        return (n < 5);
        },
        function body(cb) {
        n++;
        cb(null, n);
        },
        function done(err, arg) {
        test.ok(!err, 'error unset');
        test.equal(n, 5, 'n == 5');
        test.equal(n, arg, 'n == arg');
        test.end();
        });
});
 
mod_tap.test('whilst return object', function (test) {
    var n = 0;
 
    var w = mod_vasync.whilst(
        function condition() {
        return (n < 5);
        },
        function body(cb) {
        n++;
 
        test.equal(n, w.iterations, 'n == w.iterations: ' + n);
 
        cb(null, n, 'foo');
        },
        function done(err, arg1, arg2, arg3) {
        test.ok(!err, 'error unset');
        test.equal(w.iterations, 5, 'whilst had 5 iterations');
        test.equal(w.finished, true, 'whilst has finished');
        test.equal(arg1, n, 'whilst arg1 == n');
        test.equal(arg2, 'foo', 'whilst arg2 == "foo"');
        test.equal(arg3, undefined, 'whilst arg3 == undefined');
        test.end();
        });
 
    test.equal(typeof (w), 'object', 'whilst returns an object');
    test.equal(w.finished, false, 'whilst is not finished');
    test.equal(w.iterations, 0, 'whilst has not started yet');
});
 
mod_tap.test('whilst false condition', function (test) {
    mod_vasync.whilst(
        function condition() {
        return (false);
        },
        function body(cb) {
        cb();
        },
        function done(err, arg) {
        test.ok(!err, 'error is unset');
        test.ok(!arg, 'arg is unset');
        test.end();
        });
});
 
mod_tap.test('whilst error', function (test) {
    var n = 0;
 
    var w = mod_vasync.whilst(
        function condition() {
        return (true);
        },
        function body(cb) {
        n++;
 
        if (n > 5) {
            cb(new Error('n > 5'), 'bar');
        } else {
            cb(null, 'foo');
        }
        },
        function done(err, arg) {
        test.ok(err, 'error is set');
        test.equal(err.message, 'n > 5');
        test.equal(arg, 'bar');
 
        test.equal(w.finished, true, 'whilst is finished');
 
        /*
         * Iterations is bumped after the test condition is run and
         * before the iteration function is run.  Because the condition
         * in this example is inside the iteration function (the test
         * condition always returns true), the iteration count will be
         * 1 higher than expected, since it will fail when (n > 5), or
         * when iterations is 6.
         */
        test.equal(w.iterations, 6, 'whilst had 6 iterations');
 
        test.end();
        });
});