diff options
| author | tomoya ishida <tomoyapenguin@gmail.com> | 2022-10-18 14:44:04 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2022-10-18 05:44:07 +0000 |
| commit | a09f764ce52838a363b006ea434287eca431dbae (patch) | |
| tree | 8da2c8147cb80ea8caf9484bdf7a1d5915e30b06 /test/irb/test_ruby_lex.rb | |
| parent | 344e6c915f41d99df024c7e90403baca0d5213a5 (diff) | |
[ruby/irb] Always use local variables in current context to parse code (https://github.com/ruby/irb/pull/397)
* Use local_variables for colorize, code_block_open check, nesting_level and assignment_expression check
* Check if expression is an assignment BEFORE evaluating it. evaluate might define new localvars and change result of assignment_expression?
* Add local_variables dependent code test
* pend local variable dependent test on truffleruby
code_block_open is not working on truffleruby
* Always pass context to RubyLex#lex
* Rename local_variable_assign_code generator method name
* Add assignment expression truncate test
* Add Context#local_variables and make generate_local_variables_assign_code more simple
* Update lib/irb/input-method.rb
Co-authored-by: Stan Lo <stan001212@gmail.com>
* Add a comment why assignment expression check should be done before evaluate
https://github.com/ruby/irb/commit/c8b3877281
Co-authored-by: Stan Lo <stan001212@gmail.com>
Co-authored-by: Takashi Kokubun <takashikkbn@gmail.com>
Diffstat (limited to 'test/irb/test_ruby_lex.rb')
| -rw-r--r-- | test/irb/test_ruby_lex.rb | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index beda53fc89..1388d08962 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -34,13 +34,27 @@ module TestIRB ruby_lex.set_auto_indent(context) end - def assert_nesting_level(lines, expected) + def assert_nesting_level(lines, expected, local_variables: []) + ruby_lex = ruby_lex_for_lines(lines, local_variables: local_variables) + error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}" + assert_equal(expected, ruby_lex.instance_variable_get(:@indent), error_message) + end + + def assert_code_block_open(lines, expected, local_variables: []) + ruby_lex = ruby_lex_for_lines(lines, local_variables: local_variables) + error_message = "Wrong result of code_block_open for:\n #{lines.join("\n")}" + assert_equal(expected, ruby_lex.instance_variable_get(:@code_block_open), error_message) + end + + def ruby_lex_for_lines(lines, local_variables: []) ruby_lex = RubyLex.new() io = proc{ lines.join("\n") } ruby_lex.set_input(io, io) - ruby_lex.lex - error_message = "Calculated the wrong number of nesting level for:\n #{lines.join("\n")}" - assert_equal(expected, ruby_lex.instance_variable_get(:@indent), error_message) + unless local_variables.empty? + context = OpenStruct.new(local_variables: local_variables) + end + ruby_lex.lex(context) + ruby_lex end def test_auto_indent @@ -514,6 +528,15 @@ module TestIRB end end + def test_local_variables_dependent_code + pend if RUBY_ENGINE == 'truffleruby' + lines = ["a /1#/ do", "2"] + assert_nesting_level(lines, 1) + assert_code_block_open(lines, true) + assert_nesting_level(lines, 0, local_variables: ['a']) + assert_code_block_open(lines, false, local_variables: ['a']) + end + def test_heredoc_with_indent input_with_correct_indents = [ Row.new(%q(<<~Q), 0, 0, 0), |
