From 6446e84c1a0d72ecdba1046c3e15b4467eae9e5b Mon Sep 17 00:00:00 2001 From: Lucas Uyezu Date: Thu, 2 Aug 2012 21:23:06 -0300 Subject: Indentation should consider line number character count. --- actionpack/lib/action_view/template/error.rb | 4 ++-- actionpack/test/fixtures/test/_raise_indentation.html.erb | 13 +++++++++++++ actionpack/test/template/render_test.rb | 9 +++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 actionpack/test/fixtures/test/_raise_indentation.html.erb diff --git a/actionpack/lib/action_view/template/error.rb b/actionpack/lib/action_view/template/error.rb index d8258f7b11..f2bef4bded 100644 --- a/actionpack/lib/action_view/template/error.rb +++ b/actionpack/lib/action_view/template/error.rb @@ -84,13 +84,13 @@ module ActionView start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min - indent = ' ' * indentation + indent = end_on_line.to_s.size + indentation line_counter = start_on_line return unless source_code = source_code[start_on_line..end_on_line] source_code.sum do |line| line_counter += 1 - "#{indent}#{line_counter}: #{line}\n" + "%#{indent}s: %s\n" % [line_counter, line] end end diff --git a/actionpack/test/fixtures/test/_raise_indentation.html.erb b/actionpack/test/fixtures/test/_raise_indentation.html.erb new file mode 100644 index 0000000000..f9a93728fe --- /dev/null +++ b/actionpack/test/fixtures/test/_raise_indentation.html.erb @@ -0,0 +1,13 @@ +

First paragraph

+

Second paragraph

+

Third paragraph

+

Fourth paragraph

+

Fifth paragraph

+

Sixth paragraph

+

Seventh paragraph

+

Eight paragraph

+

Ninth paragraph

+

Tenth paragraph

+<%= raise "error here!" %> +

Eleventh paragraph

+

Twelfth paragraph

\ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 164b8b9fa1..b26354e7cc 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -203,6 +203,15 @@ module RenderTestCases assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name end + def test_render_error_indentation + e = assert_raises(ActionView::Template::Error) { @view.render(:partial => "test/raise_indentation") } + error_lines = e.annoted_source_code.split("\n") + assert_match %r!error\shere!, e.message + assert_equal "11", e.line_number + assert_equal " 9:

Ninth paragraph

", error_lines.second + assert_equal " 10:

Tenth paragraph

", error_lines.third + end + def test_render_sub_template_with_errors e = assert_raises(ActionView::Template::Error) { @view.render(:template => "test/sub_template_raise") } assert_match %r!method.*doesnt_exist!, e.message -- cgit v1.2.3