diff --git a/.gitignore b/.gitignore index 6a8d668..ed066d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.nyc_output .vscode coverage node_modules diff --git a/lib/index.js b/lib/index.js index 0d18cda..1eafee3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -223,7 +223,9 @@ const Channel = function(length = 0) { slice: (start, end = Infinity) => { const output = Channel(); + (async () => { + // Consume values before the starting point. for (let index = 0; index < start; index++) { const value = await readOnly.shift(); diff --git a/package.json b/package.json index 74ccd75..d164b9e 100644 --- a/package.json +++ b/package.json @@ -13,14 +13,11 @@ "dependencies": {}, "devDependencies": { "@nodeguy/assert": "0.1.4", - "mocha": "4.0.1" + "mocha": "4.0.1", + "nyc": "11.3.0" }, "homepage": "https://github.com/NodeGuy/channel", - "keywords": [ - "CSP", - "channel", - "concurrency" - ], + "keywords": ["CSP", "channel", "concurrency"], "license": "Apache-2.0", "main": "lib/index.js", "repository": { @@ -28,6 +25,14 @@ "url": "git+https://github.com/NodeGuy/channel.git" }, "scripts": { - "test": "mocha --recursive" + "test": "nyc mocha --recursive" + }, + "nyc": { + "check-coverage": true, + "branches": 100, + "functions": 100, + "lines": 100, + "statements": 100, + "reporter": ["html", "text-summary"] } } diff --git a/test/index.js b/test/index.js index f6b01b9..c1af825 100644 --- a/test/index.js +++ b/test/index.js @@ -136,6 +136,7 @@ describe(`Channel`, function() { const channel = Channel(); Channel.select([channel.push(`cancelled`)]).cancel(); const closed = Channel.of(); + assert.equal( await Channel.select([channel.shift(), closed.shift()]), closed @@ -211,6 +212,18 @@ describe(`Channel object`, function() { channel.close(); assert.strictEqual(await channel.shift(), undefined); }); + + it(`Don't set 'lastValue' to 'undefined' when closing a channel with a cancelled shift.`, async function() { + const channel = Channel(); + + // Set lastValue to 0. + channel.push(0); + await channel.shift(); + + channel.shift().cancel(); + channel.close(); + assert.strictEqual(channel.value(), 0); + }); }); it(`concat`, async function() { @@ -386,6 +399,15 @@ describe(`Channel object`, function() { [1, 2, 3] ); }); + + it(`start after end of channel`, async function() { + assert.deepEqual( + await Channel.of(0, 1, 2) + .slice(10) + .values(), + [] + ); + }); }); it(`some`, async function() {