ruby-changes:74330
From: Stan <ko1@a...>
Date: Fri, 4 Nov 2022 01:32:39 +0900 (JST)
Subject: [ruby-changes:74330] c5d6a483f5 (master): [ruby/irb] Refactor RubyLex and its tests
https://git.ruby-lang.org/ruby.git/commit/?id=c5d6a483f5 From c5d6a483f5f771aa904ea85dad35a368ddf8047a Mon Sep 17 00:00:00 2001 From: Stan Lo <stan001212@g...> Date: Thu, 3 Nov 2022 16:32:10 +0000 Subject: [ruby/irb] Refactor RubyLex and its tests (https://github.com/ruby/irb/pull/427) * Make sure `RubyLex#set_input`'s context is always present in tests In real-world scenarios, the context should always be non-nil: https://github.com/ruby/irb/blob/master/lib/irb.rb#L489 So we should make sure our test setup reflects that. * Make context a required keyword Since in practice, `set_input`'s context should always be non-nil, its parameters should reflect that. And since `RubyLex#check_state` is only called by `#lex` and `#set_input`, both of which now always require context, we can assume its context should be non-nil too. https://github.com/ruby/irb/commit/1aeeb86203 --- lib/irb/ruby-lex.rb | 4 ++-- test/irb/test_ruby_lex.rb | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 54ea2a9e7b..544392228e 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -48,7 +48,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L48 end # io functions - def set_input(io, p = nil, context: nil, &block) + def set_input(io, p = nil, context:, &block) @io = io if @io.respond_to?(:check_termination) @io.check_termination do |code| @@ -216,7 +216,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L216 ltype = process_literal_type(tokens) indent = process_nesting_level(tokens) continue = process_continue(tokens) - lvars_code = self.class.generate_local_variables_assign_code(context&.local_variables || []) + lvars_code = self.class.generate_local_variables_assign_code(context.local_variables) code = "#{lvars_code}\n#{code}" if lvars_code code_block_open = check_code_block(code, tokens) [ltype, indent, continue, code_block_open] diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 1388d08962..5d66dc5bb0 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -24,13 +24,14 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L24 last_line_index = lines.length - 1 byte_pointer = lines.last.length + context = build_context + context.auto_indent_mode = true ruby_lex = RubyLex.new() io = MockIO_AutoIndent.new([lines, last_line_index, byte_pointer, add_new_line]) do |auto_indent| error_message = "Calculated the wrong number of spaces for:\n #{lines.join("\n")}" assert_equal(correct_space_count, auto_indent, error_message) end - ruby_lex.set_input(io) - context = OpenStruct.new(auto_indent_mode: true) + ruby_lex.set_input(io, context: context) ruby_lex.set_auto_indent(context) end @@ -48,11 +49,10 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L49 def ruby_lex_for_lines(lines, local_variables: []) ruby_lex = RubyLex.new() + + context = build_context(local_variables) io = proc{ lines.join("\n") } - ruby_lex.set_input(io, io) - unless local_variables.empty? - context = OpenStruct.new(local_variables: local_variables) - end + ruby_lex.set_input(io, io, context: context) ruby_lex.lex(context) ruby_lex end @@ -620,7 +620,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L620 ruby_lex.set_prompt do |ltype, indent, continue, line_no| '%03d:%01d:%1s:%s ' % [line_no, indent, ltype, continue ? '*' : '>'] end - ruby_lex.set_input(io) + context = build_context + ruby_lex.set_input(io, context: context) end def test_dyanmic_prompt @@ -697,5 +698,19 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_ruby_lex.rb#L698 assert_equal('<<A', string_literal&.tok) end end + + private + + def build_context(local_variables = nil) + workspace = IRB::WorkSpace.new + + if local_variables + local_variables.each do |n| + workspace.binding.local_variable_set(n, nil) + end + end + + IRB::Context.new(nil, workspace) + end end end -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/