Skip to content

Commit

Permalink
Enforce 100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Aug 3, 2015
1 parent c3cbaa2 commit 324d615
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
13 changes: 7 additions & 6 deletions lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ CodeGen.prototype = {
}
},

empty: function(loc = this.currentLocation || {start: {}}) {
empty: function() {
let loc = this.currentLocation || {start: {}};
return new SourceNode(loc.start.line, loc.start.column, this.srcFile);
},
wrap: function(chunk, loc = this.currentLocation || {start: {}}) {
Expand Down Expand Up @@ -137,22 +138,22 @@ CodeGen.prototype = {
},


generateList: function(entries, loc) {
let ret = this.empty(loc);
generateList: function(entries) {
let ret = this.empty();

for (let i = 0, len = entries.length; i < len; i++) {
if (i) {
ret.add(',');
}

ret.add(castChunk(entries[i], this, loc));
ret.add(castChunk(entries[i], this));
}

return ret;
},

generateArray: function(entries, loc) {
let ret = this.generateList(entries, loc);
generateArray: function(entries) {
let ret = this.generateList(entries);
ret.prepend('[');
ret.add(']');

Expand Down
17 changes: 9 additions & 8 deletions lib/handlebars/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,20 @@ export function prepareBlock(openBlock, program, inverseAndProgram, close, inver

export function prepareProgram(statements, loc) {
if (!loc && statements.length) {
const first = statements[0].loc,
last = statements[statements.length - 1].loc;
const firstLoc = statements[0].loc,
lastLoc = statements[statements.length - 1].loc;

if (first && last) {
/* istanbul ignore else */
if (firstLoc && lastLoc) {
loc = {
source: first.source,
source: firstLoc.source,
start: {
line: first.start.line,
column: first.start.column
line: firstLoc.start.line,
column: firstLoc.start.column
},
end: {
line: last.end.line,
column: last.end.column
line: lastLoc.end.line,
column: lastLoc.end.column
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions spec/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ describe('ast', function() {
});

describe('PartialStatement', function() {
it('provides default params', function() {
var pn = new handlebarsEnv.AST.PartialStatement('so_partial', undefined, {}, {}, LOCATION_INFO);
equals(pn.params.length, 0);
});
it('stores location info', function() {
var pn = new handlebarsEnv.AST.PartialStatement('so_partial', [], {}, {}, LOCATION_INFO);
testLocationInfoStorage(pn);
Expand All @@ -113,6 +117,13 @@ describe('ast', function() {
});
});

describe('SubExpression', function() {
it('provides default params', function() {
var pn = new handlebarsEnv.AST.SubExpression('path', undefined, {}, LOCATION_INFO);
equals(pn.params.length, 0);
});
});

describe('Line Numbers', function() {
var ast, body;

Expand Down
7 changes: 7 additions & 0 deletions spec/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ describe('blocks', function() {
shouldCompileTo(string, hash, 'Goodbye cruel sad OMG!');
});

it('works with cached blocks', function() {
var template = CompilerContext.compile('{{#each person}}{{#with .}}{{first}} {{last}}{{/with}}{{/each}}', {data: false});

var result = template({person: [{first: 'Alan', last: 'Johnson'}, {first: 'Alan', last: 'Johnson'}]});
equals(result, 'Alan JohnsonAlan Johnson');
});

describe('inverted sections', function() {
it('inverted sections with unset value', function() {
var string = '{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}';
Expand Down
14 changes: 13 additions & 1 deletion tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@ module.exports = function(grunt) {
done();
});
});
grunt.registerTask('test', ['test:bin', 'test:cov']);

grunt.registerTask('test:check-cov', function() {
var done = this.async();

var runner = childProcess.fork('node_modules/.bin/istanbul', ['check-coverage', '--statements', '100', '--functions', '100', '--branches', '100', '--lines 100'], {stdio: 'inherit'});
runner.on('close', function(code) {
if (code != 0) {
grunt.fatal('Coverage check failed: ' + code);
}
done();
});
});
grunt.registerTask('test', ['test:bin', 'test:cov', 'test:check-cov']);
};

0 comments on commit 324d615

Please sign in to comment.