/
mustache_test.rb
404 lines (331 loc) · 9.23 KB
/
mustache_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# -*- coding: utf-8 -*-
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class MustacheTest < Test::Unit::TestCase
def test_passenger
assert_equal <<-end_passenger, Passenger.to_text
<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/example.com
RailsEnv production
</VirtualHost>
end_passenger
end
def test_complex_view
assert_equal <<-end_complex, ComplexView.render
<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>
end_complex
end
def test_nested_objects
assert_equal <<-end_complex, NestedObjects.render
<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>
end_complex
end
def test_single_line_sections
html = %(<p class="flash-notice" {{# no_flash }}style="display: none;"{{/ no_flash }}>)
instance = Mustache.new
instance.template = html
instance[:no_flash] = true
assert_equal <<-rendered.strip, instance.render
<p class="flash-notice" style="display: none;">
rendered
end
def test_two_line_sections
html = %(<p class="flash-notice" {{# no_flash }}style="display: none;"\n{{/ no_flash }}>)
instance = Mustache.new
instance.template = html
instance[:no_flash] = true
assert_equal <<-rendered.strip, instance.render
<p class="flash-notice" style="display: none;"\n>
rendered
end
def test_simple
assert_equal <<-end_simple, Simple.render
Hello Chris
You have just won $10000!
Well, $6000.0, after taxes.
end_simple
end
def test_hash_assignment
view = Simple.new
view[:name] = 'Bob'
view[:value] = '4000'
view[:in_ca] = false
assert_equal <<-end_simple, view.render
Hello Bob
You have just won $4000!
end_simple
end
def test_crazier_hash_assignment
view = Simple.new
view[:name] = 'Crazy'
view[:in_ca] = [
{ :taxed_value => 1 },
{ :taxed_value => 2 },
{ :taxed_value => 3 },
]
assert_equal <<-end_simple, view.render
Hello Crazy
You have just won $10000!
Well, $1, after taxes.
Well, $2, after taxes.
Well, $3, after taxes.
end_simple
end
def test_fileless_templates
view = Simple.new
view.template = 'Hi {{person}}!'
view[:person] = 'mom'
assert_equal 'Hi mom!', view.render
end
def test_delimiters
assert_equal <<-end_template, Delimiters.render
* It worked the first time.
* And it worked the second time.
* As well as the third.
* Then, surprisingly, it worked the final time.
end_template
end
def test_double_section
assert_equal <<-end_section.strip, DoubleSection.render.strip
* first
* second
* third
end_section
end
def test_inverted_section
assert_equal <<-end_section.strip, InvertedSection.render.strip
* first
* second
* third
end_section
end
def test_comments
assert_equal "<h1>A Comedy of Errors</h1>\n", Comments.render
end
def test_multi_linecomments
view = Comments.new
view.template = "<h1>{{title}}{{! just something interesting... \n#or not... }}</h1>\n"
assert_equal "<h1>A Comedy of Errors</h1>\n", view.render
end
def test_escaped
assert_equal '<h1>Bear > Shark</h1>', Escaped.render
end
def test_unescaped
assert_equal '<h1>Bear > Shark</h1>', Unescaped.render
end
def test_unescaped_ampersand
view = Mustache.new
view.template = "<h1>{{& title}}</h1>"
view[:title] = "Bear > Shark"
assert_equal '<h1>Bear > Shark</h1>', view.render
end
def test_classify
assert_equal 'TemplatePartial', Mustache.classify('template_partial')
end
def test_underscore
assert_equal 'template_partial', Mustache.underscore('TemplatePartial')
end
def test_anon_subclass_underscore
klass = Class.new(TemplatePartial)
assert_equal 'template_partial', klass.underscore
end
def test_namespaced_underscore
assert_equal 'stat_stuff', Mustache.underscore('Views::StatStuff')
end
def test_render
assert_equal 'Hello World!', Mustache.render('Hello World!')
end
def test_render_with_params
assert_equal 'Hello World!', Mustache.render('Hello {{planet}}!', :planet => 'World')
end
def test_render_from_file
expected = <<-data
<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/example.com
RailsEnv production
</VirtualHost>
data
template = File.read(File.dirname(__FILE__) + "/fixtures/passenger.conf")
assert_equal expected, Mustache.render(template, :stage => 'production',
:server => 'example.com',
:deploy_to => '/var/www/example.com' )
end
def test_doesnt_execute_what_it_doesnt_need_to
instance = Mustache.new
instance[:show] = false
instance.instance_eval do
def die
raise "bummer"
end
end
instance.template = '{{#show}} <li>{{die}}</li> {{/show}} yay'
assert_equal "yay", instance.render
end
def test_reports_unclosed_sections
instance = Mustache.new
instance[:list] = [ :item => 1234 ]
instance.template = '{{#list}} <li>{{item}}</li> {{/gist}}'
begin
instance.render
rescue => e
end
assert e.message.include?('Unclosed section')
end
def test_unclosed_sections_reports_the_line_number
instance = Mustache.new
instance[:list] = [ :item => 1234 ]
instance.template = "hi\nmom\n{{#list}} <li>{{item}}</li> {{/gist}}"
begin
instance.render
rescue => e
end
assert e.message.include?('Line 3')
end
def test_enumerable_sections_accept_a_hash_as_a_context
instance = Mustache.new
instance[:list] = { :item => 1234 }
instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
assert_equal '<li>1234</li>', instance.render.strip
end
def test_enumerable_sections_accept_a_string_keyed_hash_as_a_context
instance = Mustache.new
instance[:list] = { 'item' => 1234 }
instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
assert_equal '<li>1234</li>', instance.render.strip
end
def test_not_found_in_context_renders_empty_string
instance = Mustache.new
instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
assert_equal '', instance.render.strip
end
def test_not_found_in_nested_context_renders_empty_string
instance = Mustache.new
instance[:list] = { :item => 1234 }
instance.template = '{{#list}} <li>{{prefix}}{{item}}</li> {{/list}}'
assert_equal '<li>1234</li>', instance.render.strip
end
def test_not_found_in_context_raises_when_asked_to
instance = Mustache.new
instance.raise_on_context_miss = true
instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
assert_raises Mustache::ContextMiss do
instance.render.strip
end
end
def test_knows_when_its_been_compiled_when_set_with_string
klass = Class.new(Mustache)
assert ! klass.compiled?
klass.template = 'Hi, {{person}}!'
assert klass.compiled?
end
def test_knows_when_its_been_compiled_when_using_a_file_template
klass = Class.new(Simple)
klass.template_file = File.dirname(__FILE__) + '/fixtures/simple.mustache'
assert ! klass.compiled?
klass.render
assert klass.compiled?
end
def test_an_instance_knows_when_its_class_is_compiled
instance = Simple.new
assert ! Simple.compiled?
assert ! instance.compiled?
Simple.render
assert Simple.compiled?
assert instance.compiled?
end
def test_knows_when_its_been_compiled_at_the_instance_level
klass = Class.new(Mustache)
instance = klass.new
assert ! instance.compiled?
instance.template = 'Hi, {{person}}!'
assert instance.compiled?
end
def test_lots_of_staches
template = "{{{{foo}}}}"
begin
Mustache.render(template, :foo => "defunkt")
rescue => e
end
assert e.message.include?("Illegal content in tag")
end
def test_liberal_tag_names
template = "{{first-name}} {{middle_name!}} {{lastName?}}"
hash = {
'first-name' => 'chris',
'middle_name!' => 'j',
'lastName?' => 'strath'
}
assert_equal "chris j strath", Mustache.render(template, hash)
end
def test_nested_sections_same_names
template = <<template
{{#items}}
start
{{#items}}
{{a}}
{{/items}}
end
{{/items}}
template
data = {
"items" => [
{ "items" => [ {"a" => 1}, {"a" => 2}, {"a" => 3} ] },
{ "items" => [ {"a" => 4}, {"a" => 5}, {"a" => 6} ] },
{ "items" => [ {"a" => 7}, {"a" => 8}, {"a" => 9} ] }
]
}
assert_equal <<expected, Mustache.render(template, data)
start
1
2
3
end
start
4
5
6
end
start
7
8
9
end
expected
end
def test_id_with_nested_context
html = %(<div>{{id}}</div>\n<div>{{# has_a? }}{{id}}{{/ has_a? }}</div>\n<div>{{# has_b? }}{{id}}{{/ has_b? }}</div>)
instance = Mustache.new
instance.template = html
instance[:id] = 3
instance[:has_a?] = true
instance[:has_b?] = true
assert_equal <<-rendered.strip, instance.render
<div>3</div>
<div>3</div>
<div>3</div>
rendered
end
def test_utf8
klass = Class.new(Mustache)
klass.template_name = 'utf8'
klass.template_path = 'test/fixtures'
view = klass.new
view[:test] = "中文"
assert_equal <<-rendered, view.render
<h1>中文 中文</h1>
<h2>中文又来啦</h2>
rendered
end
end