diff options
author | Lisa Ugray <lisa.ugray@shopify.com> | 2017-07-11 12:57:16 -0400 |
---|---|---|
committer | Lisa Ugray <lisa.ugray@shopify.com> | 2017-07-17 14:37:53 -0400 |
commit | a218a35e6602ac5591c95715d3d8fdad34009239 (patch) | |
tree | 5758272ac3c2d65737c84b41360e53bc89d932f2 /activesupport/test | |
parent | e319b01de8d42e1c8dd511d882b988121c81604f (diff) | |
download | rails-a218a35e6602ac5591c95715d3d8fdad34009239.tar.gz rails-a218a35e6602ac5591c95715d3d8fdad34009239.tar.bz2 rails-a218a35e6602ac5591c95715d3d8fdad34009239.zip |
Fix HashWithIndifferentAccess#default when include?(nil)
The implementation of HashWithIndifferentAccess#default didn't
distinguish `default` from `default(nil)`, which caused an incorrect
result for `default` if `nil` was used as a key.
Define HashWithIndifferentAccess#dig so that hackery that behaves
differently from Hash#default can be removed from
HashWithIndifferentAccess#default.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/hash_with_indifferent_access_test.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb index 4acdd10de6..77fc039459 100644 --- a/activesupport/test/hash_with_indifferent_access_test.rb +++ b/activesupport/test/hash_with_indifferent_access_test.rb @@ -537,6 +537,32 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase assert_equal 1234, data.dig(:this, :views) end + def test_argless_default_with_existing_nil_key + h = Hash.new(:default).merge(nil => "defined").with_indifferent_access + + assert_equal :default, h.default + end + + def test_default_with_argument + h = Hash.new { 5 }.merge(1 => 2).with_indifferent_access + + assert_equal 5, h.default(1) + end + + def test_default_proc + h = ActiveSupport::HashWithIndifferentAccess.new { |hash, key| key } + + assert_nil h.default + assert_equal "foo", h.default("foo") + assert_equal "foo", h.default(:foo) + end + + def test_double_conversion_with_nil_key + h = { nil => "defined" }.with_indifferent_access.with_indifferent_access + + assert_equal nil, h[:undefined_key] + end + def test_assorted_keys_not_stringified original = { Object.new => 2, 1 => 2, [] => true } indiff = original.with_indifferent_access |