Debian Codegraph @

test/partial_test.rb

raw symbols SCIP
1 require_relative 'helper'
2
3 class PartialTest < Minitest::Test
4 def test_view_partial
5 assert_equal <<-end_partial.strip, PartialWithModule.render
6 <h1>Welcome</h1>
7 Hello Bob
8 You have just won $100000!
9
10 <h3>Fair enough, right?</h3>
11 end_partial
12 end
13
14 def test_partial_with_slashes
15 klass = Class.new(Mustache)
16 klass.template = '{{> test/fixtures/inner_partial}}'
17 view = klass.new
18 view[:title] = 'success'
19
20 assert_equal "Again, success!", view.render
21 end
22
23 def test_partial_inlining
24 view = Mustache.new :inline_partials_at_compile_time => true
25 view.template = '{{> test/fixtures/inner_partial}}'
26 view[:title] = 'success'
27
28 # Test the rendered result first
29 assert_equal "Again, success!", view.render
30
31 # Now the template should be compiled.
32 # There should be no :partial instruction as the partial has been in-lined.
33 assert_equal false, view.template.tokens.flatten.include?(:partial)
34 end
35
36 def test_view_partial_inherits_context
37 klass = Class.new(TemplatePartial)
38 view = klass.new
39 view.template_path = File.dirname(__FILE__) + '/fixtures'
40 view[:titles] = [{:title => :One}, {:title => :Two}]
41 view.template = <<-end_template
42 <h1>Context Test</h1>
43 <ul>
44 {{#titles}}
45 <li>{{>inner_partial}}</li>
46 {{/titles}}
47 </ul>
48 end_template
49 assert_equal <<-end_partial, view.render
50 <h1>Context Test</h1>
51 <ul>
52 <li>Again, One!</li>
53 <li>Again, Two!</li>
54 </ul>
55 end_partial
56 end
57
58 def test_view_partial_inherits_context_of_class_methods
59 klass = Class.new(TemplatePartial)
60 klass.template_path = File.dirname(__FILE__) + '/fixtures'
61 klass.send(:define_method, :titles) do
62 [{:title => :One}, {:title => :Two}]
63 end
64 view = klass.new
65 view.template = <<-end_template
66 <h1>Context Test</h1>
67 <ul>
68 {{#titles}}
69 <li>{{>inner_partial}}</li>
70 {{/titles}}
71 </ul>
72 end_template
73 assert_equal <<-end_partial, view.render
74 <h1>Context Test</h1>
75 <ul>
76 <li>Again, One!</li>
77 <li>Again, Two!</li>
78 </ul>
79 end_partial
80 end
81
82 def test_template_partial
83 assert_equal <<-end_partial.strip, TemplatePartial.render
84 <h1>Welcome</h1>
85 Again, Welcome!
86 end_partial
87 end
88
89 def test_template_partial_with_custom_extension
90 partial = Class.new(TemplatePartial)
91 partial.template_extension = 'txt'
92 partial.template_path = File.dirname(__FILE__) + '/fixtures'
93
94 assert_equal <<-end_partial.strip, partial.render.strip
95 Welcome
96 -------
97
98 ## Again, Welcome! ##
99 end_partial
100 end
101
102 def test_recursive_partials
103 assert_equal <<-end_partial, Recursive.render
104 It works!
105 end_partial
106 end
107
108 def test_crazy_recursive_partials
109 assert_equal <<-end_partial.strip, CrazyRecursive.render
110 <html>
111 <body>
112 <ul>
113 <li>
114 1
115 <ul>
116 <li>
117 2
118 <ul>
119 <li>
120 3
121 <ul>
122 </ul>
123 </li>
124 </ul>
125 </li>
126 <li>
127 4
128 <ul>
129 <li>
130 5
131 <ul>
132 <li>
133 6
134 <ul>
135 </ul>
136 </li>
137 </ul>
138 </li>
139 </ul>
140 </li>
141 </ul>
142 </li>
143 </ul>
144 </body>
145 </html>
146 end_partial
147 end
148
149 def test_partials_use_proper_context
150 assert_equal "OuterThing OuterThing", OuterThing.render('{{name}} {{> p}}')
151
152 assert_equal "InnerThing InnerThing", InnerThing.render('{{name}} {{> p}}')
153
154 assert_equal "OuterThing InnerThing InnerThing",
155 OuterThing.render('{{name}} {{#inner}}{{name}} {{> p}}{{/inner}}')
156 end
157
158 def test_partials_render_returned_strings
159 assert_equal "ok", MiddleThing.render('{{> some_partial }}')
160 end
161 end
162
163 class InnerThing < Mustache
164 def partial(p) self.class end
165 def name; self.class end
166 end
167
168 class OuterThing < Mustache
169 def inner
170 InnerThing.new
171 end
172
173 def partial(p) self.class end
174 def name; self.class end
175 end
176
177 class MiddleThing < Mustache
178 def partial(name) "{{#{name}}}" end
179 def some_partial; "ok" end
180 end