Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec for compare_by_identity behaviour of Hash#replace #1160

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions core/hash/replace_spec.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 0 additions & 51 deletions core/hash/shared/replace.rb

This file was deleted.