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

Merge pull request #5865 from tiegz/minor_fixes

Catch nil.to_sym errors in partial_renderer, and raise ArgumentError instead
This commit is contained in:
Aaron Patterson 2012-04-20 16:23:27 -07:00
commit ed8108300b
2 changed files with 22 additions and 35 deletions

View file

@ -451,7 +451,7 @@ module ActionView
end end
def retrieve_variable(path) def retrieve_variable(path)
variable = @options[:as].try(:to_sym) || path[%r'_?(\w+)(\.\w+)*$', 1].to_sym variable = @options.fetch(:as) { path[%r'_?(\w+)(\.\w+)*$', 1] }.try(:to_sym)
variable_counter = :"#{variable}_counter" if @collection variable_counter = :"#{variable}_counter" if @collection
[variable, variable_counter] [variable, variable_counter]
end end

View file

@ -21,9 +21,7 @@ module RenderTestCases
end end
def test_render_without_options def test_render_without_options
@view.render() e = assert_raises(ArgumentError) { @view.render() }
flunk "Render did not raise ArgumentError"
rescue ArgumentError => e
assert_match "You invoked render but did not give any of :partial, :template, :inline, :file or :text option.", e.message assert_match "You invoked render but did not give any of :partial, :template, :inline, :file or :text option.", e.message
end end
@ -153,25 +151,26 @@ module RenderTestCases
end end
def test_render_partial_with_invalid_name def test_render_partial_with_invalid_name
@view.render(:partial => "test/200") e = assert_raises(ArgumentError) { @view.render(:partial => "test/200") }
flunk "Render did not raise ArgumentError"
rescue ArgumentError => e
assert_equal "The partial name (test/200) is not a valid Ruby identifier; " + assert_equal "The partial name (test/200) is not a valid Ruby identifier; " +
"make sure your partial name starts with a letter or underscore, " + "make sure your partial name starts with a letter or underscore, " +
"and is followed by any combinations of letters, numbers, or underscores.", e.message "and is followed by any combinations of letters, numbers, or underscores.", e.message
end
def test_render_partial_with_missing_filename
e = assert_raises(ArgumentError) { @view.render(:partial => "test/") }
assert_equal "The partial name (test/) is not a valid Ruby identifier; " +
"make sure your partial name starts with a letter or underscore, " +
"and is followed by any combinations of letters, numbers, or underscores.", e.message
end end
def test_render_partial_with_incompatible_object def test_render_partial_with_incompatible_object
@view.render(:partial => nil) e = assert_raises(ArgumentError) { @view.render(:partial => nil) }
flunk "Render did not raise ArgumentError"
rescue ArgumentError => e
assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.", e.message assert_equal "'#{nil.inspect}' is not an ActiveModel-compatible object. It must implement :to_partial_path.", e.message
end end
def test_render_partial_with_errors def test_render_partial_with_errors
@view.render(:partial => "test/raise") e = assert_raises(ActionView::Template::Error) { @view.render(:partial => "test/raise") }
flunk "Render did not raise Template::Error"
rescue ActionView::Template::Error => e
assert_match %r!method.*doesnt_exist!, e.message assert_match %r!method.*doesnt_exist!, e.message
assert_equal "", e.sub_template_message assert_equal "", e.sub_template_message
assert_equal "1", e.line_number assert_equal "1", e.line_number
@ -180,9 +179,7 @@ module RenderTestCases
end end
def test_render_sub_template_with_errors def test_render_sub_template_with_errors
@view.render(:template => "test/sub_template_raise") e = assert_raises(ActionView::Template::Error) { @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 assert_match %r!method.*doesnt_exist!, e.message
assert_equal "Trace of template inclusion: #{File.expand_path("#{FIXTURE_LOAD_PATH}/test/sub_template_raise.html.erb")}", e.sub_template_message 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 "1", e.line_number
@ -190,9 +187,7 @@ module RenderTestCases
end end
def test_render_file_with_errors def test_render_file_with_errors
@view.render(:file => File.expand_path("test/_raise", FIXTURE_LOAD_PATH)) e = assert_raises(ActionView::Template::Error) { @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_match %r!method.*doesnt_exist!, e.message
assert_equal "", e.sub_template_message assert_equal "", e.sub_template_message
assert_equal "1", e.line_number assert_equal "1", e.line_number
@ -274,7 +269,7 @@ module RenderTestCases
# TODO: The reason for this test is unclear, improve documentation # TODO: The reason for this test is unclear, improve documentation
def test_render_missing_xml_partial_and_raise_missing_template def test_render_missing_xml_partial_and_raise_missing_template
@view.formats = [:xml] @view.formats = [:xml]
assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") } assert_raises(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") }
ensure ensure
@view.formats = nil @view.formats = nil
end end
@ -325,7 +320,7 @@ module RenderTestCases
def test_render_ignores_templates_with_malformed_template_handlers def test_render_ignores_templates_with_malformed_template_handlers
ActiveSupport::Deprecation.silence do ActiveSupport::Deprecation.silence do
%w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name| %w(malformed malformed.erb malformed.html.erb malformed.en.html.erb).each do |name|
assert_raise(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") } assert_raises(ActionView::MissingTemplate) { @view.render(:file => "test/malformed/#{name}") }
end end
end end
end end
@ -450,23 +445,15 @@ class LazyViewRenderTest < ActiveSupport::TestCase
def test_render_utf8_template_with_incompatible_external_encoding def test_render_utf8_template_with_incompatible_external_encoding
with_external_encoding Encoding::SHIFT_JIS do with_external_encoding Encoding::SHIFT_JIS do
begin e = assert_raises(ActionView::Template::Error) { @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield") }
@view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield") assert_match 'Your template was not saved as valid Shift_JIS', e.original_exception.message
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
end end
end end
def test_render_utf8_template_with_partial_with_incompatible_encoding def test_render_utf8_template_with_partial_with_incompatible_encoding
with_external_encoding Encoding::SHIFT_JIS do with_external_encoding Encoding::SHIFT_JIS do
begin e = assert_raises(ActionView::Template::Error) { @view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield") }
@view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield") assert_match 'Your template was not saved as valid Shift_JIS', e.original_exception.message
flunk 'Should have raised incompatible encoding error'
rescue ActionView::Template::Error => error
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
end
end end
end end