From 94d8b4badecd5a807d20718e2937d8b732a94fba Mon Sep 17 00:00:00 2001 From: Herwin Date: Sun, 16 Jun 2024 08:42:22 +0200 Subject: [PATCH 1/2] Remove shared spec for hash_replace This one was used in just one location, moving the specs inline. --- core/hash/replace_spec.rb | 51 +++++++++++++++++++++++++++++++++++-- core/hash/shared/replace.rb | 51 ------------------------------------- 2 files changed, 49 insertions(+), 53 deletions(-) delete mode 100644 core/hash/shared/replace.rb diff --git a/core/hash/replace_spec.rb b/core/hash/replace_spec.rb index 92b2118fd3..182c7977de 100644 --- a/core/hash/replace_spec.rb +++ b/core/hash/replace_spec.rb @@ -1,7 +1,54 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/replace' describe "Hash#replace" do - it_behaves_like :hash_replace, :replace + it "replaces the contents of self with other" do + h = { a: 1, b: 2 } + h.replace(c: -1, d: -2).should equal(h) + h.should == { c: -1, d: -2 } + end + + it "tries to convert the passed argument to a hash using #to_hash" do + obj = mock('{1=>2,3=>4}') + obj.should_receive(:to_hash).and_return({ 1 => 2, 3 => 4 }) + + h = {} + h.replace(obj) + h.should == { 1 => 2, 3 => 4 } + end + + it "calls to_hash on hash subclasses" do + h = {} + h.replace(HashSpecs::ToHashHash[1 => 2]) + h.should == { 1 => 2 } + end + + it "does not transfer default values" do + hash_a = {} + hash_b = Hash.new(5) + hash_a.replace(hash_b) + hash_a.default.should == 5 + + hash_a = {} + hash_b = Hash.new { |h, k| k * 2 } + hash_a.replace(hash_b) + hash_a.default(5).should == 10 + + hash_a = Hash.new { |h, k| k * 5 } + hash_b = Hash.new(-> { raise "Should not invoke lambda" }) + hash_a.replace(hash_b) + hash_a.default.should == hash_b.default + end + + it "raises a FrozenError if called on a frozen instance that would not be modified" do + -> do + HashSpecs.frozen_hash.replace(HashSpecs.frozen_hash) + end.should raise_error(FrozenError) + end + + it "raises a FrozenError if called on a frozen instance that is modified" do + -> do + HashSpecs.frozen_hash.replace(HashSpecs.empty_frozen_hash) + end.should raise_error(FrozenError) + end end diff --git a/core/hash/shared/replace.rb b/core/hash/shared/replace.rb deleted file mode 100644 index bea64384bb..0000000000 --- a/core/hash/shared/replace.rb +++ /dev/null @@ -1,51 +0,0 @@ -describe :hash_replace, shared: true do - it "replaces the contents of self with other" do - h = { a: 1, b: 2 } - h.send(@method, c: -1, d: -2).should equal(h) - h.should == { c: -1, d: -2 } - end - - it "tries to convert the passed argument to a hash using #to_hash" do - obj = mock('{1=>2,3=>4}') - obj.should_receive(:to_hash).and_return({ 1 => 2, 3 => 4 }) - - h = {} - h.send(@method, obj) - h.should == { 1 => 2, 3 => 4 } - end - - it "calls to_hash on hash subclasses" do - h = {} - h.send(@method, HashSpecs::ToHashHash[1 => 2]) - h.should == { 1 => 2 } - end - - it "does not transfer default values" do - hash_a = {} - hash_b = Hash.new(5) - hash_a.send(@method, hash_b) - hash_a.default.should == 5 - - hash_a = {} - hash_b = Hash.new { |h, k| k * 2 } - hash_a.send(@method, hash_b) - hash_a.default(5).should == 10 - - hash_a = Hash.new { |h, k| k * 5 } - hash_b = Hash.new(-> { raise "Should not invoke lambda" }) - hash_a.send(@method, hash_b) - hash_a.default.should == hash_b.default - end - - it "raises a FrozenError if called on a frozen instance that would not be modified" do - -> do - HashSpecs.frozen_hash.send(@method, HashSpecs.frozen_hash) - end.should raise_error(FrozenError) - end - - it "raises a FrozenError if called on a frozen instance that is modified" do - -> do - HashSpecs.frozen_hash.send(@method, HashSpecs.empty_frozen_hash) - end.should raise_error(FrozenError) - end -end From 5bdb1cd86563311e3fbdbab95beb51bdf9e67519 Mon Sep 17 00:00:00 2001 From: Herwin Date: Sun, 16 Jun 2024 08:47:27 +0200 Subject: [PATCH 2/2] Add spec for compare_by_identity in Hash#replace --- core/hash/replace_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/hash/replace_spec.rb b/core/hash/replace_spec.rb index 182c7977de..a26a31f5f9 100644 --- a/core/hash/replace_spec.rb +++ b/core/hash/replace_spec.rb @@ -23,6 +23,22 @@ h.should == { 1 => 2 } end + it "transfers the compare_by_identity flag" do + hash_a = { a: 1 } + hash_b = { b: 2 } + hash_b.compare_by_identity + hash_a.should_not.compare_by_identity? + hash_a.replace(hash_b) + hash_a.should.compare_by_identity? + + hash_a = { a: 1 } + hash_b = { b: 2 } + hash_a.compare_by_identity + hash_a.should.compare_by_identity? + hash_a.replace(hash_b) + hash_a.should_not.compare_by_identity? + end + it "does not transfer default values" do hash_a = {} hash_b = Hash.new(5)