mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
assertions.rb: syntax check by iseq
* test/lib/test/unit/assertions.rb (assert_valid_syntax): use RubyVM::InstructionSequence.compile to get rid of executing the code, instead of catch&throw. sample/trick2015/kinaba/entry.rb no longer raises an Invalid return. * test/lib/test/unit/assertions.rb (assert_syntax_error): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
11e386cf6c
commit
6b5f927718
1 changed files with 6 additions and 14 deletions
|
@ -456,11 +456,7 @@ EOT
|
|||
alias pend skip
|
||||
|
||||
def assert_valid_syntax(code, fname = caller_locations(1, 1)[0], mesg = fname.to_s, verbose: nil)
|
||||
code = code.b
|
||||
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
|
||||
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
|
||||
}
|
||||
code.force_encoding(Encoding::UTF_8)
|
||||
code = code.dup.force_encoding(Encoding::UTF_8)
|
||||
verbose, $VERBOSE = $VERBOSE, verbose
|
||||
yield if defined?(yield)
|
||||
case
|
||||
|
@ -469,21 +465,17 @@ EOT
|
|||
when defined?(fname.path) && defined?(fname.lineno)
|
||||
fname, line = fname.path, fname.lineno
|
||||
else
|
||||
line = 0
|
||||
line = 1
|
||||
end
|
||||
assert_nothing_raised(SyntaxError, mesg) do
|
||||
assert_equal(:ok, catch {|tag| eval(code, binding, fname, line)}, mesg)
|
||||
RubyVM::InstructionSequence.compile(code, fname, fname, line)
|
||||
end
|
||||
ensure
|
||||
$VERBOSE = verbose
|
||||
end
|
||||
|
||||
def assert_syntax_error(code, error, fname = caller_locations(1, 1)[0], mesg = fname.to_s)
|
||||
code = code.b
|
||||
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
|
||||
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ng}\n"
|
||||
}
|
||||
code.force_encoding(Encoding::US_ASCII)
|
||||
code = code.dup.force_encoding(Encoding::US_ASCII)
|
||||
verbose, $VERBOSE = $VERBOSE, nil
|
||||
yield if defined?(yield)
|
||||
case
|
||||
|
@ -492,10 +484,10 @@ EOT
|
|||
when defined?(fname.path) && defined?(fname.lineno)
|
||||
fname, line = fname.path, fname.lineno
|
||||
else
|
||||
line = 0
|
||||
line = 1
|
||||
end
|
||||
e = assert_raise(SyntaxError, mesg) do
|
||||
catch {|tag| eval(code, binding, fname, line)}
|
||||
RubyVM::InstructionSequence.compile(code, fname, fname, line)
|
||||
end
|
||||
assert_match(error, e.message, mesg)
|
||||
e
|
||||
|
|
Loading…
Add table
Reference in a new issue