diff options
| author | Stan Lo <stan001212@gmail.com> | 2023-10-04 13:13:27 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-10-04 12:13:33 +0000 |
| commit | b43cc51dcad9859ea6c54cb4f03105c8511582de (patch) | |
| tree | 9d7080c47f6c4cb260debaa0deb5d1352fcf6d2f /test | |
| parent | 94bcae1b2a2840c1a8031ef936870be693425551 (diff) | |
[ruby/irb] Clear all context usages in RubyLex
(https://github.com/ruby/irb/pull/684)
After this change, `RubyLex` will not interact with `Context` directly
in any way. This decoupling has a few benefits:
- It makes `RubyLex` easier to test as it no longer has a dependency on
`Context`. We can see this from the removal of `build_context` from
`test_ruby_lex.rb`.
- It will make `RubyLex` easier to understand as it will not be affected
by state changes in `Context` objects.
- It allows `RubyLex` to be used in places where `Context` is not available.
https://github.com/ruby/irb/commit/d5b262a076
Diffstat (limited to 'test')
| -rw-r--r-- | test/irb/test_irb.rb | 2 | ||||
| -rw-r--r-- | test/irb/test_ruby_lex.rb | 36 |
2 files changed, 10 insertions, 28 deletions
diff --git a/test/irb/test_irb.rb b/test/irb/test_irb.rb index 4e5a94b1be..4870f35f39 100644 --- a/test/irb/test_irb.rb +++ b/test/irb/test_irb.rb @@ -584,7 +584,7 @@ module TestIRB def assert_indent_level(lines, expected) code = lines.map { |l| "#{l}\n" }.join # code should end with "\n" - _tokens, opens, _ = @irb.scanner.check_code_state(code) + _tokens, opens, _ = @irb.scanner.check_code_state(code, local_variables: []) indent_level = @irb.scanner.calc_indent_level(opens) error_message = "Calculated the wrong number of indent level for:\n #{lines.join("\n")}" assert_equal(expected, indent_level, error_message) diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index b9e55c7330..5cfd81dbe8 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -149,8 +149,7 @@ module TestIRB end def test_assignment_expression - context = build_context - ruby_lex = IRB::RubyLex.new(context) + ruby_lex = IRB::RubyLex.new [ "foo = bar", @@ -173,7 +172,7 @@ module TestIRB "foo\nfoo = bar", ].each do |exp| assert( - ruby_lex.assignment_expression?(exp), + ruby_lex.assignment_expression?(exp, local_variables: []), "#{exp.inspect}: should be an assignment expression" ) end @@ -186,20 +185,18 @@ module TestIRB "foo = bar\nfoo", ].each do |exp| refute( - ruby_lex.assignment_expression?(exp), + ruby_lex.assignment_expression?(exp, local_variables: []), "#{exp.inspect}: should not be an assignment expression" ) end end def test_assignment_expression_with_local_variable - context = build_context - ruby_lex = IRB::RubyLex.new(context) + ruby_lex = IRB::RubyLex.new code = "a /1;x=1#/" - refute(ruby_lex.assignment_expression?(code), "#{code}: should not be an assignment expression") - context.workspace.binding.eval('a = 1') - assert(ruby_lex.assignment_expression?(code), "#{code}: should be an assignment expression") - refute(ruby_lex.assignment_expression?(""), "empty code should not be an assignment expression") + refute(ruby_lex.assignment_expression?(code, local_variables: []), "#{code}: should not be an assignment expression") + assert(ruby_lex.assignment_expression?(code, local_variables: [:a]), "#{code}: should be an assignment expression") + refute(ruby_lex.assignment_expression?("", local_variables: [:a]), "empty code should not be an assignment expression") end def test_initialising_the_old_top_level_ruby_lex @@ -211,20 +208,6 @@ module TestIRB private - def build_context(local_variables = nil) - IRB.init_config(nil) - workspace = IRB::WorkSpace.new(TOPLEVEL_BINDING.dup) - - if local_variables - local_variables.each do |n| - workspace.binding.local_variable_set(n, nil) - end - end - - IRB.conf[:VERBOSE] = false - IRB::Context.new(nil, workspace, TestInputMethod.new) - end - def assert_indent_level(lines, expected, local_variables: []) indent_level, _continue, _code_block_open = check_state(lines, local_variables: local_variables) error_message = "Calculated the wrong number of indent level for:\n #{lines.join("\n")}" @@ -244,10 +227,9 @@ module TestIRB end def check_state(lines, local_variables: []) - context = build_context(local_variables) code = lines.map { |l| "#{l}\n" }.join # code should end with "\n" - ruby_lex = IRB::RubyLex.new(context) - tokens, opens, terminated = ruby_lex.check_code_state(code) + ruby_lex = IRB::RubyLex.new + tokens, opens, terminated = ruby_lex.check_code_state(code, local_variables: local_variables) indent_level = ruby_lex.calc_indent_level(opens) continue = ruby_lex.should_continue?(tokens) [indent_level, continue, !terminated] |
