2017-01-29 04:51:55 -05:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require 'test/unit'
|
|
|
|
require 'irb/ruby-lex'
|
2017-01-29 07:20:26 -05:00
|
|
|
require 'stringio'
|
2017-01-29 04:51:55 -05:00
|
|
|
|
|
|
|
module TestIRB
|
|
|
|
class TestRubyLex < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@scanner = RubyLex.new
|
|
|
|
end
|
|
|
|
|
2017-01-29 22:21:15 -05:00
|
|
|
def teardown
|
|
|
|
RubyLex.debug_level = 0
|
|
|
|
end
|
|
|
|
|
2017-01-29 04:51:55 -05:00
|
|
|
def test_set_input_proc
|
|
|
|
called = false
|
2017-01-29 07:20:26 -05:00
|
|
|
@scanner.set_input(nil) {called = true; nil}
|
|
|
|
@scanner.each_top_level_statement {}
|
2017-01-29 04:51:55 -05:00
|
|
|
assert(called)
|
|
|
|
end
|
2017-01-29 07:20:26 -05:00
|
|
|
|
|
|
|
def test_comment
|
|
|
|
assert_equal([["#\n", 1]], top_level_statement("#\n"))
|
|
|
|
end
|
|
|
|
|
2017-01-29 07:53:23 -05:00
|
|
|
def test_top_level_statement
|
|
|
|
result = top_level_statement("#{<<-"begin;"}#{<<~"end;"}")
|
|
|
|
begin;
|
|
|
|
begin
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
assert_equal([
|
|
|
|
["begin\n""end\n", 1],
|
|
|
|
["begin\n""end\n", 3],
|
|
|
|
],
|
|
|
|
result)
|
|
|
|
end
|
|
|
|
|
2017-01-29 07:53:24 -05:00
|
|
|
def test_immature_statement
|
|
|
|
src = "if false\n"
|
|
|
|
assert_equal([[src, 1]], top_level_statement(src))
|
|
|
|
end
|
|
|
|
|
2017-01-29 22:21:15 -05:00
|
|
|
def test_prompt
|
|
|
|
prompts = []
|
|
|
|
@scanner.set_prompt {|*a|
|
|
|
|
a << @scanner.instance_variable_get(:@lex_state)
|
|
|
|
unless prompts.last == a
|
|
|
|
prompts << a
|
|
|
|
end
|
|
|
|
}
|
2017-01-29 23:47:44 -05:00
|
|
|
src, lineno = "#{<<-"begin;"}#{<<~'end;'}", __LINE__+1
|
2017-01-29 22:21:15 -05:00
|
|
|
begin;
|
2017-01-29 22:57:46 -05:00
|
|
|
# #;# LTYPE:INDENT:CONTINUE
|
2017-01-29 23:47:44 -05:00
|
|
|
x #;# -:0:- # FIXME: a comment should not `continue'
|
2017-01-29 22:57:46 -05:00
|
|
|
x( #;# -:0:-
|
|
|
|
) #;# -:1:*
|
|
|
|
a \ #;# -:0:-
|
|
|
|
#;# -:0:*
|
|
|
|
a; #;# -:0:-
|
2017-01-29 23:47:44 -05:00
|
|
|
a #;# -:0:- # FIXME: a semicolon should not `continue'
|
2017-01-29 22:57:46 -05:00
|
|
|
#;# -:0:-
|
2017-01-29 23:47:44 -05:00
|
|
|
a #;# -:0:- # FIXME: an empty line should not `continue'
|
2017-01-29 22:57:46 -05:00
|
|
|
a = #;# -:0:-
|
|
|
|
' #;# -:0:*
|
|
|
|
' #;# ':0:*
|
|
|
|
if false or #;# -:0:-
|
|
|
|
true #;# -:1:*
|
|
|
|
a #;# -:1:-
|
|
|
|
" #;# -:1:-
|
|
|
|
" #;# ":1:-
|
|
|
|
begin #;# -:1:-
|
2017-01-29 23:47:44 -05:00
|
|
|
a #;# -:2:- # FIXME: the first line should not be `continue'd
|
2017-01-29 22:57:46 -05:00
|
|
|
a #;# -:2:-
|
|
|
|
end #;# -:2:-
|
|
|
|
else #;# -:1:-
|
2017-01-29 23:47:44 -05:00
|
|
|
nil #;# -:1:- # FIXME: just after `else' should not be `continue'd
|
2017-01-29 22:57:46 -05:00
|
|
|
end #;# -:1:-
|
2017-01-29 22:21:15 -05:00
|
|
|
end;
|
2017-01-29 22:57:46 -05:00
|
|
|
top_level_statement(src.gsub(/[ \t]*#;#.*/, ''))
|
|
|
|
src.each_line.with_index(1) do |line, i|
|
|
|
|
p = prompts.shift
|
2017-01-29 23:47:44 -05:00
|
|
|
next unless /#;#\s*(?:-|(?<ltype>\S)):(?<indent>\d+):(?:(?<cont>\*)|-)(?:.*FIXME:(?<fixme>.*))?/ =~ line
|
|
|
|
indent = indent.to_i
|
|
|
|
cont = (fixme && /`continue'/.match?(fixme)) ^ cont
|
|
|
|
assert_equal([ltype, indent, cont, i], p[0..3], "#{lineno+i}:#{p[4]}: #{line}")
|
2017-01-29 22:57:46 -05:00
|
|
|
end
|
2017-01-29 22:21:15 -05:00
|
|
|
end
|
|
|
|
|
2017-01-29 07:20:26 -05:00
|
|
|
def top_level_statement(lines)
|
|
|
|
input = InputLines.new(lines, "r")
|
|
|
|
scanned = []
|
|
|
|
@scanner.set_input(input)
|
|
|
|
@scanner.each_top_level_statement {|*e|
|
|
|
|
scanned << e
|
|
|
|
yield(*e) if defined?(yield)
|
|
|
|
}
|
|
|
|
scanned
|
|
|
|
end
|
|
|
|
|
|
|
|
class InputLines < StringIO
|
|
|
|
alias encoding external_encoding
|
|
|
|
end
|
2017-01-29 04:51:55 -05:00
|
|
|
end
|
|
|
|
end
|