erb: lineno and location setters

* lib/erb.rb (ERB#lineno): accessor for line number to eval.
* lib/erb.rb (ERB#location=): setter of file name and line number.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48785 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-12-12 10:31:47 +00:00
parent 4d3677e14b
commit e82f4195d4
3 changed files with 37 additions and 2 deletions

View File

@ -1,3 +1,9 @@
Fri Dec 12 19:31:44 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/erb.rb (ERB#lineno): accessor for line number to eval.
* lib/erb.rb (ERB#location=): setter of file name and line number.
Fri Dec 12 13:09:13 2014 Koichi Sasada <ko1@atdot.net> Fri Dec 12 13:09:13 2014 Koichi Sasada <ko1@atdot.net>
* gc.c (gc_latest_gc_info): return :state field to show current * gc.c (gc_latest_gc_info): return :state field to show current

View File

@ -799,6 +799,7 @@ class ERB
set_eoutvar(compiler, eoutvar) set_eoutvar(compiler, eoutvar)
@src, @enc = *compiler.compile(str) @src, @enc = *compiler.compile(str)
@filename = nil @filename = nil
@lineno = 0
end end
## ##
@ -815,6 +816,15 @@ class ERB
# is run # is run
attr_accessor :filename attr_accessor :filename
# The optional _lineno_ argument passed to Kernel#eval when the ERB code
# is run
attr_accessor :lineno
def location=((filename, lineno))
@filename = filename
@lineno = lineno if lineno
end
# #
# Can be used to set _eoutvar_ as described in ERB::new. It's probably # Can be used to set _eoutvar_ as described in ERB::new. It's probably
# easier to just use the constructor though, since calling this method # easier to just use the constructor though, since calling this method
@ -844,10 +854,10 @@ class ERB
if @safe_level if @safe_level
proc { proc {
$SAFE = @safe_level $SAFE = @safe_level
eval(@src, b, (@filename || '(erb)'), 0) eval(@src, b, (@filename || '(erb)'), @lineno)
}.call }.call
else else
eval(@src, b, (@filename || '(erb)'), 0) eval(@src, b, (@filename || '(erb)'), @lineno)
end end
end end

View File

@ -39,6 +39,25 @@ class TestERB < Test::Unit::TestCase
assert_match(/\Atest filename:1\b/, e.backtrace[0]) assert_match(/\Atest filename:1\b/, e.backtrace[0])
end end
def test_with_filename_lineno
erb = ERB.new("<% raise ::TestERB::MyError %>")
erb.filename = "test filename"
erb.lineno = 100
e = assert_raise(MyError) {
erb.result
}
assert_match(/\Atest filename:101\b/, e.backtrace[0])
end
def test_with_location
erb = ERB.new("<% raise ::TestERB::MyError %>")
erb.location = ["test filename", 200]
e = assert_raise(MyError) {
erb.result
}
assert_match(/\Atest filename:201\b/, e.backtrace[0])
end
def test_html_escape def test_html_escape
assert_equal(" !&quot;\#$%&amp;&#39;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", assert_equal(" !&quot;\#$%&amp;&#39;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
ERB::Util.html_escape(" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")) ERB::Util.html_escape(" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"))