diff options
author | tomoya ishida <tomoyapenguin@gmail.com> | 2023-01-03 04:25:07 +0900 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-01-02 19:25:11 +0000 |
commit | 34f8ca1714a91e4e051ef80f22df2d0e5e0acfae (patch) | |
tree | 49f02e987d273b90399ab2c66dc61f21b9342e3d | |
parent | 5be0d42d2c4dc765230c76738289560f9ee37f09 (diff) | |
download | ruby-34f8ca1714a91e4e051ef80f22df2d0e5e0acfae.tar.gz |
[ruby/irb] Fix prompt and code mismatch
(https://github.com/ruby/irb/pull/386)
* fix prompt and code mismatch
* Add test for prompt and code mismatch bug
https://github.com/ruby/irb/commit/a5765d8177
Co-authored-by: Stan Lo <stan001212@gmail.com>
-rw-r--r-- | lib/irb/ruby-lex.rb | 3 | ||||
-rw-r--r-- | test/irb/test_ruby_lex.rb | 32 |
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 04d9bceb07..c3171f486c 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -96,7 +96,8 @@ class RubyLex if t.tok.include?("\n") t_str = t.tok t_str.each_line("\n") do |s| - code << s << "\n" + code << s + next unless s.include?("\n") ltype, indent, continue, code_block_open = check_state(code, partial_tokens, context: context) result << @prompt.call(ltype, indent, continue || code_block_open, @line_no + line_num_offset) line_num_offset += 1 diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 3caa036ce5..1c1ce92e6a 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -661,6 +661,38 @@ module TestIRB assert_dynamic_prompt(lines, expected_prompt_list) end + def test_dyanmic_prompt_with_double_newline_braking_code + input_with_prompt = [ + PromptRow.new('001:1: :* ', %q(if true)), + PromptRow.new('002:1: :* ', %q(%)), + PromptRow.new('003:1: :* ', %q(;end)), + PromptRow.new('004:1: :* ', %q(;hello)), + PromptRow.new('005:0: :> ', %q(end)), + ] + + lines = input_with_prompt.map(&:content) + expected_prompt_list = input_with_prompt.map(&:prompt) + assert_dynamic_prompt(lines, expected_prompt_list) + end + + def test_dyanmic_prompt_with_multiline_literal + input_with_prompt = [ + PromptRow.new('001:1: :* ', %q(if true)), + PromptRow.new('002:1:]:* ', %q( %w[)), + PromptRow.new('003:1:]:* ', %q( a)), + PromptRow.new('004:1: :* ', %q( ])), + PromptRow.new('005:1: :* ', %q( b)), + PromptRow.new('006:1:]:* ', %q( %w[)), + PromptRow.new('007:1:]:* ', %q( c)), + PromptRow.new('008:1: :* ', %q( ])), + PromptRow.new('009:0: :> ', %q(end)), + ] + + lines = input_with_prompt.map(&:content) + expected_prompt_list = input_with_prompt.map(&:prompt) + assert_dynamic_prompt(lines, expected_prompt_list) + end + def test_dyanmic_prompt_with_blank_line input_with_prompt = [ PromptRow.new('001:0:]:* ', %q(%w[)), |