From 6ff887321b930197a8d62ddd0fc1dcb965ccda29 Mon Sep 17 00:00:00 2001 From: Mark McSpadden Date: Thu, 10 May 2012 16:05:19 -0500 Subject: Add Hash#transform_keys and Hash#transform_keys! and refactor *_keys methods to use them. --- activesupport/test/core_ext/hash_ext_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 0b0e7da4a5..b13ee4c9ff 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -28,10 +28,13 @@ class HashExtTest < ActiveSupport::TestCase @mixed = { :a => 1, 'b' => 2 } @fixnums = { 0 => 1, 1 => 2 } @illegal_symbols = { [] => 3 } + @upcase_strings = { 'A' => 1, 'B' => 2 } end def test_methods h = {} + assert_respond_to h, :transform_keys + assert_respond_to h, :transform_keys! assert_respond_to h, :symbolize_keys assert_respond_to h, :symbolize_keys! assert_respond_to h, :stringify_keys @@ -40,6 +43,18 @@ class HashExtTest < ActiveSupport::TestCase assert_respond_to h, :to_options! end + def test_transform_keys + assert_equal @upcase_strings, @strings.transform_keys{ |key| key.to_s.upcase } + assert_equal @upcase_strings, @symbols.transform_keys{ |key| key.to_s.upcase } + assert_equal @upcase_strings, @mixed.transform_keys{ |key| key.to_s.upcase } + end + + def test_transform_keys! + assert_equal @upcase_strings, @symbols.dup.transform_keys!{ |key| key.to_s.upcase } + assert_equal @upcase_strings, @strings.dup.transform_keys!{ |key| key.to_s.upcase } + assert_equal @upcase_strings, @mixed.dup.transform_keys!{ |key| key.to_s.upcase } + end + def test_symbolize_keys assert_equal @symbols, @symbols.symbolize_keys assert_equal @symbols, @strings.symbolize_keys -- cgit v1.2.3