Procházet zdrojové kódy

tests for brokenness, now in Rhino too

tags/0.4.0
Ben Cherry před 15 roky
rodič
revize
b15c6fb842
4 změnil soubory, kde provedl 192 přidání a 169 odebrání
  1. +1
    -4
      examples/two_sections.html
  2. +1
    -4
      examples/two_sections.js
  3. +1
    -3
      examples/two_sections.txt
  4. +189
    -158
      test/mustache_spec.rb

+ 1
- 4
examples/two_sections.html Zobrazit soubor

@@ -1,7 +1,4 @@
{{#foo}}
BAR
{{/foo}}
BAR
{{^bar}}
BAR
{{#bar}}
{{/bar}}

+ 1
- 4
examples/two_sections.js Zobrazit soubor

@@ -1,4 +1 @@
var two_sections = {
foo: true,
bar: false
};
var two_sections = {};

+ 1
- 3
examples/two_sections.txt Zobrazit soubor

@@ -1,3 +1 @@
BAR
BAR
BAR


+ 189
- 158
test/mustache_spec.rb Zobrazit soubor

@@ -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


Načítá se…
Zrušit
Uložit