From b15c6fb8421e764cd83443dc3fd660aedf8fc11c Mon Sep 17 00:00:00 2001 From: Ben Cherry Date: Tue, 30 Nov 2010 13:27:13 -0800 Subject: [PATCH] tests for brokenness, now in Rhino too --- examples/two_sections.html | 5 +- examples/two_sections.js | 5 +- examples/two_sections.txt | 4 +- test/mustache_spec.rb | 347 ++++++++++++++++++++----------------- 4 files changed, 192 insertions(+), 169 deletions(-) diff --git a/examples/two_sections.html b/examples/two_sections.html index 356ea66..a4b9f2a 100644 --- a/examples/two_sections.html +++ b/examples/two_sections.html @@ -1,7 +1,4 @@ {{#foo}} - BAR {{/foo}} -BAR -{{^bar}} - BAR +{{#bar}} {{/bar}} diff --git a/examples/two_sections.js b/examples/two_sections.js index bbd9cb9..8546f64 100644 --- a/examples/two_sections.js +++ b/examples/two_sections.js @@ -1,4 +1 @@ -var two_sections = { - foo: true, - bar: false -}; \ No newline at end of file +var two_sections = {}; \ No newline at end of file diff --git a/examples/two_sections.txt b/examples/two_sections.txt index 8a62495..8b13789 100644 --- a/examples/two_sections.txt +++ b/examples/two_sections.txt @@ -1,3 +1 @@ - BAR -BAR - BAR + diff --git a/test/mustache_spec.rb b/test/mustache_spec.rb index b4067a3..32c75e3 100644 --- a/test/mustache_spec.rb +++ b/test/mustache_spec.rb @@ -23,191 +23,222 @@ def load_test(dir, name, partial=false) end describe "mustache" do - before(:all) do - mustache = File.read(__DIR__ + "/../mustache.js") - stubbed_gettext = <<-JS - // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache. - function _(text) { - return text; - } - JS - - @boilerplate = <<-JS - #{mustache} - #{stubbed_gettext} - JS - end - - it "should return the same when invoked multiple times" do - js = <<-JS - #{@boilerplate} - Mustache.to_html("x") - print(Mustache.to_html("x")); - JS - run_js(js).should == "x\n" - - end - - it "should clear the context after each run" do - js = <<-JS - #{@boilerplate} - Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]}) - try { - print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]})); - } catch(e) { - print('ERROR: ' + e.message); - } - JS - run_js(js).should == "\n" - end - it "should not double-render" do - js = <<-JS - #{@boilerplate} - var template = "{{#foo}}{{bar}}{{/foo}}"; - var ctx = { - foo: true, - bar: "{{win}}", - win: "FAIL" - }; + shared_examples_for "Mustache rendering" do + + before(:all) do + mustache = File.read(__DIR__ + "/../mustache.js") + stubbed_gettext = <<-JS + // Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache. + function _(text) { + return text; + } + JS + + @boilerplate = <<-JS + #{mustache} + #{stubbed_gettext} + JS + end - print(Mustache.to_html(template, ctx)) - JS + it "should return the same when invoked multiple times" do + js = <<-JS + #{@boilerplate} + Mustache.to_html("x") + print(Mustache.to_html("x")); + JS + run_js(@run_js, js).should == "x\n" - run_js(js).strip.should == "{{win}}" - end + end - it "should work with i18n" do - js = <<-JS - #{@boilerplate} + it "should clear the context after each run" do + js = <<-JS + #{@boilerplate} + Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{x: 1}]}) + try { + print(Mustache.to_html("{{#list}}{{x}}{{/list}}", {list: [{}]})); + } catch(e) { + print('ERROR: ' + e.message); + } + JS + run_js(@run_js, js).should == "\n" + end - var template = "{{_i}}foo {{bar}}{{/i}}"; - var ctx = { - bar: "BAR" - }; + it "should not double-render" do + js = <<-JS + #{@boilerplate} + var template = "{{#foo}}{{bar}}{{/foo}}"; + var ctx = { + foo: true, + bar: "{{win}}", + win: "FAIL" + }; - print(Mustache.to_html(template, ctx)); - JS + print(Mustache.to_html(template, ctx)) + JS - run_js(js).strip.should == "foo BAR" - end + run_js(@run_js, js).strip.should == "{{win}}" + end - it "should work with empty sections" do - js = <<-JS - try{ - #{@boilerplate} + it "should work with i18n" do + js = <<-JS + #{@boilerplate} - print(Mustache.to_html("{{#foo}}{{/foo}}foo{{#bar}}{{/bar}}", {})); - } catch(e) { - print(e); - } - JS + var template = "{{_i}}foo {{bar}}{{/i}}"; + var ctx = { + bar: "BAR" + }; - run_js(js).strip.should == "foo" - end + print(Mustache.to_html(template, ctx)); + JS + run_js(@run_js, js).strip.should == "foo BAR" + end - non_partials.each do |testname| - describe testname do - it "should generate the correct html" do + it "should work with empty sections" do + js = <<-JS + try{ + #{@boilerplate} - view, template, expect = load_test(__DIR__, testname) + print(Mustache.to_html("{{#foo}}{{/foo}}foo{{#bar}}{{/bar}}", {})); + } catch(e) { + print(e); + } + JS - runner = <<-JS - try { - #{@boilerplate} - #{view} - var template = #{template}; - var result = Mustache.to_html(template, #{testname}); - print(result); - } catch(e) { - print('ERROR: ' + e.message); - } - JS + run_js(@run_js, js).strip.should == "foo" + end - run_js(runner).should == expect - end - it "should sendFun the correct html" do - - view, template, expect = load_test(__DIR__, testname) - - runner = <<-JS - try { - #{@boilerplate} - #{view} - var chunks = []; - var sendFun = function(chunk) { - if (chunk != "") { - chunks.push(chunk); + non_partials.each do |testname| + describe testname do + it "should generate the correct html" do + + view, template, expect = load_test(__DIR__, testname) + + runner = <<-JS + try { + #{@boilerplate} + #{view} + var template = #{template}; + var result = Mustache.to_html(template, #{testname}); + print(result); + } catch(e) { + print('ERROR: ' + e.message); + } + JS + + run_js(@run_js, runner).should == expect + end + it "should sendFun the correct html" do + + view, template, expect = load_test(__DIR__, testname) + + runner = <<-JS + try { + #{@boilerplate} + #{view} + var chunks = []; + var sendFun = function(chunk) { + if (chunk != "") { + chunks.push(chunk); + } } + var template = #{template}; + Mustache.to_html(template, #{testname}, null, sendFun); + print(chunks.join("\\n")); + } catch(e) { + print('ERROR: ' + e.message); } - var template = #{template}; - Mustache.to_html(template, #{testname}, null, sendFun); - print(chunks.join("\\n")); - } catch(e) { - print('ERROR: ' + e.message); - } - JS - - run_js(runner).strip.should == expect.strip + JS + + run_js(@run_js, runner).strip.should == expect.strip + end end end - end - partials.each do |testname| - describe testname do - it "should generate the correct html" do - - view, template, partial, expect = - load_test(__DIR__, testname, true) - - runner = <<-JS - try { - #{@boilerplate} - #{view} - var template = #{template}; - var partials = {"partial": #{partial}}; - var result = Mustache.to_html(template, partial_context, partials); - print(result); - } catch(e) { - print('ERROR: ' + e.message); - } - JS - - run_js(runner).should == expect - end - it "should sendFun the correct html" do - - view, template, partial, expect = - load_test(__DIR__, testname, true) - - runner = <<-JS - try { - #{@boilerplate} - #{view}; - var template = #{template}; - var partials = {"partial": #{partial}}; - var chunks = []; - var sendFun = function(chunk) { - if (chunk != "") { - chunks.push(chunk); + partials.each do |testname| + describe testname do + it "should generate the correct html" do + + view, template, partial, expect = + load_test(__DIR__, testname, true) + + runner = <<-JS + try { + #{@boilerplate} + #{view} + var template = #{template}; + var partials = {"partial": #{partial}}; + var result = Mustache.to_html(template, partial_context, partials); + print(result); + } catch(e) { + print('ERROR: ' + e.message); + } + JS + + run_js(@run_js, runner).should == expect + end + it "should sendFun the correct html" do + + view, template, partial, expect = + load_test(__DIR__, testname, true) + + runner = <<-JS + try { + #{@boilerplate} + #{view}; + var template = #{template}; + var partials = {"partial": #{partial}}; + var chunks = []; + var sendFun = function(chunk) { + if (chunk != "") { + chunks.push(chunk); + } } + Mustache.to_html(template, partial_context, partials, sendFun); + print(chunks.join("\\n")); + } catch(e) { + print('ERROR: ' + e.message); } - Mustache.to_html(template, partial_context, partials, sendFun); - print(chunks.join("\\n")); - } catch(e) { - print('ERROR: ' + e.message); - } - JS - - run_js(runner).strip.should == expect.strip + JS + + run_js(@run_js, runner).strip.should == expect.strip + end end end end - def run_js(js) + context "running in JavaScriptCore (WebKit, Safari)" do + before(:each) do + @run_js = :run_js_jsc + end + it_should_behave_like "Mustache rendering" + end + + context "running in Rhino (Mozilla)" do + before(:each) do + @run_js = :run_js_rhino + end + + it_should_behave_like "Mustache rendering" + end + + def run_js(runner, js) + send(runner, js) + # run_js_rhino(js) + # js_jsc = run_js_jsc(js) + # js_rhino = run_js_rhino(js) + # return js_jsc unless js_jsc != js_rhino + end + + def run_js_jsc(js) + File.open("runner.js", 'w') {|f| f << js} + `jsc runner.js` + end + + def run_js_rhino(js) File.open("runner.js", 'w') {|f| f << js} - `js runner.js` + `java org.mozilla.javascript.tools.shell.Main runner.js` end end