summaryrefslogtreecommitdiff
path: root/spec/ruby/core/objectspace/weakkeymap/element_reference_spec.rb
blob: 51368e8d3ba3af91bb0e04f50b4eb1875094049a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require_relative '../../../spec_helper'
require_relative 'fixtures/classes'

ruby_version_is "3.3" do
  describe "ObjectSpace::WeakKeyMap#[]" do
    it "is faithful to the map's content" do
      map = ObjectSpace::WeakKeyMap.new
      key1, key2 = %w[a b].map(&:upcase)
      ref1, ref2 = %w[x y]
      map[key1] = ref1
      map[key1].should == ref1
      map[key1] = ref1
      map[key1].should == ref1
      map[key2] = ref2
      map[key1].should == ref1
      map[key2].should == ref2
    end

    it "compares keys with #eql? semantics" do
      map = ObjectSpace::WeakKeyMap.new
      key = [1.0]
      map[key] = "x"
      map[[1]].should == nil
      map[[1.0]].should == "x"
      key.should == [1.0] # keep the key alive until here to keep the map entry

      map = ObjectSpace::WeakKeyMap.new
      key = [1]
      map[key] = "x"
      map[[1.0]].should == nil
      map[[1]].should == "x"
      key.should == [1] # keep the key alive until here to keep the map entry

      map = ObjectSpace::WeakKeyMap.new
      key1, key2 = %w[a a].map(&:upcase)
      ref = "x"
      map[key1] = ref
      map[key2].should == ref
    end

    it "compares key via #hash first" do
      x = mock('0')
      x.should_receive(:hash).and_return(0)

      map = ObjectSpace::WeakKeyMap.new
      key = 'foo'
      map[key] = :bar
      map[x].should == nil
    end

    it "does not compare keys with different #hash values via #eql?" do
      x = mock('x')
      x.should_not_receive(:eql?)
      x.stub!(:hash).and_return(0)

      y = mock('y')
      y.should_not_receive(:eql?)
      y.stub!(:hash).and_return(1)

      map = ObjectSpace::WeakKeyMap.new
      map[y] = 1
      map[x].should == nil
    end

    it "compares keys with the same #hash value via #eql?" do
      x = mock('x')
      x.should_receive(:eql?).and_return(true)
      x.stub!(:hash).and_return(42)

      y = mock('y')
      y.should_not_receive(:eql?)
      y.stub!(:hash).and_return(42)

      map = ObjectSpace::WeakKeyMap.new
      map[y] = 1
      map[x].should == 1
    end

    it "finds a value via an identical key even when its #eql? isn't reflexive" do
      x = mock('x')
      x.should_receive(:hash).at_least(1).and_return(42)
      x.stub!(:eql?).and_return(false) # Stubbed for clarity and latitude in implementation; not actually sent by MRI.

      map = ObjectSpace::WeakKeyMap.new
      map[x] = :x
      map[x].should == :x
    end

    it "supports keys with private #hash method" do
      key = WeakKeyMapSpecs::KeyWithPrivateHash.new
      map = ObjectSpace::WeakKeyMap.new
      map[key] = 42
      map[key].should == 42
    end

    it "returns nil and does not raise error when a key cannot be garbage collected" do
      map = ObjectSpace::WeakKeyMap.new

      map[1].should == nil
      map[1.0].should == nil
      map[:a].should == nil
      map[true].should == nil
      map[false].should == nil
      map[nil].should == nil
    end
  end
end