@@ -242,27 +242,37 @@ def test_indifferent_update
242
242
hash [ :a ] = "a"
243
243
hash [ "b" ] = "b"
244
244
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 )
248
246
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 )
252
250
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 )
256
252
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
259
258
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 ] )
261
268
end
262
269
263
270
def test_update_with_multiple_arguments
264
271
hash = HashWithIndifferentAccess . new
265
- hash . update ( { "a" => 1 } , { "b" => 2 } )
272
+ hash . update (
273
+ { "a" => 1 } . with_indifferent_access ,
274
+ { "b" => 2 }
275
+ )
266
276
267
277
assert_equal 1 , hash [ "a" ]
268
278
assert_equal 2 , hash [ "b" ]
@@ -591,6 +601,13 @@ def test_indifferent_to_hash
591
601
assert_equal @strings , roundtrip
592
602
assert_equal "1234" , roundtrip . default
593
603
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
+
594
611
# Ensure nested hashes are not HashWithIndifferentAccess
595
612
new_to_hash = @nested_mixed . with_indifferent_access . to_hash
596
613
assert_not new_to_hash . instance_of? ( HashWithIndifferentAccess )
0 commit comments