diff options
author | Stan Lo <stan001212@gmail.com> | 2022-11-03 16:32:10 +0000 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-11-03 16:32:22 +0000 |
commit | c5d6a483f5f771aa904ea85dad35a368ddf8047a (patch) | |
tree | db87ec96f59ce622276fb73931351a44d7b5bb46 | |
parent | 68ef97d788cf8bff42d981bda41cd26128220740 (diff) | |
download | ruby-c5d6a483f5f771aa904ea85dad35a368ddf8047a.tar.gz |
[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
-rw-r--r-- | lib/irb/ruby-lex.rb | 4 | ||||
-rw-r--r-- | 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 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 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 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 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 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 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 |