Debian Codegraph @

test/mustache_test.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require_relative 'helper'
3 require 'json'
4
5 class MustacheTest < Minitest::Test
6 def test_instance_render
7 klass = Class.new(Mustache)
8 klass.template = "Hi {{thing}}!"
9 assert_equal "Hi world!", klass.render(:thing => :world)
10 assert_equal "Nice.", klass.render("{{compliment}}.", :compliment => "Nice")
11 assert_equal <<-end_simple, Simple.new.render(:name => "yo", :in_ca => false)
12 Hello yo
13 You have just won $10000!
14 end_simple
15 end
16
17 def test_passenger
18 assert_equal <<-end_passenger, Passenger.render
19 <VirtualHost *>
20 ServerName example.com
21 DocumentRoot /var/www/example.com
22 RailsEnv production
23 </VirtualHost>
24 end_passenger
25 end
26
27 def test_complex_view
28 assert_equal <<-end_complex, ComplexView.render
29 <h1>Colors</h1>
30 <ul>
31 <li><strong>red</strong></li>
32 <li><a href="#Green">green</a></li>
33 <li><a href="#Blue">blue</a></li>
34 </ul>
35
36 end_complex
37 end
38
39 def test_nested_objects
40 assert_equal <<-end_complex, NestedObjects.render
41 <h1>Colors</h1>
42 <ul>
43 <li><strong>red</strong></li>
44 <li><a href="#Green">green</a></li>
45 <li><a href="#Blue">blue</a></li>
46 </ul>
47
48 end_complex
49 end
50
51 def test_single_line_sections
52 html = %(<p class="flash-notice" {{# no_flash }}style="display: none;"{{/ no_flash }}>)
53
54 instance = Mustache.new
55 instance.template = html
56 instance[:no_flash] = true
57 assert_equal %Q'<p class="flash-notice" style="display: none;">', instance.render
58 end
59
60 def test_strings_as_sections_do_not_enumerate
61 instance = Mustache.new
62 instance[:contact] = "Call 1-888-FLOWERS\nAsk for Johnson."
63 instance.template = "{{#contact}}<div id='contact'>{{contact}}</div>{{/contact}}"
64
65 assert_equal "<div id='contact'>Call 1-888-FLOWERS\nAsk for Johnson.</div>",
66 instance.render
67 end
68
69 def test_sassy_single_line_sections
70 instance = Mustache.new
71 instance[:full_time] = true
72 instance.template = "\n {{#full_time}}full time{{/full_time}}\n"
73
74 assert_equal "\n full time\n", instance.render
75 end
76
77 def test_sassier_single_line_sections
78 instance = Mustache.new
79 instance.template = "\t{{#list}}\r\n\t{{/list}}"
80
81 assert_equal "", instance.render
82 end
83
84 def test_padding_before_section
85 instance = Mustache.new
86 instance.template = "\t{{#list}}a{{/list}}"
87
88 assert_equal "\taa", instance.render(:list => [1, 2])
89 end
90
91 def test_padding_before_section_on_eos
92 instance = Mustache.new
93 instance.template = "{{#list}}\n\t{{/list}}"
94
95 assert_equal "", instance.render(:list => [1, 2])
96 end
97
98 def test_two_line_sections
99 html = %(<p class="flash-notice" {{# no_flash }}style="display: none;"\n{{/ no_flash }}>)
100
101 instance = Mustache.new
102 instance.template = html
103 instance[:no_flash] = true
104 assert_equal %Q'<p class="flash-notice" style="display: none;"\n>', instance.render
105 end
106
107 def test_multi_line_sections_preserve_trailing_newline
108 view = Mustache.new
109 view.template = <<template
110 {{#something}}
111 yay
112 {{/something}}
113 Howday.
114 template
115
116 view[:something] = true
117 assert_equal <<-rendered, view.render
118 yay
119 Howday.
120 rendered
121 end
122
123 def test_single_line_inverted_sections
124 html = %(<p class="flash-notice" {{^ flash }}style="display: none;"{{/ flash }}>)
125
126 instance = Mustache.new
127 instance.template = html
128 assert_equal %Q'<p class="flash-notice" style="display: none;">', instance.render
129 end
130
131 def test_simple
132 assert_equal <<-end_simple, Simple.render
133 Hello Chris
134 You have just won $10000!
135 Well, $6000.0, after taxes.
136 end_simple
137 end
138
139 def test_hash_assignment
140 view = Simple.new
141 view[:name] = 'Bob'
142 view[:value] = '4000'
143 view[:in_ca] = false
144
145 assert_equal <<-end_simple, view.render
146 Hello Bob
147 You have just won $4000!
148 end_simple
149 end
150
151 def test_crazier_hash_assignment
152 view = Simple.new
153 view[:name] = 'Crazy'
154 view[:in_ca] = [
155 { :taxed_value => 1 },
156 { :taxed_value => 2 },
157 { :taxed_value => 3 },
158 ]
159
160 assert_equal <<-end_simple, view.render
161 Hello Crazy
162 You have just won $10000!
163 Well, $1, after taxes.
164 Well, $2, after taxes.
165 Well, $3, after taxes.
166 end_simple
167 end
168
169 def test_fileless_templates
170 view = Simple.new
171 view.template = 'Hi {{person}}!'
172 view[:person] = 'mom'
173
174 assert_equal 'Hi mom!', view.render
175 end
176
177 def test_delimiters
178 assert_equal <<-end_template, Delimiters.render
179 * It worked the first time.
180 * And it worked the second time.
181 * As well as the third.
182 * Then, surprisingly, it worked the final time.
183 end_template
184 end
185
186 def test_double_section
187 assert_equal <<-end_section, DoubleSection.render
188 * first
189 * second
190 * third
191 end_section
192 end
193
194 def test_inverted_section
195 assert_equal <<-end_section, InvertedSection.render
196 * first
197 * second
198 * third
199 end_section
200 end
201
202 def test_comments
203 assert_equal "<h1>A Comedy of Errors</h1>\n", Comments.render
204 end
205
206 def test_multi_linecomments
207 view = Comments.new
208 view.template = "<h1>{{title}}{{! just something interesting... \n#or not... }}</h1>\n"
209 assert_equal "<h1>A Comedy of Errors</h1>\n", view.render
210 end
211
212 def test_escaped
213 assert_equal '<h1>Bear &gt; Shark</h1>', Escaped.render
214 end
215
216 def test_unescaped
217 assert_equal '<h1>Bear > Shark</h1>', Unescaped.render
218 end
219
220 def test_unescaped_ampersand
221 view = Mustache.new
222 view.template = "<h1>{{& title}}</h1>"
223 view[:title] = "Bear > Shark"
224 assert_equal '<h1>Bear > Shark</h1>', view.render
225 end
226
227 def test_classify
228 assert_equal 'TemplatePartial', Mustache.classify('template_partial')
229 assert_equal 'Admin::TemplatePartial', Mustache.classify('admin/template_partial')
230 end
231
232 def test_underscore
233 assert_equal 'template_partial', Mustache.underscore('TemplatePartial')
234 assert_equal 'admin/template_partial', Mustache.underscore('Admin::TemplatePartial')
235 assert_equal 'views/in/sub/directories', Mustache.underscore('Views::In::Sub::Directories')
236 end
237
238 def test_anon_subclass_underscore
239 klass = Class.new(TemplatePartial)
240 assert_equal 'template_partial', klass.underscore
241 end
242
243 def test_namespaced_underscore
244 Object.const_set(:Views, Class.new)
245 klass = Class.new(Mustache)
246 klass.view_namespace = Views
247 assert_equal 'stat_stuff', klass.underscore('Views::StatStuff')
248
249 assert_equal 'views/stat_stuff', Mustache.underscore('Views::StatStuff')
250 end
251
252 def test_render
253 assert_equal 'Hello World!', Mustache.render('Hello World!')
254 end
255
256 def test_render_with_params
257 assert_equal 'Hello World!', Mustache.render('Hello {{planet}}!', :planet => 'World')
258 end
259
260 def test_render_from_file
261 expected = <<-data
262 <VirtualHost *>
263 ServerName example.com
264 DocumentRoot /var/www/example.com
265 RailsEnv production
266 </VirtualHost>
267 data
268 template = File.read(File.dirname(__FILE__) + "/fixtures/passenger.conf")
269 assert_equal expected, Mustache.render(template, :stage => 'production',
270 :server => 'example.com',
271 :deploy_to => '/var/www/example.com' )
272 end
273
274 def test_render_from_symbol
275 expected = <<-data
276 <VirtualHost *>
277 ServerName example.com
278 DocumentRoot /var/www/example.com
279 RailsEnv production
280 </VirtualHost>
281 data
282 old_path = Mustache.template_path
283 old_extension = Mustache.template_extension
284
285 begin
286 Mustache.template_path = File.dirname(__FILE__) + "/fixtures"
287 Mustache.template_extension = "conf"
288
289 assert_equal expected, Mustache.render(:passenger, :stage => 'production',
290 :server => 'example.com',
291 :deploy_to => '/var/www/example.com')
292 ensure
293 Mustache.template_path, Mustache.template_extension = old_path, old_extension
294 end
295 end
296
297 def test_doesnt_execute_what_it_doesnt_need_to
298 instance = Mustache.new
299 instance[:show] = false
300 instance.instance_eval do
301 def die
302 raise "bummer"
303 end
304 end
305 instance.template = '{{#show}} <li>{{die}}</li> {{/show}} yay'
306
307 assert_equal " yay", instance.render
308 end
309
310 def test_reports_unclosed_sections
311 instance = Mustache.new
312 instance[:list] = [ :item => 1234 ]
313 instance.template = '{{#list}} <li>{{item}}</li> {{/gist}}'
314
315 begin
316 instance.render
317 rescue => e
318 end
319
320 assert e.message.include?('Unclosed section')
321 end
322
323 def test_unclosed_sections_reports_the_line_number
324 instance = Mustache.new
325 instance[:list] = [ :item => 1234 ]
326 instance.template = "hi\nmom\n{{#list}} <li>{{item}}</li> {{/gist}}"
327
328 begin
329 instance.render
330 rescue => e
331 end
332
333 assert e.message.include?('Line 3')
334 end
335
336 def test_enumerable_sections_accept_a_hash_as_a_context
337 instance = Mustache.new
338 instance[:list] = { :item => 1234 }
339 instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
340
341 assert_equal ' <li>1234</li> ', instance.render
342 end
343
344 def test_enumerable_sections_accept_a_string_keyed_hash_as_a_context
345 instance = Mustache.new
346 instance[:list] = { 'item' => 1234 }
347 instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
348
349 assert_equal ' <li>1234</li> ', instance.render
350 end
351
352 def test_enumerable_sections_enumerate_mustache_enumerables
353 person = Struct.new(:name, :age)
354 people_array = []
355 people_array << person.new('Juliet', 13)
356 people_array << person.new('Romeo', 16)
357 people = Class.new do
358 include Enumerable
359 include Mustache::Enumerable
360
361 def initialize array
362 @people = array
363 end
364
365 def each *args, &block
366 @people.each(*args, &block)
367 end
368 end
369
370 view = Mustache.new
371 view[:people] = people.new(people_array)
372 view.template = <<-TEMPLATE
373 {{#people}}
374 {{name}} is {{age}}
375 {{/people}}
376 TEMPLATE
377 assert_equal <<-EXPECTED, view.render
378 Juliet is 13
379 Romeo is 16
380 EXPECTED
381 end
382
383 def test_enumerable_sections_do_not_enumerate_untagged_enumerables
384 people = Struct.new(:first, :second, :third)
385 person = Struct.new(:name, :age)
386
387 view = Mustache.new
388 view[:people] = people.new(person.new("Mercutio", 17), person.new("Tybalt", 20), person.new("Benvolio", 15))
389 view.template = <<-TEMPLATE
390 {{#people}}
391 {{#first}}
392 {{name}} is {{age}}
393 {{/first}}
394 {{#second}}
395 {{name}} is {{age}}
396 {{/second}}
397 {{#third}}
398 {{name}} is {{age}}
399 {{/third}}
400 {{/people}}
401 TEMPLATE
402 assert_equal <<-EXPECTED, view.render
403 Mercutio is 17
404 Tybalt is 20
405 Benvolio is 15
406 EXPECTED
407 end
408
409 def test_not_found_in_context_renders_empty_string
410 instance = Mustache.new
411 instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
412
413 assert_equal '', instance.render
414 end
415
416 def test_not_found_in_nested_context_renders_empty_string
417 instance = Mustache.new
418 instance[:list] = { :item => 1234 }
419 instance.template = '{{#list}} <li>{{prefix}}{{item}}</li> {{/list}}'
420
421 assert_equal ' <li>1234</li> ', instance.render
422 end
423
424 def test_not_found_in_context_raises_when_asked_to
425 instance = Mustache.new
426 instance.raise_on_context_miss = true
427 instance.template = '{{#list}} <li>{{item}}</li> {{/list}}'
428
429 assert_raises Mustache::ContextMiss do
430 instance.render
431 end
432 end
433
434 def test_not_found_deep_in_context_raises_when_asked_to
435 instance = Mustache.new
436 instance.raise_on_context_miss = true
437
438 instance[:list] = { :item => { :value => 1234 } }
439
440 instance.template = '{{list.item.no_value}}'
441
442 assert_raises Mustache::ContextMiss do
443 instance.render
444 end
445 end
446
447 def test_found_in_nested_context_when_asked_to_raise
448 instance = Mustache.new
449 instance.raise_on_context_miss = true
450
451 instance[:item] = { :list => [ { :value => 1235, :deep_list => [{:x => 'y'}]}] }
452
453 instance.template = '{{#item.list}}{{#deep_list}}{{value}}{{/deep_list}}{{/item.list}}'
454 assert_equal '1235', instance.render
455 end
456
457 def test_knows_when_its_been_compiled_when_set_with_string
458 klass = Class.new(Mustache)
459
460 refute klass.compiled?
461 klass.template = 'Hi, {{person}}!'
462 assert klass.compiled?
463 end
464
465 def test_knows_when_its_been_compiled_when_using_a_file_template
466 klass = Class.new(Simple)
467 klass.template_file = File.dirname(__FILE__) + '/fixtures/simple.mustache'
468
469 refute klass.compiled?
470 klass.render
471 assert klass.compiled?
472 end
473
474 def test_an_instance_knows_when_its_class_is_compiled
475 klass = Class.new(Simple)
476 instance = klass.new
477
478 refute klass.compiled?, "Simple was already compiled (from class)."
479 refute instance.compiled?, "Simple was already compiled (from instance)."
480
481 klass.render
482
483 assert klass.compiled?, "Simple was not compiled (from class)."
484 assert instance.compiled?, "Simple was not compiled (from instance)."
485 end
486
487 def test_knows_when_its_been_compiled_at_the_instance_level
488 klass = Class.new(Mustache)
489 instance = klass.new
490
491 refute instance.compiled?
492 instance.template = 'Hi, {{person}}!'
493 assert instance.compiled?
494 end
495
496 def test_sections_returning_lambdas_get_called_with_text
497 view = Lambda.new
498 view[:name] = 'Chris'
499
500 assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
501 assert_equal 1, view.calls
502
503 assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
504 assert_equal 1, view.calls
505
506 assert_equal "Hi Chris.\n\nHi {{name}}.", view.render.chomp
507 assert_equal 1, view.calls
508 end
509
510 def test_sections_returning_lambdas_get_called_dynamically_with_text
511 view = Mustache.new
512 view.template = '{{name}}'
513 view[:name] = lambda { '{{dynamic_name}}' }
514 view[:dynamic_name] = 'Chris'
515
516 assert_equal "Chris", view.render.chomp
517 end
518
519 def test_sections_returning_lambdas_get_not_called_dynamically_with_text_if_static
520 view = Mustache.new :static_lambdas => true
521 view.template = '{{name}}'
522 view[:name] = lambda { '{{dynamic_name}}' }
523 view[:dynamic_name] = 'Chris'
524
525 assert_equal "{{dynamic_name}}", view.render.chomp
526 end
527
528 def test_sections_which_refer_to_unary_method_call_them_as_proc
529 kls = Class.new(Mustache) do
530 def unary_method(arg)
531 "(#{arg})"
532 end
533 end
534
535 str = kls.render("{{#unary_method}}test{{/unary_method}}")
536 assert_equal "(test)", str
537 end
538
539 def test_lots_of_staches
540 template = "{{{{foo}}}}"
541
542 begin
543 Mustache.render(template, :foo => "defunkt")
544 rescue => e
545 end
546
547 assert e.message.include?("Illegal content in tag")
548 end
549
550 def test_liberal_tag_names
551 template = "{{first-name}} {{middle_name!}} {{lastName?}}"
552 hash = {
553 'first-name' => 'chris',
554 'middle_name!' => 'j',
555 'lastName?' => 'strath'
556 }
557
558 assert_equal "chris j strath", Mustache.render(template, hash)
559 end
560
561 def test_liberal_tag_names_in_class
562 assert_equal <<-end_liberal, Liberal.render
563 kevin j sheurs 123 Somewhere St
564 end_liberal
565 end
566
567 def test_nested_sections_same_names
568 template = <<template
569 {{#items}}
570 start
571 {{#items}}
572 {{a}}
573 {{/items}}
574 end
575 {{/items}}
576 template
577
578 data = {
579 "items" => [
580 { "items" => [ {"a" => 1}, {"a" => 2}, {"a" => 3} ] },
581 { "items" => [ {"a" => 4}, {"a" => 5}, {"a" => 6} ] },
582 { "items" => [ {"a" => 7}, {"a" => 8}, {"a" => 9} ] }
583 ]
584 }
585
586 assert_equal <<expected, Mustache.render(template, data)
587 start
588 1
589 2
590 3
591 end
592 start
593 4
594 5
595 6
596 end
597 start
598 7
599 8
600 9
601 end
602 expected
603 end
604
605 def test_id_with_nested_context
606 html = %(<div>{{id}}</div>\n<div>{{# has_a? }}{{id}}{{/ has_a? }}</div>\n<div>{{# has_b? }}{{id}}{{/ has_b? }}</div>\n)
607
608 instance = Mustache.new
609 instance.template = html
610 instance[:id] = 3
611 instance[:has_a?] = true
612 instance[:has_b?] = true
613 assert_equal <<-rendered, instance.render
614 <div>3</div>
615 <div>3</div>
616 <div>3</div>
617 rendered
618 end
619
620 def test_utf8
621 klass = Class.new(Mustache)
622 klass.template_name = 'utf8'
623 klass.template_path = 'test/fixtures'
624 view = klass.new
625 view[:test] = "中文"
626
627 assert_equal <<-rendered, view.render
628 <h1>中文 中文</h1>
629
630 <h2>中文又来啦</h2>
631 rendered
632 end
633
634 def test_indentation
635 view = Mustache.new
636 view[:name] = 'indent'
637 view[:text] = 'puts :indented!'
638 view.template = <<template
639 def {{name}}
640 {{text}}
641 end
642 template
643
644 assert_equal <<template, view.render
645 def indent
646 puts :indented!
647 end
648 template
649 end
650
651 def test_struct
652 person = Struct.new(:name, :age)
653 view = Mustache.new
654 view[:person] = person.new('Marvin', 25)
655 view.template = '{{#person}}{{name}} is {{age}}{{/person}}'
656 assert_equal 'Marvin is 25', view.render
657 end
658
659 def test_method_missing
660 assert_equal('[ 0 1 2 3 4 5 6 7 8 9 10 ]', MethodMissing.render)
661 end
662
663 def test_custom_html_escaping
664 view = Class.new(Mustache) do
665 def escapeHTML(str)
666 "pong"
667 end
668 end
669
670 assert_equal 'pong', view.render("{{thing}}", :thing => "nothing")
671 assert_equal 'nothing', Mustache.render("{{thing}}", :thing => "nothing")
672 end
673
674 def test_custom_escaping
675 view = Class.new(Mustache) do
676 def escape(str)
677 JSON.dump(str)
678 end
679 end
680
681 assert_equal '{ "key": "a\"b" }', view.render('{ "key": {{thing}} }', :thing => 'a"b')
682 assert_equal 'nothing', Mustache.render("{{thing}}", :thing => "nothing")
683 end
684
685 def test_implicit_iterator
686 view = Mustache.new
687 view.template = "{{#people}}* {{.}}\n{{/people}}"
688 view[:people] = %w( Chris Mark Scott )
689
690 assert_equal <<text, view.render
691 * Chris
692 * Mark
693 * Scott
694 text
695 end
696
697 def test_unescaped_implicit_iterator
698 view = Mustache.new
699 view.template = "{{#people}}* {{{.}}}\n{{/people}}"
700 view[:people] = %w( Chris Mark Scott )
701
702 assert_equal <<text, view.render
703 * Chris
704 * Mark
705 * Scott
706 text
707 end
708
709 def test_dot_notation
710 assert_equal <<-text, DotNotation.render
711 * Chris Firescythe
712 * 24
713 * Cincinnati, OH
714 * Cincinnati, OH
715 * Cincinnati, OH
716 * Cincinnati, OH
717 * Normal
718
719 * Chris Firescythe
720 * Cincinnati, OH
721 text
722 end
723
724 def test_inherited_attributes
725 Object.const_set :TestNamespace, Module.new
726 base = Class.new(Mustache)
727 tmpl = Class.new(base)
728
729 {:template_path => File.expand_path('./foo'),
730 :template_extension => 'stache',
731 :view_namespace => TestNamespace,
732 :view_path => './foo'
733 }.each do |attr, value|
734 base.send("#{attr}=", value)
735 assert_equal value, tmpl.send(attr)
736 end
737 end
738
739 def test_hash_default_proc
740 template = <<template
741 {{greetings.Peter}}
742 {{greetings.Paul}}
743 {{greetings.Mary}}
744 template
745 data = {
746 'greetings' => Hash.new { |hash, key| hash[key] = "Hello, #{key}!" }
747 }
748
749 assert_equal <<expected, Mustache.render(template, data)
750 Hello, Peter!
751 Hello, Paul!
752 Hello, Mary!
753 expected
754 end
755
756 def test_array_of_arrays
757 template = <<template
758 {{#items}}
759 start
760 {{#map}}
761 {{a}}
762 {{/map}}
763 end
764 {{/items}}
765 template
766
767 data = {
768 "items" => [
769 [ {"a" => 1}, {"a" => 2}, {"a" => 3} ],
770 [ {"a" => 4}, {"a" => 5}, {"a" => 6} ],
771 [ {"a" => 7}, {"a" => 8}, {"a" => 9} ]
772 ]
773 }
774
775 assert_equal <<expected, Mustache.render(template, data)
776 start
777 1
778 2
779 3
780 end
781 start
782 4
783 5
784 6
785 end
786 start
787 7
788 8
789 9
790 end
791 expected
792 end
793
794 def test_indentation_again
795 template = <<template
796 SELECT
797 {{#cols}}
798 {{name}},
799 {{/cols}}
800 FROM
801 DUMMY1
802 template
803
804 view = Mustache.new
805 view[:cols] = [{:name => 'Name'}, {:name => 'Age'}, {:name => 'Weight'}]
806 view.template = template
807
808 assert_equal <<template, view.render
809 SELECT
810 Name,
811 Age,
812 Weight,
813 FROM
814 DUMMY1
815 template
816 end
817
818 def test_cast_to_hash_in_context
819 hashlike = Object.new
820 def hashlike.title
821 'title'
822 end
823 def hashlike.to_hash
824 { title: 'title' }
825 end
826
827 template = '%%{{title}}%%'
828
829 assert_equal '%%title%%', Mustache.render(template, hashlike)
830 end
831
832 def test_instance_with_initialize_render
833 klass = Class.new(Mustache) do
834 def initialize(name)
835 super
836 @name = name
837 end
838 attr_reader :name
839 end
840
841 klass.template = "Hi {{name}}!"
842 assert_equal "Hi Dougal!", klass.new("Dougal").render
843 end
844 end