diff options
-rw-r--r-- | lib/handlebars/base.js | 2 | ||||
-rw-r--r-- | spec/blocks.js | 5 | ||||
-rw-r--r-- | spec/builtins.js | 102 | ||||
-rw-r--r-- | spec/env/common.js | 4 | ||||
-rw-r--r-- | spec/partials.js | 12 |
5 files changed, 111 insertions, 14 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index f39241f..e11f032 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -219,7 +219,7 @@ export var logger = { } }; -export function log(level, obj) { logger.log(level, obj); } +export var log = logger.log; export var createFrame = function(object) { var frame = Utils.extend({}, object); diff --git a/spec/blocks.js b/spec/blocks.js index 061947a..9a5cb40 100644 --- a/spec/blocks.js +++ b/spec/blocks.js @@ -8,7 +8,12 @@ describe('blocks', function() { shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!", "Arrays ignore the contents when empty"); + }); + it('array without data', function() { + var string = '{{#goodbyes}}{{text}}{{/goodbyes}} {{#goodbyes}}{{text}}{{/goodbyes}}'; + var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'}; + shouldCompileTo(string, [hash,,,,false], 'goodbyeGoodbyeGOODBYE goodbyeGoodbyeGOODBYE'); }); it("array with @index", function() { diff --git a/spec/builtins.js b/spec/builtins.js index 77f3701..d2fa88f 100644 --- a/spec/builtins.js +++ b/spec/builtins.js @@ -66,9 +66,29 @@ describe('builtin helpers', function() { "each with array argument ignores the contents when empty"); }); + it('each without data', function() { + var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!'; + var hash = {goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}], world: 'world'}; + shouldCompileTo(string, [hash,,,,false], 'goodbye! Goodbye! GOODBYE! cruel world!'); + + hash = {goodbyes: 'cruel', world: 'world'}; + shouldCompileTo('{{#each .}}{{.}}{{/each}}', [hash,,,,false], 'cruelworld'); + }); + + it('each without context', function() { + var string = '{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!'; + shouldCompileTo(string, [,,,,], 'cruel !'); + }); + it("each with an object and @key", function() { var string = "{{#each goodbyes}}{{@key}}. {{text}}! {{/each}}cruel {{world}}!"; - var hash = {goodbyes: {"<b>#1</b>": {text: "goodbye"}, 2: {text: "GOODBYE"}}, world: "world"}; + + function Clazz() { + this['<b>#1</b>'] = {text: 'goodbye'}; + this[2] = {text: 'GOODBYE'}; + } + Clazz.prototype.foo = 'fail'; + var hash = {goodbyes: new Clazz(), world: 'world'}; // Object property iteration order is undefined according to ECMA spec, // so we need to check both possible orders @@ -193,19 +213,77 @@ describe('builtin helpers', function() { }); }); - it("#log", function() { - var string = "{{log blah}}"; - var hash = { blah: "whee" }; + describe("#log", function() { + var info, + error; + beforeEach(function() { + info = console.info; + error = console.error; + }); + afterEach(function() { + console.info = info; + console.error = error; + }); + + it('should call logger at default level', function() { + var string = "{{log blah}}"; + var hash = { blah: "whee" }; - var levelArg, logArg; - handlebarsEnv.log = function(level, arg){ - levelArg = level; - logArg = arg; - }; + var levelArg, logArg; + handlebarsEnv.log = function(level, arg){ + levelArg = level; + logArg = arg; + }; - shouldCompileTo(string, hash, "", "log should not display"); - equals(1, levelArg, "should call log with 1"); - equals("whee", logArg, "should call log with 'whee'"); + shouldCompileTo(string, hash, "", "log should not display"); + equals(1, levelArg, "should call log with 1"); + equals("whee", logArg, "should call log with 'whee'"); + }); + it('should call logger at data level', function() { + var string = "{{log blah}}"; + var hash = { blah: "whee" }; + + var levelArg, logArg; + handlebarsEnv.log = function(level, arg){ + levelArg = level; + logArg = arg; + }; + + shouldCompileTo(string, [hash,,,,{level: '03'}], ""); + equals(3, levelArg); + equals("whee", logArg); + }); + it('should not output to console', function() { + var string = "{{log blah}}"; + var hash = { blah: "whee" }; + + console.info = function() { + throw new Error(); + }; + + shouldCompileTo(string, hash, "", "log should not display"); + }); + it('should log at data level', function() { + var string = "{{log blah}}"; + var hash = { blah: "whee" }; + var called; + + console.error = function(log) { + equals("whee", log); + called = true; + }; + + shouldCompileTo(string, [hash,,,,{level: '03'}], ""); + equals(true, called); + }); + it('should handle missing logger', function() { + var string = "{{log blah}}"; + var hash = { blah: "whee" }; + + console.error = undefined; + + shouldCompileTo(string, [hash,,,,{level: '03'}], ""); + }); }); diff --git a/spec/env/common.js b/spec/env/common.js index e551837..92cc611 100644 --- a/spec/env/common.js +++ b/spec/env/common.js @@ -19,6 +19,10 @@ global.compileWithPartials = function(string, hashOrArray, partials) { ary.push(hashOrArray[0]); ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] }); options = {compat: hashOrArray[3]}; + if (hashOrArray[4] != null) { + options.data = !!hashOrArray[4]; + ary[1].data = hashOrArray[4]; + } } else { ary = [hashOrArray]; } diff --git a/spec/partials.js b/spec/partials.js index cc464b9..d401d2e 100644 --- a/spec/partials.js +++ b/spec/partials.js @@ -139,7 +139,17 @@ describe('partials', function() { var string = "Dudes: {{#dudes}}{{> dude}}{{/dudes}}"; var partial = ""; var hash = {dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]}; - shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: "); }); + shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: "); + }); + + it("throw on missing partial", function() { + var compile = handlebarsEnv.compile; + handlebarsEnv.compile = undefined; + shouldThrow(function() { + shouldCompileTo('{{> dude}}', [{}, {}, {dude: 'fail'}], ''); + }, Error, /The partial dude could not be compiled/); + handlebarsEnv.compile = compile; + }); describe('standalone partials', function() { it("indented partials", function() { |