зеркало из https://github.com/github/ruby.git
[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
This commit is contained in:
Родитель
68ef97d788
Коммит
c5d6a483f5
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче