Sha256: 8ccfc53d34281d163f6ead3122b6c937bbe2f0ee4ab2fc34778b4489801fac39

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

describe Darthjee::CoreExt::Hash::KeyChangeable do
  describe '#remap_keys' do
    subject(:hash) { { a: 1, b: 2 } }

    it 'remaps the keys' do
      expect(hash.remap_keys(a: :b, b: :c)).to eq(b: 1, c: 2)
    end
  end

  describe '#change_keys' do
    subject(:hash) { { '1' => 1, '2' => { '3' => 2 } } }

    context 'when not passing options' do
      let(:result) do
        hash.change_keys do |k|
          (k.to_i + 1).to_s.to_sym
        end
      end

      it 'uses the block to change the keys' do
        expect(result).to eq('2': 1, '3': { '4': 2 })
      end
    end

    context 'when passing recursive option' do
      let(:result) do
        hash.change_keys(recursive: false) do |k|
          (k.to_i + 1).to_s.to_sym
        end
      end

      it 'uses the block to change the keys' do
        expect(result).to eq('2': 1, '3': { '3' => 2 })
      end
    end
  end

  describe '#chain_change_keys' do
    subject(:hash) { { first: 1, second: 2 } }

    it 'uses the block to change the keys' do
      result = hash.chain_change_keys(:to_s, :size, :to_s, :to_sym)
      expect(result).to eq('5': 1, '6': 2)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
darthjee-core_ext-1.7.4 spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb