1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/template/render_test.rb

131 lines
5 KiB
Ruby

require 'abstract_unit'
require 'controller/fake_models'
class ViewRenderTest < Test::Unit::TestCase
def setup
@assigns = { :secret => 'in the sauce' }
@view = ActionView::Base.new([FIXTURE_LOAD_PATH], @assigns)
end
def test_render_file
assert_equal "Hello world!", @view.render("test/hello_world.erb")
end
def test_render_file_not_using_full_path
assert_equal "Hello world!", @view.render(:file => "test/hello_world.erb")
end
def test_render_file_without_specific_extension
assert_equal "Hello world!", @view.render("test/hello_world")
end
def test_render_file_with_full_path
template_path = File.join(File.dirname(__FILE__), '../fixtures/test/hello_world.erb')
assert_equal "Hello world!", @view.render(:file => template_path)
end
def test_render_file_with_instance_variables
assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_ivar.erb")
end
def test_render_file_with_locals
locals = { :secret => 'in the sauce' }
assert_equal "The secret is in the sauce\n", @view.render("test/render_file_with_locals.erb", locals)
end
def test_render_file_not_using_full_path_with_dot_in_path
assert_equal "The secret is in the sauce\n", @view.render("test/dot.directory/render_file_with_ivar")
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
def test_render_partial
assert_equal "only partial", @view.render(:partial => "test/partial_only")
end
def test_render_partial_with_errors
assert_raise(ActionView::TemplateError) { @view.render(:partial => "test/raise") }
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
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_without_as
assert_equal "local_inspector,local_inspector_counter,object",
@view.render(:partial => "test/local_inspector", :collection => [ Customer.new("mary") ])
end
# 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_js_partial_and_fallback_to_erb_layout
@view.template_format = :js
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
@view.template_format = :xml
assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
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
assert_equal "Hello, World!", @view.render(:inline => "Hello, World!", :type => :foo)
end
class CustomHandler < ActionView::TemplateHandler
def render(template, local_assigns)
[template.source, local_assigns].inspect
end
end
def test_render_inline_with_custom_type
ActionView::Template.register_template_handler :foo, CustomHandler
assert_equal '["Hello, World!", {}]', @view.render(:inline => "Hello, World!", :type => :foo)
end
def test_render_inline_with_locals_and_custom_type
ActionView::Template.register_template_handler :foo, CustomHandler
assert_equal '["Hello, <%= name %>!", {:name=>"Josh"}]', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
end
class CompilableCustomHandler < ActionView::TemplateHandler
include ActionView::TemplateHandlers::Compilable
def compile(template)
"@output_buffer = ''\n" +
"@output_buffer << 'source: #{template.source.inspect}'\n"
end
end
def test_render_inline_with_compilable_custom_type
ActionView::Template.register_template_handler :foo, CompilableCustomHandler
assert_equal 'source: "Hello, World!"', @view.render(:inline => "Hello, World!", :type => :foo)
end
def test_render_inline_with_locals_and_compilable_custom_type
ActionView::Template.register_template_handler :foo, CompilableCustomHandler
assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
end
end