From 324d61572655e251ad2b06032a04cfab09bb0076 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Mon, 3 Aug 2015 17:28:05 -0500 Subject: Enforce 100% code coverage --- lib/handlebars/compiler/code-gen.js | 13 +++++++------ lib/handlebars/compiler/helpers.js | 17 +++++++++-------- spec/ast.js | 11 +++++++++++ spec/blocks.js | 7 +++++++ tasks/test.js | 14 +++++++++++++- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/lib/handlebars/compiler/code-gen.js b/lib/handlebars/compiler/code-gen.js index bc7bc07..3af4f8c 100644 --- a/lib/handlebars/compiler/code-gen.js +++ b/lib/handlebars/compiler/code-gen.js @@ -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: {}}) { @@ -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(']'); diff --git a/lib/handlebars/compiler/helpers.js b/lib/handlebars/compiler/helpers.js index 1c8ab0d..f2edfa1 100644 --- a/lib/handlebars/compiler/helpers.js +++ b/lib/handlebars/compiler/helpers.js @@ -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 } }; } diff --git a/spec/ast.js b/spec/ast.js index ce4c090..627554c 100644 --- a/spec/ast.js +++ b/spec/ast.js @@ -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); @@ -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; diff --git a/spec/blocks.js b/spec/blocks.js index 80f1580..3584ed7 100644 --- a/spec/blocks.js +++ b/spec/blocks.js @@ -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}}'; diff --git a/tasks/test.js b/tasks/test.js index ad8a911..7447324 100644 --- a/tasks/test.js +++ b/tasks/test.js @@ -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']); }; -- cgit v1.1