Skip to content

Commit 5726938

Browse files
committed
[GR-19220] Add spec for keyword argument caller to allocate/reuse hash object (#2283)
PullRequest: truffleruby/2466
2 parents 6305204 + 7d2102a commit 5726938

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spec/ruby/language/hash_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,42 @@ def h.to_hash; {:b => 2, :c => 3}; end
167167
usascii_hash.keys.first.encoding.should == Encoding::US_ASCII
168168
end
169169
end
170+
171+
describe "The ** operator" do
172+
it "makes a copy when calling a method taking a keyword rest argument" do
173+
def m(**h)
174+
h.delete(:one); h
175+
end
176+
177+
h = { one: 1, two: 2 }
178+
m(**h).should == { two: 2 }
179+
m(**h).should_not.equal?(h)
180+
h.should == { one: 1, two: 2 }
181+
end
182+
183+
ruby_version_is ""..."3.0" do
184+
it "makes a caller-side copy when calling a method taking a positional Hash" do
185+
def m(h)
186+
h.delete(:one); h
187+
end
188+
189+
h = { one: 1, two: 2 }
190+
m(**h).should == { two: 2 }
191+
m(**h).should_not.equal?(h)
192+
h.should == { one: 1, two: 2 }
193+
end
194+
end
195+
196+
ruby_version_is "3.0" do
197+
it "does not copy when calling a method taking a positional Hash" do
198+
def m(h)
199+
h.delete(:one); h
200+
end
201+
202+
h = { one: 1, two: 2 }
203+
m(**h).should == { two: 2 }
204+
m(**h).should.equal?(h)
205+
h.should == { two: 2 }
206+
end
207+
end
208+
end

spec/tags/language/hash_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:The ** operator makes a caller-side copy when calling a method taking a positional Hash

0 commit comments

Comments
 (0)