aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/render_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test/template/render_test.rb')
-rw-r--r--actionpack/test/template/render_test.rb107
1 files changed, 93 insertions, 14 deletions
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index 059dcedad8..dd86bfed04 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -69,8 +69,6 @@ module RenderTestCases
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
@@ -116,7 +114,7 @@ module RenderTestCases
end
def test_render_sub_template_with_errors
- @view.render(:file => "test/sub_template_raise")
+ @view.render(:template => "test/sub_template_raise")
flunk "Render did not raise Template::Error"
rescue ActionView::Template::Error => e
assert_match %r!method.*doesnt_exist!, e.message
@@ -125,15 +123,35 @@ module RenderTestCases
assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
end
+ def test_render_file_with_errors
+ @view.render(:file => File.expand_path("test/_raise", FIXTURE_LOAD_PATH))
+ flunk "Render did not raise Template::Error"
+ rescue ActionView::Template::Error => e
+ assert_match %r!method.*doesnt_exist!, e.message
+ assert_equal "", e.sub_template_message
+ assert_equal "1", e.line_number
+ assert_equal "1: <%= doesnt_exist %>", e.annoted_source_code.strip
+ assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name
+ end
+
def test_render_object
assert_equal "Hello: david", @view.render(:partial => "test/customer", :object => Customer.new("david"))
end
+ def test_render_object_with_array
+ assert_equal "[1, 2, 3]", @view.render(:partial => "test/object_inspector", :object => [1, 2, 3])
+ end
+
def test_render_partial_collection
assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
end
- def test_render_partial_collection_as
+ def test_render_partial_collection_as_by_string
+ assert_equal "david david davidmary mary mary",
+ @view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => 'customer')
+ end
+
+ def test_render_partial_collection_as_by_symbol
assert_equal "david david davidmary mary mary",
@view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
end
@@ -191,6 +209,11 @@ module RenderTestCases
@view.formats = nil
end
+ def test_render_layout_with_block_and_other_partial_inside
+ render = @view.render(:layout => "test/layout_with_partial_and_yield.html.erb") { "Yield!" }
+ assert_equal "Before\npartial html\nYield!\nAfter\n", render
+ end
+
def test_render_inline
assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
end
@@ -208,6 +231,20 @@ module RenderTestCases
"@output_buffer << 'source: #{template.source.inspect}'\n"
end
+ WithViewHandler = lambda do |template, view|
+ %'"#{template.class} #{view.class}"'
+ end
+
+ def test_render_inline_with_render_from_to_proc
+ ActionView::Template.register_template_handler :ruby_handler, :source.to_proc
+ assert_equal '3', @view.render(:inline => "(1 + 2).to_s", :type => :ruby_handler)
+ end
+
+ def test_render_inline_with_template_handler_with_view
+ ActionView::Template.register_template_handler :with_view, WithViewHandler
+ assert_equal 'ActionView::Template ActionView::Base', @view.render(:inline => "Hello, World!", :type => :with_view)
+ end
+
def test_render_inline_with_compilable_custom_type
ActionView::Template.register_template_handler :foo, CustomHandler
assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
@@ -234,13 +271,49 @@ module RenderTestCases
@view.render(:file => "test/hello_world.erb", :layout => "layouts/yield_with_render_inline_inside")
end
+ def test_render_with_layout_which_renders_another_partial
+ assert_equal %(partial html\nHello world!\n),
+ @view.render(:file => "test/hello_world.erb", :layout => "layouts/yield_with_render_partial_inside")
+ end
- # TODO: Move to deprecated_tests.rb
- def test_render_with_nested_layout_deprecated
- assert_deprecated do
- assert_equal %(<title>title</title>\n\n<div id="column">column</div>\n<div id="content">content</div>\n),
- @view.render(:file => "test/deprecated_nested_layout.erb", :layout => "layouts/yield")
- end
+ def test_render_layout_with_block_and_yield
+ assert_equal %(Content from block!\n),
+ @view.render(:layout => "layouts/yield_only") { "Content from block!" }
+ end
+
+ def test_render_layout_with_block_and_yield_with_params
+ assert_equal %(Yield! Content from block!\n),
+ @view.render(:layout => "layouts/yield_with_params") { |param| "#{param} Content from block!" }
+ end
+
+ def test_render_layout_with_block_which_renders_another_partial_and_yields
+ assert_equal %(partial html\nContent from block!\n),
+ @view.render(:layout => "layouts/partial_and_yield") { "Content from block!" }
+ end
+
+ def test_render_partial_and_layout_without_block_with_locals
+ assert_equal %(Before (Foo!)\npartial html\nAfter),
+ @view.render(:partial => 'test/partial', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
+ end
+
+ def test_render_partial_and_layout_without_block_with_locals_and_rendering_another_partial
+ assert_equal %(Before (Foo!)\npartial html\npartial with partial\n\nAfter),
+ @view.render(:partial => 'test/partial_with_partial', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
+ end
+
+ def test_render_layout_with_a_nested_render_layout_call
+ assert_equal %(Before (Foo!)\nBefore (Bar!)\npartial html\nAfter\npartial with layout\n\nAfter),
+ @view.render(:partial => 'test/partial_with_layout', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
+ end
+
+ def test_render_layout_with_a_nested_render_layout_call_using_block_with_render_partial
+ assert_equal %(Before (Foo!)\nBefore (Bar!)\n\n partial html\n\nAfterpartial with layout\n\nAfter),
+ @view.render(:partial => 'test/partial_with_layout_block_partial', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
+ end
+
+ def test_render_layout_with_a_nested_render_layout_call_using_block_with_render_content
+ assert_equal %(Before (Foo!)\nBefore (Bar!)\n\n Content from inside layout!\n\nAfterpartial with layout\n\nAfter),
+ @view.render(:partial => 'test/partial_with_layout_block_content', :layout => 'test/layout_for_partial', :locals => { :name => 'Foo!'})
end
def test_render_with_nested_layout
@@ -252,6 +325,11 @@ module RenderTestCases
assert_equal %(\n<title>title</title>\n\n),
@view.render(:file => "test/layout_render_file.erb")
end
+
+ def test_render_layout_with_object
+ assert_equal %(<title>David</title>),
+ @view.render(:file => "test/layout_render_object.erb")
+ end
end
class CachedViewRenderTest < ActiveSupport::TestCase
@@ -303,7 +381,7 @@ class LazyViewRenderTest < ActiveSupport::TestCase
end
def test_render_utf8_template_with_incompatible_external_encoding
- with_external_encoding Encoding::SJIS do
+ with_external_encoding Encoding::SHIFT_JIS do
begin
result = @view.render(:file => "test/utf8.html.erb", :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
@@ -314,7 +392,7 @@ class LazyViewRenderTest < ActiveSupport::TestCase
end
def test_render_utf8_template_with_partial_with_incompatible_encoding
- with_external_encoding Encoding::SJIS do
+ with_external_encoding Encoding::SHIFT_JIS do
begin
result = @view.render(:file => "test/utf8_magic_with_bare_partial.html.erb", :layouts => "layouts/yield")
flunk 'Should have raised incompatible encoding error'
@@ -325,10 +403,11 @@ class LazyViewRenderTest < ActiveSupport::TestCase
end
def with_external_encoding(encoding)
- old, Encoding.default_external = Encoding.default_external, encoding
+ old = Encoding.default_external
+ silence_warnings { Encoding.default_external = encoding }
yield
ensure
- Encoding.default_external = old
+ silence_warnings { Encoding.default_external = old }
end
end
end