Test the parser, and clean up ID having multiple parts
This commit is contained in:
+15
-2
@@ -30,9 +30,22 @@ Handlebars.AST.ContentNode = function(string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
Handlebars.AST.IdNode = function(id) {
|
||||
Handlebars.AST.IdNode = function(parts) {
|
||||
this.type = "ID"
|
||||
this.id = id;
|
||||
this.parts = parts;
|
||||
|
||||
var dig = [], depth = 0;
|
||||
|
||||
for(var i=0,l=parts.length; i<l; i++) {
|
||||
var part = parts[i];
|
||||
|
||||
if(part === "..") { depth++; }
|
||||
else if(part === "." || part === "this") { break; }
|
||||
else { dig.push(part) }
|
||||
}
|
||||
|
||||
this.dig = dig;
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
Handlebars.AST.StringNode = function(string) {
|
||||
|
||||
@@ -79,8 +79,8 @@ Handlebars.PrintVisitor.prototype.STRING = function(string) {
|
||||
};
|
||||
|
||||
Handlebars.PrintVisitor.prototype.ID = function(id) {
|
||||
var path = id.id.join("/");
|
||||
if(id.id.length > 1) {
|
||||
var path = id.parts.join("/");
|
||||
if(id.parts.length > 1) {
|
||||
return "PATH:" + path;
|
||||
} else {
|
||||
return "ID:" + path;
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe "Parser" do
|
||||
let(:handlebars) { @context["Handlebars"] }
|
||||
|
||||
def program(&block)
|
||||
ASTBuilder.build do
|
||||
program do
|
||||
instance_eval(&block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def ast_for(string)
|
||||
ast = handlebars.parse(string)
|
||||
handlebars.print(ast)
|
||||
end
|
||||
|
||||
class ASTBuilder
|
||||
def self.build(&block)
|
||||
ret = new
|
||||
ret.evaluate(&block)
|
||||
ret.out
|
||||
end
|
||||
|
||||
attr_reader :out
|
||||
|
||||
def initialize
|
||||
@padding = 0
|
||||
@out = ""
|
||||
end
|
||||
|
||||
def evaluate(&block)
|
||||
instance_eval(&block)
|
||||
end
|
||||
|
||||
def pad(string)
|
||||
@out << (" " * @padding) + string + "\n"
|
||||
end
|
||||
|
||||
def with_padding
|
||||
@padding += 1
|
||||
ret = yield
|
||||
@padding -= 1
|
||||
ret
|
||||
end
|
||||
|
||||
def program
|
||||
pad("PROGRAM:")
|
||||
with_padding { yield }
|
||||
end
|
||||
|
||||
def inverse
|
||||
pad("{{^}}")
|
||||
with_padding { yield }
|
||||
end
|
||||
|
||||
def block
|
||||
pad("BLOCK:")
|
||||
with_padding { yield }
|
||||
end
|
||||
|
||||
def mustache(id, *params)
|
||||
pad("{{ #{id} [#{params.join(", ")}] }}")
|
||||
end
|
||||
|
||||
def partial(id)
|
||||
pad("{{> #{id} }}")
|
||||
end
|
||||
|
||||
def comment(comment)
|
||||
pad("{{! '#{comment}' }}")
|
||||
end
|
||||
|
||||
def content(string)
|
||||
pad("CONTENT[ '#{string}' ]")
|
||||
end
|
||||
|
||||
def string(string)
|
||||
string.inspect
|
||||
end
|
||||
|
||||
def id(id)
|
||||
"ID:#{id}"
|
||||
end
|
||||
|
||||
def path(*parts)
|
||||
"PATH:#{parts.join("/")}"
|
||||
end
|
||||
end
|
||||
|
||||
it "parses simple mustaches" do
|
||||
ast_for("{{foo}}").should == program { mustache id("foo") }
|
||||
end
|
||||
|
||||
it "parses mustaches with paths" do
|
||||
ast_for("{{foo/bar}}").should == program { mustache path("foo", "bar") }
|
||||
end
|
||||
|
||||
it "parses mustaches with parameters" do
|
||||
ast_for("{{foo bar}}").should == program { mustache id("foo"), id("bar") }
|
||||
end
|
||||
|
||||
it "parses mustaches with string parameters" do
|
||||
ast_for("{{foo bar \"baz\" }}").should == program { mustache id("foo"), id("bar"), string("baz")}
|
||||
end
|
||||
|
||||
it "parses contents followed by a mustache" do
|
||||
ast_for("foo bar {{baz}}").should == program do
|
||||
content "foo bar "
|
||||
mustache id("baz")
|
||||
end
|
||||
end
|
||||
|
||||
it "parses a partial" do
|
||||
ast_for("{{> foo }}").should == program { partial id("foo") }
|
||||
end
|
||||
|
||||
it "parses a comment" do
|
||||
ast_for("{{! this is a comment }}").should == program do
|
||||
comment " this is a comment "
|
||||
end
|
||||
end
|
||||
|
||||
it "parses an inverse section" do
|
||||
ast_for("{{#foo}} bar {{^}} baz {{/foo}}").should == program do
|
||||
block do
|
||||
mustache id("foo")
|
||||
|
||||
program do
|
||||
content " bar "
|
||||
end
|
||||
|
||||
inverse do
|
||||
content " baz "
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "raises if there's a Parse error" do
|
||||
lambda { ast_for("{{foo}") }.should raise_error(V8::JSError, /Parse error on line 1/)
|
||||
lambda { ast_for("{{foo &}}")}.should raise_error(V8::JSError, /Parse error on line 1/)
|
||||
end
|
||||
|
||||
it "knows how to report the correct line number in errors" do
|
||||
lambda { ast_for("hello\nmy\n{{foo}") }.should raise_error(V8::JSError, /Parse error on line 3/m)
|
||||
lambda { ast_for("hello\n\nmy\n\n{{foo}") }.should raise_error(V8::JSError, /Parse error on line 5/m)
|
||||
end
|
||||
|
||||
it "knows how to report the correct line number in errors when the first character is a newline" do
|
||||
lambda { ast_for("\n\nhello\n\nmy\n\n{{foo}") }.should raise_error(V8::JSError, /Parse error on line 7/m)
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,11 @@ require.paths.push(__dirname + "/lib");
|
||||
var Handlebars = require("handlebars").Handlebars;
|
||||
|
||||
var string = "foo {{ bar ../baz/bat/bam \"baz\" }}baz{{! foo bar baz }}{{#foo}} bar {{^}} baz {{/foo}}{{> partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg"
|
||||
var string = "foo {{person/name/0}} '{{ join person/name \", \" }}' baz";
|
||||
|
||||
var ast = Handlebars.parse(string);
|
||||
require("sys").print(Handlebars.print(ast));
|
||||
//require("sys").print(Handlebars.print(ast));
|
||||
//
|
||||
var runtime = new Handlebars.Runtime({person: {name: ["Katz", "Yehuda"]}}, {join: function(name, sep) { return name.join(sep); }})
|
||||
runtime.accept(ast)
|
||||
require("sys").print(runtime.buffer)
|
||||
|
||||
Reference in New Issue
Block a user