mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Store all kinds of syntax errors [Bug #17345]
This commit is contained in:
parent
1df3896382
commit
f5ca3ff4db
2 changed files with 32 additions and 4 deletions
|
@ -191,7 +191,9 @@ class Ripper
|
|||
def on_error(mesg)
|
||||
@errors.push Elem.new([lineno(), column()], __callee__, token(), state(), mesg)
|
||||
end
|
||||
alias on_parse_error on_error
|
||||
PARSER_EVENTS.grep(/_error\z/) do |e|
|
||||
alias_method "on_#{e}", :on_error
|
||||
end
|
||||
alias compile_error on_error
|
||||
|
||||
(SCANNER_EVENTS.map {|event|:"on_#{event}"} - private_instance_methods(false)).each do |event|
|
||||
|
|
|
@ -146,15 +146,41 @@ class TestRipper::Lexer < Test::Unit::TestCase
|
|||
assert_equal [[1, 17], :on_embexpr_end, "}", state(:EXPR_ARG)], token
|
||||
end
|
||||
|
||||
BAD_CODE = {
|
||||
parse_error: 'def req(true) end',
|
||||
assign_error: 'begin; nil = 1; end',
|
||||
alias_error: 'begin; alias $x $1; end',
|
||||
class_name_error: 'class bad; end',
|
||||
param_error: 'def req(@a) end',
|
||||
}
|
||||
|
||||
def test_raise_errors_keyword
|
||||
assert_raise(SyntaxError) { Ripper.tokenize('def req(true) end', raise_errors: true) }
|
||||
all_assertions do |all|
|
||||
BAD_CODE.each do |err, code|
|
||||
all.for(err) do
|
||||
assert_raise(SyntaxError) { Ripper.tokenize(code, raise_errors: true) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_tokenize_with_syntax_error
|
||||
assert_equal "end", Ripper.tokenize("def req(true) end").last
|
||||
all_assertions do |all|
|
||||
BAD_CODE.each do |err, code|
|
||||
all.for(err) do
|
||||
assert_equal "end", Ripper.tokenize(code).last
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_lex_with_syntax_error
|
||||
assert_equal [[1, 14], :on_kw, "end", state(:EXPR_END)], Ripper.lex("def req(true) end").last
|
||||
all_assertions do |all|
|
||||
BAD_CODE.each do |err, code|
|
||||
all.for(err) do
|
||||
assert_equal [[1, code.size-3], :on_kw, "end", state(:EXPR_END)], Ripper.lex(code).last
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue