diff options
| author | Stan Lo <stan001212@gmail.com> | 2023-08-11 19:44:48 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-08-11 18:44:52 +0000 |
| commit | 0781e55206d94079c15ab315fc082f49bf8bf780 (patch) | |
| tree | 16ee7c71b17b87ad166fd23b4afc31d40b88da94 /test | |
| parent | c173c637ab7e971a5b6c56deabe9e1a7c95667fb (diff) | |
[ruby/irb] Move assignment check to RubyLex
(https://github.com/ruby/irb/pull/670)
Since assignment check relies on tokenization with `Ripper`, it feels like
the responsibility of `RubyLex`. `Irb#eval_input` should simply get the result
when calling `each_top_level_statement` on `RubyLex`.
https://github.com/ruby/irb/commit/89d1adb3fd
Diffstat (limited to 'test')
| -rw-r--r-- | test/irb/test_context.rb | 53 | ||||
| -rw-r--r-- | test/irb/test_ruby_lex.rb | 54 |
2 files changed, 54 insertions, 53 deletions
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index 6ce0cb1228..5847df172e 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -202,59 +202,6 @@ module TestIRB assert_equal(true, @context.use_autocomplete?) end - def test_assignment_expression - input = TestInputMethod.new - irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) - [ - "foo = bar", - "@foo = bar", - "$foo = bar", - "@@foo = bar", - "::Foo = bar", - "a::Foo = bar", - "Foo = bar", - "foo.bar = 1", - "foo[1] = bar", - "foo += bar", - "foo -= bar", - "foo ||= bar", - "foo &&= bar", - "foo, bar = 1, 2", - "foo.bar=(1)", - "foo; foo = bar", - "foo; foo = bar; ;\n ;", - "foo\nfoo = bar", - ].each do |exp| - assert( - irb.assignment_expression?(exp), - "#{exp.inspect}: should be an assignment expression" - ) - end - - [ - "foo", - "foo.bar", - "foo[0]", - "foo = bar; foo", - "foo = bar\nfoo", - ].each do |exp| - refute( - irb.assignment_expression?(exp), - "#{exp.inspect}: should not be an assignment expression" - ) - end - end - - def test_assignment_expression_with_local_variable - input = TestInputMethod.new - irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) - code = "a /1;x=1#/" - refute(irb.assignment_expression?(code), "#{code}: should not be an assignment expression") - irb.context.workspace.binding.eval('a = 1') - assert(irb.assignment_expression?(code), "#{code}: should be an assignment expression") - refute(irb.assignment_expression?(""), "empty code should not be an assignment expression") - end - def test_echo_on_assignment input = TestInputMethod.new([ "a = 1\n", diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 910c595970..dbc8d560d7 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -813,6 +813,60 @@ module TestIRB assert_indent_level(code_with_embdoc.lines, expected) end + def test_assignment_expression + context = build_context + ruby_lex = RubyLex.new(context) + + [ + "foo = bar", + "@foo = bar", + "$foo = bar", + "@@foo = bar", + "::Foo = bar", + "a::Foo = bar", + "Foo = bar", + "foo.bar = 1", + "foo[1] = bar", + "foo += bar", + "foo -= bar", + "foo ||= bar", + "foo &&= bar", + "foo, bar = 1, 2", + "foo.bar=(1)", + "foo; foo = bar", + "foo; foo = bar; ;\n ;", + "foo\nfoo = bar", + ].each do |exp| + assert( + ruby_lex.assignment_expression?(exp), + "#{exp.inspect}: should be an assignment expression" + ) + end + + [ + "foo", + "foo.bar", + "foo[0]", + "foo = bar; foo", + "foo = bar\nfoo", + ].each do |exp| + refute( + ruby_lex.assignment_expression?(exp), + "#{exp.inspect}: should not be an assignment expression" + ) + end + end + + def test_assignment_expression_with_local_variable + context = build_context + ruby_lex = RubyLex.new(context) + 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") + end + private def build_context(local_variables = nil) |
