summaryrefslogtreecommitdiff
path: root/spec/ruby/language/hash_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/language/hash_spec.rb')
-rw-r--r--spec/ruby/language/hash_spec.rb69
1 files changed, 49 insertions, 20 deletions
diff --git a/spec/ruby/language/hash_spec.rb b/spec/ruby/language/hash_spec.rb
index b119b6ca73..7a4a2e37c9 100644
--- a/spec/ruby/language/hash_spec.rb
+++ b/spec/ruby/language/hash_spec.rb
@@ -81,7 +81,7 @@ describe "Hash literal" do
end
it "with '==>' in the middle raises SyntaxError" do
- -> { eval("{:a ==> 1}") }.should raise_error(SyntaxError)
+ -> { eval("{:a ==> 1}") }.should.raise(SyntaxError)
end
it "recognizes '!' at the end of the key" do
@@ -93,7 +93,7 @@ describe "Hash literal" do
end
it "raises a SyntaxError if there is no space between `!` and `=>`" do
- -> { eval("{:a!=> 1}") }.should raise_error(SyntaxError)
+ -> { eval("{:a!=> 1}") }.should.raise(SyntaxError)
end
it "recognizes '?' at the end of the key" do
@@ -105,7 +105,7 @@ describe "Hash literal" do
end
it "raises a SyntaxError if there is no space between `?` and `=>`" do
- -> { eval("{:a?=> 1}") }.should raise_error(SyntaxError)
+ -> { eval("{:a?=> 1}") }.should.raise(SyntaxError)
end
it "constructs a new hash with the given elements" do
@@ -149,6 +149,37 @@ describe "Hash literal" do
{a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4}
end
+ ruby_version_is ""..."3.4" do
+ it "does not expand nil using ** into {} and raises TypeError" do
+ h = nil
+ -> { {a: 1, **h} }.should.raise(TypeError, "no implicit conversion of nil into Hash")
+
+ -> { {a: 1, **nil} }.should.raise(TypeError, "no implicit conversion of nil into Hash")
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "expands nil using ** into {}" do
+ h = nil
+ {**h}.should == {}
+ {a: 1, **h}.should == {a: 1}
+
+ {**nil}.should == {}
+ {a: 1, **nil}.should == {a: 1}
+ end
+
+ it "expands nil using ** into {} and provides a copy to the callable" do
+ ScratchPad.record []
+ insert = -> key, **kw do
+ kw[key] = 1
+ ScratchPad << kw
+ end
+ insert.call(:foo, **nil)
+ insert.call(:bar, **nil)
+ ScratchPad.recorded.should == [{ foo: 1 }, { bar: 1 }]
+ end
+ end
+
it "expands an '**{}' or '**obj' element with the last key/value pair taking precedence" do
-> {
@h = eval "{a: 1, **{a: 2, b: 3, c: 1}, c: 3}"
@@ -192,13 +223,13 @@ describe "Hash literal" do
obj = mock("hash splat")
obj.should_receive(:to_hash).and_return(obj)
- -> { {**obj} }.should raise_error(TypeError)
+ -> { {**obj} }.should.raise(TypeError)
end
it "raises a TypeError if the object does not respond to #to_hash" do
obj = 42
- -> { {**obj} }.should raise_error(TypeError)
- -> { {a: 1, **obj} }.should raise_error(TypeError)
+ -> { {**obj} }.should.raise(TypeError)
+ -> { {a: 1, **obj} }.should.raise(TypeError)
end
it "does not change encoding of literal string keys during creation" do
@@ -218,7 +249,7 @@ describe "Hash literal" do
ScratchPad.record []
-> {
eval 'ScratchPad << 1; {:"\xC3" => 1}'
- }.should raise_error(SyntaxError, /invalid symbol/)
+ }.should.raise(SyntaxError, /invalid symbol/)
ScratchPad.recorded.should == []
end
@@ -226,7 +257,7 @@ describe "Hash literal" do
ScratchPad.record []
-> {
eval 'ScratchPad << 1; {"\xC3": 1}'
- }.should raise_error(SyntaxError, /invalid symbol/)
+ }.should.raise(SyntaxError, /invalid symbol/)
ScratchPad.recorded.should == []
end
end
@@ -244,17 +275,15 @@ describe "The ** operator" do
h.should == { one: 1, two: 2 }
end
- ruby_bug "#20012", ""..."3.3" do
- it "makes a copy when calling a method taking a positional Hash" do
- def m(h)
- h.delete(:one); h
- end
-
- h = { one: 1, two: 2 }
- m(**h).should == { two: 2 }
- m(**h).should_not.equal?(h)
- h.should == { one: 1, two: 2 }
+ it "makes a copy when calling a method taking a positional Hash" do
+ def m(h)
+ h.delete(:one); h
end
+
+ h = { one: 1, two: 2 }
+ m(**h).should == { two: 2 }
+ m(**h).should_not.equal?(h)
+ h.should == { one: 1, two: 2 }
end
describe "hash with omitted value" do
@@ -294,11 +323,11 @@ describe "The ** operator" do
end
it "raises a SyntaxError when the hash key ends with `!`" do
- -> { eval("{a!:}") }.should raise_error(SyntaxError, /identifier a! is not valid to get/)
+ -> { eval("{a!:}") }.should.raise(SyntaxError, /identifier a! is not valid to get/)
end
it "raises a SyntaxError when the hash key ends with `?`" do
- -> { eval("{a?:}") }.should raise_error(SyntaxError, /identifier a\? is not valid to get/)
+ -> { eval("{a?:}") }.should.raise(SyntaxError, /identifier a\? is not valid to get/)
end
end
end