aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/hash_ext_test.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-12-17 00:49:41 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-12-17 00:49:41 +0000
commit86deb270953f9c5b62813d3e1938f33cc807bd7f (patch)
treeb057cb0c34d15e3242f07a7b2fe54f9a9e1c16f3 /activesupport/test/core_ext/hash_ext_test.rb
parent05a5209f823f18e423e320f6bce4a91532c8bd03 (diff)
downloadrails-86deb270953f9c5b62813d3e1938f33cc807bd7f.tar.gz
rails-86deb270953f9c5b62813d3e1938f33cc807bd7f.tar.bz2
rails-86deb270953f9c5b62813d3e1938f33cc807bd7f.zip
Hash#slice(*keys) returns a new hash with only the given keys. #slice! replaces the hash with only the given keys. Works with HashWithIndifferentAccess also.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5726 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.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 8e20f15518..90468fd5a9 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -243,6 +243,35 @@ class HashExtTest < Test::Unit::TestCase
def test_diff
assert_equal({ :a => 2 }, { :a => 2, :b => 5 }.diff({ :a => 1, :b => 5 }))
end
+
+ def test_slice
+ original = { :a => 'x', :b => 'y', :c => 10 }
+ expected = { :a => 'x', :b => 'y' }
+
+ # Should return a new hash with only the given keys.
+ assert_equal expected, original.slice(:a, :b)
+ assert_not_equal expected, original
+
+ # Should replace the hash with only the given keys.
+ assert_equal expected, original.slice!(:a, :b)
+ assert_equal expected, original
+ end
+
+ def test_indifferent_slice
+ original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
+ expected = { :a => 'x', :b => 'y' }.with_indifferent_access
+
+ [['a', 'b'], [:a, :b]].each do |keys|
+ # Should return a new hash with only the given keys.
+ assert_equal expected, original.slice(*keys), keys.inspect
+ assert_not_equal expected, original
+
+ # Should replace the hash with only the given keys.
+ copy = original.dup
+ assert_equal expected, copy.slice!(*keys)
+ assert_equal expected, copy
+ end
+ end
end
class IWriteMyOwnXML