diff --git a/common.mk b/common.mk index ecaa61fb9..8152bea4a 100644 --- a/common.mk +++ b/common.mk @@ -23,10 +23,12 @@ test: ifneq (,$(wildcard ./test/__setup.js)) @mocha --recursive --ui tdd \ --require babel-register \ + --require babel-polyfill \ --require ./test/__setup else @mocha --recursive --ui tdd \ - --require babel-register + --require babel-register \ + --require babel-polyfill endif html: $(SOURCES) diff --git a/fluent/test/cached_iterable_test.js b/fluent/test/cached_iterable_test.js index 9ec60a9a3..61451609f 100644 --- a/fluent/test/cached_iterable_test.js +++ b/fluent/test/cached_iterable_test.js @@ -2,6 +2,21 @@ import assert from 'assert'; import CachedIterable from '../src/cached_iterable'; +/** + * Return a promise for an array with all the elements of the iterable. + * + * It uses for-await to support async iterables which can't be spread with + * ...iterable. See https://github.com/tc39/proposal-async-iteration/issues/103 + * + */ +async function toArray(iterable) { + const result = []; + for await (const elem of iterable) { + result.push(elem); + } + return result; +} + suite('CachedIterable', function() { suite('constructor errors', function(){ test('no argument', function() { @@ -41,7 +56,7 @@ suite('CachedIterable', function() { }); }); - suite('iteration', function(){ + suite('sync iteration', function(){ let o1, o2; suiteSetup(function() { @@ -50,14 +65,14 @@ suite('CachedIterable', function() { }); test('eager iterable', function() { - const iter = new CachedIterable([o1, o2]); - assert.deepEqual([...iter], [o1, o2]); + const iterable = new CachedIterable([o1, o2]); + assert.deepEqual([...iterable], [o1, o2]); }); test('eager iterable works more than once', function() { - const iter = new CachedIterable([o1, o2]); - assert.deepEqual([...iter], [o1, o2]); - assert.deepEqual([...iter], [o1, o2]); + const iterable = new CachedIterable([o1, o2]); + assert.deepEqual([...iterable], [o1, o2]); + assert.deepEqual([...iterable], [o1, o2]); }); test('lazy iterable', function() { @@ -65,8 +80,8 @@ suite('CachedIterable', function() { yield *[o1, o2]; } - const iter = new CachedIterable(generate()); - assert.deepEqual([...iter], [o1, o2]); + const iterable = new CachedIterable(generate()); + assert.deepEqual([...iterable], [o1, o2]); }); test('lazy iterable works more than once', function() { @@ -78,9 +93,41 @@ suite('CachedIterable', function() { } } - const iter = new CachedIterable(generate()); - const first = [...iter]; - assert.deepEqual([...iter], first); + const iterable = new CachedIterable(generate()); + const first = [...iterable]; + assert.deepEqual([...iterable], first); + }); + }); + + suite('async iteration', function(){ + let o1, o2; + + suiteSetup(function() { + o1 = Object(); + o2 = Object(); + }); + + test('lazy iterable', async function() { + async function *generate() { + yield *[o1, o2]; + } + + const iterable = new CachedIterable(generate()); + assert.deepEqual(await toArray(iterable), [o1, o2]); + }); + + test('lazy iterable works more than once', async function() { + async function *generate() { + let i = 2; + + while (--i) { + yield Object(); + } + } + + const iterable = new CachedIterable(generate()); + const first = await toArray(iterable); + assert.deepEqual(await toArray(iterable), first); }); }); @@ -93,44 +140,44 @@ suite('CachedIterable', function() { }); test('consumes an element into the cache', function() { - const iter = new CachedIterable([o1, o2]); - assert.equal(iter.seen.length, 0); - iter.touchNext(); - assert.equal(iter.seen.length, 1); + const iterable = new CachedIterable([o1, o2]); + assert.equal(iterable.seen.length, 0); + iterable.touchNext(); + assert.equal(iterable.seen.length, 1); }); test('allows to consume multiple elements into the cache', function() { - const iter = new CachedIterable([o1, o2]); - iter.touchNext(); - iter.touchNext(); - assert.equal(iter.seen.length, 2); + const iterable = new CachedIterable([o1, o2]); + iterable.touchNext(); + iterable.touchNext(); + assert.equal(iterable.seen.length, 2); }); test('stops at the last element', function() { - const iter = new CachedIterable([o1, o2]); - iter.touchNext(); - iter.touchNext(); - iter.touchNext(); - assert.equal(iter.seen.length, 3); - - iter.touchNext(); - assert.equal(iter.seen.length, 3); + const iterable = new CachedIterable([o1, o2]); + iterable.touchNext(); + iterable.touchNext(); + iterable.touchNext(); + assert.equal(iterable.seen.length, 3); + + iterable.touchNext(); + assert.equal(iterable.seen.length, 3); }); test('works on an empty iterable', function() { - const iter = new CachedIterable([]); - iter.touchNext(); - iter.touchNext(); - iter.touchNext(); - assert.equal(iter.seen.length, 1); + const iterable = new CachedIterable([]); + iterable.touchNext(); + iterable.touchNext(); + iterable.touchNext(); + assert.equal(iterable.seen.length, 1); }); test('iteration for such cache works', function() { - const iter = new CachedIterable([o1, o2]); - iter.touchNext(); - iter.touchNext(); - iter.touchNext(); - assert.deepEqual([...iter], [o1, o2]); + const iterable = new CachedIterable([o1, o2]); + iterable.touchNext(); + iterable.touchNext(); + iterable.touchNext(); + assert.deepEqual([...iterable], [o1, o2]); }); }); }); diff --git a/package.json b/package.json index c1bf1b88e..64e6f2415 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "babel-plugin-external-helpers": "^6.22.0", "babel-plugin-transform-builtin-extend": "^1.1.2", "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", + "babel-polyfill": "^6.26.0", "babel-preset-env": "^1.6.1", "babel-preset-stage-3": "^6.24.1", "babel-register": "^6.26.0",