Skip to content

Commit 0e49002

Browse files
committed
Add node to test suite
1 parent f6273dd commit 0e49002

File tree

2 files changed

+118
-111
lines changed

2 files changed

+118
-111
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ templated_build "jQuery"
5151
templated_build "qooxdoo"
5252
templated_build "Dojo", :location => "dojox/string"
5353
templated_build "YUI3", :location => "yui3/mustache"
54-
templated_build "requirejs"
54+
templated_build "RequireJS"

test/mustache_spec.rb

Lines changed: 117 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
11
require 'rubygems'
22
require 'json'
33

4-
__DIR__ = File.dirname(__FILE__)
4+
ROOT = File.expand_path("../..", __FILE__)
55

6-
testnames = Dir.glob(__DIR__ + '/../examples/*.js').map do |name|
6+
MUSTACHE = File.read(File.join(ROOT, "mustache.js"))
7+
8+
TESTS = Dir.glob(File.join(ROOT, 'examples', '*.js')).map do |name|
79
File.basename name, '.js'
810
end
911

10-
non_partials = testnames.select{|t| not t.include? "partial"}
11-
partials = testnames.select{|t| t.include? "partial"}
12-
13-
def load_test(dir, name, partial=false)
14-
view = File.read(dir + "/../examples/#{name}.js")
15-
template = File.read(dir + "/../examples/#{name}.html").to_json
16-
expect = File.read(dir + "/../examples/#{name}.txt")
17-
if not partial
18-
[view, template, expect]
19-
else
20-
partial = File.read(dir + "/../examples/#{name}.2.html").to_json
21-
[view, template, partial, expect]
22-
end
23-
end
12+
PARTIALS = TESTS.select {|t| t.include? "partial" }
13+
NON_PARTIALS = TESTS.select {|t| not t.include? "partial" }
2414

25-
JS_PATH = `which js`.strip()
15+
NODE_PATH = `which node`.strip
16+
JS_PATH = `which js`.strip
2617
JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
2718
RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
2819

29-
engines_run = 0
20+
def load_test(name, is_partial=false)
21+
view = File.read(File.join(ROOT, "examples", "#{name}.js"))
22+
template = File.read(File.join(ROOT, "examples", "#{name}.html")).to_json
23+
expect = File.read(File.join(ROOT, "examples", "#{name}.txt"))
3024

31-
describe "mustache" do
25+
test = [view, template, expect]
3226

33-
shared_examples_for "Mustache rendering" do
27+
if is_partial
28+
test << File.read(File.join(ROOT, "examples", "#{name}.2.html")).to_json
29+
end
3430

35-
before(:all) do
36-
engines_run += 1
37-
mustache = File.read(__DIR__ + "/../mustache.js")
38-
stubbed_gettext = <<-JS
39-
// Stubbed gettext translation method for {{_i}}{{/i}} tags in Mustache.
40-
function _(params) {
41-
if (typeof params === "string") {
42-
return params
43-
}
44-
45-
return params.text;
46-
}
47-
JS
31+
test
32+
end
4833

49-
@boilerplate = <<-JS
50-
#{mustache}
51-
#{stubbed_gettext}
52-
JS
34+
def run_js(runner, js)
35+
cmd = case runner
36+
when :spidermonkey
37+
JS_PATH
38+
when :jsc
39+
JSC_PATH
40+
when :rhino
41+
"java #{RHINO_JAR}"
42+
when :node
43+
NODE_PATH
44+
end
45+
46+
runner_file = "runner.js"
47+
File.open(runner_file, 'w') {|file| file.write(js) }
48+
`#{cmd} #{runner_file}`
49+
ensure
50+
FileUtils.rm_r(runner_file)
51+
end
52+
53+
$engines_run = 0
54+
55+
describe "mustache" do
56+
shared_examples_for "mustache rendering" do
57+
before(:all) do
58+
$engines_run += 1
5359
end
5460

5561
it "should return the same when invoked multiple times" do
@@ -58,8 +64,8 @@ def load_test(dir, name, partial=false)
5864
Mustache.to_html("x")
5965
print(Mustache.to_html("x"));
6066
JS
61-
run_js(@run_js, js).should == "x\n"
6267

68+
run_js(@runner, js).should == "x\n"
6369
end
6470

6571
it "should clear the context after each run" do
@@ -72,34 +78,34 @@ def load_test(dir, name, partial=false)
7278
print('ERROR: ' + e.message);
7379
}
7480
JS
75-
run_js(@run_js, js).should == "\n"
81+
82+
run_js(@runner, js).should == "\n"
7683
end
7784

78-
non_partials.each do |testname|
79-
describe testname do
85+
NON_PARTIALS.each do |test|
86+
describe test do
8087
it "should generate the correct html" do
88+
view, template, expect = load_test(test)
8189

82-
view, template, expect = load_test(__DIR__, testname)
83-
84-
runner = <<-JS
90+
js = <<-JS
8591
try {
8692
#{@boilerplate}
8793
#{view}
8894
var template = #{template};
89-
var result = Mustache.to_html(template, #{testname});
95+
var result = Mustache.to_html(template, #{test});
9096
print(result);
9197
} catch(e) {
9298
print('ERROR: ' + e.message);
9399
}
94100
JS
95101

96-
run_js(@run_js, runner).should == expect
102+
run_js(@runner, js).should == expect
97103
end
98-
it "should sendFun the correct html" do
99104

100-
view, template, expect = load_test(__DIR__, testname)
105+
it "should sendFun the correct html" do
106+
view, template, expect = load_test(test)
101107

102-
runner = <<-JS
108+
js = <<-JS
103109
try {
104110
#{@boilerplate}
105111
#{view}
@@ -110,26 +116,24 @@ def load_test(dir, name, partial=false)
110116
}
111117
}
112118
var template = #{template};
113-
Mustache.to_html(template, #{testname}, null, sendFun);
119+
Mustache.to_html(template, #{test}, null, sendFun);
114120
print(chunks.join("\\n"));
115121
} catch(e) {
116122
print('ERROR: ' + e.message);
117123
}
118124
JS
119125

120-
run_js(@run_js, runner).strip.should == expect.strip
126+
run_js(@runner, js).strip.should == expect.strip
121127
end
122128
end
123129
end
124130

125-
partials.each do |testname|
126-
describe testname do
131+
PARTIALS.each do |test|
132+
describe test do
127133
it "should generate the correct html" do
134+
view, template, expect, partial = load_test(test, true)
128135

129-
view, template, partial, expect =
130-
load_test(__DIR__, testname, true)
131-
132-
runner = <<-JS
136+
js = <<-JS
133137
try {
134138
#{@boilerplate}
135139
#{view}
@@ -142,14 +146,13 @@ def load_test(dir, name, partial=false)
142146
}
143147
JS
144148

145-
run_js(@run_js, runner).should == expect
149+
run_js(@runner, js).should == expect
146150
end
147-
it "should sendFun the correct html" do
148151

149-
view, template, partial, expect =
150-
load_test(__DIR__, testname, true)
152+
it "should sendFun the correct html" do
153+
view, template, expect, partial = load_test(test, true)
151154

152-
runner = <<-JS
155+
js = <<-JS
153156
try {
154157
#{@boilerplate}
155158
#{view};
@@ -168,100 +171,104 @@ def load_test(dir, name, partial=false)
168171
}
169172
JS
170173

171-
run_js(@run_js, runner).strip.should == expect.strip
174+
run_js(@runner, js).strip.should == expect.strip
172175
end
173176
end
174177
end
175178
end
176179

177-
context "running in SpiderMonkey (Mozilla, Firefox)" do
178-
if File.exist?(JS_PATH)
179-
before(:each) do
180-
@run_js = :run_js_js
180+
context "running in node" do
181+
if File.exist?(NODE_PATH)
182+
before(:all) do
183+
$stdout.write "Testing in node "
184+
@runner = :node
185+
@boilerplate = MUSTACHE.dup
186+
@boilerplate << <<-JS
187+
function print(message) {
188+
console.log(message);
189+
}
190+
JS
191+
end
192+
193+
after(:all) do
194+
puts " Done!"
181195
end
182196

197+
it_should_behave_like "mustache rendering"
198+
else
199+
puts "Skipping tests in node (node not found)"
200+
end
201+
end
202+
203+
context "running in SpiderMonkey (Mozilla, Firefox)" do
204+
if File.exist?(JS_PATH)
183205
before(:all) do
184-
puts "\nTesting mustache.js in SpiderMonkey:\n"
206+
$stdout.write "Testing in SpiderMonkey "
207+
@runner = :spidermonkey
208+
@boilerplate = MUSTACHE.dup
185209
end
186210

187211
after(:all) do
188-
puts "\nDone\n"
212+
puts " Done!"
189213
end
190214

191-
it_should_behave_like "Mustache rendering"
215+
it_should_behave_like "mustache rendering"
192216
else
193-
puts "\nSkipping tests in SpiderMonkey (js not found)\n"
217+
puts "Skipping tests in SpiderMonkey (js not found)"
194218
end
195219
end
196220

197221
context "running in JavaScriptCore (WebKit, Safari)" do
198222
if File.exist?(JSC_PATH)
199-
before(:each) do
200-
@run_js = :run_js_jsc
201-
end
202-
203223
before(:all) do
204-
puts "\nTesting mustache.js in JavaScriptCore:\n"
224+
$stdout.write "Testing in JavaScriptCore "
225+
@runner = :jsc
226+
@boilerplate = MUSTACHE.dup
205227
end
206228

207229
after(:all) do
208-
puts "\nDone\n"
230+
puts " Done!"
209231
end
210232

211-
it_should_behave_like "Mustache rendering"
233+
it_should_behave_like "mustache rendering"
212234
else
213-
puts "\nSkipping tests in JavaScriptCore (jsc not found)\n"
235+
puts "Skipping tests in JavaScriptCore (jsc not found)"
214236
end
215237
end
216238

217239
context "running in Rhino (Mozilla)" do
218-
if !`java #{RHINO_JAR} 'foo' 2>&1`.match(/ClassNotFoundException/)
219-
before(:each) do
220-
@run_js = :run_js_rhino
221-
end
222-
240+
if `java #{RHINO_JAR} 'foo' 2>&1` !~ /ClassNotFoundException/
223241
before(:all) do
224-
puts "\nTesting mustache.js in Rhino:\n"
242+
$stdout.write "Testing in Rhino "
243+
@runner = :rhino
244+
@boilerplate = MUSTACHE.dup
225245
end
226246

227247
after(:all) do
228-
puts "\nDone\n"
248+
puts " Done!"
229249
end
230250

231-
it_should_behave_like "Mustache rendering"
251+
it_should_behave_like "mustache rendering"
232252
else
233-
puts "\nSkipping tests in Rhino (JAR #{RHINO_JAR} was not found)\n"
253+
puts "Skipping tests in Rhino (JAR #{RHINO_JAR} was not found)"
234254
end
235255
end
236256

237257
context "suite" do
238-
before(:all) do
239-
puts "\nVerifying that we ran at the tests in at least one engine\n"
258+
before(:each) do
259+
$stdout.write "Verifying that we ran at the tests in at least one engine ... "
240260
end
241261

242-
after(:all) do
243-
puts "\nDone\n"
262+
after(:each) do
263+
if @exception.nil?
264+
puts "OK"
265+
else
266+
puts "ERROR!"
267+
end
244268
end
245269

246270
it "should have run at least one time" do
247-
engines_run.should > 0
271+
$engines_run.should > 0
248272
end
249273
end
250-
251-
def run_js(runner, js)
252-
cmd = case runner
253-
when :run_js_js
254-
JS_PATH
255-
when :run_js_jsc
256-
JSC_PATH
257-
when :run_js_rhino
258-
"java #{RHINO_JAR}"
259-
end
260-
261-
runner_file = "runner.js"
262-
File.open(runner_file, 'w') {|f| f << js}
263-
`#{cmd} #{runner_file}`
264-
ensure
265-
FileUtils.rm_r(runner_file)
266-
end
267274
end

0 commit comments

Comments
 (0)