summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
authorStan Lo <stan001212@gmail.com>2023-01-10 20:43:33 +0000
committergit <svn-admin@ruby-lang.org>2023-01-14 09:19:09 +0000
commitcb9b885e78bb87195d483df1afedf58d0bb81e41 (patch)
tree1ba732ebb576213869f099b5bcb1b1d33620de16 /test/irb
parent2082ba7c69c1d38508bfa549df3f2980cf8d066d (diff)
downloadruby-cb9b885e78bb87195d483df1afedf58d0bb81e41.tar.gz
[ruby/irb] Store context in RubyLex
Some background for this refactor: 1. Through a RubyLex instance's lifetime, the context passed to its methods should be the same. Given that `Context` is only initialised in `Irb#initialize`, this should be true. 2. When `RubyLex` is initialised, the context object should be accessible. This is also true in all 3 of `RubyLex.new`'s invocations. With the above observations, we should be able to store the context in `RubyLex` as an instance variable. And doing so will make `RubyLex`'s instance methods easier to use and maintain. https://github.com/ruby/irb/commit/5c8d3df2df
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_ruby_lex.rb31
1 files changed, 17 insertions, 14 deletions
diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb
index 8c8aa74079..e4dea3d662 100644
--- a/test/irb/test_ruby_lex.rb
+++ b/test/irb/test_ruby_lex.rb
@@ -36,13 +36,13 @@ module TestIRB
context = build_context
context.auto_indent_mode = true
- ruby_lex = RubyLex.new()
+ ruby_lex = RubyLex.new(context)
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: context)
- ruby_lex.set_auto_indent(context)
+ ruby_lex.set_input(io)
+ ruby_lex.set_auto_indent
end
def assert_nesting_level(lines, expected, local_variables: [])
@@ -58,14 +58,14 @@ module TestIRB
end
def ruby_lex_for_lines(lines, local_variables: [])
- ruby_lex = RubyLex.new()
-
context = build_context(local_variables)
+ ruby_lex = RubyLex.new(context)
+
io = proc{ lines.join("\n") }
- ruby_lex.set_input(io, context: context) do
+ ruby_lex.set_input(io) do
lines.join("\n")
end
- ruby_lex.lex(context)
+ ruby_lex.lex
ruby_lex
end
@@ -633,7 +633,8 @@ module TestIRB
def assert_dynamic_prompt(lines, expected_prompt_list)
pend if RUBY_ENGINE == 'truffleruby'
- ruby_lex = RubyLex.new()
+ context = build_context
+ ruby_lex = RubyLex.new(context)
io = MockIO_DynamicPrompt.new(lines) do |prompt_list|
error_message = <<~EOM
Expected dynamic prompt:
@@ -647,8 +648,7 @@ module TestIRB
ruby_lex.set_prompt do |ltype, indent, continue, line_no|
'%03d:%01d:%1s:%s ' % [line_no, indent, ltype, continue ? '*' : '>']
end
- context = build_context
- ruby_lex.set_input(io, context: context)
+ ruby_lex.set_input(io)
end
def test_dyanmic_prompt
@@ -751,9 +751,10 @@ module TestIRB
end
def test_unterminated_heredoc_string_literal
+ context = build_context
['<<A;<<B', "<<A;<<B\n", "%W[\#{<<A;<<B", "%W[\#{<<A;<<B\n"].each do |code|
tokens = RubyLex.ripper_lex_without_warning(code)
- string_literal = RubyLex.new.check_string_literal(tokens)
+ string_literal = RubyLex.new(context).check_string_literal(tokens)
assert_equal('<<A', string_literal&.tok)
end
end
@@ -779,8 +780,9 @@ module TestIRB
p(
)
EOC
+ context = build_context
[reference_code, code_with_heredoc, code_with_embdoc].each do |code|
- lex = RubyLex.new
+ lex = RubyLex.new(context)
lines = code.lines
lex.instance_variable_set('@tokens', RubyLex.ripper_lex_without_warning(code))
assert_equal 2, lex.check_corresponding_token_depth(lines, lines.size)
@@ -788,7 +790,7 @@ module TestIRB
end
def test_find_prev_spaces_with_multiline_literal
- lex = RubyLex.new
+ lex = RubyLex.new(build_context)
reference_code = <<~EOC.chomp
if true
1
@@ -813,8 +815,9 @@ module TestIRB
world
end
EOC
+ context = build_context
[reference_code, code_with_percent_string, code_with_quoted_string].each do |code|
- lex = RubyLex.new
+ lex = RubyLex.new(context)
lex.instance_variable_set('@tokens', RubyLex.ripper_lex_without_warning(code))
prev_spaces = (1..code.lines.size).map { |index| lex.find_prev_spaces index }
assert_equal [0, 2, 2, 2, 2, 0], prev_spaces