Skip to content

Commit

Permalink
Fix coverage on loop labels. Fixes hapijs#355.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed May 12, 2015
1 parent b632b3b commit 4b505f6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ internals.instrument = function (filename) {
'ForStatement',
'ForInStatement',
'SwitchStatement',
'WithStatement'
'WithStatement',
'LabeledStatement'
];

if (trackedTypes.indexOf(node.type) !== -1 &&
(node.type !== 'VariableDeclaration' || (node.parent.type !== 'ForStatement' && node.parent.type !== 'ForInStatement')) &&
(node.type !== 'ExpressionStatement' || node.expression.value !== 'use strict')) {
(node.type !== 'ExpressionStatement' || node.expression.value !== 'use strict') &&
node.parent.type !== 'LabeledStatement') {

tracking.push(node.loc.start.line);
node.set('__$$labCov._line(\'' + filename + '\',' + node.loc.start.line + ');' + node.source());
Expand Down
24 changes: 24 additions & 0 deletions test/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ describe('Coverage', function () {
done();
});

it('should work with loop labels', function (done) {

var Test = require('./coverage/loop-labels.js');
expect(Test.method()).to.deep.equal([1, 0]);

var cov = Lab.coverage.analyze({ coveragePath: Path.join(__dirname, 'coverage/loop-labels') });
var source = cov.files[0].source;
var missedChunks = [];
Object.keys(source).forEach(function (lineNumber) {

var line = source[lineNumber];
if (line.miss) {
missedChunks.push.apply(missedChunks, line.chunks.filter(function (chunk) {

return !!chunk.miss;
}));
}
});

expect(missedChunks).to.have.length(1).and.to.deep.equal([{ source: 'j < 1', miss: 'true' }]);

done();
});

describe('#analyze', function () {

it('sorts file paths in report', function (done) {
Expand Down
19 changes: 19 additions & 0 deletions test/coverage/loop-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Load modules


// Declare internals

var internals = {};


exports.method = function () {

outer: for (var i = 0; i < 1; ++i) {
inner: for (var j = 0; j < 1; ++j) {
continue outer;
}
}

return [i, j];
};

0 comments on commit 4b505f6

Please sign in to comment.