Sha256: c913505a7b7da93ebbc24efee7ab56fda0242bd20cde7d16c2db02217b6c499d

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

require 'facets/hash/rekey'
require 'test/unit'

class Test_Hash_Rekey < Test::Unit::TestCase

  def test_rekey
    foo = { :a=>1, :b=>2 }
    foo = foo.rekey(:c, :a)
    assert_equal( 1, foo[:c] )
    assert_equal( 2, foo[:b] )
    assert_equal( nil, foo[:a] )
  end

  def test_rekey!
    foo = { :a=>1, :b=>2 }
    foo.rekey!(:c, :a)
    assert_equal( 1, foo[:c] )
    assert_equal( 2, foo[:b] )
    assert_equal( nil, foo[:a] )
  end

  def test_rekey_with_block
    foo = { :a=>1, :b=>2 }
    foo = foo.rekey{ |k| k.to_s }
    assert_equal( 1, foo['a'] )
    assert_equal( 2, foo['b'] )
    assert_equal( nil, foo[:a] )
    assert_equal( nil, foo[:b] )
  end

  def test_rekey_with_block!
    foo = { :a=>1, :b=>2 }
    foo.rekey!{ |k| k.to_s }
    assert_equal( 1, foo['a'] )
    assert_equal( 2, foo['b'] )
    assert_equal( nil, foo[:a] )
    assert_equal( nil, foo[:b] )
  end

  #

  def test_rekey_01
    foo = { :a=>1, :b=>2 }
    assert_equal( { "a"=>1, "b"=>2 }, foo.rekey{|k|k.to_s} )
    assert_equal( { :a =>1, :b=>2 }, foo  )
  end

  def test_rekey_02
    foo = { :a=>1, :b=>2 }
    assert_equal( { "a"=>1, "b"=>2 }, foo.rekey!{|k|k.to_s}  )
    assert_equal( { "a"=>1, "b"=>2 }, foo )
  end

  def test_rekey_03
    foo = { :a=>1, :b=>2 }
    assert_equal( { "a"=>1, "b"=>2 }, foo.rekey(:to_s) )
    assert_equal( { :a =>1, :b=>2 }, foo  )
  end

  def test_rekey_04
    foo = { :a=>1, :b=>2 }
    assert_equal( { "a"=>1, "b"=>2 }, foo.rekey!(:to_s) )
    assert_equal( { "a"=>1, "b"=>2 }, foo )
  end

  def test_rekey_05
    foo = { "a"=>1, "b"=>2 }
    assert_equal( { :a=>1, :b=>2 }, foo.rekey! )
    assert_equal( { :a=>1, :b=>2 }, foo )
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
facets-2.8.4 test/core/hash/test_rekey.rb
facets-2.8.3 test/core/hash/test_rekey.rb
facets-2.8.2 test/core/hash/test_rekey.rb
facets-2.8.1 test/core/hash/test_rekey.rb
facets-2.8.0 test/core/hash/test_rekey.rb
facets-2.7.0 test/core/hash/test_rekey.rb
facets-2.6.0 test/core/hash/test_rekey.rb
facets-2.4.4 test/core/hash/test_rekey.rb
facets-2.4.3 test/core/hash/test_rekey.rb
facets-2.4.2 test/core/hash/test_rekey.rb
facets-2.5.1 test/core/hash/test_rekey.rb
facets-2.5.0 test/core/hash/test_rekey.rb
facets-2.4.5 test/core/hash/test_rekey.rb
facets-2.5.2 test/core/hash/test_rekey.rb