Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions language/assignments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ def []=(k, v, &block) @h[k] = v; end
end
end
end

context "given keyword arguments" do
before do
@klass = Class.new do
attr_reader :x

def []=(*args, **kw)
@x = [args, kw]
end
end
end

ruby_version_is ""..."3.4" do
it "supports keyword arguments in index assignments" do
a = @klass.new
eval "a[1, 2, 3, b: 4] = 5"
a.x.should == [[1, 2, 3, {b: 4}, 5], {}]
end
end

ruby_version_is "3.4" do
it "raies SyntaxError when given keyword arguments in index assignments" do
a = @klass.new
-> { eval "a[1, 2, 3, b: 4] = 5" }.should raise_error(SyntaxError,
/keywords are not allowed in index assignment expressions|keyword arg given in index assignment/) # prism|parse.y
end
end
end
end

describe 'using +=' do
Expand Down Expand Up @@ -176,6 +204,46 @@ def []=(k, v, &block) @h[k] = v; end
end
end

context "given keyword arguments" do
before do
@klass = Class.new do
attr_reader :x

def [](*args)
100
end

def []=(*args, **kw)
@x = [args, kw]
end
end
end

ruby_version_is ""..."3.3" do
it "supports keyword arguments in index assignments" do
a = @klass.new
eval "a[1, 2, 3, b: 4] += 5"
a.x.should == [[1, 2, 3, {b: 4}, 105], {}]
end
end

ruby_version_is "3.3"..."3.4" do
it "supports keyword arguments in index assignments" do
a = @klass.new
eval "a[1, 2, 3, b: 4] += 5"
a.x.should == [[1, 2, 3, 105], {b: 4}]
end
end

ruby_version_is "3.4" do
it "raies SyntaxError when given keyword arguments in index assignments" do
a = @klass.new
-> { eval "a[1, 2, 3, b: 4] += 5" }.should raise_error(SyntaxError,
/keywords are not allowed in index assignment expressions|keyword arg given in index assignment/) # prism|parse.y
end
end
end

context 'splatted argument' do
it 'correctly handles it' do
@b[:m] = 10
Expand Down
Loading