rails/actionview/test/template/template_error_test.rb
Guilherme Mansur 526a5eb10c Empty array instead of nil for source_extract
The source_extract method will return nil when it can't find the file name in
the backtrace, methods that consume this method expect an array and the nil ends
up causing type errors down the road like it happened here: #36341. This
patch refactors the source_extract method so that it returns an empty
array instead of nil when it can't find the source code.

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
2019-07-14 15:04:25 -04:00

54 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require "abstract_unit"
class TemplateErrorTest < ActiveSupport::TestCase
def test_provides_original_message
error = begin
raise Exception.new("original")
rescue Exception
raise ActionView::Template::Error.new("test") rescue $!
end
assert_equal "original", error.message
end
def test_provides_original_backtrace
error = begin
original_exception = Exception.new
original_exception.set_backtrace(%W[ foo bar baz ])
raise original_exception
rescue Exception
raise ActionView::Template::Error.new("test") rescue $!
end
assert_equal %W[ foo bar baz ], error.backtrace
end
def test_provides_useful_inspect
error = begin
raise Exception.new("original")
rescue Exception
raise ActionView::Template::Error.new("test") rescue $!
end
assert_equal "#<ActionView::Template::Error: original>", error.inspect
end
def test_annotated_source_code_returns_empty_array_if_source_cant_be_found
template = Class.new do
def identifier
"something"
end
end.new
error = begin
raise
rescue
raise ActionView::Template::Error.new(template) rescue $!
end
assert_equal [], error.annotated_source_code
end
end