aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/hash_ext_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-07 01:21:58 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-07 01:21:58 +0000
commit71742114f82562633eedabc3981d7b31ccee0267 (patch)
tree540af2f360342f2859ea668569025b4b26c66bff /activesupport/test/core_ext/hash_ext_test.rb
parent239ec08c001b5588914f08ec8063939ad143067a (diff)
downloadrails-71742114f82562633eedabc3981d7b31ccee0267.tar.gz
rails-71742114f82562633eedabc3981d7b31ccee0267.tar.bz2
rails-71742114f82562633eedabc3981d7b31ccee0267.zip
Fixed Hash#indifferent_access to also deal with include? and fetch and nested hashes #726 [Nicholas Seckar]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@872 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test/core_ext/hash_ext_test.rb')
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index d74a32e892..0a7a7a41b2 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -56,6 +56,26 @@ class HashExtTest < Test::Unit::TestCase
assert_equal @symbols[:a], @symbols['a']
assert_equal @strings['b'], @mixed['b']
assert_equal @strings[:b], @mixed['b']
+
+ hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed }
+
+ method_map = { :'[]' => 1, :fetch => 1, :index => 1, :values_at => 1,
+ :has_key? => true, :include? => true, :key => true,
+ :member? => true }
+
+ hashes.each do |name, hash|
+ method_map.sort_by { |m| m.to_s }.each do |meth, expected|
+ assert_equal(expected, hash.send(meth, 'a'), "Calling #{name}.#{meth} 'a'")
+ assert_equal(expected, hash.send(meth, :a), "Calling #{name}.#{meth} :a")
+ end
+ end
+
+ assert_equal [1, 2], @strings.values_at('a', 'b')
+ assert_equal [1, 2], @strings.values_at(:a, :b)
+ assert_equal [1, 2], @symbols.values_at('a', 'b')
+ assert_equal [1, 2], @symbols.values_at(:a, :b)
+ assert_equal [1, 2], @mixed.values_at('a', 'b')
+ assert_equal [1, 2], @mixed.values_at(:a, :b)
end
def test_indifferent_writing
@@ -71,6 +91,39 @@ class HashExtTest < Test::Unit::TestCase
assert_equal hash[3], 3
end
+ def test_indifferent_assorted
+ @strings = @strings.with_indifferent_access
+ @symbols = @symbols.with_indifferent_access
+ @mixed = @mixed.with_indifferent_access
+
+ assert_equal 'a', @strings.send(:convert_key, :a)
+
+ assert_equal 1, @strings.fetch('a')
+ assert_equal 1, @strings.fetch(:a.to_s)
+ assert_equal 1, @strings.fetch(:a)
+
+ hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed }
+ method_map = { :'[]' => 1, :fetch => 1, :values_at => [1],
+ :has_key? => true, :include? => true, :key? => true,
+ :member? => true }
+
+ hashes.each do |name, hash|
+ method_map.sort_by { |m| m.to_s }.each do |meth, expected|
+ assert_equal(expected, hash.send(meth, 'a'),
+ "Calling #{name}.#{meth} 'a'")
+ assert_equal(expected, hash.send(meth, :a),
+ "Calling #{name}.#{meth} :a")
+ end
+ end
+
+ assert_equal [1, 2], @strings.values_at('a', 'b')
+ assert_equal [1, 2], @strings.values_at(:a, :b)
+ assert_equal [1, 2], @symbols.values_at('a', 'b')
+ assert_equal [1, 2], @symbols.values_at(:a, :b)
+ assert_equal [1, 2], @mixed.values_at('a', 'b')
+ assert_equal [1, 2], @mixed.values_at(:a, :b)
+ end
+
def test_assert_valid_keys
assert_nothing_raised do
{ :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
@@ -80,4 +133,12 @@ class HashExtTest < Test::Unit::TestCase
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
end
end
+
+ def test_indifferent_subhashes
+ h = {'user' => {'id' => 5}}.with_indifferent_access
+ ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}}
+
+ h = {:user => {:id => 5}}.with_indifferent_access
+ ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}}
+ end
end