2 require 'abstract_unit'
3 require 'controller/fake_models'
7 @assigns = { :secret => 'in the sauce' }
8 @view = ActionView
::Base.new(paths
, @assigns)
10 # Reload and register danish language for testing
12 I18n
.backend
.store_translations
'da', {}
13 I18n
.backend
.store_translations
'pt-BR', {}
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
20 assert_equal
"Hello world!", @view.render(:file => "test
/hello_world
.erb
")
23 def test_render_file_not_using_full_path
24 assert_equal "Hello world!
", @view.render(:file => "test
/hello_world
.erb
")
27 def test_render_file_without_specific_extension
28 assert_equal "Hello world!
", @view.render(:file => "test
/hello_world
")
31 def test_render_file_with_localization
32 old_locale = I18n.locale
34 assert_equal "Hey verden
", @view.render(:file => "test
/hello_world
")
36 I18n.locale = old_locale
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
")
44 I18n.locale = old_locale
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
")
52 @view.template_format = old_format
55 def test_render_implicit_html_template_from_xhr_request_with_localization
56 old_locale = I18n.locale
57 old_format = @view.template_format
59 @view.template_format = :js
60 assert_equal "Hey HTML!
\n", @view.render(:file => "test
/render_implicit_html_template_from_xhr_request
")
62 I18n.locale = old_locale
63 @view.template_format = old_format
66 def test_render_file_at_top_level
67 assert_equal 'Elastica', @view.render(:file => '/shared')
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)
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
")
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)
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
")
88 def test_render_has_access_current_template
89 assert_equal "test
/template.erb", @view.render(:file => "test/template
.erb
")
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!') }
98 def test_render_partial_from_default
99 assert_equal "only partial", @view.render("test/partial_only")
102 def test_render_partial
103 assert_equal "only partial", @view.render(:partial => "test/partial_only")
106 def test_render_partial_with_format
107 assert_equal 'partial html
', @view.render(:partial => 'test
/partial
')
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
')
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
')
120 def test_render_partial_with_locals
121 assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
124 def test_render_partial_with_locals_from_default
125 assert_equal "only partial", @view.render("test/partial_only", :counter_counter => 5)
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
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
148 def test_render_object
149 assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
152 def test_render_partial_collection
153 assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
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)
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") ])
166 def test_render_partial_with_empty_collection_should_return_nil
167 assert_nil @view.render(:partial => "test/customer", :collection => [])
170 def test_render_partial_with_nil_collection_should_return_nil
171 assert_nil @view.render(:partial => "test/customer", :collection => nil)
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 ])
178 def test_render_partial_with_empty_array_should_return_nil
179 assert_nil @view.render(:partial => [])
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" })
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") }
193 def test_render_inline
194 assert_equal "Hello, World!", @view.render(:inline => "Hello
, World!
")
197 def test_render_inline_with_locals
198 assert_equal "Hello
, Josh!
", @view.render(:inline => "Hello
, <%= name
%>!
", :locals => { :name => "Josh
" })
201 def test_render_fallbacks_to_erb_for_unknown_types
202 assert_equal "Hello
, World!
", @view.render(:inline => "Hello
, World!
", :type => :bar)
205 CustomHandler = lambda do |template|
206 "@output_buffer = ''\n" +
207 "@output_buffer << 'source: #{template.source.inspect}'\n"
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)
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)
220 class LegacyHandler < ActionView::TemplateHandler
221 def render(template, local_assigns)
222 "source
: #{template.source}; locals
: #{local_assigns.inspect}"
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)
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}") }
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~')
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")
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")
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
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
)
269 ActionView
::Base.cache_template_loading
= @previous_cache_template_loading
273 class CachedRenderTest
< Test
::Unit::TestCase
274 include TemplatesSetupTeardown
275 include RenderTestCases
278 setup_view_paths_for(cache_templates
= true)
282 class ReloadableRenderTest
< Test
::Unit::TestCase
283 include TemplatesSetupTeardown
284 include RenderTestCases
287 setup_view_paths_for(cache_templates
= false)