Skip to content

Commit 7d2102a

Browse files
committed
Move specs to language/hash_spec.rb, add descriptions and cleanup
1 parent a0d5aa4 commit 7d2102a

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
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/ruby/language/method_spec.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -777,18 +777,7 @@ def m(a, **k) [a, k] end
777777
m("a" => 1, b: 2).should == [{"a" => 1, b: 2}, {}]
778778
end
779779
end
780-
781-
evaluate <<-ruby do
782-
def m(a)
783-
a.delete(:one); a
784-
end
785-
ruby
786-
h = { one: 1, two: 2 }
787-
788-
m(**h).should == { two: 2 }
789-
m(**h).should_not equal(h)
790780
end
791-
end
792781

793782
ruby_version_is "3.0" do
794783
evaluate <<-ruby do
@@ -817,17 +806,6 @@ def m(a, **k) [a, k] end
817806
m(1, a: 2, b: 3).should == [1, {a: 2, b: 3}]
818807
-> { m("a" => 1, b: 2) }.should raise_error(ArgumentError)
819808
end
820-
821-
evaluate <<-ruby do
822-
def m(a)
823-
a.delete(:one); a
824-
end
825-
ruby
826-
h = { one: 1, two: 2 }
827-
828-
m(**h).should == { two: 2 }
829-
m(**h).should equal(h)
830-
end
831809
end
832810

833811
evaluate <<-ruby do

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

spec/tags/language/method_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fails:A method assigns local variables from method parameters for definition 'def m() end'
22
fails:A method assigns local variables from method parameters for definition 'def m(*a) a end'
3-
fails:"A method assigns local variables from method parameters for definition \n def m(a)\n a.delete(:one); a\n end"
43
fails:A method assigns local variables from method parameters for definition 'def m(a = nil, **k) [a, k] end'
54
fails:A method assigns local variables from method parameters for definition 'def m(*a, **k) [a, k] end'
65
fails:A method assigns local variables from method parameters for definition 'def m(a, **nil); a end;'

0 commit comments

Comments
 (0)