Skip to content

Commit

Permalink
Tests for async CachedIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Jan 12, 2018
1 parent 62b0812 commit a717135
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 38 deletions.
4 changes: 3 additions & 1 deletion common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
121 changes: 84 additions & 37 deletions fluent/test/cached_iterable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -41,7 +56,7 @@ suite('CachedIterable', function() {
});
});

suite('iteration', function(){
suite('sync iteration', function(){
let o1, o2;

suiteSetup(function() {
Expand All @@ -50,23 +65,23 @@ 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() {
function *generate() {
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() {
Expand All @@ -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);
});
});

Expand All @@ -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]);
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a717135

Please sign in to comment.