diff options
Diffstat (limited to 'spec/ast.js')
-rw-r--r-- | spec/ast.js | 293 |
1 files changed, 106 insertions, 187 deletions
diff --git a/spec/ast.js b/spec/ast.js index ef2ef68..d464cf1 100644 --- a/spec/ast.js +++ b/spec/ast.js @@ -5,69 +5,34 @@ describe('ast', function() { } var LOCATION_INFO = { - last_line: 0, - first_line: 0, - first_column: 0, - last_column: 0 + start: { + line: 1, + column: 1 + }, + end: { + line: 1, + column: 1 + } }; function testLocationInfoStorage(node){ - var properties = [ 'firstLine', 'lastLine', 'firstColumn', 'lastColumn' ], - property, - propertiesLen = properties.length, - i; - - for (i = 0; i < propertiesLen; i++){ - property = properties[0]; - equals(node[property], 0); - } + equals(node.loc.start.line, 1); + equals(node.loc.start.column, 1); + equals(node.loc.end.line, 1); + equals(node.loc.end.column, 1); } - describe('MustacheNode', function() { - function testEscape(open, expected) { - var mustache = new handlebarsEnv.AST.MustacheNode([{}], {}, open, false); - equals(mustache.escaped, expected); - } - + describe('MustacheStatement', function() { it('should store args', function() { var id = {isSimple: true}, hash = {}, - mustache = new handlebarsEnv.AST.MustacheNode([id, 'param1'], hash, '', false, LOCATION_INFO); - equals(mustache.type, 'mustache'); - equals(mustache.hash, hash); + mustache = new handlebarsEnv.AST.MustacheStatement({}, true, {}, LOCATION_INFO); + equals(mustache.type, 'MustacheStatement'); equals(mustache.escaped, true); - equals(mustache.id, id); - equals(mustache.params.length, 1); - equals(mustache.params[0], 'param1'); - equals(!!mustache.isHelper, true); testLocationInfoStorage(mustache); }); - it('should accept token for escape', function() { - testEscape('{{', true); - testEscape('{{~', true); - testEscape('{{#', true); - testEscape('{{~#', true); - testEscape('{{/', true); - testEscape('{{~/', true); - testEscape('{{^', true); - testEscape('{{~^', true); - testEscape('{', true); - testEscape('{', true); - - testEscape('{{&', false); - testEscape('{{~&', false); - testEscape('{{{', false); - testEscape('{{~{', false); - }); - it('should accept boolean for escape', function() { - testEscape(true, true); - testEscape({}, true); - - testEscape(false, false); - testEscape(undefined, false); - }); }); - describe('BlockNode', function() { + describe('BlockStatement', function() { it('should throw on mustache mismatch', function() { shouldThrow(function() { handlebarsEnv.parse("\n {{#foo}}{{/bar}}"); @@ -75,175 +40,129 @@ describe('ast', function() { }); it('stores location info', function(){ - var sexprNode = new handlebarsEnv.AST.SexprNode([{ original: 'foo'}], null); - var mustacheNode = new handlebarsEnv.AST.MustacheNode(sexprNode, null, '{{', {}); - var block = new handlebarsEnv.AST.BlockNode(mustacheNode, - {statements: [], strip: {}}, {statements: [], strip: {}}, - { - strip: {}, - path: {original: 'foo'} - }, - LOCATION_INFO); + var sexprNode = new handlebarsEnv.AST.SubExpression([{ original: 'foo'}], null); + var mustacheNode = new handlebarsEnv.AST.MustacheStatement(sexprNode, false, {}); + var block = new handlebarsEnv.AST.BlockStatement( + mustacheNode, + {body: []}, + {body: []}, + {}, + {}, + {}, + LOCATION_INFO); testLocationInfoStorage(block); }); }); - describe('IdNode', function() { - it('should throw on invalid path', function() { - shouldThrow(function() { - new handlebarsEnv.AST.IdNode([ - {part: 'foo'}, - {part: '..'}, - {part: 'bar'} - ], {first_line: 1, first_column: 1}); - }, Handlebars.Exception, "Invalid path: foo.. - 1:1"); - shouldThrow(function() { - new handlebarsEnv.AST.IdNode([ - {part: 'foo'}, - {part: '.'}, - {part: 'bar'} - ], {first_line: 1, first_column: 1}); - }, Handlebars.Exception, "Invalid path: foo. - 1:1"); - shouldThrow(function() { - new handlebarsEnv.AST.IdNode([ - {part: 'foo'}, - {part: 'this'}, - {part: 'bar'} - ], {first_line: 1, first_column: 1}); - }, Handlebars.Exception, "Invalid path: foothis - 1:1"); - }); - + describe('PathExpression', function() { it('stores location info', function(){ - var idNode = new handlebarsEnv.AST.IdNode([], LOCATION_INFO); + var idNode = new handlebarsEnv.AST.PathExpression(false, 0, [], 'foo', LOCATION_INFO); testLocationInfoStorage(idNode); }); }); - describe("HashNode", function(){ - + describe('Hash', function(){ it('stores location info', function(){ - var hash = new handlebarsEnv.AST.HashNode([], LOCATION_INFO); + var hash = new handlebarsEnv.AST.Hash([], LOCATION_INFO); testLocationInfoStorage(hash); }); }); - describe("ContentNode", function(){ - + describe('ContentStatement', function(){ it('stores location info', function(){ - var content = new handlebarsEnv.AST.ContentNode("HI", LOCATION_INFO); + var content = new handlebarsEnv.AST.ContentStatement("HI", LOCATION_INFO); testLocationInfoStorage(content); }); }); - describe("CommentNode", function(){ - + describe('CommentStatement', function(){ it('stores location info', function(){ - var comment = new handlebarsEnv.AST.CommentNode("HI", {}, LOCATION_INFO); + var comment = new handlebarsEnv.AST.CommentStatement("HI", {}, LOCATION_INFO); testLocationInfoStorage(comment); }); }); - describe("NumberNode", function(){ - + describe('NumberLiteral', function(){ it('stores location info', function(){ - var integer = new handlebarsEnv.AST.NumberNode("6", LOCATION_INFO); + var integer = new handlebarsEnv.AST.NumberLiteral("6", LOCATION_INFO); testLocationInfoStorage(integer); }); }); - describe("StringNode", function(){ - + describe('StringLiteral', function(){ it('stores location info', function(){ - var string = new handlebarsEnv.AST.StringNode("6", LOCATION_INFO); + var string = new handlebarsEnv.AST.StringLiteral("6", LOCATION_INFO); testLocationInfoStorage(string); }); }); - describe("BooleanNode", function(){ - + describe('BooleanLiteral', function(){ it('stores location info', function(){ - var bool = new handlebarsEnv.AST.BooleanNode("true", LOCATION_INFO); + var bool = new handlebarsEnv.AST.BooleanLiteral("true", LOCATION_INFO); testLocationInfoStorage(bool); }); }); - describe("DataNode", function(){ - - it('stores location info', function(){ - var data = new handlebarsEnv.AST.DataNode("YES", LOCATION_INFO); - testLocationInfoStorage(data); - }); - }); - - describe("PartialNameNode", function(){ - - it('stores location info', function(){ - var pnn = new handlebarsEnv.AST.PartialNameNode({original: "YES"}, LOCATION_INFO); - testLocationInfoStorage(pnn); - }); - }); - - describe("PartialNode", function(){ - + describe('PartialStatement', function(){ it('stores location info', function(){ - var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, {}, LOCATION_INFO); + var pn = new handlebarsEnv.AST.PartialStatement('so_partial', {}, LOCATION_INFO); testLocationInfoStorage(pn); }); }); - describe('ProgramNode', function(){ + describe('Program', function(){ it('storing location info', function(){ - var pn = new handlebarsEnv.AST.ProgramNode([], null, {}, LOCATION_INFO); + var pn = new handlebarsEnv.AST.Program([], null, {}, LOCATION_INFO); testLocationInfoStorage(pn); }); }); describe("Line Numbers", function(){ - var ast, statements; + var ast, body; function testColumns(node, firstLine, lastLine, firstColumn, lastColumn){ - equals(node.firstLine, firstLine); - equals(node.lastLine, lastLine); - equals(node.firstColumn, firstColumn); - equals(node.lastColumn, lastColumn); + equals(node.loc.start.line, firstLine); + equals(node.loc.start.column, firstColumn); + equals(node.loc.end.line, lastLine); + equals(node.loc.end.column, lastColumn); } ast = Handlebars.parse("line 1 {{line1Token}}\n line 2 {{line2token}}\n line 3 {{#blockHelperOnLine3}}\nline 4{{line4token}}\n" + "line5{{else}}\n{{line6Token}}\n{{/blockHelperOnLine3}}"); - statements = ast.statements; + body = ast.body; it('gets ContentNode line numbers', function(){ - var contentNode = statements[0]; + var contentNode = body[0]; testColumns(contentNode, 1, 1, 0, 7); }); - it('gets MustacheNode line numbers', function(){ - var mustacheNode = statements[1]; + it('gets MustacheStatement line numbers', function(){ + var mustacheNode = body[1]; testColumns(mustacheNode, 1, 1, 7, 21); }); it('gets line numbers correct when newlines appear', function(){ - testColumns(statements[2], 1, 2, 21, 8); + testColumns(body[2], 1, 2, 21, 8); }); - it('gets MustacheNode line numbers correct across newlines', function(){ - var secondMustacheNode = statements[3]; - testColumns(secondMustacheNode, 2, 2, 8, 22); + it('gets MustacheStatement line numbers correct across newlines', function(){ + var secondMustacheStatement = body[3]; + testColumns(secondMustacheStatement, 2, 2, 8, 22); }); it('gets the block helper information correct', function(){ - var blockHelperNode = statements[5]; + var blockHelperNode = body[5]; testColumns(blockHelperNode, 3, 7, 8, 23); }); it('correctly records the line numbers the program of a block helper', function(){ - var blockHelperNode = statements[5], + var blockHelperNode = body[5], program = blockHelperNode.program; testColumns(program, 3, 5, 8, 5); }); it('correctly records the line numbers of an inverse of a block helper', function(){ - var blockHelperNode = statements[5], + var blockHelperNode = body[5], inverse = blockHelperNode.inverse; testColumns(inverse, 5, 7, 5, 0); @@ -254,118 +173,118 @@ describe('ast', function() { describe('mustache', function() { it('does not mark mustaches as standalone', function() { var ast = Handlebars.parse(' {{comment}} '); - equals(!!ast.statements[0].string, true); - equals(!!ast.statements[2].string, true); + equals(!!ast.body[0].value, true); + equals(!!ast.body[2].value, true); }); }); describe('blocks', function() { it('marks block mustaches as standalone', function() { var ast = Handlebars.parse(' {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '), - block = ast.statements[1]; + block = ast.body[1]; - equals(ast.statements[0].string, ''); + equals(ast.body[0].value, ''); - equals(block.program.statements[0].string, 'foo\n'); - equals(block.inverse.statements[0].string, ' bar \n'); + equals(block.program.body[0].value, 'foo\n'); + equals(block.inverse.body[0].value, ' bar \n'); - equals(ast.statements[2].string, ''); + equals(ast.body[2].value, ''); }); it('marks initial block mustaches as standalone', function() { var ast = Handlebars.parse('{{# comment}} \nfoo\n {{/comment}}'), - block = ast.statements[0]; + block = ast.body[0]; - equals(block.program.statements[0].string, 'foo\n'); + equals(block.program.body[0].value, 'foo\n'); }); it('marks mustaches with children as standalone', function() { var ast = Handlebars.parse('{{# comment}} \n{{foo}}\n {{/comment}}'), - block = ast.statements[0]; + block = ast.body[0]; - equals(block.program.statements[0].string, ''); - equals(block.program.statements[1].id.original, 'foo'); - equals(block.program.statements[2].string, '\n'); + equals(block.program.body[0].value, ''); + equals(block.program.body[1].sexpr.path.original, 'foo'); + equals(block.program.body[2].value, '\n'); }); it('marks nested block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}} \n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} \n{{/foo}}'), - statements = ast.statements[0].program.statements, - block = statements[1]; + body = ast.body[0].program.body, + block = body[1]; - equals(statements[0].string, ''); + equals(body[0].value, ''); - equals(block.program.statements[0].string, 'foo\n'); - equals(block.inverse.statements[0].string, ' bar \n'); + equals(block.program.body[0].value, 'foo\n'); + equals(block.inverse.body[0].value, ' bar \n'); - equals(statements[0].string, ''); + equals(body[0].value, ''); }); it('does not mark nested block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}} {{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} {{/foo}}'), - statements = ast.statements[0].program.statements, - block = statements[1]; + body = ast.body[0].program.body, + block = body[1]; - equals(statements[0].omit, undefined); + equals(body[0].omit, undefined); - equals(block.program.statements[0].string, ' \nfoo\n'); - equals(block.inverse.statements[0].string, ' bar \n '); + equals(block.program.body[0].value, ' \nfoo\n'); + equals(block.inverse.body[0].value, ' bar \n '); - equals(statements[0].omit, undefined); + equals(body[0].omit, undefined); }); it('does not mark nested initial block mustaches as standalone', function() { var ast = Handlebars.parse('{{#foo}}{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}}{{/foo}}'), - statements = ast.statements[0].program.statements, - block = statements[0]; + body = ast.body[0].program.body, + block = body[0]; - equals(block.program.statements[0].string, ' \nfoo\n'); - equals(block.inverse.statements[0].string, ' bar \n '); + equals(block.program.body[0].value, ' \nfoo\n'); + equals(block.inverse.body[0].value, ' bar \n '); - equals(statements[0].omit, undefined); + equals(body[0].omit, undefined); }); it('marks column 0 block mustaches as standalone', function() { var ast = Handlebars.parse('test\n{{# comment}} \nfoo\n {{else}} \n bar \n {{/comment}} '), - block = ast.statements[1]; + block = ast.body[1]; - equals(ast.statements[0].omit, undefined); + equals(ast.body[0].omit, undefined); - equals(block.program.statements[0].string, 'foo\n'); - equals(block.inverse.statements[0].string, ' bar \n'); + equals(block.program.body[0].value, 'foo\n'); + equals(block.inverse.body[0].value, ' bar \n'); - equals(ast.statements[2].string, ''); + equals(ast.body[2].value, ''); }); }); describe('partials', function() { it('marks partial as standalone', function() { var ast = Handlebars.parse('{{> partial }} '); - equals(ast.statements[1].string, ''); + equals(ast.body[1].value, ''); }); it('marks indented partial as standalone', function() { var ast = Handlebars.parse(' {{> partial }} '); - equals(ast.statements[0].string, ''); - equals(ast.statements[1].indent, ' '); - equals(ast.statements[2].string, ''); + equals(ast.body[0].value, ''); + equals(ast.body[1].indent, ' '); + equals(ast.body[2].value, ''); }); it('marks those around content as not standalone', function() { var ast = Handlebars.parse('a{{> partial }}'); - equals(ast.statements[0].omit, undefined); + equals(ast.body[0].omit, undefined); ast = Handlebars.parse('{{> partial }}a'); - equals(ast.statements[1].omit, undefined); + equals(ast.body[1].omit, undefined); }); }); describe('comments', function() { it('marks comment as standalone', function() { var ast = Handlebars.parse('{{! comment }} '); - equals(ast.statements[1].string, ''); + equals(ast.body[1].value, ''); }); it('marks indented comment as standalone', function() { var ast = Handlebars.parse(' {{! comment }} '); - equals(ast.statements[0].string, ''); - equals(ast.statements[2].string, ''); + equals(ast.body[0].value, ''); + equals(ast.body[2].value, ''); }); it('marks those around content as not standalone', function() { var ast = Handlebars.parse('a{{! comment }}'); - equals(ast.statements[0].omit, undefined); + equals(ast.body[0].omit, undefined); ast = Handlebars.parse('{{! comment }}a'); - equals(ast.statements[1].omit, undefined); + equals(ast.body[1].omit, undefined); }); }); }); |