Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / test / template / render_test.rb
1 # encoding: utf-8
2 require 'abstract_unit'
3 require 'controller/fake_models'
4
5 module RenderTestCases
6 def setup_view(paths)
7 @assigns = { :secret => 'in the sauce' }
8 @view = ActionView::Base.new(paths, @assigns)
9
10 # Reload and register danish language for testing
11 I18n.reload!
12 I18n.backend.store_translations 'da', {}
13 I18n.backend.store_translations 'pt-BR', {}
14
15 # Ensure original are still the same since we are reindexing view paths
16 assert_equal ORIGINAL_LOCALES, I18n.available_locales.map(&:to_s).sort
17 end
18
19 def test_render_file
20 assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
21 end
22
23 def test_render_file_not_using_full_path
24 assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
25 end
26
27 def test_render_file_without_specific_extension
28 assert_equal "Hello world!", @view.render(:file => "test/hello_world")
29 end
30
31 def test_render_file_with_localization
32 old_locale = I18n.locale
33 I18n.locale = :da
34 assert_equal "Hey verden", @view.render(:file => "test/hello_world")
35 ensure
36 I18n.locale = old_locale
37 end
38
39 def test_render_file_with_dashed_locale
40 old_locale = I18n.locale
41 I18n.locale = :"pt-BR"
42 assert_equal "Ola mundo", @view.render(:file => "test/hello_world")
43 ensure
44 I18n.locale = old_locale
45 end
46
47 def test_render_implicit_html_template_from_xhr_request
48 old_format = @view.template_format
49 @view.template_format = :js
50 assert_equal "Hello HTML!", @view.render(:file => "test/render_implicit_html_template_from_xhr_request")
51 ensure
52 @view.template_format = old_format
53 end
54
55 def test_render_implicit_html_template_from_xhr_request_with_localization
56 old_locale = I18n.locale
57 old_format = @view.template_format
58 I18n.locale = :da
59 @view.template_format = :js
60 assert_equal "Hey HTML!\n", @view.render(:file => "test/render_implicit_html_template_from_xhr_request")
61 ensure
62 I18n.locale = old_locale
63 @view.template_format = old_format
64 end
65
66 def test_render_file_at_top_level
67 assert_equal 'Elastica', @view.render(:file => '/shared')
68 end
69
70 def test_render_file_with_full_path
71 template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb')
72 assert_equal "Hello world!", @view.render(:file => template_path)
73 end
74
75 def test_render_file_with_instance_variables
76 assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_ivar.erb")
77 end
78
79 def test_render_file_with_locals
80 locals = { :secret => 'in the sauce' }
81 assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_locals.erb", :locals => locals)
82 end
83
84 def test_render_file_not_using_full_path_with_dot_in_path
85 assert_equal "The secret is in the sauce\n", @view.render(:file => "test/dot.directory/render_file_with_ivar")
86 end
87
88 def test_render_has_access_current_template
89 assert_equal "test/template.erb", @view.render(:file => "test/template.erb")
90 end
91
92 def test_render_update
93 # TODO: You should not have to stub out template because template is self!
94 @view.instance_variable_set(:@template, @view)
95 assert_equal 'alert("Hello, World!");', @view.render(:update) { |page| page.alert('Hello, World!') }
96 end
97
98 def test_render_partial_from_default
99 assert_equal "only partial", @view.render("test/partial_only")
100 end
101
102 def test_render_partial
103 assert_equal "only partial", @view.render(:partial => "test/partial_only")
104 end
105
106 def test_render_partial_with_format
107 assert_equal 'partial html', @view.render(:partial => 'test/partial')
108 end
109
110 def test_render_partial_at_top_level
111 # file fixtures/_top_level_partial_only.erb (not fixtures/test)
112 assert_equal 'top level partial', @view.render(:partial => '/top_level_partial_only')
113 end
114
115 def test_render_partial_with_format_at_top_level
116 # file fixtures/_top_level_partial.html.erb (not fixtures/test, with format extension)
117 assert_equal 'top level partial html', @view.render(:partial => '/top_level_partial')
118 end
119
120 def test_render_partial_with_locals
121 assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
122 end
123
124 def test_render_partial_with_locals_from_default
125 assert_equal "only partial", @view.render("test/partial_only", :counter_counter => 5)
126 end
127
128 def test_render_partial_with_errors
129 @view.render(:partial => "test/raise")
130 flunk "Render did not raise TemplateError"
131 rescue ActionView::TemplateError => e
132 assert_match "undefined local variable or method `doesnt_exist'", e.message
133 assert_equal "", e.sub_template_message
134 assert_equal "1", e.line_number
135 assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
136 end
137
138 def test_render_sub_template_with_errors
139 @view.render(:file => "test/sub_template_raise")
140 flunk "Render did not raise TemplateError"
141 rescue ActionView::TemplateError => e
142 assert_match "undefined local variable or method `doesnt_exist'", e.message
143 assert_equal "Trace of template inclusion: #{File.expand_path("#{FIXTURE_LOAD_PATH}/test/sub_template_raise.html.erb")}", e.sub_template_message
144 assert_equal "1", e.line_number
145 assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
146 end
147
148 def test_render_object
149 assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
150 end
151
152 def test_render_partial_collection
153 assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
154 end
155
156 def test_render_partial_collection_as
157 assert_equal "david david davidmary mary mary",
158 @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
159 end
160
161 def test_render_partial_collection_without_as
162 assert_equal "local_inspector,local_inspector_counter,object",
163 @view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
164 end
165
166 def test_render_partial_with_empty_collection_should_return_nil
167 assert_nil @view.render(:partial => "test/customer", :collection => [])
168 end
169
170 def test_render_partial_with_nil_collection_should_return_nil
171 assert_nil @view.render(:partial => "test/customer", :collection => nil)
172 end
173
174 def test_render_partial_with_nil_values_in_collection
175 assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ])
176 end
177
178 def test_render_partial_with_empty_array_should_return_nil
179 assert_nil @view.render(:partial => [])
180 end
181
182 # TODO: The reason for this test is unclear, improve documentation
183 def test_render_partial_and_fallback_to_layout
184 assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
185 end
186
187 # TODO: The reason for this test is unclear, improve documentation
188 def test_render_missing_xml_partial_and_raise_missing_template
189 @view.template_format = :xml
190 assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
191 end
192
193 def test_render_inline
194 assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
195 end
196
197 def test_render_inline_with_locals
198 assert_equal "Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" })
199 end
200
201 def test_render_fallbacks_to_erb_for_unknown_types
202 assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :bar)
203 end
204
205 CustomHandler = lambda do |template|
206 "@output_buffer = ''\n" +
207 "@output_buffer << 'source: #{template.source.inspect}'\n"
208 end
209
210 def test_render_inline_with_compilable_custom_type
211 ActionView::Template.register_template_handler :foo, CustomHandler
212 assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
213 end
214
215 def test_render_inline_with_locals_and_compilable_custom_type
216 ActionView::Template.register_template_handler :foo, CustomHandler
217 assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
218 end
219
220 class LegacyHandler < ActionView::TemplateHandler
221 def render(template, local_assigns)
222 "source: #{template.source}; locals: #{local_assigns.inspect}"
223 end
224 end
225
226 def test_render_legacy_handler_with_custom_type
227 ActionView::Template.register_template_handler :foo, LegacyHandler
228 assert_equal 'source: Hello, <%= name %>!; locals: {:name=>"Josh"}', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
229 end
230
231 def test_render_ignores_templates_with_malformed_template_handlers
232 %w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
233 assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }
234 end
235 end
236
237 def test_template_with_malformed_template_handler_is_reachable_through_its_exact_filename
238 assert_equal "Don't render me!", @view.render(:file => 'test/malformed/malformed.html.erb~')
239 end
240
241 def test_render_with_layout
242 assert_equal %(<title></title>\nHello world!\n),
243 @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield")
244 end
245
246 def test_render_with_nested_layout
247 assert_equal %(<title>title</title>\n<div id="column">column</div>\n<div id="content">content</div>\n),
248 @view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield")
249 end
250
251 if '1.9'.respond_to?(:force_encoding)
252 def test_render_utf8_template
253 result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
254 assert_equal "Русский текст\n日本語のテキスト", result
255 assert_equal Encoding::UTF_8, result.encoding
256 end
257 end
258 end
259
260 module TemplatesSetupTeardown
261 def setup_view_paths_for(new_cache_template_loading)
262 @previous_cache_template_loading, ActionView::Base.cache_template_loading = ActionView::Base.cache_template_loading, new_cache_template_loading
263 view_paths = new_cache_template_loading ? CACHED_VIEW_PATHS : ActionView::Base.process_view_paths(CACHED_VIEW_PATHS.map(&:to_s))
264 assert_equal(new_cache_template_loading ? ActionView::Template::EagerPath : ActionView::ReloadableTemplate::ReloadablePath, view_paths.first.class)
265 setup_view(view_paths)
266 end
267
268 def teardown
269 ActionView::Base.cache_template_loading = @previous_cache_template_loading
270 end
271 end
272
273 class CachedRenderTest < Test::Unit::TestCase
274 include TemplatesSetupTeardown
275 include RenderTestCases
276
277 def setup
278 setup_view_paths_for(cache_templates = true)
279 end
280 end
281
282 class ReloadableRenderTest < Test::Unit::TestCase
283 include TemplatesSetupTeardown
284 include RenderTestCases
285
286 def setup
287 setup_view_paths_for(cache_templates = false)
288 end
289 end
290