1 | var chai = require('chai');
|
2 | var Promise = require('bluebird');
|
3 | var async = require('../src/async/index');
|
4 | var await = require('../src/await/index');
|
5 | var expect = chai.expect;
|
6 | describe('async.iterable(...)', function () {
|
7 | var foo = async.iterable(function (yield_, count, accum) {
|
8 | if (count < 1 || count > 9)
|
9 | throw new Error('out of range');
|
10 | for (var i = 1; i <= count; ++i) {
|
11 | if (accum)
|
12 | accum.push(111 * i);
|
13 | yield_(111 * i);
|
14 | }
|
15 | return 'done';
|
16 | });
|
17 | describe('returns a function', function () {
|
18 | it('which returns an async iterator with next() and forEach() methods', function () {
|
19 | var syncResult = foo();
|
20 | expect(syncResult).is.an('object');
|
21 | expect(syncResult.next).is.a('function');
|
22 | expect(syncResult.forEach).is.a('function');
|
23 | });
|
24 | });
|
25 | describe('provides an iterator whose next() method', function () {
|
26 | it('synchronously returns a promise', function () {
|
27 | var iter = foo(3);
|
28 | expect(iter.next()).instanceOf(Promise);
|
29 | });
|
30 | it('begins executing synchronously and completes asynchronously', function (done) {
|
31 | var arr = [], iter = foo(3, arr);
|
32 | iter.next()
|
33 | .then(function () { return expect(arr).to.deep.equal([111, '***']); })
|
34 | .then(function () { return done(); })
|
35 | .catch(done);
|
36 | expect(arr).to.deep.equal([111]);
|
37 | arr.push('***');
|
38 | });
|
39 | it("preserves the 'this' context of the call", async.cps(function () {
|
40 | var foo = { bar: async.iterable(function (yield_) { yield_(this); return 'done'; }) }, baz = { x: 7 };
|
41 | var iter = foo.bar();
|
42 | expect(await(iter.next())).to.deep.equal({ done: false, value: foo });
|
43 | expect(await(iter.next())).to.deep.equal({ done: true });
|
44 | iter = foo.bar.call(baz);
|
45 | expect(await(iter.next())).to.deep.equal({ done: false, value: baz });
|
46 | expect(await(iter.next())).to.deep.equal({ done: true });
|
47 | }));
|
48 | it('eventually resolves with the definition\'s yielded value', async.cps(function () {
|
49 | var iter = foo(3);
|
50 | expect(await(iter.next())).to.deep.equal({ done: false, value: 111 });
|
51 | expect(await(iter.next())).to.deep.equal({ done: false, value: 222 });
|
52 | expect(await(iter.next())).to.deep.equal({ done: false, value: 333 });
|
53 | expect(await(iter.next())).to.deep.equal({ done: true });
|
54 | }));
|
55 | it('eventually rejects with the definition\'s thrown value', async.cps(function () {
|
56 | var err, iter = foo(20);
|
57 | expect(function () { return await(iter.next()); }).to.throw(Error, 'out of range');
|
58 | }));
|
59 | it('eventually rejects if the iteration is already finished', async.cps(function () {
|
60 | var err, iter = foo(1);
|
61 | expect(await(iter.next())).to.deep.equal({ done: false, value: 111 });
|
62 | expect(await(iter.next())).to.deep.equal({ done: true });
|
63 | expect(function () { return await(iter.next()); }).to.throw(Error);
|
64 | }));
|
65 | it('works with await', function (done) {
|
66 | var foo = async.iterable(function (yield_) { yield_(await(Promise.delay(20).then(function () { return 'blah'; }))); });
|
67 | foo().next()
|
68 | .then(function (result) { return expect(result).to.deep.equal({ done: false, value: 'blah' }); })
|
69 | .then(function () { return done(); })
|
70 | .catch(done);
|
71 | });
|
72 | });
|
73 | describe('provides an iterator whose forEach() method', function () {
|
74 | function nullFunc() { }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | it('synchronously returns a promise', function () {
|
81 | var iter = foo(3);
|
82 | expect(iter.forEach(nullFunc)).instanceOf(Promise);
|
83 | });
|
84 | it('begins executing synchronously and completes asynchronously', function (done) {
|
85 | var arr = [], iter = foo(3, arr);
|
86 | iter.forEach(nullFunc)
|
87 | .then(function () { return expect(arr).to.deep.equal([111, '***', 222, 333]); })
|
88 | .then(function () { return done(); })
|
89 | .catch(done);
|
90 | expect(arr).to.deep.equal([111]);
|
91 | arr.push('***');
|
92 | });
|
93 | it('iterates over all yielded values', async.cps(function () {
|
94 | var arr = [], iter = foo(4);
|
95 | await(iter.forEach(function (val) { return arr.push(val); }));
|
96 | expect(arr).to.deep.equal([111, 222, 333, 444]);
|
97 | }));
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | it('eventually rejects with the definition\'s thrown value', async.cps(function () {
|
105 | var err, iter = foo(20);
|
106 | expect(function () { return await(iter.forEach(nullFunc)); }).to.throw(Error, 'out of range');
|
107 | }));
|
108 | it('eventually rejects if the iteration is already finished', async.cps(function () {
|
109 | var err, iter = foo(1);
|
110 | await(iter.forEach(nullFunc));
|
111 | expect(function () { return await(iter.forEach(nullFunc)); }).to.throw(Error);
|
112 | }));
|
113 | it('works with await', function (done) {
|
114 | var foo = async.iterable(function (yield_) { yield_(await(Promise.delay(20).then(function () { return 'blah'; }))); }), arr = [];
|
115 | foo().forEach(function (val) { return arr.push(val); })
|
116 | .then(function (result) { return expect(result).to.not.exist; })
|
117 | .then(function () { return expect(arr).to.deep.equal(['blah']); })
|
118 | .then(function () { return done(); })
|
119 | .catch(done);
|
120 | });
|
121 | });
|
122 | });
|
123 |
|
\ | No newline at end of file |