Skip to content

Commit 9b309ad

Browse files
kwstannardbyroot
authored andcommitted
Some test improvements for hash with indifferent access
Test copying of default_proc in to_hash. Add testing of update w/ indifferent_access Add update with block test
1 parent a23de8e commit 9b309ad

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

activesupport/test/hash_with_indifferent_access_test.rb

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,37 @@ def test_indifferent_update
242242
hash[:a] = "a"
243243
hash["b"] = "b"
244244

245-
updated_with_strings = hash.update(@strings)
246-
updated_with_symbols = hash.update(@symbols)
247-
updated_with_mixed = hash.update(@mixed)
245+
assert_equal(hash.object_id, hash.update(@strings).object_id)
248246

249-
assert_equal 1, updated_with_strings[:a]
250-
assert_equal 1, updated_with_strings["a"]
251-
assert_equal 2, updated_with_strings["b"]
247+
hash.update(@symbols)
248+
hash.update(@mixed)
249+
hash.update(@mixed.with_indifferent_access)
252250

253-
assert_equal 1, updated_with_symbols[:a]
254-
assert_equal 2, updated_with_symbols["b"]
255-
assert_equal 2, updated_with_symbols[:b]
251+
assert_equal(["a", "b"], hash.keys)
256252

257-
assert_equal 1, updated_with_mixed[:a]
258-
assert_equal 2, updated_with_mixed["b"]
253+
assert_equal 1, hash[:a]
254+
assert_equal 1, hash["a"]
255+
assert_equal 2, hash[:b]
256+
assert_equal 2, hash["b"]
257+
end
259258

260-
assert [updated_with_strings, updated_with_symbols, updated_with_mixed].all? { |h| h.keys.size == 2 }
259+
def test_update_with_block
260+
h1 = { "a" => 1, "b" => "x" }.with_indifferent_access
261+
h2 = { a: 2, b: "y" }
262+
263+
merged_hash = h1.update(h2) { |k, v1, v2| [k, v1, v2].join }
264+
265+
assert_equal(["a", "b"], merged_hash.keys)
266+
assert_equal("a12", merged_hash[:a])
267+
assert_equal("bxy", merged_hash[:b])
261268
end
262269

263270
def test_update_with_multiple_arguments
264271
hash = HashWithIndifferentAccess.new
265-
hash.update({ "a" => 1 }, { "b" => 2 })
272+
hash.update(
273+
{ "a" => 1 }.with_indifferent_access,
274+
{ "b" => 2 }
275+
)
266276

267277
assert_equal 1, hash["a"]
268278
assert_equal 2, hash["b"]
@@ -591,6 +601,13 @@ def test_indifferent_to_hash
591601
assert_equal @strings, roundtrip
592602
assert_equal "1234", roundtrip.default
593603

604+
# Should preserve the default proc
605+
mixed_with_default = @mixed.dup
606+
_proc = ->(h, k) { 1 }
607+
mixed_with_default.default_proc = _proc
608+
roundtrip = mixed_with_default.with_indifferent_access.to_hash
609+
assert_equal _proc, roundtrip.default_proc
610+
594611
# Ensure nested hashes are not HashWithIndifferentAccess
595612
new_to_hash = @nested_mixed.with_indifferent_access.to_hash
596613
assert_not new_to_hash.instance_of?(HashWithIndifferentAccess)

0 commit comments

Comments
 (0)