summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/hash_spec.rb
blob: f584e730066b3e62b629654c51704098f2dbe1ac (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require_relative 'spec_helper'
require_relative '../../shared/hash/key_error'

load_extension("hash")

describe "C-API Hash function" do
  before :each do
    @s = CApiHashSpecs.new
  end

  describe "rb_hash" do
    it "calls #hash on the object" do
      obj = mock("rb_hash")
      obj.should_receive(:hash).and_return(5)
      @s.rb_hash(obj).should == 5
    end

    it "converts a Bignum returned by #hash to a Fixnum" do
      obj = mock("rb_hash bignum")
      obj.should_receive(:hash).and_return(bignum_value)

      # The actual conversion is an implementation detail.
      # We only care that ultimately we get a Fixnum instance.
      @s.rb_hash(obj).should be_an_instance_of(Fixnum)
    end

    it "calls #to_int to converts a value returned by #hash to a Fixnum" do
      obj = mock("rb_hash to_int")
      obj.should_receive(:hash).and_return(obj)
      obj.should_receive(:to_int).and_return(12)

      @s.rb_hash(obj).should == 12
    end

    it "raises a TypeError if the object does not implement #to_int" do
      obj = mock("rb_hash no to_int")
      obj.should_receive(:hash).and_return(nil)

      lambda { @s.rb_hash(obj) }.should raise_error(TypeError)
    end
  end

  describe "rb_hash_new" do
    it "returns a new hash" do
      @s.rb_hash_new.should == {}
    end

    it "creates a hash with no default proc" do
      @s.rb_hash_new {}.default_proc.should be_nil
    end
  end

  describe "rb_hash_dup" do
    it "returns a copy of the hash" do
      hsh = {}
      dup = @s.rb_hash_dup(hsh)
      dup.should == hsh
      dup.should_not equal(hsh)
    end
  end

  describe "rb_hash_freeze" do
    it "freezes the hash" do
      @s.rb_hash_freeze({}).frozen?.should be_true
    end
  end

  describe "rb_hash_aref" do
    it "returns the value associated with the key" do
      hsh = {chunky: 'bacon'}
      @s.rb_hash_aref(hsh, :chunky).should == 'bacon'
    end

    it "returns the default value if it exists" do
      hsh = Hash.new(0)
      @s.rb_hash_aref(hsh, :chunky).should == 0
      @s.rb_hash_aref_nil(hsh, :chunky).should be_false
    end

    it "returns nil if the key does not exist" do
      hsh = { }
      @s.rb_hash_aref(hsh, :chunky).should be_nil
      @s.rb_hash_aref_nil(hsh, :chunky).should be_true
    end
  end

  describe "rb_hash_aset" do
    it "adds the key/value pair and returns the value" do
      hsh = {}
      @s.rb_hash_aset(hsh, :chunky, 'bacon').should == 'bacon'
      hsh.should == {chunky: 'bacon'}
    end
  end

  describe "rb_hash_clear" do
    it "returns self that cleared keys and values" do
      hsh = { :key => 'value' }
      @s.rb_hash_clear(hsh).should equal(hsh)
      hsh.should == {}
    end
  end

  describe "rb_hash_delete" do
    it "removes the key and returns the value" do
      hsh = {chunky: 'bacon'}
      @s.rb_hash_delete(hsh, :chunky).should == 'bacon'
      hsh.should == {}
    end
  end

  describe "rb_hash_delete_if" do
    it "removes an entry if the block returns true" do
      h = { a: 1, b: 2, c: 3 }
      @s.rb_hash_delete_if(h) { |k, v| v == 2 }
      h.should == { a: 1, c: 3 }
    end

    it "returns an Enumerator when no block is passed" do
      @s.rb_hash_delete_if({a: 1}).should be_an_instance_of(Enumerator)
    end
  end

  describe "rb_hash_fetch" do
    before :each do
      @hsh = {:a => 1, :b => 2}
    end

    it "returns the value associated with the key" do
      @s.rb_hash_fetch(@hsh, :b).should == 2
    end

    it "raises a KeyError if the key is not found and default is set" do
      @hsh.default = :d
      lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError)
    end

    it "raises a KeyError if the key is not found and no default is set" do
      lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError)
    end

    context "when key is not found" do
      it_behaves_like :key_error, -> (obj, key) {
        @s.rb_hash_fetch(obj, key)
      }, { a: 1 }
    end
  end

  describe "rb_hash_foreach" do
    it "iterates over the hash" do
      hsh = {name: "Evan", sign: :libra}

      out = @s.rb_hash_foreach(hsh)
      out.equal?(hsh).should == false
      out.should == hsh
    end

    it "stops via the callback" do
      hsh = {name: "Evan", sign: :libra}

      out = @s.rb_hash_foreach_stop(hsh)
      out.size.should == 1
    end

    it "deletes via the callback" do
      hsh = {name: "Evan", sign: :libra}

      out = @s.rb_hash_foreach_delete(hsh)
      out.should == {name: "Evan", sign: :libra}
      hsh.should == {}
    end
  end

  describe "rb_hash_size" do
    it "returns the size of the hash" do
      hsh = {fast: 'car', good: 'music'}
      @s.rb_hash_size(hsh).should == 2
    end

    it "returns zero for an empty hash" do
      @s.rb_hash_size({}).should == 0
    end
  end

  describe "rb_hash_lookup" do
    it "returns the value associated with the key" do
      hsh = {chunky: 'bacon'}
      @s.rb_hash_lookup(hsh, :chunky).should == 'bacon'
    end

    it "does not return the default value if it exists" do
      hsh = Hash.new(0)
      @s.rb_hash_lookup(hsh, :chunky).should be_nil
      @s.rb_hash_lookup_nil(hsh, :chunky).should be_true
    end

    it "returns nil if the key does not exist" do
      hsh = { }
      @s.rb_hash_lookup(hsh, :chunky).should be_nil
      @s.rb_hash_lookup_nil(hsh, :chunky).should be_true
    end

    describe "rb_hash_lookup2" do
      it "returns the value associated with the key" do
        hash = {chunky: 'bacon'}

        @s.rb_hash_lookup2(hash, :chunky, nil).should == 'bacon'
      end

      it "returns the default value if the key does not exist" do
        hash = {}

        @s.rb_hash_lookup2(hash, :chunky, 10).should == 10
      end
    end
  end

  describe "rb_hash_set_ifnone" do
    it "sets the default value of non existing keys" do
      hash = {}

      @s.rb_hash_set_ifnone(hash, 10)

      hash[:chunky].should == 10
    end
  end

  describe "rb_Hash" do
    it "returns an empty hash when the argument is nil" do
      @s.rb_Hash(nil).should == {}
    end

    it "returns an empty hash when the argument is []" do
      @s.rb_Hash([]).should == {}
    end

    it "tries to convert the passed argument to a hash by calling #to_hash" do
      h = BasicObject.new
      def h.to_hash; {"bar" => "foo"}; end
      @s.rb_Hash(h).should == {"bar" => "foo"}
    end

    it "raises a TypeError if the argument does not respond to #to_hash" do
      lambda { @s.rb_Hash(42) }.should raise_error(TypeError)
    end

    it "raises a TypeError if #to_hash does not return a hash" do
      h = BasicObject.new
      def h.to_hash; 42; end
      lambda { @s.rb_Hash(h) }.should raise_error(TypeError)
    end
  end
end