Sha256: 8f52614052a4c8e146998d0725307d25f5e7c888de95b8731be6fcbfa84fc5c5

Contents?: true

Size: 1.28 KB

Versions: 50

Compression:

Stored size: 1.28 KB

Contents

require 'spec_helper'

describe FormatParser::HashUtils do
  describe '.deep_transform_keys' do
    it 'transforms all the keys in a hash' do
      hash = { aa: 1, 'bb' => 2 }
      result = described_class.deep_transform_keys(hash, &:to_s)

      expect(result).to eq('aa' => 1, 'bb' => 2)
    end

    it 'transforms all the keys in a array of hashes' do
      array = [{ aa: 1, bb: 2 }, { cc: 3, dd: [{c: 2, d: 3}] }]
      result = described_class.deep_transform_keys(array, &:to_s)

      expect(result).to eq(
        [{'aa' => 1, 'bb' => 2}, {'cc' => 3, 'dd' => [{'c' => 2, 'd' => 3}]}]
      )
    end

    it 'transforms all the keys in a hash recursively' do
      hash = { aa: 1, bb: { cc: 22, dd: 3 } }
      result = described_class.deep_transform_keys(hash, &:to_s)

      expect(result).to eq('aa' => 1, 'bb' => { 'cc' => 22, 'dd' => 3})
    end

    it 'does nothing for an non array/hash object' do
      object = Object.new
      result = described_class.deep_transform_keys(object, &:to_s)

      expect(result).to eq(object)
    end

    it 'returns the last value if different keys are transformed into the same one' do
      hash = { aa: 0, 'bb' => 2, bb: 1 }
      result = described_class.deep_transform_keys(hash, &:to_s)

      expect(result).to eq('aa' => 0, 'bb' => 1)
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
format_parser-2.10.0 spec/hash_utils_spec.rb
format_parser-2.9.0 spec/hash_utils_spec.rb
format_parser-2.8.0 spec/hash_utils_spec.rb
format_parser-2.7.2 spec/hash_utils_spec.rb
format_parser-2.7.1 spec/hash_utils_spec.rb
format_parser-2.7.0 spec/hash_utils_spec.rb
format_parser-2.6.0 spec/hash_utils_spec.rb
format_parser-2.5.0 spec/hash_utils_spec.rb
format_parser-2.4.5 spec/hash_utils_spec.rb
format_parser-2.4.4 spec/hash_utils_spec.rb
format_parser-2.4.3 spec/hash_utils_spec.rb
format_parser-2.3.0 spec/hash_utils_spec.rb
format_parser-2.2.1 spec/hash_utils_spec.rb
format_parser-2.2.0 spec/hash_utils_spec.rb
format_parser-2.1.0 spec/hash_utils_spec.rb
format_parser-2.0.0 spec/hash_utils_spec.rb
format_parser-2.0.0.pre.4 spec/hash_utils_spec.rb
format_parser-2.0.0.pre.3 spec/hash_utils_spec.rb
format_parser-2.0.0.pre.2 spec/hash_utils_spec.rb
format_parser-2.0.0.pre spec/hash_utils_spec.rb