0
@@ -282,6 +282,20 @@ class HashExtTest < Test::Unit::TestCase
0
assert_equal expected, original
0
+ # This is needed for something like hash.slice!(hash.keys.sort_by {rand} [0..4])
0
+ def test_slice_with_array_keys
0
+ original = { :a => 'x', :b => 'y', :c => 10 }
0
+ expected = { :a => 'x', :b => 'y' }
0
+ # Should return a new hash with only the given keys, when given an array of keys.
0
+ assert_equal expected, original.slice([:a, :b])
0
+ assert_not_equal expected, original
0
+ # Should replace the hash with only the given keys, when given an array of keys.
0
+ assert_equal expected, original.slice!([:a, :b])
0
+ assert_equal expected, original
0
def test_indifferent_slice
0
original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
0
expected = { :a => 'x', :b => 'y' }.with_indifferent_access
0
@@ -469,12 +483,12 @@ class HashToXmlTest < Test::Unit::TestCase
0
expected_topic_hash = {
0
@@ -552,7 +566,7 @@ class HashToXmlTest < Test::Unit::TestCase
0
assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"]
0
def test_empty_array_from_xml
0
@@ -650,13 +664,13 @@ class HashToXmlTest < Test::Unit::TestCase
0
assert_equal expected_bacon_hash, Hash.from_xml(bacon_xml)["bacon"]
0
def test_type_trickles_through_when_unknown
0
<weight type="double">0.5</weight>
0
<image type="ProductImage"><filename>image.gif</filename></image>
0
@@ -665,7 +679,7 @@ class HashToXmlTest < Test::Unit::TestCase
0
:image => {'type' => 'ProductImage', 'filename' => 'image.gif' },
0
- assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"]
0
+ assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"]
0
def test_should_use_default_value_for_unknown_key
0
@@ -699,41 +713,41 @@ class HashToXmlTest < Test::Unit::TestCase
0
assert_equal expected, hash.to_xml(@xml_options)
0
- def test_empty_string_works_for_typecast_xml_value
0
+ def test_empty_string_works_for_typecast_xml_value
0
assert_nothing_raised do
0
Hash.send!(:typecast_xml_value, "")
0
def test_escaping_to_xml
0
- :bare_string => 'First & Last Name',
0
+ :bare_string => 'First & Last Name',
0
:pre_escaped_string => 'First & Last Name'
0
expected_xml = '<person><bare-string>First & Last Name</bare-string><pre-escaped-string>First &amp; Last Name</pre-escaped-string></person>'
0
assert_equal expected_xml, hash.to_xml(@xml_options)
0
def test_unescaping_from_xml
0
xml_string = '<person><bare-string>First & Last Name</bare-string><pre-escaped-string>First &amp; Last Name</pre-escaped-string></person>'
0
- :bare_string => 'First & Last Name',
0
+ :bare_string => 'First & Last Name',
0
:pre_escaped_string => 'First & Last Name'
0
assert_equal expected_hash, Hash.from_xml(xml_string)['person']
0
def test_roundtrip_to_xml_from_xml
0
- :bare_string => 'First & Last Name',
0
+ :bare_string => 'First & Last Name',
0
:pre_escaped_string => 'First & Last Name'
0
assert_equal hash, Hash.from_xml(hash.to_xml(@xml_options))['person']
0
def test_datetime_xml_type_with_utc_time
0
@@ -744,7 +758,7 @@ class HashToXmlTest < Test::Unit::TestCase
0
assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
0
def test_datetime_xml_type_with_non_utc_time
0
@@ -755,7 +769,7 @@ class HashToXmlTest < Test::Unit::TestCase
0
assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
0
def test_datetime_xml_type_with_far_future_date
That will broke behavior when I use array as key original = {[5, 6] => ‘x’, [2, 3] => ‘y’, [1, 6] => 10 }
I think people can always use original.slice(*[:a, :b])
Could do Array(...) instead of flatten. I think that would work for both cases. Submit a patch to Lighthouse and I’ll have a look.
Yeah, I think this is crap.
if you have an array you should just splat it.
if you legitimately give it an array as a key it should be allowed
What sergey and matthew said. The implicit assumption that arrays aren’t used as keys within hashes is incorrect. The original implementation of slice was correct.
I’ve added a test to assert the original behaviour (added to the original ticket) http://rails.lighthouseapp.com/projects/8994/tickets/613-slice-and-slice-don-t-accept-an-array-of-keys#ticket-613-6