diff options
Diffstat (limited to 'spec/ruby/core/hash/reject_spec.rb')
| -rw-r--r-- | spec/ruby/core/hash/reject_spec.rb | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/spec/ruby/core/hash/reject_spec.rb b/spec/ruby/core/hash/reject_spec.rb index 21dd7425aa..aa1a074897 100644 --- a/spec/ruby/core/hash/reject_spec.rb +++ b/spec/ruby/core/hash/reject_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) -require File.expand_path('../shared/iteration', __FILE__) -require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative 'shared/iteration' +require_relative '../enumerable/shared/enumeratorized' describe "Hash#reject" do it "returns a new hash removing keys for which the block yields true" do @@ -28,13 +28,8 @@ describe "Hash#reject" do context "with extra state" do it "returns Hash instance for subclasses" do - HashSpecs::MyHash[1 => 2, 3 => 4].reject { false }.should be_kind_of(Hash) - HashSpecs::MyHash[1 => 2, 3 => 4].reject { true }.should be_kind_of(Hash) - end - - it "does not taint the resulting hash" do - h = { a: 1 }.taint - h.reject {false}.tainted?.should == false + HashSpecs::MyHash[1 => 2, 3 => 4].reject { false }.should.is_a?(Hash) + HashSpecs::MyHash[1 => 2, 3 => 4].reject { true }.should.is_a?(Hash) end end @@ -49,8 +44,29 @@ describe "Hash#reject" do reject_pairs.should == reject_bang_pairs end - it_behaves_like(:hash_iteration_no_block, :reject) - it_behaves_like(:enumeratorized_with_origin_size, :reject, { 1 => 2, 3 => 4, 5 => 6 }) + it "does not retain the default value" do + h = Hash.new(1) + h.reject { false }.default.should == nil + h[:a] = 1 + h.reject { false }.default.should == nil + end + + it "does not retain the default_proc" do + pr = proc { |h, k| h[k] = [] } + h = Hash.new(&pr) + h.reject { false }.default_proc.should == nil + h[:a] = 1 + h.reject { false }.default_proc.should == nil + end + + it "retains compare_by_identity flag" do + h = { a: 9, c: 4 }.compare_by_identity + h2 = h.reject { |k, _| k == :a } + h2.compare_by_identity?.should == true + end + + it_behaves_like :hash_iteration_no_block, :reject + it_behaves_like :enumeratorized_with_origin_size, :reject, { 1 => 2, 3 => 4, 5 => 6 } end describe "Hash#reject!" do @@ -63,7 +79,7 @@ describe "Hash#reject!" do it "removes all entries if the block is true" do h = { a: 1, b: 2, c: 3 } - h.reject! { |k,v| true }.should equal(h) + h.reject! { |k,v| true }.should.equal?(h) h.should == {} end @@ -87,14 +103,14 @@ describe "Hash#reject!" do reject_bang_pairs.should == delete_if_pairs end - it "raises a RuntimeError if called on a frozen instance that is modified" do - lambda { HashSpecs.empty_frozen_hash.reject! { true } }.should raise_error(RuntimeError) + it "raises a FrozenError if called on a frozen instance that is modified" do + -> { HashSpecs.empty_frozen_hash.reject! { true } }.should.raise(FrozenError) end - it "raises a RuntimeError if called on a frozen instance that would not be modified" do - lambda { HashSpecs.frozen_hash.reject! { false } }.should raise_error(RuntimeError) + it "raises a FrozenError if called on a frozen instance that would not be modified" do + -> { HashSpecs.frozen_hash.reject! { false } }.should.raise(FrozenError) end - it_behaves_like(:hash_iteration_no_block, :reject!) - it_behaves_like(:enumeratorized_with_origin_size, :reject!, { 1 => 2, 3 => 4, 5 => 6 }) + it_behaves_like :hash_iteration_no_block, :reject! + it_behaves_like :enumeratorized_with_origin_size, :reject!, { 1 => 2, 3 => 4, 5 => 6 } end |
