Skip to content

Commit 89b1497

Browse files
andrykonchinheadius
authored andcommitted
Fix retaining compare_by_identity flag for Hash methods #select, #slice, #transform_values and Hash[]
1 parent 0c6439c commit 89b1497

File tree

12 files changed

+80
-17
lines changed

12 files changed

+80
-17
lines changed

core/hash/compact_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
hash.compact.default_proc.should == pr
3636
end
3737

38-
it "retains compare_by_identity_flag" do
38+
it "retains compare_by_identity flag" do
3939
hash = {}.compare_by_identity
4040
hash.compact.compare_by_identity?.should == true
4141
hash[:a] = 1

core/hash/constructor_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ def obj.to_hash() { 1 => 2, 3 => 4 } end
118118
end
119119

120120
ruby_version_is '3.3' do
121-
it "does not retain compare_by_identity_flag" do
122-
hash = {}.compare_by_identity
121+
it "does not retain compare_by_identity flag" do
122+
hash = { a: 1 }.compare_by_identity
123123
Hash[hash].compare_by_identity?.should == false
124-
hash[:a] = 1
124+
125+
hash = {}.compare_by_identity
125126
Hash[hash].compare_by_identity?.should == false
126127
end
127128
end

core/hash/except_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
r.class.should == Hash
3030
r.default.should == nil
3131
end
32+
33+
it "retains compare_by_identity flag" do
34+
h = { a: 9, c: 4 }.compare_by_identity
35+
h2 = h.except(:a)
36+
h2.compare_by_identity?.should == true
37+
end
3238
end

core/hash/invert_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@
2424
HashSpecs::MyHash[1 => 2, 3 => 4].invert.class.should == Hash
2525
HashSpecs::MyHash[].invert.class.should == Hash
2626
end
27+
28+
it "does not retain compare_by_identity flag" do
29+
h = { a: 9, c: 4 }.compare_by_identity
30+
h2 = h.invert
31+
h2.compare_by_identity?.should == false
32+
end
2733
end

core/hash/merge_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@
9393
merged.should eql(hash)
9494
merged.should_not equal(hash)
9595
end
96+
97+
it "retains compare_by_identity flag" do
98+
h = { a: 9, c: 4 }.compare_by_identity
99+
h2 = h.merge(b: 1, d: 2)
100+
h2.compare_by_identity?.should == true
101+
end
102+
103+
it "ignores compare_by_identity flag of an argument" do
104+
h = { a: 9, c: 4 }.compare_by_identity
105+
h2 = { b: 1, d: 2 }.merge(h)
106+
h2.compare_by_identity?.should == false
107+
end
96108
end
97109

98110
describe "Hash#merge!" do

core/hash/reject_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def h.to_a() end
4444
reject_pairs.should == reject_bang_pairs
4545
end
4646

47+
it "retains compare_by_identity flag" do
48+
h = { a: 9, c: 4 }.compare_by_identity
49+
h2 = h.reject { |k, _| k == :a }
50+
h2.compare_by_identity?.should == true
51+
end
52+
4753
it_behaves_like :hash_iteration_no_block, :reject
4854
it_behaves_like :enumeratorized_with_origin_size, :reject, { 1 => 2, 3 => 4, 5 => 6 }
4955
end

core/hash/replace_spec.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@
2323
h.should == { 1 => 2 }
2424
end
2525

26-
it "transfers the compare_by_identity flag" do
27-
hash_a = { a: 1 }
28-
hash_b = { b: 2 }
29-
hash_b.compare_by_identity
30-
hash_a.should_not.compare_by_identity?
31-
hash_a.replace(hash_b)
32-
hash_a.should.compare_by_identity?
26+
it "transfers compare_by_identity flag of an argument" do
27+
h = { a: 1, c: 3 }
28+
h2 = { b: 2, d: 4 }.compare_by_identity
29+
h.replace(h2)
30+
h.compare_by_identity?.should == true
31+
end
3332

34-
hash_a = { a: 1 }
35-
hash_b = { b: 2 }
36-
hash_a.compare_by_identity
37-
hash_a.should.compare_by_identity?
38-
hash_a.replace(hash_b)
39-
hash_a.should_not.compare_by_identity?
33+
it "does not retain compare_by_identity flag" do
34+
h = { a: 1, c: 3 }.compare_by_identity
35+
h.replace(b: 2, d: 4)
36+
h.compare_by_identity?.should == false
4037
end
4138

4239
it "does not transfer default values" do

core/hash/shared/select.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
@empty.send(@method).should be_an_instance_of(Enumerator)
4141
end
4242

43+
it "retains compare_by_identity flag" do
44+
h = { a: 9, c: 4 }.compare_by_identity
45+
h2 = h.send(@method) { |k, _| k == :a }
46+
h2.compare_by_identity?.should == true
47+
end
48+
4349
it_should_behave_like :hash_iteration_no_block
4450

4551
before :each do

core/hash/slice_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ def [](value)
5050

5151
ScratchPad.recorded.should == []
5252
end
53+
54+
it "retains compare_by_identity flag" do
55+
h = { a: 9, c: 4 }.compare_by_identity
56+
h2 = h.slice(:a)
57+
h2.compare_by_identity?.should == true
58+
end
5359
end

core/hash/to_h_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
@h.to_h.default_proc.should == prc
3131
@h[42].should == 84
3232
end
33+
34+
it "retains compare_by_identity flag" do
35+
@h.compare_by_identity
36+
@h.to_h.compare_by_identity?.should == true
37+
end
3338
end
3439

3540
context "with block" do
@@ -78,5 +83,11 @@
7883
{ a: 1 }.to_h { |k| x }
7984
end.should raise_error(TypeError, /wrong element type MockObject/)
8085
end
86+
87+
it "does not retain compare_by_identity flag" do
88+
h = { a: 9, c: 4 }.compare_by_identity
89+
h2 = h.to_h { |k, v| [k.to_s, v*v]}
90+
h2.compare_by_identity?.should == false
91+
end
8192
end
8293
end

0 commit comments

Comments
 (0)