diff options
Diffstat (limited to 'spec/ruby/core/hash/transform_keys_spec.rb')
-rw-r--r-- | spec/ruby/core/hash/transform_keys_spec.rb | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/spec/ruby/core/hash/transform_keys_spec.rb b/spec/ruby/core/hash/transform_keys_spec.rb index 63403a25b0..2fbb17a8e2 100644 --- a/spec/ruby/core/hash/transform_keys_spec.rb +++ b/spec/ruby/core/hash/transform_keys_spec.rb @@ -42,6 +42,18 @@ describe "Hash#transform_keys" do r.keys.should == [:xfoo] r.class.should == Hash end + + it "allows a hash argument" do + @hash.transform_keys({ a: :A, b: :B, c: :C }).should == { A: 1, B: 2, C: 3 } + end + + it "allows a partial transformation of keys when using a hash argument" do + @hash.transform_keys({ a: :A, c: :C }).should == { A: 1, b: 2, C: 3 } + end + + it "allows a combination of hash and block argument" do + @hash.transform_keys({ a: :A }, &:to_s).should == { A: 1, 'b' => 2, 'c' => 3 } + end end describe "Hash#transform_keys!" do @@ -59,33 +71,13 @@ describe "Hash#transform_keys!" do @hash.should == { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 } end - # https://bugs.ruby-lang.org/issues/14380 - ruby_version_is ""..."2.5.1" do - it "does not prevent conflicts between new keys and old ones" do - @hash.transform_keys!(&:succ) - @hash.should == { e: 1 } - end + it "prevents conflicts between new keys and old ones" do + @hash.transform_keys!(&:succ) + @hash.should == { b: 1, c: 2, d: 3, e: 4 } end - ruby_version_is "2.5.1" do - it "prevents conflicts between new keys and old ones" do - @hash.transform_keys!(&:succ) - @hash.should == { b: 1, c: 2, d: 3, e: 4 } - end - end - - ruby_version_is ""..."2.5.1" do - it "partially modifies the contents if we broke from the block" do - @hash.transform_keys! do |v| - break if v == :c - v.succ - end - @hash.should == { c: 1, d: 4 } - end - end - - ruby_version_is "2.5.1"..."3.1" do - it "returns the processed keys if we broke from the block" do + ruby_version_is ""..."3.0.2" do # https://bugs.ruby-lang.org/issues/17735 + it "returns the processed keys if we break from the block" do @hash.transform_keys! do |v| break if v == :c v.succ @@ -94,8 +86,8 @@ describe "Hash#transform_keys!" do end end - ruby_version_is "3.1" do - it "returns the processed keys and non evaluated keys if we broke from the block" do + ruby_version_is "3.0.2" do + it "returns the processed keys and non evaluated keys if we break from the block" do @hash.transform_keys! do |v| break if v == :c v.succ @@ -117,6 +109,11 @@ describe "Hash#transform_keys!" do end end + it "allows a hash argument" do + @hash.transform_keys!({ a: :A, b: :B, c: :C, d: :D }) + @hash.should == { A: 1, B: 2, C: 3, D: 4 } + end + describe "on frozen instance" do before :each do @hash.freeze @@ -131,6 +128,10 @@ describe "Hash#transform_keys!" do @hash.should == @initial_pairs end + it "raises a FrozenError on hash argument" do + ->{ @hash.transform_keys!({ a: :A, b: :B, c: :C }) }.should raise_error(FrozenError) + end + context "when no block is given" do it "does not raise an exception" do @hash.transform_keys!.should be_an_instance_of(Enumerator) |