Sha256: ff3fc08af98731e2354fe80622e3648ed8b6d5405e2fa664e7707141389d8439

Contents?: true

Size: 947 Bytes

Versions: 18

Compression:

Stored size: 947 Bytes

Contents

# -*- encoding : utf-8 -*-
require 'spec_helper'

describe Hash do
  context 'to_key_indexed_array' do
    it 'should convert hash to array using keys as indexes' do
      hash = {1 => 'x', 2 => 1, 5 => 'a'}
      hash.to_key_indexed_array.should == [nil, 'x', 1, nil, nil, 'a']
    end

    it 'should throw exception if not all of the keys are numbers' do
      hash = {1 => 'x', 'b' => 2}
      expect { hash.to_key_indexed_array }.to raise_error(ArgumentError)
    end

    context 'should take optional min_size and default parameters' do
      let(:hash) { {1 => 'x', 2 => 1, 5 => 'a'} }
      it 'should fill array with defaults up to min_size' do
        hash.to_key_indexed_array(min_size: 8, default: 0).should == [0, 'x', 1, 0, 0, 'a', 0, 0]
      end
      it 'should use default value where key is not in hash' do
        hash.to_key_indexed_array(min_size: 2, default: 0).should == [0, 'x', 1, 0, 0, 'a']
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
git_stats-1.0.17 spec/hash_extension_spec.rb
git_stats-1.0.16 spec/hash_extension_spec.rb
git_stats-1.0.15 spec/hash_extension_spec.rb
git_stats-1.0.14 spec/hash_extension_spec.rb
git_stats-1.0.13 spec/hash_extension_spec.rb
git_stats-1.0.12 spec/hash_extension_spec.rb
git_stats-1.0.11 spec/hash_extension_spec.rb
git_stats-1.0.10 spec/hash_extension_spec.rb
git_stats-1.0.9 spec/hash_extension_spec.rb
git_stats-1.0.8 spec/hash_extension_spec.rb
git_stats-1.0.7 spec/hash_extension_spec.rb
git_stats-1.0.6 spec/hash_extension_spec.rb
git_stats-1.0.5 spec/hash_extension_spec.rb
git_stats-1.0.4 spec/hash_extension_spec.rb
git_stats-1.0.3 spec/hash_extension_spec.rb
git_stats-1.0.2 spec/hash_extension_spec.rb
git_stats-1.0.1 spec/hash_extension_spec.rb
git_stats-1.0.0 spec/hash_extension_spec.rb