1
1
require 'rubygems'
2
2
require 'json'
3
3
4
- __DIR__ = File . dirname ( __FILE__ )
4
+ ROOT = File . expand_path ( "../.." , __FILE__ )
5
5
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 |
7
9
File . basename name , '.js'
8
10
end
9
11
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" }
24
14
25
- JS_PATH = `which js` . strip ( )
15
+ NODE_PATH = `which node` . strip
16
+ JS_PATH = `which js` . strip
26
17
JSC_PATH = "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
27
18
RHINO_JAR = "org.mozilla.javascript.tools.shell.Main"
28
19
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" ) )
30
24
31
- describe "mustache" do
25
+ test = [ view , template , expect ]
32
26
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
34
30
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
48
33
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
53
59
end
54
60
55
61
it "should return the same when invoked multiple times" do
@@ -58,8 +64,8 @@ def load_test(dir, name, partial=false)
58
64
Mustache.to_html("x")
59
65
print(Mustache.to_html("x"));
60
66
JS
61
- run_js ( @run_js , js ) . should == "x\n "
62
67
68
+ run_js ( @runner , js ) . should == "x\n "
63
69
end
64
70
65
71
it "should clear the context after each run" do
@@ -72,34 +78,34 @@ def load_test(dir, name, partial=false)
72
78
print('ERROR: ' + e.message);
73
79
}
74
80
JS
75
- run_js ( @run_js , js ) . should == "\n "
81
+
82
+ run_js ( @runner , js ) . should == "\n "
76
83
end
77
84
78
- non_partials . each do |testname |
79
- describe testname do
85
+ NON_PARTIALS . each do |test |
86
+ describe test do
80
87
it "should generate the correct html" do
88
+ view , template , expect = load_test ( test )
81
89
82
- view , template , expect = load_test ( __DIR__ , testname )
83
-
84
- runner = <<-JS
90
+ js = <<-JS
85
91
try {
86
92
#{ @boilerplate }
87
93
#{ view }
88
94
var template = #{ template } ;
89
- var result = Mustache.to_html(template, #{ testname } );
95
+ var result = Mustache.to_html(template, #{ test } );
90
96
print(result);
91
97
} catch(e) {
92
98
print('ERROR: ' + e.message);
93
99
}
94
100
JS
95
101
96
- run_js ( @run_js , runner ) . should == expect
102
+ run_js ( @runner , js ) . should == expect
97
103
end
98
- it "should sendFun the correct html" do
99
104
100
- view , template , expect = load_test ( __DIR__ , testname )
105
+ it "should sendFun the correct html" do
106
+ view , template , expect = load_test ( test )
101
107
102
- runner = <<-JS
108
+ js = <<-JS
103
109
try {
104
110
#{ @boilerplate }
105
111
#{ view }
@@ -110,26 +116,24 @@ def load_test(dir, name, partial=false)
110
116
}
111
117
}
112
118
var template = #{ template } ;
113
- Mustache.to_html(template, #{ testname } , null, sendFun);
119
+ Mustache.to_html(template, #{ test } , null, sendFun);
114
120
print(chunks.join("\\ n"));
115
121
} catch(e) {
116
122
print('ERROR: ' + e.message);
117
123
}
118
124
JS
119
125
120
- run_js ( @run_js , runner ) . strip . should == expect . strip
126
+ run_js ( @runner , js ) . strip . should == expect . strip
121
127
end
122
128
end
123
129
end
124
130
125
- partials . each do |testname |
126
- describe testname do
131
+ PARTIALS . each do |test |
132
+ describe test do
127
133
it "should generate the correct html" do
134
+ view , template , expect , partial = load_test ( test , true )
128
135
129
- view , template , partial , expect =
130
- load_test ( __DIR__ , testname , true )
131
-
132
- runner = <<-JS
136
+ js = <<-JS
133
137
try {
134
138
#{ @boilerplate }
135
139
#{ view }
@@ -142,14 +146,13 @@ def load_test(dir, name, partial=false)
142
146
}
143
147
JS
144
148
145
- run_js ( @run_js , runner ) . should == expect
149
+ run_js ( @runner , js ) . should == expect
146
150
end
147
- it "should sendFun the correct html" do
148
151
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 )
151
154
152
- runner = <<-JS
155
+ js = <<-JS
153
156
try {
154
157
#{ @boilerplate }
155
158
#{ view } ;
@@ -168,100 +171,104 @@ def load_test(dir, name, partial=false)
168
171
}
169
172
JS
170
173
171
- run_js ( @run_js , runner ) . strip . should == expect . strip
174
+ run_js ( @runner , js ) . strip . should == expect . strip
172
175
end
173
176
end
174
177
end
175
178
end
176
179
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!"
181
195
end
182
196
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 )
183
205
before ( :all ) do
184
- puts "\n Testing mustache.js in SpiderMonkey:\n "
206
+ $stdout. write "Testing in SpiderMonkey "
207
+ @runner = :spidermonkey
208
+ @boilerplate = MUSTACHE . dup
185
209
end
186
210
187
211
after ( :all ) do
188
- puts "\n Done \n "
212
+ puts " Done! "
189
213
end
190
214
191
- it_should_behave_like "Mustache rendering"
215
+ it_should_behave_like "mustache rendering"
192
216
else
193
- puts "\n Skipping tests in SpiderMonkey (js not found)\n "
217
+ puts "Skipping tests in SpiderMonkey (js not found)"
194
218
end
195
219
end
196
220
197
221
context "running in JavaScriptCore (WebKit, Safari)" do
198
222
if File . exist? ( JSC_PATH )
199
- before ( :each ) do
200
- @run_js = :run_js_jsc
201
- end
202
-
203
223
before ( :all ) do
204
- puts "\n Testing mustache.js in JavaScriptCore:\n "
224
+ $stdout. write "Testing in JavaScriptCore "
225
+ @runner = :jsc
226
+ @boilerplate = MUSTACHE . dup
205
227
end
206
228
207
229
after ( :all ) do
208
- puts "\n Done \n "
230
+ puts " Done! "
209
231
end
210
232
211
- it_should_behave_like "Mustache rendering"
233
+ it_should_behave_like "mustache rendering"
212
234
else
213
- puts "\n Skipping tests in JavaScriptCore (jsc not found)\n "
235
+ puts "Skipping tests in JavaScriptCore (jsc not found)"
214
236
end
215
237
end
216
238
217
239
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/
223
241
before ( :all ) do
224
- puts "\n Testing mustache.js in Rhino:\n "
242
+ $stdout. write "Testing in Rhino "
243
+ @runner = :rhino
244
+ @boilerplate = MUSTACHE . dup
225
245
end
226
246
227
247
after ( :all ) do
228
- puts "\n Done \n "
248
+ puts " Done! "
229
249
end
230
250
231
- it_should_behave_like "Mustache rendering"
251
+ it_should_behave_like "mustache rendering"
232
252
else
233
- puts "\n Skipping tests in Rhino (JAR #{ RHINO_JAR } was not found)\n "
253
+ puts "Skipping tests in Rhino (JAR #{ RHINO_JAR } was not found)"
234
254
end
235
255
end
236
256
237
257
context "suite" do
238
- before ( :all ) do
239
- puts " \n Verifying 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 ... "
240
260
end
241
261
242
- after ( :all ) do
243
- puts "\n Done\n "
262
+ after ( :each ) do
263
+ if @exception . nil?
264
+ puts "OK"
265
+ else
266
+ puts "ERROR!"
267
+ end
244
268
end
245
269
246
270
it "should have run at least one time" do
247
- engines_run . should > 0
271
+ $ engines_run. should > 0
248
272
end
249
273
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
267
274
end
0 commit comments