Sha256: a3b99875958a1a4a4dc959c077c6f1270743abbbe13c1b0ebf1d95d40de8bcc9

Contents?: true

Size: 1.93 KB

Versions: 124

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

RSpec.describe :transform_keys do
  link :transform_keys, from: :ree_hash

  context "deep" do
    it {
      hash = {id: 1, name: 'John', array: [1,2,3], hash: {id: 1, name: 'Doe'}}

      result = transform_keys(hash) { _1.to_s }

      expect(result).to eq(
        {'id' => 1, 'name' => 'John', 'array' => [1,2,3], 'hash' => {'id' => 1, 'name' => 'Doe'}}
      )
    }
  end

  context "deep = false" do
    it {
      hash = {id: 1, name: 'John', array: [1,2,3], hash: {id: 1, name: 'Doe'}}

      result = transform_keys(hash, deep: false) { _1.to_s }

      expect(result).to eq(
        {'id' => 1, 'name' => 'John', 'array' => [1,2,3], 'hash' => {id: 1, name: 'Doe'}}
      )
    }
  end

  context "default" do
    it {
      hash = {id: 1, name: 'John', array: [1,2,3], hash: {id: 1, name: 'Doe'}}
      hash.default = 1

      result = transform_keys(hash, deep: false) { _1.to_s }

      expect(result.default).to eq(1)
      expect(result['hash'].default).to eq(nil)
    }

    it {
      hash = {id: 1, name: 'John', array: [1,2,3], hash: {id: 1, name: 'Doe'}}
      hash.default_proc = Proc.new { 1 }

      result = transform_keys(hash, deep: false) { _1.to_s }

      expect(result.default_proc).to eq(hash.default_proc)
      expect(result['hash'].default).to eq(nil)
    }

    it {
      obj = {id: 1, name: 'Doe'}
      obj.default = 2

      hash = {id: 1, name: 'John', array: [1,2,3], hash: obj}
      hash.default = 1

      result = transform_keys(hash, deep: false) { _1.to_s }

      expect(result.default).to eq(1)
      expect(result['hash'].default).to eq(2)
    }

    it {
      obj = {id: 1, name: 'Doe'}
      obj.default_proc = Proc.new { 2 }

      hash = {id: 1, name: 'John', array: [1,2,3], hash: obj}
      hash.default = 1

      result = transform_keys(hash, deep: false) { _1.to_s }

      expect(result.default).to eq(1)
      expect(result['hash'].default_proc).to eq(obj.default_proc)
    }
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_hash/spec/ree_hash/functions/transform_keys_spec.rb