Sha256: 3f5cd69e506378f0dba17708ed320e70fbbd64476eed38ed4d0c51435c51b575

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 KB

Contents

require 'abstractivator/sort'

describe Abstractivator::Sort do
  include Abstractivator::Sort
  describe '#deep_sort_hash' do
    context 'when empty hash' do
      let(:hash) { {} }
      it 'returns empty hash' do
        expect_same_order(deep_sort, {})
      end
    end

    context 'when single level' do
      let(:hash) { {one: 1, two: '2', three: 3, four: '4'} }
      it 'returns new hash with the keys sorted alpha-numerically' do
        expected = {four: '4', one: 1, three: 3, two: '2'}
        expect_same_order(deep_sort, expected)
      end
    end

    context 'when mult-level hash' do
      let(:hash) { {one: 1, two: {three: {four: '4', five: 5}}, six: 6} }
      it 'returns new hash with the keys sorted alpha-numerically' do
        expected = {one: 1, six: 6, two: {three: {five: 5, four: '4'}}}
        expect_same_order(deep_sort, expected)
      end
    end

    context 'when contains an array' do
      let(:hash) { {one: 1, two: [{three: {four: '4', five: 5}}, 'six', :seven], eight: 8} }
      it 'returns new hash with the keys sorted alpha-numerically (does not sort arrays)' do
        expected = {eight: 8, one: 1, two: [{three: {five: 5, four: '4'}}, 'six', :seven]}
        expect_same_order(deep_sort, expected)
      end
    end

    def deep_sort
      deep_sort_hash(hash)
    end

    def expect_same_order(actual, expected)
      case actual
      when Hash
        expect(actual.keys).to eql expected.keys
        expect_same_order(actual.values, expected.values)
      when Array
        expect(actual.size).to eql expected.size
        actual.zip(expected).each { |a, e| expect_same_order(a, e) }
      else
        expect(actual).to eql expected
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
abstractivator-0.20.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.19.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.18.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.17.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.16.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.15.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.14.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.13.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.12.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.11.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.10.0 spec/lib/abstractivator/sort_spec.rb
abstractivator-0.8.0 spec/lib/abstractivator/sort_spec.rb