aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2019-05-01 15:36:49 -0500
committerGitHub <noreply@github.com>2019-05-01 15:36:49 -0500
commitb8f88b961092b6b289d18017ab72f3958a315b1d (patch)
treefa978b7021a06c72399870b2320b2eb935190e9b /activesupport/test
parentcc760e9106404f73ae682f67806e23f36d22add3 (diff)
parent75a2ca6e9dca2c36d80478bcc78ac7bd25bf38f7 (diff)
downloadrails-b8f88b961092b6b289d18017ab72f3958a315b1d.tar.gz
rails-b8f88b961092b6b289d18017ab72f3958a315b1d.tar.bz2
rails-b8f88b961092b6b289d18017ab72f3958a315b1d.zip
Merge pull request #34642 from azimux/improve-hwia-initialize-by-skipping-to_h-if-already-a-hash
HashWithIndifferentAccess#initialize performance improvement
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/hash_with_indifferent_access_test.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb
index 1d6a07ec5f..af96231801 100644
--- a/activesupport/test/hash_with_indifferent_access_test.rb
+++ b/activesupport/test/hash_with_indifferent_access_test.rb
@@ -836,4 +836,32 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase
assert_equal 3, hash_wia[:foo]
assert_equal 3, hash_wia[:bar]
end
+
+ def test_should_copy_the_default_when_converting_non_hash_to_hash_with_indifferent_access
+ non_hash = Object.new
+
+ def non_hash.to_hash
+ h = { foo: :bar }
+ h.default = :baz
+ h
+ end
+
+ hash_wia = HashWithIndifferentAccess.new(non_hash)
+ assert_equal :bar, hash_wia[:foo]
+ assert_equal :baz, hash_wia[:missing]
+ end
+
+ def test_should_copy_the_default_proc_when_converting_non_hash_to_hash_with_indifferent_access
+ non_hash = Object.new
+
+ def non_hash.to_hash
+ h = { foo: :bar }
+ h.default_proc = ->(hash, key) { hash[key] = :baz }
+ h
+ end
+
+ hash_wia = HashWithIndifferentAccess.new(non_hash)
+ assert_equal :bar, hash_wia[:foo]
+ assert_equal :baz, hash_wia[:missing]
+ end
end