Skip to content

Commit

Permalink
Update NestingParser test
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jun 12, 2023
1 parent 70da8ac commit 16be166
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions test/irb/test_nesting_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parse_by_line(code)
IRB::NestingParser.parse_by_line(RubyLex.ripper_lex_without_warning(code))
end

def test_scan
def test_open_tokens
code = <<~'EOS'
class A
def f
Expand Down Expand Up @@ -78,7 +78,11 @@ def f() = 1
end
EOS
line_results = parse_by_line(code)
line_results[1...-1].each {|result| assert_equal(['class'], result[2].map(&:tok)) }
assert_equal(code.lines.size, line_results.size)
class_open, *inner_line_results, class_close = line_results
assert_equal(['class'], class_open[2].map(&:tok))
inner_line_results.each {|result| assert_equal(['class'], result[2].map(&:tok)) }
assert_equal([], class_close[2].map(&:tok))
end

def test_multiline_string
Expand All @@ -93,13 +97,14 @@ def test_multiline_string
A
EOS
line_results = parse_by_line(code)
assert_equal(code.lines.size, line_results.size)
string_content_line, string_opens = line_results[1]
assert_equal("\naaa\nbbb\n", string_content_line.map(&:first).map(&:tok).join)
assert_equal("aaa\n", string_content_line.map(&:last).join)
assert_equal("\naaa\nbbb\n", string_content_line.first.first.tok)
assert_equal("aaa\n", string_content_line.first.last)
assert_equal(['"'], string_opens.map(&:tok))
heredoc_content_line, heredoc_opens = line_results[6]
assert_equal("aaa\nbbb\n", heredoc_content_line.map(&:first).map(&:tok).join)
assert_equal("bbb\n", heredoc_content_line.map(&:last).join)
assert_equal("aaa\nbbb\n", heredoc_content_line.first.first.tok)
assert_equal("bbb\n", heredoc_content_line.first.last)
assert_equal(['<<A'], heredoc_opens.map(&:tok))
_line, _prev_opens, next_opens, _min_depth = line_results.last
assert_equal([], next_opens)
Expand Down Expand Up @@ -230,7 +235,7 @@ def test_for_in
here
end
EOS
line_results = parse_by_line(code).select { |tokens,| tokens.map(&:last).include? 'here' }
line_results = parse_by_line(code).select { |tokens,| tokens.map(&:last).include?('here') }
assert_equal(7, line_results.size)
line_results.each do |_tokens, _prev_opens, next_opens, _min_depth|
assert_equal(['for'], next_opens.map(&:tok))
Expand Down Expand Up @@ -268,7 +273,7 @@ def test_while_until
EOS
%w[while until].each do |keyword|
code = base_code.gsub('while_or_until', keyword)
line_results = parse_by_line(code).select { |tokens,| tokens.map(&:last).include? 'here' }
line_results = parse_by_line(code).select { |tokens,| tokens.map(&:last).include?('here') }
assert_equal(7, line_results.size)
line_results.each do |_tokens, _prev_opens, next_opens, _min_depth|
assert_equal([keyword], next_opens.map(&:tok) )
Expand Down

0 comments on commit 16be166

Please sign in to comment.