spec/integration/hash_spec.rb in transproc-0.1.3 vs spec/integration/hash_spec.rb in transproc-0.2.0

- old
+ new

@@ -1,8 +1,32 @@ require 'spec_helper' describe 'Hash mapping with Transproc' do + describe 'map_keys' do + it 'returns a new hash with given proc applied to keys' do + map_keys = t(:map_keys, ->(key) { key.strip }) + + input = { ' foo ' => 'bar' } + output = { 'foo' => 'bar' } + + expect(map_keys[input]).to eql(output) + expect(input).to eql(' foo ' => 'bar') + end + end + + describe 'map_keys!' do + it 'returns updated hash with given proc applied to keys' do + map_keys = t(:map_keys!, ->(key) { key.strip }) + + input = { ' foo ' => 'bar' } + output = { 'foo' => 'bar' } + + expect(map_keys[input]).to eql(output) + expect(input).to eql('foo' => 'bar') + end + end + describe 'symbolize_keys' do it 'returns a new hash with symbolized keys' do symbolize_keys = t(:symbolize_keys) input = { 'foo' => 'bar' } @@ -24,10 +48,108 @@ expect(input).to eql(output) end end + describe 'stringify_keys' do + it 'returns a new hash with stringified keys' do + stringify_keys = t(:stringify_keys) + + input = { foo: 'bar' } + output = { 'foo' => 'bar' } + + expect(stringify_keys[input]).to eql(output) + expect(input).to eql(foo: 'bar') + end + end + + describe 'stringify_keys!' do + it 'returns a new hash with stringified keys' do + stringify_keys = t(:stringify_keys!) + + input = { foo: 'bar' } + output = { 'foo' => 'bar' } + + expect(stringify_keys[input]).to eql(output) + expect(input).to eql('foo' => 'bar') + end + end + + describe 'map_values' do + it 'returns a new hash with given proc applied to values' do + map_values = t(:map_values, ->(value) { value.strip }) + + input = { 'foo' => ' bar ' } + output = { 'foo' => 'bar' } + + expect(map_values[input]).to eql(output) + expect(input).to eql('foo' => ' bar ') + end + end + + describe 'map_values!' do + it 'returns updated hash with given proc applied to values' do + map_values = t(:map_values!, ->(value) { value.strip }) + + input = { 'foo' => ' bar ' } + output = { 'foo' => 'bar' } + + expect(map_values[input]).to eql(output) + expect(input).to eql('foo' => 'bar') + end + end + + describe 'rename_keys' do + it 'returns a new hash with applied functions' do + map = t(:rename_keys, 'foo' => :foo) + + input = { 'foo' => 'bar', :bar => 'baz' } + output = { foo: 'bar', bar: 'baz' } + + expect(map[input]).to eql(output) + expect(input).to eql('foo' => 'bar', :bar => 'baz') + end + end + + describe 'rename_keys!' do + it 'returns updated hash with applied functions' do + map = t(:rename_keys!, 'foo' => :foo) + + input = { 'foo' => 'bar', :bar => 'baz' } + output = { foo: 'bar', bar: 'baz' } + + map[input] + + expect(input).to eql(output) + end + end + + describe 'map_value' do + it 'applies function to value under specified key' do + transformation = t(:map_value, :user, t(:symbolize_keys)) + + input = { user: { 'name' => 'Jane' } } + output = { user: { name: 'Jane' } } + + expect(transformation[input]).to eql(output) + expect(input).to eql(user: { 'name' => 'Jane' }) + end + end + + describe 'map_value!' do + it 'applies function to value under specified key' do + transformation = t(:map_value!, :user, t(:symbolize_keys)) + + input = { user: { 'name' => 'Jane' } } + output = { user: { name: 'Jane' } } + + transformation[input] + + expect(input).to eql(output) + end + end + describe 'nest' do it 'returns new hash with keys nested under a new key' do nest = t(:nest, :baz, ['foo']) input = { 'foo' => 'bar' } @@ -99,64 +221,14 @@ 'one' => nil, 'two' => false}) end end - describe 'map_hash' do - it 'returns a new hash with applied functions' do - map = t(:map_hash, 'foo' => :foo) - - input = { 'foo' => 'bar', :bar => 'baz' } - output = { foo: 'bar', bar: 'baz' } - - expect(map[input]).to eql(output) - expect(input).to eql('foo' => 'bar', :bar => 'baz') - end - end - - describe 'map_hash!' do - it 'returns updated hash with applied functions' do - map = t(:map_hash!, 'foo' => :foo) - - input = { 'foo' => 'bar', :bar => 'baz' } - output = { foo: 'bar', bar: 'baz' } - - map[input] - - expect(input).to eql(output) - end - end - - describe 'map_key' do - it 'applies function to value under specified key' do - transformation = t(:map_key, :user, t(:symbolize_keys)) - - input = { user: { 'name' => 'Jane' } } - output = { user: { name: 'Jane' } } - - expect(transformation[input]).to eql(output) - expect(input).to eql(user: { 'name' => 'Jane' }) - end - end - - describe 'map_key!' do - it 'applies function to value under specified key' do - transformation = t(:map_key!, :user, t(:symbolize_keys)) - - input = { user: { 'name' => 'Jane' } } - output = { user: { name: 'Jane' } } - - transformation[input] - - expect(input).to eql(output) - end - end - describe 'nested transform' do it 'applies functions to nested hashes' do symbolize_keys = t(:symbolize_keys) - map_user_key = t(:map_key, :user, symbolize_keys) + map_user_key = t(:map_value, :user, symbolize_keys) transformation = symbolize_keys >> map_user_key input = { 'user' => { 'name' => 'Jane' } } output = { user: { name: 'Jane' } } @@ -166,10 +238,10 @@ end describe 'combining transformations' do it 'applies functions to the hash' do symbolize_keys = t(:symbolize_keys) - map = t(:map_hash, user_name: :name, user_email: :email) + map = t(:rename_keys, user_name: :name, user_email: :email) transformation = symbolize_keys >> map input = { 'user_name' => 'Jade', 'user_email' => 'jade@doe.org' } output = { name: 'Jade', email: 'jade@doe.org' }