diff options
Diffstat (limited to 'spec/blocks.js')
-rw-r--r-- | spec/blocks.js | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/spec/blocks.js b/spec/blocks.js index 8f7c242..9a5cb40 100644 --- a/spec/blocks.js +++ b/spec/blocks.js @@ -1,4 +1,4 @@ -/*global CompilerContext, shouldCompileTo */ +/*global CompilerContext, shouldCompileTo, shouldThrow */ describe('blocks', function() { it("array", function() { var string = "{{#goodbyes}}{{text}}! {{/goodbyes}}cruel {{world}}!"; @@ -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() { @@ -39,6 +44,13 @@ describe('blocks', function() { "Templates can access variables in contexts up the stack with relative path syntax"); }); + it('multiple blocks with complex lookup', function() { + var string = '{{#goodbyes}}{{../name}}{{../name}}{{/goodbyes}}'; + var hash = {name: 'Alan', goodbyes: [{text: 'goodbye'}, {text: 'Goodbye'}, {text: 'GOODBYE'}]}; + + shouldCompileTo(string, hash, 'AlanAlanAlanAlanAlanAlan'); + }); + it("block with complex lookup using nested context", function() { var string = "{{#goodbyes}}{{text}} cruel {{foo/../name}}! {{/goodbyes}}"; @@ -48,10 +60,10 @@ describe('blocks', function() { }); it("block with deep nested complex lookup", function() { - var string = "{{#outer}}Goodbye {{#inner}}cruel {{../../omg}}{{/inner}}{{/outer}}"; - var hash = {omg: "OMG!", outer: [{ inner: [{ text: "goodbye" }] }] }; + var string = "{{#outer}}Goodbye {{#inner}}cruel {{../sibling}} {{../../omg}}{{/inner}}{{/outer}}"; + var hash = {omg: "OMG!", outer: [{ sibling: 'sad', inner: [{ text: "goodbye" }] }] }; - shouldCompileTo(string, hash, "Goodbye cruel OMG!"); + shouldCompileTo(string, hash, "Goodbye cruel sad OMG!"); }); describe('inverted sections', function() { @@ -83,4 +95,36 @@ describe('blocks', function() { "No people"); }); }); + + describe('standalone sections', function() { + it('block standalone else sections', function() { + shouldCompileTo('{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n', {none: 'No people'}, + 'No people\n'); + shouldCompileTo('{{#none}}\n{{.}}\n{{^}}\n{{none}}\n{{/none}}\n', {none: 'No people'}, + 'No people\n'); + shouldCompileTo('\n{{#people}}\n{{name}}\n{{^}}\n{{none}}\n{{/people}}\n', {none: 'No people'}, + 'No people\n'); + }); + }); + + describe('compat mode', function() { + it("block with deep recursive lookup lookup", function() { + var string = "{{#outer}}Goodbye {{#inner}}cruel {{omg}}{{/inner}}{{/outer}}"; + var hash = {omg: "OMG!", outer: [{ inner: [{ text: "goodbye" }] }] }; + + shouldCompileTo(string, [hash, undefined, undefined, true], "Goodbye cruel OMG!"); + }); + it("block with deep recursive pathed lookup", function() { + var string = "{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}"; + var hash = {omg: {yes: "OMG!"}, outer: [{ inner: [{ yes: 'no', text: "goodbye" }] }] }; + + shouldCompileTo(string, [hash, undefined, undefined, true], "Goodbye cruel OMG!"); + }); + it("block with missed recursive lookup", function() { + var string = "{{#outer}}Goodbye {{#inner}}cruel {{omg.yes}}{{/inner}}{{/outer}}"; + var hash = {omg: {no: "OMG!"}, outer: [{ inner: [{ yes: 'no', text: "goodbye" }] }] }; + + shouldCompileTo(string, [hash, undefined, undefined, true], "Goodbye cruel "); + }); + }); }); |