2009-02-04 23:10:03 -05:00
|
|
|
# encoding: utf-8
|
2008-06-25 06:47:01 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'controller/fake_models'
|
|
|
|
|
2009-08-11 18:02:05 -04:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
end
|
|
|
|
|
2008-11-28 15:31:54 -05:00
|
|
|
module RenderTestCases
|
|
|
|
def setup_view(paths)
|
2008-06-25 06:47:01 -04:00
|
|
|
@assigns = { :secret => 'in the sauce' }
|
2008-11-28 15:31:54 -05:00
|
|
|
@view = ActionView::Base.new(paths, @assigns)
|
2010-03-18 18:52:43 -04:00
|
|
|
@controller_view = TestController.new.view_context
|
2009-01-25 23:50:02 -05:00
|
|
|
|
|
|
|
# Reload and register danish language for testing
|
|
|
|
I18n.reload!
|
|
|
|
I18n.backend.store_translations 'da', {}
|
2009-02-07 00:23:50 -05:00
|
|
|
I18n.backend.store_translations 'pt-BR', {}
|
2009-01-25 23:50:02 -05:00
|
|
|
|
|
|
|
# Ensure original are still the same since we are reindexing view paths
|
2009-06-08 16:33:18 -04:00
|
|
|
assert_equal ORIGINAL_LOCALES, I18n.available_locales.map {|l| l.to_s }.sort
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_not_using_full_path
|
2008-07-02 22:38:58 -04:00
|
|
|
assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_without_specific_extension
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal "Hello world!", @view.render(:file => "test/hello_world")
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
2009-01-25 23:50:02 -05:00
|
|
|
def test_render_file_with_localization
|
2010-03-10 16:11:48 -05:00
|
|
|
old_locale, @view.locale = @view.locale, :da
|
2009-09-19 14:22:09 -04:00
|
|
|
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
|
|
|
|
ensure
|
2010-03-10 16:11:48 -05:00
|
|
|
@view.locale = old_locale
|
2009-01-25 23:50:02 -05:00
|
|
|
end
|
|
|
|
|
2009-02-07 00:23:50 -05:00
|
|
|
def test_render_file_with_dashed_locale
|
2010-03-10 16:11:48 -05:00
|
|
|
old_locale, @view.locale = @view.locale, :"pt-BR"
|
2009-04-22 20:16:28 -04:00
|
|
|
assert_equal "Ola mundo", @view.render(:file => "test/hello_world")
|
2009-02-07 00:23:50 -05:00
|
|
|
ensure
|
2010-03-10 16:11:48 -05:00
|
|
|
@view.locale = old_locale
|
2009-02-07 00:23:50 -05:00
|
|
|
end
|
|
|
|
|
2008-08-13 20:15:35 -04:00
|
|
|
def test_render_file_at_top_level
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal 'Elastica', @view.render(:file => '/shared')
|
2008-08-13 20:15:35 -04:00
|
|
|
end
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
def test_render_file_with_full_path
|
|
|
|
template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb')
|
2008-07-02 22:38:58 -04:00
|
|
|
assert_equal "Hello world!", @view.render(:file => template_path)
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_with_instance_variables
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_ivar.erb")
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_with_locals
|
|
|
|
locals = { :secret => 'in the sauce' }
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal "The secret is in the sauce\n", @view.render(:file => "test/render_file_with_locals.erb", :locals => locals)
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_not_using_full_path_with_dot_in_path
|
2008-11-19 08:00:16 -05:00
|
|
|
assert_equal "The secret is in the sauce\n", @view.render(:file => "test/dot.directory/render_file_with_ivar")
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_update
|
|
|
|
# TODO: You should not have to stub out template because template is self!
|
|
|
|
@view.instance_variable_set(:@template, @view)
|
|
|
|
assert_equal 'alert("Hello, World!");', @view.render(:update) { |page| page.alert('Hello, World!') }
|
|
|
|
end
|
|
|
|
|
2008-11-19 08:00:16 -05:00
|
|
|
def test_render_partial_from_default
|
|
|
|
assert_equal "only partial", @view.render("test/partial_only")
|
|
|
|
end
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
def test_render_partial
|
|
|
|
assert_equal "only partial", @view.render(:partial => "test/partial_only")
|
|
|
|
end
|
|
|
|
|
2008-08-13 20:15:35 -04:00
|
|
|
def test_render_partial_with_format
|
|
|
|
assert_equal 'partial html', @view.render(:partial => 'test/partial')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_at_top_level
|
|
|
|
# file fixtures/_top_level_partial_only.erb (not fixtures/test)
|
|
|
|
assert_equal 'top level partial', @view.render(:partial => '/top_level_partial_only')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_with_format_at_top_level
|
|
|
|
# file fixtures/_top_level_partial.html.erb (not fixtures/test, with format extension)
|
|
|
|
assert_equal 'top level partial html', @view.render(:partial => '/top_level_partial')
|
|
|
|
end
|
|
|
|
|
2008-08-09 11:34:19 -04:00
|
|
|
def test_render_partial_with_locals
|
|
|
|
assert_equal "5", @view.render(:partial => "test/counter", :locals => { :counter_counter => 5 })
|
|
|
|
end
|
|
|
|
|
2008-11-19 08:00:16 -05:00
|
|
|
def test_render_partial_with_locals_from_default
|
|
|
|
assert_equal "only partial", @view.render("test/partial_only", :counter_counter => 5)
|
|
|
|
end
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
def test_render_partial_with_errors
|
2008-09-20 23:57:45 -04:00
|
|
|
@view.render(:partial => "test/raise")
|
2009-12-02 23:01:01 -05:00
|
|
|
flunk "Render did not raise Template::Error"
|
|
|
|
rescue ActionView::Template::Error => e
|
2010-03-02 17:04:57 -05:00
|
|
|
assert_match %r!method.*doesnt_exist!, e.message
|
2008-09-20 23:57:45 -04:00
|
|
|
assert_equal "", e.sub_template_message
|
|
|
|
assert_equal "1", e.line_number
|
2010-05-09 05:52:26 -04:00
|
|
|
assert_equal "1: <%= doesnt_exist %>", e.annoted_source_code.strip
|
2008-09-20 23:57:45 -04:00
|
|
|
assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_sub_template_with_errors
|
|
|
|
@view.render(:file => "test/sub_template_raise")
|
2009-12-02 23:01:01 -05:00
|
|
|
flunk "Render did not raise Template::Error"
|
|
|
|
rescue ActionView::Template::Error => e
|
2010-03-02 17:04:57 -05:00
|
|
|
assert_match %r!method.*doesnt_exist!, e.message
|
2008-09-20 23:57:45 -04:00
|
|
|
assert_equal "Trace of template inclusion: #{File.expand_path("#{FIXTURE_LOAD_PATH}/test/sub_template_raise.html.erb")}", e.sub_template_message
|
|
|
|
assert_equal "1", e.line_number
|
|
|
|
assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
2009-03-07 15:05:18 -05:00
|
|
|
def test_render_object
|
|
|
|
assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
|
|
|
|
end
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
def test_render_partial_collection
|
|
|
|
assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
|
|
|
|
end
|
2008-08-09 11:34:19 -04:00
|
|
|
|
2008-07-02 11:38:50 -04:00
|
|
|
def test_render_partial_collection_as
|
2008-08-09 11:34:19 -04:00
|
|
|
assert_equal "david david davidmary mary mary",
|
2008-07-02 11:38:50 -04:00
|
|
|
@view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
|
|
|
|
end
|
2008-08-09 11:34:19 -04:00
|
|
|
|
2008-07-02 12:19:41 -04:00
|
|
|
def test_render_partial_collection_without_as
|
2009-08-12 02:43:15 -04:00
|
|
|
assert_equal "local_inspector,local_inspector_counter",
|
2008-07-02 12:19:41 -04:00
|
|
|
@view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
|
|
|
|
end
|
2008-06-25 06:47:01 -04:00
|
|
|
|
2008-08-19 20:09:04 -04:00
|
|
|
def test_render_partial_with_empty_collection_should_return_nil
|
|
|
|
assert_nil @view.render(:partial => "test/customer", :collection => [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_with_nil_collection_should_return_nil
|
|
|
|
assert_nil @view.render(:partial => "test/customer", :collection => nil)
|
|
|
|
end
|
|
|
|
|
2008-10-30 16:07:47 -04:00
|
|
|
def test_render_partial_with_nil_values_in_collection
|
|
|
|
assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ])
|
|
|
|
end
|
|
|
|
|
2008-08-22 11:04:27 -04:00
|
|
|
def test_render_partial_with_empty_array_should_return_nil
|
|
|
|
assert_nil @view.render(:partial => [])
|
|
|
|
end
|
|
|
|
|
2009-08-11 18:02:05 -04:00
|
|
|
def test_render_partial_using_string
|
|
|
|
assert_equal "Hello: Anonymous", @controller_view.render('customer')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_with_locals_using_string
|
|
|
|
assert_equal "Hola: david", @controller_view.render('customer_greeting', :greeting => 'Hola', :customer_greeting => Customer.new("david"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_using_object
|
|
|
|
assert_equal "Hello: lifo",
|
|
|
|
@controller_view.render(Customer.new("lifo"), :greeting => "Hello")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_using_collection
|
|
|
|
customers = [ Customer.new("Amazon"), Customer.new("Yahoo") ]
|
|
|
|
assert_equal "Hello: AmazonHello: Yahoo",
|
|
|
|
@controller_view.render(customers, :greeting => "Hello")
|
|
|
|
end
|
|
|
|
|
2008-06-25 06:47:01 -04:00
|
|
|
# TODO: The reason for this test is unclear, improve documentation
|
|
|
|
def test_render_partial_and_fallback_to_layout
|
|
|
|
assert_equal "Before (Josh)\n\nAfter", @view.render(:partial => "test/layout_for_partial", :locals => { :name => "Josh" })
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: The reason for this test is unclear, improve documentation
|
|
|
|
def test_render_missing_xml_partial_and_raise_missing_template
|
2009-01-22 17:18:10 -05:00
|
|
|
@view.formats = [:xml]
|
2008-06-25 06:47:01 -04:00
|
|
|
assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
|
2009-08-15 01:32:40 -04:00
|
|
|
ensure
|
|
|
|
@view.formats = nil
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline
|
|
|
|
assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline_with_locals
|
|
|
|
assert_equal "Hello, Josh!", @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" })
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_fallbacks_to_erb_for_unknown_types
|
2008-11-28 12:18:28 -05:00
|
|
|
assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :bar)
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
2008-07-18 17:00:20 -04:00
|
|
|
CustomHandler = lambda do |template|
|
|
|
|
"@output_buffer = ''\n" +
|
|
|
|
"@output_buffer << 'source: #{template.source.inspect}'\n"
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline_with_compilable_custom_type
|
2008-07-18 17:00:20 -04:00
|
|
|
ActionView::Template.register_template_handler :foo, CustomHandler
|
2008-07-11 16:39:22 -04:00
|
|
|
assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline_with_locals_and_compilable_custom_type
|
2008-07-18 17:00:20 -04:00
|
|
|
ActionView::Template.register_template_handler :foo, CustomHandler
|
2008-07-11 16:39:22 -04:00
|
|
|
assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
2008-10-28 18:31:33 -04:00
|
|
|
|
2009-02-24 11:41:45 -05:00
|
|
|
def test_render_ignores_templates_with_malformed_template_handlers
|
|
|
|
%w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
|
|
|
|
assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-28 18:31:33 -04:00
|
|
|
def test_render_with_layout
|
|
|
|
assert_equal %(<title></title>\nHello world!\n),
|
|
|
|
@view.render(:file => "test/hello_world.erb", :layout => "layouts/yield")
|
|
|
|
end
|
|
|
|
|
Add deprecation notices for <% %>.
* The approach is to compile <% %> into a method call that checks whether
the value returned from a block is a String. If it is, it concats to the buffer and
prints a deprecation warning.
* <%= %> uses exactly the same logic to compile the template, which first checks
to see whether it's compiling a block.
* This should have no impact on other uses of block in templates. For instance, in
<% [1,2,3].each do |i| %><%= i %><% end %>, the call to each returns an Array,
not a String, so the result is not concatenated
* In two cases (#capture and #cache), a String can be returned that should *never*
be concatenated. We have temporarily created a String subclass called NonConcattingString
which behaves (and is serialized) identically to String, but is not concatenated
by the code that handles deprecated <% %> block helpers. Once we remove support
for <% %> block helpers, we can remove NonConcattingString.
2010-03-15 14:58:05 -04:00
|
|
|
# TODO: Move to deprecated_tests.rb
|
|
|
|
def test_render_with_nested_layout_deprecated
|
|
|
|
assert_deprecated do
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n),
|
Add deprecation notices for <% %>.
* The approach is to compile <% %> into a method call that checks whether
the value returned from a block is a String. If it is, it concats to the buffer and
prints a deprecation warning.
* <%= %> uses exactly the same logic to compile the template, which first checks
to see whether it's compiling a block.
* This should have no impact on other uses of block in templates. For instance, in
<% [1,2,3].each do |i| %><%= i %><% end %>, the call to each returns an Array,
not a String, so the result is not concatenated
* In two cases (#capture and #cache), a String can be returned that should *never*
be concatenated. We have temporarily created a String subclass called NonConcattingString
which behaves (and is serialized) identically to String, but is not concatenated
by the code that handles deprecated <% %> block helpers. Once we remove support
for <% %> block helpers, we can remove NonConcattingString.
2010-03-15 14:58:05 -04:00
|
|
|
@view.render(:file => "test/deprecated_nested_layout.erb", :layout => "layouts/yield")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-28 18:31:33 -04:00
|
|
|
def test_render_with_nested_layout
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n),
|
2008-10-28 18:31:33 -04:00
|
|
|
@view.render(:file => "test/nested_layout.erb", :layout => "layouts/yield")
|
|
|
|
end
|
2009-02-04 23:10:03 -05:00
|
|
|
|
2010-02-23 12:50:05 -05:00
|
|
|
def test_render_with_file_in_layout
|
|
|
|
assert_equal %(\n<title>title</title>\n\n),
|
|
|
|
@view.render(:file => "test/layout_render_file.erb")
|
|
|
|
end
|
2008-06-25 06:47:01 -04:00
|
|
|
end
|
2008-11-28 12:18:28 -05:00
|
|
|
|
2009-02-02 14:49:32 -05:00
|
|
|
class CachedViewRenderTest < ActiveSupport::TestCase
|
2008-11-28 15:31:54 -05:00
|
|
|
include RenderTestCases
|
|
|
|
|
|
|
|
# Ensure view path cache is primed
|
|
|
|
def setup
|
|
|
|
view_paths = ActionController::Base.view_paths
|
2010-03-08 10:32:40 -05:00
|
|
|
assert_equal ActionView::FileSystemResolver, view_paths.first.class
|
2008-11-28 15:31:54 -05:00
|
|
|
setup_view(view_paths)
|
|
|
|
end
|
2010-04-02 22:21:19 -04:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
GC.start
|
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
end
|
|
|
|
|
2009-02-02 14:49:32 -05:00
|
|
|
class LazyViewRenderTest < ActiveSupport::TestCase
|
2008-11-28 15:31:54 -05:00
|
|
|
include RenderTestCases
|
|
|
|
|
2008-11-28 12:18:28 -05:00
|
|
|
# Test the same thing as above, but make sure the view path
|
|
|
|
# is not eager loaded
|
|
|
|
def setup
|
2010-03-08 10:32:40 -05:00
|
|
|
path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
|
2008-12-29 20:27:19 -05:00
|
|
|
view_paths = ActionView::Base.process_view_paths(path)
|
2010-03-08 10:32:40 -05:00
|
|
|
assert_equal ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH), view_paths.first
|
2008-11-28 15:31:54 -05:00
|
|
|
setup_view(view_paths)
|
2008-11-28 12:18:28 -05:00
|
|
|
end
|
2010-04-02 22:21:19 -04:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
GC.start
|
|
|
|
end
|
2010-04-08 22:27:22 -04:00
|
|
|
|
|
|
|
if '1.9'.respond_to?(:force_encoding)
|
|
|
|
def test_render_utf8_template_with_magic_comment
|
|
|
|
with_external_encoding Encoding::ASCII_8BIT do
|
|
|
|
result = @view.render(:file => "test/utf8_magic.html.erb", :layouts => "layouts/yield")
|
|
|
|
assert_equal Encoding::UTF_8, result.encoding
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
2010-04-08 22:27:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_utf8_template_with_default_external_encoding
|
|
|
|
with_external_encoding Encoding::UTF_8 do
|
|
|
|
result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
|
|
|
|
assert_equal Encoding::UTF_8, result.encoding
|
2010-04-08 23:22:07 -04:00
|
|
|
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
2010-04-08 22:27:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_utf8_template_with_incompatible_external_encoding
|
|
|
|
with_external_encoding Encoding::SJIS do
|
|
|
|
begin
|
|
|
|
result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
|
|
|
|
flunk 'Should have raised incompatible encoding error'
|
|
|
|
rescue ActionView::Template::Error => error
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
2010-04-08 22:27:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-08 23:22:07 -04:00
|
|
|
def test_render_utf8_template_with_partial_with_incompatible_encoding
|
|
|
|
with_external_encoding Encoding::SJIS do
|
|
|
|
begin
|
|
|
|
result = @view.render(:file => "test/utf8_magic_with_bare_partial.html.erb", :layouts => "layouts/yield")
|
|
|
|
flunk 'Should have raised incompatible encoding error'
|
|
|
|
rescue ActionView::Template::Error => error
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
2010-04-08 23:22:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-08 22:27:22 -04:00
|
|
|
def with_external_encoding(encoding)
|
|
|
|
old, Encoding.default_external = Encoding.default_external, encoding
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Encoding.default_external = old
|
|
|
|
end
|
|
|
|
end
|
2008-11-28 12:18:28 -05:00
|
|
|
end
|