diff options
Diffstat (limited to 'spec/ruby/core/hash/transform_keys_spec.rb')
-rw-r--r-- | spec/ruby/core/hash/transform_keys_spec.rb | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/spec/ruby/core/hash/transform_keys_spec.rb b/spec/ruby/core/hash/transform_keys_spec.rb index 70a4cdde36..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,14 +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 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 ""..."3.0.2" do # https://bugs.ruby-lang.org/issues/17735 - it "returns the processed keys if we broke from the block" do + it "returns the processed keys if we break from the block" do @hash.transform_keys! do |v| break if v == :c v.succ @@ -76,7 +87,7 @@ describe "Hash#transform_keys!" do end ruby_version_is "3.0.2" do - it "returns the processed keys and non evaluated keys if we broke from the block" 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 @@ -98,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 @@ -112,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) |