Sha256: 6efbe9be0581344267ddd46dc0e9844845bac8eafbe28f5a6c1191755b16031f

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

require 'spec_helper'

describe 'key_conversion' do

  it 'should convert keys to lowercase by default' do
    { 'HELLO' => 'world' }.convert_keys.should eq 'hello' => 'world'
  end

  it 'should convert the key to a string if it is a symbol' do
    { hello: "world" }.convert_keys.should eq 'hello' => 'world'
  end

  it 'should accept any method you can call on a string to convert the key' do
    { hello: "world" }.convert_keys(:upcase).should eq 'HELLO' => 'world'
  end

  it 'should convert nested hashes with the same logic' do
    { 'HELLO' => { 'MR' => 'world' } }.convert_keys.should eq 'hello' => {'mr' => 'world'}
  end

  it 'should be both an instance and class method' do
    Hash.new.should respond_to(:convert_keys)
    Hash.should respond_to(:convert_keys)
  end

  it 'should convert an array of hashes with the same logic' do
    Hash.convert_keys([{ 'HELLO' => 'world' }, { 'FOO' => 'bar' }]).should eq [{ 'hello' => 'world' }, { 'foo' => 'bar' }]
  end

  it "should convert an array of hashes directly on the array" do
    [{ 'HELLO' => 'world' }, { 'FOO' => 'bar' }].convert_keys.should eq [{ 'hello' => 'world' }, { 'foo' => 'bar' }]
  end

  it "should only convert hashes in the array" do
    [{ 'HELLO' => 'world' }, 123].convert_keys.should eq [{ 'hello' => 'world' }, 123]
  end

  it 'should convert arrays of hashes' do
    { 'HELLO' => [{ 'MR' => 'world' }] }.convert_keys.should eq 'hello' => [{'mr' => 'world'}]
  end

  it 'should not convert values other than arrays and hashes' do
    Hash.convert_keys("HELLO WORLD").should eq "HELLO WORLD"
  end

  it 'should support lambdas for conversion methods' do
    converter = -> (str) { str.sub "hello", "goodbye" }
    { 'hello' => 'world' }.convert_keys(converter).should eq 'goodbye' => 'world'
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hash-pipe-0.5.0 spec/hash-pipe/key_conversion_spec.rb
hash-pipe-0.4.1 spec/hash-pipe/key_conversion_spec.rb
hash-pipe-0.4.0 spec/hash-pipe/key_conversion_spec.rb