mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 13:04:13 +02:00

When an implicit array is used in a write, is causes the whole
expression to become a statement. For example:
```ruby
a = *b
a = 1, 2, 3
```
Even though these expressions are exactly equivalent to their
explicit array counterparts:
```ruby
a = [*b]
a = [1, 2, 3]
```
As such, these expressions cannot be joined with other expressions
by operators or modifiers except if, unless, while, until, or
rescue.
7cd2407272
59 lines
1.5 KiB
Ruby
59 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "../test_helper"
|
|
|
|
module Prism
|
|
class ImplicitArrayTest < TestCase
|
|
def test_call_node
|
|
assert_implicit_array("a.a = *b")
|
|
assert_implicit_array("a.a = 1, 2, 3")
|
|
assert_implicit_array("a[a] = *b")
|
|
assert_implicit_array("a[a] = 1, 2, 3")
|
|
end
|
|
|
|
def test_class_variable_write_node
|
|
assert_implicit_array("@@a = *b")
|
|
assert_implicit_array("@@a = 1, 2, 3")
|
|
end
|
|
|
|
def test_constant_path_write_node
|
|
assert_implicit_array("A::A = *b")
|
|
assert_implicit_array("A::A = 1, 2, 3")
|
|
end
|
|
|
|
def test_constant_write_node
|
|
assert_implicit_array("A = *b")
|
|
assert_implicit_array("A = 1, 2, 3")
|
|
end
|
|
|
|
def test_global_variable_write_node
|
|
assert_implicit_array("$a = *b")
|
|
assert_implicit_array("$a = 1, 2, 3")
|
|
end
|
|
|
|
def test_instance_variable_write_node
|
|
assert_implicit_array("@a = *b")
|
|
assert_implicit_array("@a = 1, 2, 3")
|
|
end
|
|
|
|
def test_local_variable_write_node
|
|
assert_implicit_array("a = *b")
|
|
assert_implicit_array("a = 1, 2, 3")
|
|
end
|
|
|
|
def test_multi_write_node
|
|
assert_implicit_array("a, b, c = *b")
|
|
assert_implicit_array("a, b, c = 1, 2, 3")
|
|
end
|
|
|
|
private
|
|
|
|
def assert_implicit_array(source)
|
|
assert Prism.parse_success?(source)
|
|
assert Prism.parse_failure?("if #{source} then end")
|
|
|
|
assert_valid_syntax(source)
|
|
refute_valid_syntax("if #{source} then end")
|
|
end
|
|
end
|
|
end
|