Skip to content

Commit

Permalink
Add missing Hash specs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Sep 22, 2012
1 parent d6352f2 commit c24ea20
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/hash.rb
Expand Up @@ -166,12 +166,13 @@ def delete(key)
var map = #{self}.map, result;
if (result = map[key]) {
result = bucket[1];
result = result[1];
delete map[key];
return result;
}
return result;
return nil;
}
end

Expand Down
13 changes: 13 additions & 0 deletions spec/core/hash/constructor_spec.rb
@@ -0,0 +1,13 @@
describe "Hash.[]" do
describe "passed zero arguments" do
it "returns an empty hash" do
Hash[].should == {}
end
end

it "creates a Hash; values can be provided as the argument list" do
Hash[:a, 1, :b, 2].should == { :a => 1, :b => 2 }
Hash[].should == {}
Hash[:a, 1, :b, {:c => 2}].should == {:a => 1, :b => {:c => 2}}
end
end
20 changes: 20 additions & 0 deletions spec/core/hash/default_proc_spec.rb
@@ -0,0 +1,20 @@
describe "Hash#default_proc" do
it "returns the block passed to Hash.new" do
h = Hash.new { |i| 'Paris' }
p = h.default_proc
p.call(1).should == 'Paris'
end

it "returns nil if no block was passed to proc" do
{}.default_proc.should == nil
end
end

describe "Hash#default_proc=" do
it "replaces the block passed to Hash.new" do
h = Hash.new { |i| 'Paris' }
h.default_proc = Proc.new { 'Montreal' }
p = h.default_proc
p.call(1).should == 'Montreal'
end
end
8 changes: 8 additions & 0 deletions spec/core/hash/default_spec.rb
Expand Up @@ -6,4 +6,12 @@
Hash.new.default.should == nil
Hash.new.default(4).should == nil
end
end

describe "Hash#default=" do
it "sets the default value" do
h = {}
h.default = 99
h.default.should == 99
end
end
11 changes: 11 additions & 0 deletions spec/core/hash/delete_spec.rb
@@ -0,0 +1,11 @@
describe "Hash#delete" do
it "removes the entry and returns the deleted value" do
h = {a: 5, b: 2}
h.delete(:b).should == 2
h.should == {a: 5}
end

it "returns nil if the key is not found" do
{}.delete(:a).should == nil
end
end
10 changes: 10 additions & 0 deletions spec/core/hash/dup_spec.rb
@@ -0,0 +1,10 @@
describe "Hash#dup" do
it "copies instance variable but not the objects they refer to" do
hash = {'key' => 'value'}

clone = hash.dup

clone.should == hash
clone.object_id.should_not == hash.object_id
end
end
18 changes: 18 additions & 0 deletions spec/core/hash/reject_spec.rb
@@ -0,0 +1,18 @@
describe "Hash#reject" do
it "returns a new hash removing keys for which the block yields true" do
h = {1=>false, 2=>true, 3=>false, 4=>true}
h.reject { |k,v| v }.keys.should == [1,3]
end

it "is equivalent to hsh.dup.delete_if" do
h = {:a => 'a', :b => 'b', :c => 'd'}
h.reject { |k,v| k == 'd' }.should == (h.dup.delete_if { |k, v| k == 'd' })

all_args_reject = []
all_args_delete_if = []
h = {1 => 2, 3 => 4}
h.reject { |*args| all_args_reject << args }
h.delete_if { |*args| all_args_delete_if << args }
all_args_reject.should == all_args_delete_if
end
end
13 changes: 13 additions & 0 deletions spec/core/hash/to_a_spec.rb
@@ -0,0 +1,13 @@
describe "Hash#to_a" do
it "returns a list of [key, value] pairs with same order as each()" do
h = {:a => 1, 1 => :a, 3 => :b, :b => 5}
pairs = []

h.each_pair do |key, value|
pairs << [key, value]
end

h.to_a.should be_kind_of(Array)
h.to_a.should == pairs
end
end

0 comments on commit c24ea20

Please sign in to comment.