aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/hash
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/hash')
-rw-r--r--spec/ruby/core/hash/delete_spec.rb4
-rw-r--r--spec/ruby/core/hash/transform_values_spec.rb21
2 files changed, 23 insertions, 2 deletions
diff --git a/spec/ruby/core/hash/delete_spec.rb b/spec/ruby/core/hash/delete_spec.rb
index a45b8cd171..a41fad3586 100644
--- a/spec/ruby/core/hash/delete_spec.rb
+++ b/spec/ruby/core/hash/delete_spec.rb
@@ -11,13 +11,13 @@ describe "Hash#delete" do
it "calls supplied block if the key is not found" do
{ a: 1, b: 10, c: 100 }.delete(:d) { 5 }.should == 5
Hash.new(:default).delete(:d) { 5 }.should == 5
- Hash.new { :defualt }.delete(:d) { 5 }.should == 5
+ Hash.new { :default }.delete(:d) { 5 }.should == 5
end
it "returns nil if the key is not found when no block is given" do
{ a: 1, b: 10, c: 100 }.delete(:d).should == nil
Hash.new(:default).delete(:d).should == nil
- Hash.new { :defualt }.delete(:d).should == nil
+ Hash.new { :default }.delete(:d).should == nil
end
# MRI explicitly implements this behavior
diff --git a/spec/ruby/core/hash/transform_values_spec.rb b/spec/ruby/core/hash/transform_values_spec.rb
index 7e4ff45bea..0c9e43d621 100644
--- a/spec/ruby/core/hash/transform_values_spec.rb
+++ b/spec/ruby/core/hash/transform_values_spec.rb
@@ -24,6 +24,15 @@ ruby_version_is "2.4" do
enumerator.each(&:succ).should == { a: 2, b: 3, c: 4 }
end
end
+
+ it "returns a Hash instance, even on subclasses" do
+ klass = Class.new(Hash)
+ h = klass.new
+ h[:foo] = 42
+ r = h.transform_values{|v| 2 * v}
+ r[:foo].should == 84
+ r.class.should == Hash
+ end
end
describe "Hash#transform_values!" do
@@ -41,6 +50,14 @@ ruby_version_is "2.4" do
@hash.should == { a: 2, b: 3, c: 4 }
end
+ it "partially modifies the contents if we broke from the block" do
+ @hash.transform_values! do |v|
+ break if v == 3
+ 100 + v
+ end
+ @hash.should == { a: 101, b: 102, c: 3}
+ end
+
context "when no block is given" do
it "returns a sized Enumerator" do
enumerator = @hash.transform_values!
@@ -56,6 +73,10 @@ ruby_version_is "2.4" do
@hash.freeze
end
+ it "raises a RuntimeError on an empty hash" do
+ ->{ {}.freeze.transform_values!(&:succ) }.should raise_error(RuntimeError)
+ end
+
it "keeps pairs and raises a RuntimeError" do
->{ @hash.transform_values!(&:succ) }.should raise_error(RuntimeError)
@hash.should == @initial_pairs