diff options
Diffstat (limited to 'spec/ruby/language/hash_spec.rb')
| -rw-r--r-- | spec/ruby/language/hash_spec.rb | 213 |
1 files changed, 144 insertions, 69 deletions
diff --git a/spec/ruby/language/hash_spec.rb b/spec/ruby/language/hash_spec.rb index c84564d3ea..7a4a2e37c9 100644 --- a/spec/ruby/language/hash_spec.rb +++ b/spec/ruby/language/hash_spec.rb @@ -33,7 +33,7 @@ describe "Hash literal" do end it "freezes string keys on initialization" do - key = "foo" + key = +"foo" h = {key => "bar"} key.reverse! h["foo"].should == "bar" @@ -60,14 +60,12 @@ describe "Hash literal" do @h.should == {1000 => :foo} end - ruby_version_is "3.1" do - it "checks duplicated float keys on initialization" do - -> { - @h = eval "{1.0 => :bar, 1.0 => :foo}" - }.should complain(/key 1.0 is duplicated|duplicated key/) - @h.keys.size.should == 1 - @h.should == {1.0 => :foo} - end + it "checks duplicated float keys on initialization" do + -> { + @h = eval "{1.0 => :bar, 1.0 => :foo}" + }.should complain(/key 1.0 is duplicated|duplicated key/) + @h.keys.size.should == 1 + @h.should == {1.0 => :foo} end it "accepts a hanging comma" do @@ -77,13 +75,37 @@ describe "Hash literal" do end it "recognizes '=' at the end of the key" do - eval("{:a==>1}").should == {:"a=" => 1} - eval("{:a= =>1}").should == {:"a=" => 1} - eval("{:a= => 1}").should == {:"a=" => 1} + {:a==>1}.should == {:"a=" => 1} + {:a= =>1}.should == {:"a=" => 1} + {:a= => 1}.should == {:"a=" => 1} 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 + {:a! =>1}.should == {:"a!" => 1} + {:a! => 1}.should == {:"a!" => 1} + + {a!:1}.should == {:"a!" => 1} + {a!: 1}.should == {:"a!" => 1} + end + + it "raises a SyntaxError if there is no space between `!` and `=>`" do + -> { eval("{:a!=> 1}") }.should.raise(SyntaxError) + end + + it "recognizes '?' at the end of the key" do + {:a? =>1}.should == {:"a?" => 1} + {:a? => 1}.should == {:"a?" => 1} + + {a?:1}.should == {:"a?" => 1} + {a?: 1}.should == {:"a?" => 1} + end + + it "raises a SyntaxError if there is no space between `?` and `=>`" do + -> { eval("{:a?=> 1}") }.should.raise(SyntaxError) end it "constructs a new hash with the given elements" do @@ -105,7 +127,7 @@ describe "Hash literal" do it "accepts mixed 'key: value', 'key => value' and '\"key\"': value' syntax" do h = {:a => 1, :b => 2, "c" => 3, :d => 4} - eval('{a: 1, :b => 2, "c" => 3, "d": 4}').should == h + {a: 1, :b => 2, "c" => 3, "d": 4}.should == h end it "expands an '**{}' element into the containing Hash literal initialization" do @@ -127,11 +149,55 @@ describe "Hash literal" do {a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4} end - it "expands an '**{}' element with the last key/value pair taking precedence" do + 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}" }.should complain(/key :a is duplicated|duplicated key/) @h.should == {a: 2, b: 3, c: 3} + + -> { + h = {a: 2, b: 3, c: 1} + @h = eval "{a: 1, **h, c: 3}" + }.should_not complain + @h.should == {a: 2, b: 3, c: 3} + end + + it "expands an '**{}' and warns when finding an additional duplicate key afterwards" do + -> { + @h = eval "{d: 1, **{a: 2, b: 3, c: 1}, c: 3}" + }.should complain(/key :c is duplicated|duplicated key/) + @h.should == {a: 2, b: 3, c: 3, d: 1} end it "merges multiple nested '**obj' in Hash literals" do @@ -157,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 @@ -177,6 +243,24 @@ describe "Hash literal" do utf8_hash.keys.first.should == usascii_hash.keys.first usascii_hash.keys.first.encoding.should == Encoding::US_ASCII end + + ruby_bug "#20280", ""..."3.4" do + it "raises a SyntaxError at parse time when Symbol key with invalid bytes" do + ScratchPad.record [] + -> { + eval 'ScratchPad << 1; {:"\xC3" => 1}' + }.should.raise(SyntaxError, /invalid symbol/) + ScratchPad.recorded.should == [] + end + + it "raises a SyntaxError at parse time when Symbol key with invalid bytes and 'key: value' syntax used" do + ScratchPad.record [] + -> { + eval 'ScratchPad << 1; {"\xC3": 1}' + }.should.raise(SyntaxError, /invalid symbol/) + ScratchPad.recorded.should == [] + end + end end describe "The ** operator" do @@ -191,68 +275,59 @@ describe "The ** operator" do h.should == { one: 1, two: 2 } end - ruby_version_is ""..."3.0" do - it "makes a caller-side 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 - ruby_version_is "3.0" do - it "does not copy when calling a method taking a positional Hash" do - def m(h) - h.delete(:one); h - end + describe "hash with omitted value" do + it "accepts short notation 'key' for 'key: value' syntax" do + a, b, c = 1, 2, 3 + h = {a:} + {a: 1}.should == h + h = {a:, b:, c:} + {a: 1, b: 2, c: 3}.should == h + end - h = { one: 1, two: 2 } - m(**h).should == { two: 2 } - m(**h).should.equal?(h) - h.should == { two: 2 } + it "ignores hanging comma on short notation" do + a, b, c = 1, 2, 3 + h = {a:, b:, c:,} + {a: 1, b: 2, c: 3}.should == h end - end - ruby_version_is "3.1" do - describe "hash with omitted value" do - it "accepts short notation 'key' for 'key: value' syntax" do - a, b, c = 1, 2, 3 - h = eval('{a:}') - {a: 1}.should == h - h = eval('{a:, b:, c:}') - {a: 1, b: 2, c: 3}.should == h - end + it "accepts mixed syntax" do + a, e = 1, 5 + h = {a:, b: 2, "c" => 3, :d => 4, e:} + {a: 1, :b => 2, "c" => 3, "d": 4, e: 5}.should == h + end - it "ignores hanging comma on short notation" do - a, b, c = 1, 2, 3 - h = eval('{a:, b:, c:,}') - {a: 1, b: 2, c: 3}.should == h - end + it "works with methods and local vars" do + a = Class.new + a.class_eval(<<-RUBY) + def bar + "baz" + end - it "accepts mixed syntax" do - a, e = 1, 5 - h = eval('{a:, b: 2, "c" => 3, :d => 4, e:}') - eval('{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}').should == h - end + def foo(val) + {bar:, val:} + end + RUBY - it "works with methods and local vars" do - a = Class.new - a.class_eval(<<-RUBY) - def bar - "baz" - end + a.new.foo(1).should == {bar: "baz", val: 1} + end - def foo(val) - {bar:, val:} - end - RUBY + it "raises a SyntaxError when the hash key ends with `!`" do + -> { eval("{a!:}") }.should.raise(SyntaxError, /identifier a! is not valid to get/) + end - a.new.foo(1).should == {bar: "baz", val: 1} - end + it "raises a SyntaxError when the hash key ends with `?`" do + -> { eval("{a?:}") }.should.raise(SyntaxError, /identifier a\? is not valid to get/) end end end |
