diff options
Diffstat (limited to 'spec/ruby/core/hash/transform_values_spec.rb')
-rw-r--r-- | spec/ruby/core/hash/transform_values_spec.rb | 21 |
1 files changed, 21 insertions, 0 deletions
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 |