Sha256: beca2e9f45e12525cd0530b6c3d5ceaf27980377ad7f2d0f70c9665336301678

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

require 'test_helper'

module Tins
  class HashSymbolizeKeysRecursiveTest < Test::Unit::TestCase
    require 'tins/xt/hash_symbolize_keys_recursive'

    def test_symbolize
      hash = {
        'key' => [
          {
            'key' => {
              'key' => true
            },
            'o' => Object.new,
          }
        ],
      }
      hash2 = hash.symbolize_keys_recursive
      assert hash2[:key][0][:key][:key]
      hash.symbolize_keys_recursive!
      assert hash[:key][0][:key][:key]
    end

    def test_symbolize_bang
      hash = { 'foo' => 'bar' }
      hash.symbolize_keys_recursive!
      assert_equal({ foo: 'bar' }, hash)
    end

    def test_symbolize_with_circular_array
      circular_array = [].tap { |a| a << a }
      assert_equal(
        { foo: [ nil ] },
        { 'foo' => circular_array }.symbolize_keys_recursive
      )
      assert_equal(
        { foo: [ :circular ] },
        { 'foo' => circular_array }.symbolize_keys_recursive(circular: :circular)
      )
    end

    def test_symbolize_with_circular_hash
      circular_hash = {}.tap { |h| h['foo'] = h }
      circular_hash_symbol = {}.tap { |h| h[:foo] = nil }
      assert_equal(
        { bar: circular_hash_symbol },
        { 'bar' => circular_hash }.symbolize_keys_recursive
      )
      assert_equal(
        { bar: { foo: :circular } },
        { 'bar' => circular_hash }.symbolize_keys_recursive(circular: :circular)
      )
    end

    def test_symbolize_deeper_nesting
      hash = { 'foo' => [ true, [ { 'bar' => {}.tap { |h| h['foo'] = h } }, 3.141, [].tap { |arr| arr << arr } ] ] }
      assert_equal(
        {foo: [true, [{bar: {foo: :circular}}, 3.141, [:circular]]]},
        hash.symbolize_keys_recursive(circular: :circular)
      )
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tins-1.37.0 tests/hash_symbolize_keys_recursive_test.rb
tins-1.36.1 tests/hash_symbolize_keys_recursive_test.rb
tins-1.36.0 tests/hash_symbolize_keys_recursive_test.rb
tins-1.35.0 tests/hash_symbolize_keys_recursive_test.rb