class HashExtTest
Public Instance Methods
setup()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 13 def setup @strings = { "a" => 1, "b" => 2 } @nested_strings = { "a" => { "b" => { "c" => 3 } } } @symbols = { a: 1, b: 2 } @nested_symbols = { a: { b: { c: 3 } } } @mixed = { :a => 1, "b" => 2 } @nested_mixed = { "a" => { b: { "c" => 3 } } } @integers = { 0 => 1, 1 => 2 } @nested_integers = { 0 => { 1 => { 2 => 3 } } } @illegal_symbols = { [] => 3 } @nested_illegal_symbols = { [] => { [] => 3 } } @upcase_strings = { "A" => 1, "B" => 2 } @nested_upcase_strings = { "A" => { "B" => { "C" => 3 } } } @string_array_of_hashes = { "a" => [ { "b" => 2 }, { "c" => 3 }, 4 ] } @symbol_array_of_hashes = { a: [ { b: 2 }, { c: 3 }, 4 ] } @mixed_array_of_hashes = { a: [ { b: 2 }, { "c" => 3 }, 4 ] } @upcase_array_of_hashes = { "A" => [ { "B" => 2 }, { "C" => 3 }, 4 ] } end
test_assert_valid_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 242 def test_assert_valid_keys assert_nothing_raised do { failure: "stuff", funny: "business" }.assert_valid_keys([ :failure, :funny ]) { failure: "stuff", funny: "business" }.assert_valid_keys(:failure, :funny) end # not all valid keys are required to be present assert_nothing_raised do { failure: "stuff", funny: "business" }.assert_valid_keys([ :failure, :funny, :sunny ]) { failure: "stuff", funny: "business" }.assert_valid_keys(:failure, :funny, :sunny) end exception = assert_raise ArgumentError do { failore: "stuff", funny: "business" }.assert_valid_keys([ :failure, :funny ]) end assert_equal "Unknown key: :failore. Valid keys are: :failure, :funny", exception.message exception = assert_raise ArgumentError do { failore: "stuff", funny: "business" }.assert_valid_keys(:failure, :funny) end assert_equal "Unknown key: :failore. Valid keys are: :failure, :funny", exception.message exception = assert_raise ArgumentError do { failore: "stuff", funny: "business" }.assert_valid_keys([ :failure ]) end assert_equal "Unknown key: :failore. Valid keys are: :failure", exception.message exception = assert_raise ArgumentError do { failore: "stuff", funny: "business" }.assert_valid_keys(:failure) end assert_equal "Unknown key: :failore. Valid keys are: :failure", exception.message end
test_compact()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 459 def test_compact hash_contain_nil_value = @symbols.merge(z: nil) hash_with_only_nil_values = { a: nil, b: nil } h = hash_contain_nil_value.dup assert_equal(@symbols, h.compact) assert_equal(hash_contain_nil_value, h) h = hash_with_only_nil_values.dup assert_equal({}, h.compact) assert_equal(hash_with_only_nil_values, h) h = @symbols.dup assert_equal(@symbols, h.compact) assert_equal(@symbols, h) end
test_compact!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 476 def test_compact! hash_contain_nil_value = @symbols.merge(z: nil) hash_with_only_nil_values = { a: nil, b: nil } h = hash_contain_nil_value.dup assert_equal(@symbols, h.compact!) assert_equal(@symbols, h) h = hash_with_only_nil_values.dup assert_equal({}, h.compact!) assert_equal({}, h) h = @symbols.dup assert_nil(h.compact!) assert_equal(@symbols, h) end
test_deep_merge()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 274 def test_deep_merge hash_1 = { a: "a", b: "b", c: { c1: "c1", c2: "c2", c3: { d1: "d1" } } } hash_2 = { a: 1, c: { c1: 2, c3: { d2: "d2" } } } expected = { a: 1, b: "b", c: { c1: 2, c2: "c2", c3: { d1: "d1", d2: "d2" } } } assert_equal expected, hash_1.deep_merge(hash_2) hash_1.deep_merge!(hash_2) assert_equal expected, hash_1 end
test_deep_merge_with_block()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 284 def test_deep_merge_with_block hash_1 = { a: "a", b: "b", c: { c1: "c1", c2: "c2", c3: { d1: "d1" } } } hash_2 = { a: 1, c: { c1: 2, c3: { d2: "d2" } } } expected = { a: [:a, "a", 1], b: "b", c: { c1: [:c1, "c1", 2], c2: "c2", c3: { d1: "d1", d2: "d2" } } } assert_equal(expected, hash_1.deep_merge(hash_2) { |k, o, n| [k, o, n] }) hash_1.deep_merge!(hash_2) { |k, o, n| [k, o, n] } assert_equal expected, hash_1 end
test_deep_merge_with_falsey_values()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 294 def test_deep_merge_with_falsey_values hash_1 = { e: false } hash_2 = { e: "e" } expected = { e: [:e, false, "e"] } assert_equal(expected, hash_1.deep_merge(hash_2) { |k, o, n| [k, o, n] }) hash_1.deep_merge!(hash_2) { |k, o, n| [k, o, n] } assert_equal expected, hash_1 end
test_deep_stringify_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 198 def test_deep_stringify_keys assert_equal @nested_strings, @nested_symbols.deep_stringify_keys assert_equal @nested_strings, @nested_strings.deep_stringify_keys assert_equal @nested_strings, @nested_mixed.deep_stringify_keys assert_equal @string_array_of_hashes, @string_array_of_hashes.deep_stringify_keys assert_equal @string_array_of_hashes, @symbol_array_of_hashes.deep_stringify_keys assert_equal @string_array_of_hashes, @mixed_array_of_hashes.deep_stringify_keys end
test_deep_stringify_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 226 def test_deep_stringify_keys! assert_equal @nested_strings, @nested_symbols.deep_dup.deep_stringify_keys! assert_equal @nested_strings, @nested_strings.deep_dup.deep_stringify_keys! assert_equal @nested_strings, @nested_mixed.deep_dup.deep_stringify_keys! assert_equal @string_array_of_hashes, @string_array_of_hashes.deep_dup.deep_stringify_keys! assert_equal @string_array_of_hashes, @symbol_array_of_hashes.deep_dup.deep_stringify_keys! assert_equal @string_array_of_hashes, @mixed_array_of_hashes.deep_dup.deep_stringify_keys! end
test_deep_stringify_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 207 def test_deep_stringify_keys_not_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_stringify_keys assert_equal @nested_mixed, transformed_hash end
test_deep_stringify_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 235 def test_deep_stringify_keys_with_bang_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_stringify_keys! assert_equal @nested_strings, transformed_hash assert_equal({ "a" => { b: { "c" => 3 } } }, @nested_mixed) end
test_deep_symbolize_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 122 def test_deep_symbolize_keys assert_equal @nested_symbols, @nested_symbols.deep_symbolize_keys assert_equal @nested_symbols, @nested_strings.deep_symbolize_keys assert_equal @nested_symbols, @nested_mixed.deep_symbolize_keys assert_equal @symbol_array_of_hashes, @string_array_of_hashes.deep_symbolize_keys assert_equal @symbol_array_of_hashes, @symbol_array_of_hashes.deep_symbolize_keys assert_equal @symbol_array_of_hashes, @mixed_array_of_hashes.deep_symbolize_keys end
test_deep_symbolize_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 150 def test_deep_symbolize_keys! assert_equal @nested_symbols, @nested_symbols.deep_dup.deep_symbolize_keys! assert_equal @nested_symbols, @nested_strings.deep_dup.deep_symbolize_keys! assert_equal @nested_symbols, @nested_mixed.deep_dup.deep_symbolize_keys! assert_equal @symbol_array_of_hashes, @string_array_of_hashes.deep_dup.deep_symbolize_keys! assert_equal @symbol_array_of_hashes, @symbol_array_of_hashes.deep_dup.deep_symbolize_keys! assert_equal @symbol_array_of_hashes, @mixed_array_of_hashes.deep_dup.deep_symbolize_keys! end
test_deep_symbolize_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 131 def test_deep_symbolize_keys_not_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_symbolize_keys assert_equal @nested_mixed, transformed_hash end
test_deep_symbolize_keys_preserves_integer_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 181 def test_deep_symbolize_keys_preserves_integer_keys assert_equal @nested_integers, @nested_integers.deep_symbolize_keys assert_equal @nested_integers, @nested_integers.deep_dup.deep_symbolize_keys! end
test_deep_symbolize_keys_preserves_keys_that_cant_be_symbolized()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 171 def test_deep_symbolize_keys_preserves_keys_that_cant_be_symbolized assert_equal @nested_illegal_symbols, @nested_illegal_symbols.deep_symbolize_keys assert_equal @nested_illegal_symbols, @nested_illegal_symbols.deep_dup.deep_symbolize_keys! end
test_deep_symbolize_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 159 def test_deep_symbolize_keys_with_bang_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_symbolize_keys! assert_equal @nested_symbols, transformed_hash assert_equal({ "a" => { b: { "c" => 3 } } }, @nested_mixed) end
test_deep_transform_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 66 def test_deep_transform_keys assert_equal @nested_upcase_strings, @nested_symbols.deep_transform_keys { |key| key.to_s.upcase } assert_equal @nested_upcase_strings, @nested_strings.deep_transform_keys { |key| key.to_s.upcase } assert_equal @nested_upcase_strings, @nested_mixed.deep_transform_keys { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @string_array_of_hashes.deep_transform_keys { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @symbol_array_of_hashes.deep_transform_keys { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @mixed_array_of_hashes.deep_transform_keys { |key| key.to_s.upcase } end
test_deep_transform_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 94 def test_deep_transform_keys! assert_equal @nested_upcase_strings, @nested_symbols.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @nested_upcase_strings, @nested_strings.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @nested_upcase_strings, @nested_mixed.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @string_array_of_hashes.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @symbol_array_of_hashes.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_array_of_hashes, @mixed_array_of_hashes.deep_dup.deep_transform_keys! { |key| key.to_s.upcase } end
test_deep_transform_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 75 def test_deep_transform_keys_not_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_transform_keys { |key| key.to_s.upcase } assert_equal @nested_mixed, transformed_hash end
test_deep_transform_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 103 def test_deep_transform_keys_with_bang_mutates transformed_hash = @nested_mixed.deep_dup transformed_hash.deep_transform_keys! { |key| key.to_s.upcase } assert_equal @nested_upcase_strings, transformed_hash assert_equal({ "a" => { b: { "c" => 3 } } }, @nested_mixed) end
test_except()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 421 def test_except original = { a: "x", b: "y", c: 10 } expected = { a: "x", b: "y" } # Should return a new hash without the given keys. assert_equal expected, original.except(:c) assert_not_equal expected, original # Should replace the hash without the given keys. assert_equal expected, original.except!(:c) assert_equal expected, original end
test_except_does_not_delete_values_in_original()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 452 def test_except_does_not_delete_values_in_original original = { a: "x", b: "y" } assert_not_called(original, :delete) do original.except(:a) end end
test_except_with_more_than_one_argument()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 434 def test_except_with_more_than_one_argument original = { a: "x", b: "y", c: 10 } expected = { a: "x" } assert_equal expected, original.except(:b, :c) assert_equal expected, original.except!(:b, :c) assert_equal expected, original end
test_except_with_original_frozen()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 444 def test_except_with_original_frozen original = { a: "x", b: "y" } original.freeze assert_nothing_raised { original.except(:a) } assert_raise(RuntimeError) { original.except!(:a) } end
test_extract()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 402 def test_extract original = { a: 1, b: 2, c: 3, d: 4 } expected = { a: 1, b: 2 } remaining = { c: 3, d: 4 } assert_equal expected, original.extract!(:a, :b, :x) assert_equal remaining, original end
test_extract_nils()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 411 def test_extract_nils original = { a: nil, b: nil } expected = { a: nil } extracted = original.extract!(:a, :x) assert_equal expected, extracted assert_nil extracted[:a] assert_nil extracted[:x] end
test_methods()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 32 def test_methods h = {} assert_respond_to h, :transform_keys assert_respond_to h, :transform_keys! assert_respond_to h, :deep_transform_keys assert_respond_to h, :deep_transform_keys! assert_respond_to h, :symbolize_keys assert_respond_to h, :symbolize_keys! assert_respond_to h, :deep_symbolize_keys assert_respond_to h, :deep_symbolize_keys! assert_respond_to h, :stringify_keys assert_respond_to h, :stringify_keys! assert_respond_to h, :deep_stringify_keys assert_respond_to h, :deep_stringify_keys! assert_respond_to h, :to_options assert_respond_to h, :to_options! assert_respond_to h, :compact assert_respond_to h, :compact! assert_respond_to h, :except assert_respond_to h, :except! end
test_reverse_merge()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 304 def test_reverse_merge defaults = { d: 0, a: "x", b: "y", c: 10 }.freeze options = { a: 1, b: 2 } expected = { d: 0, a: 1, b: 2, c: 10 } # Should merge defaults into options, creating a new hash. assert_equal expected, options.reverse_merge(defaults) assert_not_equal expected, options # Should merge! defaults into options, replacing options. merged = options.dup assert_equal expected, merged.reverse_merge!(defaults) assert_equal expected, merged # Make the order consistent with the non-overwriting reverse merge. assert_equal expected.keys, merged.keys # Should be an alias for reverse_merge! merged = options.dup assert_equal expected, merged.reverse_update(defaults) assert_equal expected, merged end
test_slice()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 342 def test_slice original = { a: "x", b: "y", c: 10 } expected = { a: "x", b: "y" } # Should return a new hash with only the given keys. assert_equal expected, original.slice(:a, :b) assert_not_equal expected, original end
test_slice_bang_does_not_override_default()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 384 def test_slice_bang_does_not_override_default hash = Hash.new(0) hash.update(a: 1, b: 2) hash.slice!(:a) assert_equal 0, hash[:c] end
test_slice_bang_does_not_override_default_proc()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 393 def test_slice_bang_does_not_override_default_proc hash = Hash.new { |h, k| h[k] = [] } hash.update(a: 1, b: 2) hash.slice!(:a) assert_equal [], hash[:c] end
test_slice_inplace()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 351 def test_slice_inplace original = { a: "x", b: "y", c: 10 } expected = { c: 10 } # Should replace the hash with only the given keys. assert_equal expected, original.slice!(:a, :b) end
test_slice_inplace_with_an_array_key()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 368 def test_slice_inplace_with_an_array_key original = { :a => "x", :b => "y", :c => 10, [:a, :b] => "an array key" } expected = { a: "x", b: "y" } # Should replace the hash with only the given keys when given an array key. assert_equal expected, original.slice!([:a, :b], :c) end
test_slice_with_an_array_key()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 359 def test_slice_with_an_array_key original = { :a => "x", :b => "y", :c => 10, [:a, :b] => "an array key" } expected = { [:a, :b] => "an array key", :c => 10 } # Should return a new hash with only the given keys when given an array key. assert_equal expected, original.slice([:a, :b], :c) assert_not_equal expected, original end
test_slice_with_splatted_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 376 def test_slice_with_splatted_keys original = { :a => "x", :b => "y", :c => 10, [:a, :b] => "an array key" } expected = { a: "x", b: "y" } # Should grab each of the splatted keys. assert_equal expected, original.slice(*[:a, :b]) end
test_stringify_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 186 def test_stringify_keys assert_equal @strings, @symbols.stringify_keys assert_equal @strings, @strings.stringify_keys assert_equal @strings, @mixed.stringify_keys end
test_stringify_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 213 def test_stringify_keys! assert_equal @strings, @symbols.dup.stringify_keys! assert_equal @strings, @strings.dup.stringify_keys! assert_equal @strings, @mixed.dup.stringify_keys! end
test_stringify_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 192 def test_stringify_keys_not_mutates transformed_hash = @mixed.dup transformed_hash.stringify_keys assert_equal @mixed, transformed_hash end
test_stringify_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 219 def test_stringify_keys_with_bang_mutates transformed_hash = @mixed.dup transformed_hash.stringify_keys! assert_equal @strings, transformed_hash assert_equal({ :a => 1, "b" => 2 }, @mixed) end
test_symbolize_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 110 def test_symbolize_keys assert_equal @symbols, @symbols.symbolize_keys assert_equal @symbols, @strings.symbolize_keys assert_equal @symbols, @mixed.symbolize_keys end
test_symbolize_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 137 def test_symbolize_keys! assert_equal @symbols, @symbols.dup.symbolize_keys! assert_equal @symbols, @strings.dup.symbolize_keys! assert_equal @symbols, @mixed.dup.symbolize_keys! end
test_symbolize_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 116 def test_symbolize_keys_not_mutates transformed_hash = @mixed.dup transformed_hash.symbolize_keys assert_equal @mixed, transformed_hash end
test_symbolize_keys_preserves_integer_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 176 def test_symbolize_keys_preserves_integer_keys assert_equal @integers, @integers.symbolize_keys assert_equal @integers, @integers.dup.symbolize_keys! end
test_symbolize_keys_preserves_keys_that_cant_be_symbolized()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 166 def test_symbolize_keys_preserves_keys_that_cant_be_symbolized assert_equal @illegal_symbols, @illegal_symbols.symbolize_keys assert_equal @illegal_symbols, @illegal_symbols.dup.symbolize_keys! end
test_symbolize_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 143 def test_symbolize_keys_with_bang_mutates transformed_hash = @mixed.dup transformed_hash.deep_symbolize_keys! assert_equal @symbols, transformed_hash assert_equal({ :a => 1, "b" => 2 }, @mixed) end
test_transform_keys()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 54 def test_transform_keys assert_equal @upcase_strings, @strings.transform_keys { |key| key.to_s.upcase } assert_equal @upcase_strings, @symbols.transform_keys { |key| key.to_s.upcase } assert_equal @upcase_strings, @mixed.transform_keys { |key| key.to_s.upcase } end
test_transform_keys!()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 81 def test_transform_keys! assert_equal @upcase_strings, @symbols.dup.transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_strings, @strings.dup.transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_strings, @mixed.dup.transform_keys! { |key| key.to_s.upcase } end
test_transform_keys_not_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 60 def test_transform_keys_not_mutates transformed_hash = @mixed.dup transformed_hash.transform_keys { |key| key.to_s.upcase } assert_equal @mixed, transformed_hash end
test_transform_keys_with_bang_mutates()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 87 def test_transform_keys_with_bang_mutates transformed_hash = @mixed.dup transformed_hash.transform_keys! { |key| key.to_s.upcase } assert_equal @upcase_strings, transformed_hash assert_equal({ :a => 1, "b" => 2 }, @mixed) end
test_with_defaults_aliases_reverse_merge()
click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 327 def test_with_defaults_aliases_reverse_merge defaults = { a: "x", b: "y", c: 10 }.freeze options = { a: 1, b: 2 } expected = { a: 1, b: 2, c: 10 } # Should be an alias for reverse_merge assert_equal expected, options.with_defaults(defaults) assert_not_equal expected, options # Should be an alias for reverse_merge! merged = options.dup assert_equal expected, merged.with_defaults!(defaults) assert_equal expected, merged end