diff options
author | Jeremy Evans <[email protected]> | 2021-12-27 14:41:43 -0800 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2022-01-14 12:17:57 -0800 |
commit | a93cc3e23b4044762e80820fc7a45606587e11db (patch) | |
tree | be06c72142c01f40013a6f58bc334a4f907bea88 /spec/ruby/core/hash | |
parent | 6b7eff90860d4fb4db01ec4d1f522afa6d809632 (diff) |
Make Hash#shift return nil for empty hash
Fixes [Bug #16908]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5360
Diffstat (limited to 'spec/ruby/core/hash')
-rw-r--r-- | spec/ruby/core/hash/shift_spec.rb | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/spec/ruby/core/hash/shift_spec.rb b/spec/ruby/core/hash/shift_spec.rb index 9d43e640f5..ea36488a04 100644 --- a/spec/ruby/core/hash/shift_spec.rb +++ b/spec/ruby/core/hash/shift_spec.rb @@ -30,23 +30,45 @@ describe "Hash#shift" do h.should == {} end - it "calls #default with nil if the Hash is empty" do - h = {} - def h.default(key) - key.should == nil - :foo + ruby_version_is '3.2' do + it "returns nil if the Hash is empty" do + h = {} + def h.default(key) + raise + end + h.shift.should == nil + end + end + + ruby_version_is ''...'3.2' do + it "calls #default with nil if the Hash is empty" do + h = {} + def h.default(key) + key.should == nil + :foo + end + h.shift.should == :foo end - h.shift.should == :foo end it "returns nil from an empty hash" do {}.shift.should == nil end - it "returns (computed) default for empty hashes" do - Hash.new(5).shift.should == 5 - h = Hash.new { |*args| args } - h.shift.should == [h, nil] + ruby_version_is '3.2' do + it "returns nil for empty hashes with defaults and default procs" do + Hash.new(5).shift.should == nil + h = Hash.new { |*args| args } + h.shift.should == nil + end + end + + ruby_version_is ''...'3.2' do + it "returns (computed) default for empty hashes" do + Hash.new(5).shift.should == 5 + h = Hash.new { |*args| args } + h.shift.should == [h, nil] + end end it "preserves Hash invariants when removing the last item" do |