diff options
author | kpdecker <kpdecker@gmail.com> | 2012-12-30 17:10:38 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2012-12-30 17:10:38 -0600 |
commit | 51fd64c3341a37c18f0a301d5028fd07a07a1ce2 (patch) | |
tree | 4f6277456e69934b303440bee62e21f9cef02c63 /spec | |
parent | dfaf698b4494aa2748f21b32dd34bea8dcab34fd (diff) | |
parent | 28f377d169e878959619b922661a9377d3e22e97 (diff) | |
download | handlebars.js-51fd64c3341a37c18f0a301d5028fd07a07a1ce2.zip handlebars.js-51fd64c3341a37c18f0a301d5028fd07a07a1ce2.tar.gz handlebars.js-51fd64c3341a37c18f0a301d5028fd07a07a1ce2.tar.bz2 |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'spec')
-rw-r--r-- | spec/parser_spec.rb | 136 | ||||
-rw-r--r-- | spec/qunit_spec.js | 48 | ||||
-rw-r--r-- | spec/spec_helper.rb | 27 | ||||
-rw-r--r-- | spec/tokenizer_spec.rb | 16 |
4 files changed, 184 insertions, 43 deletions
diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 2b234d3..d9187ce 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -114,6 +114,10 @@ describe "Parser" do "@#{id}" end + def partial_name(name) + "PARTIAL:#{name}" + end + def path(*parts) "PATH:#{parts.join("/")}" end @@ -218,11 +222,15 @@ describe "Parser" do end it "parses a partial" do - ast_for("{{> foo }}").should == root { partial id("foo") } + ast_for("{{> foo }}").should == root { partial partial_name("foo") } end it "parses a partial with context" do - ast_for("{{> foo bar}}").should == root { partial id("foo"), id("bar") } + ast_for("{{> foo bar}}").should == root { partial partial_name("foo"), id("bar") } + end + + it "parses a partial with a complex name" do + ast_for("{{> shared/partial}}").should == root { partial partial_name("shared/partial") } end it "parses a comment" do @@ -253,6 +261,130 @@ describe "Parser" do end end + it "parses an inverse ('else'-style) section" do + ast_for("{{#foo}} bar {{else}} baz {{/foo}}").should == root do + block do + mustache id("foo") + + program do + content " bar " + end + + inverse do + content " baz " + end + end + end + end + + it "parses empty blocks" do + ast_for("{{#foo}}{{/foo}}").should == root do + block do + mustache id("foo") + + program do + # empty program + end + end + end + end + + it "parses empty blocks with empty inverse section" do + ast_for("{{#foo}}{{^}}{{/foo}}").should == root do + block do + mustache id("foo") + + program do + # empty program + end + + inverse do + # empty inverse + end + end + end + end + + it "parses empty blocks with empty inverse ('else'-style) section" do + ast_for("{{#foo}}{{else}}{{/foo}}").should == root do + block do + mustache id("foo") + + program do + # empty program + end + + inverse do + # empty inverse + end + end + end + end + + it "parses non-empty blocks with empty inverse section" do + ast_for("{{#foo}} bar {{^}}{{/foo}}").should == root do + block do + mustache id("foo") + + program do + content " bar " + end + + inverse do + # empty inverse + end + end + end + end + + it "parses non-empty blocks with empty inverse ('else'-style) section" do + ast_for("{{#foo}} bar {{else}}{{/foo}}").should == root do + block do + mustache id("foo") + + program do + content " bar " + end + + inverse do + # empty inverse + end + end + end + end + + it "parses empty blocks with non-empty inverse section" do + ast_for("{{#foo}}{{^}} bar {{/foo}}").should == root do + block do + mustache id("foo") + + program do + # empty program + end + + inverse do + content " bar " + end + end + end + end + + it "parses empty blocks with non-empty inverse ('else'-style) section" do + ast_for("{{#foo}}{{else}} bar {{/foo}}").should == root do + block do + mustache id("foo") + + program do + # empty program + end + + inverse do + content " bar " + end + end + end + end + it "parses a standalone inverse section" do ast_for("{{^foo}}bar{{/foo}}").should == root do block do diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 5c94253..5203cfc 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -456,12 +456,17 @@ test("providing a helpers hash", function() { "Goodbye cruel world!", "helpers hash is available inside other blocks"); }); -test("in cases of conflict, the explicit hash wins", function() { - +test("in cases of conflict, helpers win", function() { + shouldCompileTo("{{{lookup}}}", [{lookup: 'Explicit'}, {lookup: function() { return 'helpers'; }}], "helpers", + "helpers hash has precedence escaped expansion"); + shouldCompileTo("{{lookup}}", [{lookup: 'Explicit'}, {lookup: function() { return 'helpers'; }}], "helpers", + "helpers hash has precedence simple expansion"); }); test("the helpers hash is available is nested contexts", function() { - + shouldCompileTo("{{#outer}}{{#inner}}{{helper}}{{/inner}}{{/outer}}", + [{'outer': {'inner': {'unused':[]}}}, {'helper': function() { return 'helper'; }}], "helper", + "helpers hash is available in nested contexts."); }); suite("partials"); @@ -521,13 +526,21 @@ test("GH-14: a partial preceding a selector", function() { shouldCompileToWithPartials(string, [hash, {}, {dude:dude}], true, "Dudes: Jeepers Creepers", "Regular selectors can follow a partial"); }); -test("Partials with literal paths", function() { - var string = "Dudes: {{> [dude]}}"; +test("Partials with slash paths", function() { + var string = "Dudes: {{> shared/dude}}"; var dude = "{{name}}"; var hash = {name:"Jeepers", another_dude:"Creepers"}; - shouldCompileToWithPartials(string, [hash, {}, {dude:dude}], true, "Dudes: Jeepers", "Partials can use literal paths"); + shouldCompileToWithPartials(string, [hash, {}, {'shared/dude':dude}], true, "Dudes: Jeepers", "Partials can use literal paths"); }); +test("Partials with integer path", function() { + var string = "Dudes: {{> 404}}"; + var dude = "{{name}}"; + var hash = {name:"Jeepers", another_dude:"Creepers"}; + shouldCompileToWithPartials(string, [hash, {}, {404:dude}], true, "Dudes: Jeepers", "Partials can use literal paths"); +}); + + suite("String literal parameters"); test("simple literals work", function() { @@ -730,6 +743,23 @@ test("each with @index", function() { equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used"); }); +test("data passed to helpers", function() { + var string = "{{#each letters}}{{this}}{{detectDataInsideEach}}{{/each}}"; + var hash = {letters: ['a', 'b', 'c']}; + + var template = CompilerContext.compile(string); + var result = template(hash, { + data: { + exclaim: '!' + } + }); + equal(result, 'a!b!c!'); +}); + +Handlebars.registerHelper('detectDataInsideEach', function(options) { + return options.data && options.data.exclaim; +}); + test("log", function() { var string = "{{log blah}}"; var hash = { blah: "whee" }; @@ -1245,3 +1275,9 @@ test("bug reported by @fat where lambdas weren't being properly resolved", funct var output = "<strong>This is a slightly more complicated blah.</strong>.\n\nCheck this out:\n\n<ul>\n\n<li class=one>@fat</li>\n\n<li class=two>@dhg</li>\n\n<li class=three>@sayrer</li>\n</ul>.\n\n"; shouldCompileTo(string, data, output); }); + +test("Passing falsy values to Handlebars.compile throws an error", function() { + shouldThrow(function() { + CompilerContext.compile(null); + }, "You must pass a string to Handlebars.compile. You passed null"); +}); diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cf73801..605433c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,32 +1,5 @@ require "v8" -# Monkey patches due to bugs in RubyRacer -class V8::JSError - def initialize(try, to) - @to = to - begin - super(initialize_unsafe(try)) - rescue Exception => e - # Original code does not make an Array here - @boundaries = [Boundary.new(:rbframes => e.backtrace)] - @value = e - super("BUG! please report. JSError#initialize failed!: #{e.message}") - end - end - - def parse_js_frames(try) - raw = @to.rb(try.StackTrace()) - if raw && !raw.empty? - raw.split("\n")[1..-1].tap do |frames| - # Original code uses strip!, and the frames are not guaranteed to be strippable - frames.each {|frame| frame.strip.chomp!(",")} - end - else - [] - end - end -end - module Handlebars module Spec def self.js_backtrace(context) diff --git a/spec/tokenizer_spec.rb b/spec/tokenizer_spec.rb index 7a771ba..cb7f6eb 100644 --- a/spec/tokenizer_spec.rb +++ b/spec/tokenizer_spec.rb @@ -132,24 +132,24 @@ describe "Tokenizer" do result[4].should be_token("CONTENT", " baz") end - it "tokenizes a partial as 'OPEN_PARTIAL ID CLOSE'" do + it "tokenizes a partial as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do result = tokenize("{{> foo}}") - result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE)) + result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE)) end - it "tokenizes a partial with context as 'OPEN_PARTIAL ID ID CLOSE'" do + it "tokenizes a partial with context as 'OPEN_PARTIAL PARTIAL_NAME ID CLOSE'" do result = tokenize("{{> foo bar }}") - result.should match_tokens(%w(OPEN_PARTIAL ID ID CLOSE)) + result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME ID CLOSE)) end - it "tokenizes a partial without spaces as 'OPEN_PARTIAL ID CLOSE'" do + it "tokenizes a partial without spaces as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do result = tokenize("{{>foo}}") - result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE)) + result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE)) end - it "tokenizes a partial space at the end as 'OPEN_PARTIAL ID CLOSE'" do + it "tokenizes a partial space at the end as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do result = tokenize("{{>foo }}") - result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE)) + result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE)) end it "tokenizes a comment as 'COMMENT'" do |