forked from mustache/mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mustache_test.rb
677 lines (562 loc) · 15.6 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# -*- coding: utf-8 -*-
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'helper'
class MustacheTest < Test::Unit::TestCase
def test_instance_render
klass = Class.new(Mustache)
klass.template = "Hi {{thing}}!"
assert_equal "Hi world!", klass.render(:thing => :world)
assert_equal "Nice.", klass.render("{{compliment}}.", :compliment => "Nice")
assert_equal <<-end_simple, Simple.new.render(:name => "yo", :in_ca => false)
Hello yo
You have just won $10000!
end_simple
end
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 %Q'<p class="flash-notice" style="display: none;">', instance.render
end
def test_strings_as_sections_do_not_enumerate
instance = Mustache.new
instance[:contact] = "Call 1-888-FLOWERS\nAsk for Johnson."
instance.template = "{{#contact}}<div id='contact'>{{contact}}</div>{{/contact}}"
assert_equal "<div id='contact'>Call 1-888-FLOWERS\nAsk for Johnson.</div>",
instance.render
end
def test_sassy_single_line_sections
instance = Mustache.new
instance[:full_time] = true
instance.template = "\n {{#full_time}}full time{{/full_time}}\n"
assert_equal "\n full time\n", instance.render
end
def test_sassier_single_line_sections
instance = Mustache.new
instance.template = "\t{{#list}}\r\n\t{{/list}}"
assert_equal "", instance.render
end
def test_padding_before_section
instance = Mustache.new
instance.template = "\t{{#list}}a{{/list}}"
assert_equal "\taa", instance.render(:list => [1, 2])
end
def test_padding_before_section_on_eos
instance = Mustache.new
instance.template = "{{#list}}\n\t{{/list}}"
assert_equal "", instance.render(:list => [1, 2])
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 %Q'<p class="flash-notice" style="display: none;"\n>', instance.render
end
def test_multi_line_sections_preserve_trailing_newline
view = Mustache.new
view.template = <<template
{{#something}}
yay
{{/something}}
Howday.
template
view[:something] = true
assert_equal <<-rendered, view.render
yay
Howday.
rendered
end
def test_single_line_inverted_sections
html = %(<p class="flash-notice" {{^ flash }}style="display: none;"{{/ flash }}>)
instance = Mustache.new
instance.template = html
assert_equal %Q'<p class="flash-notice" style="display: none;">', instance.render
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, DoubleSection.render
* first
* second
* third
end_section
end
def test_inverted_section
assert_equal <<-end_section, InvertedSection.render
* 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')
assert_equal 'Admin::TemplatePartial', Mustache.classify('admin/template_partial')
end
def test_underscore
assert_equal 'template_partial', Mustache.underscore('TemplatePartial')
assert_equal 'admin/template_partial', Mustache.underscore('Admin::TemplatePartial')
assert_equal 'views/in/sub/directories', Mustache.underscore('Views::In::Sub::Directories')
end
def test_anon_subclass_underscore
klass = Class.new(TemplatePartial)
assert_equal 'template_partial', klass.underscore
end
def test_namespaced_underscore
Object.const_set(:Views, Class.new)
klass = Class.new(Mustache)
klass.view_namespace = Views
assert_equal 'stat_stuff', klass.underscore('Views::StatStuff')
assert_equal 'views/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_render_from_symbol
expected = <<-data
<VirtualHost *>
ServerName example.com
DocumentRoot /var/www/example.com
RailsEnv production
</VirtualHost>
data
old_path, Mustache.template_path = Mustache.template_path, File.dirname(__FILE__) + "/fixtures"
old_extension, Mustache.template_extension = Mustache.template_extension, "conf"
assert_equal expected, Mustache.render(:passenger, :stage => 'production',
:server => 'example.com',
:deploy_to => '/var/www/example.com' )
Mustache.template_path, Mustache.template_extension = old_path, old_extension
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
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
end
def test_not_found_in_context_renders_empty_string
instance = Mustache.new
instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
assert_equal '', instance.render
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
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
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_sections_returning_lambdas_get_called_with_text
view = Lambda.new
view[:name] = 'Chris'
assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
assert_equal 1, view.calls
assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
assert_equal 1, view.calls
assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
assert_equal 1, view.calls
end
def test_sections_which_refer_to_unary_method_call_them_as_proc
kls = Class.new(Mustache) do
def unary_method(arg)
"(#{arg})"
end
end
str = kls.render("{{#unary_method}}test{{/unary_method}}")
assert_equal "(test)", str
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>\n)
instance = Mustache.new
instance.template = html
instance[:id] = 3
instance[:has_a?] = true
instance[:has_b?] = true
assert_equal <<-rendered, 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
def test_indentation
view = Mustache.new
view[:name] = 'indent'
view[:text] = 'puts :indented!'
view.template = <<template
def {{name}}
{{text}}
end
template
assert_equal <<template, view.render
def indent
puts :indented!
end
template
end
def test_struct
person = Struct.new(:name, :age)
view = Mustache.new
view[:person] = person.new('Marvin', 25)
view.template = '{{#person}}{{name}} is {{age}}{{/person}}'
assert_equal 'Marvin is 25', view.render
end
def test_method_missing
assert_equal('[ 0 1 2 3 4 5 6 7 8 9 10 ]', MethodMissing.render)
end
def test_custom_escaping
view = Class.new(Mustache) do
def escapeHTML(str)
"pong"
end
end
assert_equal 'pong', view.render("{{thing}}", :thing => "nothing")
assert_equal 'nothing', Mustache.render("{{thing}}", :thing => "nothing")
end
def test_implicit_iterator
view = Mustache.new
view.template = "{{#people}}* {{.}}\n{{/people}}"
view[:people] = %w( Chris Mark Scott )
assert_equal <<text, view.render
* Chris
* Mark
* Scott
text
end
def test_unescaped_implicit_iterator
view = Mustache.new
view.template = "{{#people}}* {{{.}}}\n{{/people}}"
view[:people] = %w( Chris Mark Scott )
assert_equal <<text, view.render
* Chris
* Mark
* Scott
text
end
def test_dot_notation
assert_equal <<-text, DotNotation.render
* Chris Firescythe
* 24
* Cincinnati, OH
* Cincinnati, OH
* Cincinnati, OH
* Cincinnati, OH
* Normal
* Chris Firescythe
* Cincinnati, OH
text
end
def test_inherited_attributes
Object.const_set :TestNamespace, Module.new
base = Class.new(Mustache)
tmpl = Class.new(base)
{:template_path => File.expand_path('./foo'),
:template_extension => 'stache',
:view_namespace => TestNamespace,
:view_path => './foo'
}.each do |attr, value|
base.send("#{attr}=", value)
assert_equal value, tmpl.send(attr)
end
end
def test_array_of_arrays
template = <<template
{{#items}}
start
{{#map}}
{{a}}
{{/map}}
end
{{/items}}
template
data = {
"items" => [
[ {"a" => 1}, {"a" => 2}, {"a" => 3} ],
[ {"a" => 4}, {"a" => 5}, {"a" => 6} ],
[ {"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_indentation_again
template = <<template
SELECT
{{#cols}}
{{name}},
{{/cols}}
FROM
DUMMY1
template
view = Mustache.new
view[:cols] = [{:name => 'Name'}, {:name => 'Age'}, {:name => 'Weight'}]
view.template = template
assert_equal <<template, view.render
SELECT
Name,
Age,
Weight,
FROM
DUMMY1
template
end
end