Skip to content

Commit cac92fc

Browse files
committed
Add specs for keyword arguments in index assignments
1 parent 7e2b4a5 commit cac92fc

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

language/assignments_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ def []=(k, v, &block) @h[k] = v; end
7272
end
7373
end
7474
end
75+
76+
context "given keyword arguments" do
77+
before do
78+
@klass = Class.new do
79+
attr_reader :x
80+
81+
def []=(*args, **kw)
82+
@x = [args, kw]
83+
end
84+
end
85+
end
86+
87+
ruby_version_is ""..."3.4" do
88+
it "supports keyword arguments in index assignments" do
89+
a = @klass.new
90+
eval "a[1, 2, 3, b: 4] = 5"
91+
a.x.should == [[1, 2, 3, {:b=>4}, 5], {}]
92+
end
93+
end
94+
95+
ruby_version_is "3.4" do
96+
it "raies SyntaxError when given keyword arguments in index assignments" do
97+
a = @klass.new
98+
-> { eval "a[1, 2, 3, b: 4] = 5" }.should raise_error(SyntaxError, /keywords are not allowed in index assignment expressions/)
99+
end
100+
end
101+
end
75102
end
76103

77104
describe 'using +=' do
@@ -176,6 +203,37 @@ def []=(k, v, &block) @h[k] = v; end
176203
end
177204
end
178205

206+
context "given keyword arguments" do
207+
before do
208+
@klass = Class.new do
209+
attr_reader :x
210+
211+
def [](*args)
212+
100
213+
end
214+
215+
def []=(*args, **kw)
216+
@x = [args, kw]
217+
end
218+
end
219+
end
220+
221+
ruby_version_is ""..."3.4" do
222+
it "supports keyword arguments in index assignments" do
223+
a = @klass.new
224+
eval "a[1, 2, 3, b: 4] += 5"
225+
a.x.should == [[1, 2, 3, {b: 4}, 105], {}]
226+
end
227+
end
228+
229+
ruby_version_is "3.4" do
230+
it "raies SyntaxError when given keyword arguments in index assignments" do
231+
a = @klass.new
232+
-> { eval "a[1, 2, 3, b: 4] += 5" }.should raise_error(SyntaxError, /keywords are not allowed in index assignment expressions/)
233+
end
234+
end
235+
end
236+
179237
context 'splatted argument' do
180238
it 'correctly handles it' do
181239
@b[:m] = 10

0 commit comments

Comments
 (0)