Sha256: 51007f87f4800aab28b30d26eaae896687c4e8003478f66c01ca700971faba14

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'abstract_unit'
require 'active_support/core_ext/hash/keys'

class TransformKeysTest < ActiveSupport::TestCase
  test "transform_keys returns a new hash with the keys computed from the block" do
    original = { a: 'a', b: 'b' }
    mapped = original.transform_keys { |k| "#{k}!".to_sym }

    assert_equal({ a: 'a', b: 'b' }, original)
    assert_equal({ a!: 'a', b!: 'b' }, mapped)
  end

  test "transform_keys! modifies the keys of the original" do
    original = { a: 'a', b: 'b' }
    mapped = original.transform_keys! { |k| "#{k}!".to_sym }

    assert_equal({ a!: 'a', b!: 'b' }, original)
    assert_same original, mapped
  end

  test "transform_keys returns an Enumerator if no block is given" do
    original = { a: 'a', b: 'b' }
    enumerator = original.transform_keys
    assert_equal Enumerator, enumerator.class
  end

  test "transform_keys is chainable with Enumerable methods" do
    original = { a: 'a', b: 'b' }
    mapped = original.transform_keys.with_index { |k, i| [k, i].join.to_sym }
    assert_equal({ a0: 'a', b1: 'b' }, mapped)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activejob-lock-0.0.2 rails/activesupport/test/core_ext/hash/transform_keys_test.rb
activejob-lock-0.0.1 rails/activesupport/test/core_ext/hash/transform_keys_test.rb