spec/integration/hash_spec.rb in transproc-0.1.2 vs spec/integration/hash_spec.rb in transproc-0.1.3
- old
+ new
@@ -1,11 +1,11 @@
require 'spec_helper'
describe 'Hash mapping with Transproc' do
describe 'symbolize_keys' do
it 'returns a new hash with symbolized keys' do
- symbolize_keys = Transproc(:symbolize_keys)
+ symbolize_keys = t(:symbolize_keys)
input = { 'foo' => 'bar' }
output = { foo: 'bar' }
expect(symbolize_keys[input]).to eql(output)
@@ -13,11 +13,11 @@
end
end
describe 'symbolize_keys!' do
it 'returns updated hash with symbolized keys' do
- symbolize_keys = Transproc(:symbolize_keys!)
+ symbolize_keys = t(:symbolize_keys!)
input = { 'foo' => 'bar' }
output = { foo: 'bar' }
symbolize_keys[input]
@@ -26,11 +26,11 @@
end
end
describe 'nest' do
it 'returns new hash with keys nested under a new key' do
- nest = Transproc(:nest, :baz, ['foo'])
+ nest = t(:nest, :baz, ['foo'])
input = { 'foo' => 'bar' }
output = { baz: { 'foo' => 'bar' } }
expect(nest[input]).to eql(output)
@@ -38,35 +38,74 @@
end
end
describe 'nest!' do
it 'returns new hash with keys nested under a new key' do
- nest = Transproc(:nest!, :baz, ['one', 'two', 'not-here'])
+ nest = t(:nest!, :baz, ['one', 'two', 'not-here'])
input = { 'foo' => 'bar', 'one' => nil, 'two' => false }
output = { 'foo' => 'bar', baz: { 'one' => nil, 'two' => false } }
nest[input]
expect(input).to eql(output)
end
it 'returns new hash with an empty hash under a new key when nest-keys are missing' do
- nest = Transproc(:nest!, :baz, ['foo'])
+ nest = t(:nest!, :baz, ['foo'])
input = { 'bar' => 'foo' }
output = { 'bar' => 'foo', baz: {} }
nest[input]
expect(input).to eql(output)
end
end
+ describe 'unwrap!' do
+ it 'returns updated hash with nested keys lifted to the root' do
+ unwrap = t(:unwrap!, 'wrapped', ['one', 'two'])
+
+ input = { 'foo' => 'bar', 'wrapped' => { 'one' => nil, 'two' => false } }
+ output = { 'foo' => 'bar', 'one' => nil, 'two' => false }
+
+ unwrap[input]
+
+ expect(input).to eql(output)
+ end
+
+ it 'lifts all keys if none are passed' do
+ unwrap = t(:unwrap!, 'wrapped')
+
+ input = { 'wrapped' => { 'one' => nil, 'two' => false } }
+ output = { 'one' => nil, 'two' => false }
+
+ unwrap[input]
+
+ expect(input).to eql(output)
+ end
+ end
+
+ describe 'unwrap' do
+ it 'returns new hash with nested keys lifted to the root' do
+ unwrap = t(:unwrap, 'wrapped', ['one', 'two'])
+
+ input = {
+ 'foo' => 'bar',
+ 'wrapped' => { 'one' => nil, 'two' => false }
+ }.freeze
+
+ expect(unwrap[input]).to eql({'foo' => 'bar',
+ 'one' => nil,
+ 'two' => false})
+ end
+ end
+
describe 'map_hash' do
it 'returns a new hash with applied functions' do
- map = Transproc(:map_hash, 'foo' => :foo)
+ map = t(:map_hash, 'foo' => :foo)
input = { 'foo' => 'bar', :bar => 'baz' }
output = { foo: 'bar', bar: 'baz' }
expect(map[input]).to eql(output)
@@ -74,11 +113,11 @@
end
end
describe 'map_hash!' do
it 'returns updated hash with applied functions' do
- map = Transproc(:map_hash!, 'foo' => :foo)
+ map = t(:map_hash!, 'foo' => :foo)
input = { 'foo' => 'bar', :bar => 'baz' }
output = { foo: 'bar', bar: 'baz' }
map[input]
@@ -87,11 +126,11 @@
end
end
describe 'map_key' do
it 'applies function to value under specified key' do
- transformation = Transproc(:map_key, :user, Transproc(:symbolize_keys))
+ transformation = t(:map_key, :user, t(:symbolize_keys))
input = { user: { 'name' => 'Jane' } }
output = { user: { name: 'Jane' } }
expect(transformation[input]).to eql(output)
@@ -99,11 +138,11 @@
end
end
describe 'map_key!' do
it 'applies function to value under specified key' do
- transformation = Transproc(:map_key!, :user, Transproc(:symbolize_keys))
+ transformation = t(:map_key!, :user, t(:symbolize_keys))
input = { user: { 'name' => 'Jane' } }
output = { user: { name: 'Jane' } }
transformation[input]
@@ -112,27 +151,27 @@
end
end
describe 'nested transform' do
it 'applies functions to nested hashes' do
- symbolize_keys = Transproc(:symbolize_keys)
- map_user_key = Transproc(:map_key, :user, symbolize_keys)
+ symbolize_keys = t(:symbolize_keys)
+ map_user_key = t(:map_key, :user, symbolize_keys)
- transformation = symbolize_keys + map_user_key
+ transformation = symbolize_keys >> map_user_key
input = { 'user' => { 'name' => 'Jane' } }
output = { user: { name: 'Jane' } }
expect(transformation[input]).to eql(output)
end
end
describe 'combining transformations' do
it 'applies functions to the hash' do
- symbolize_keys = Transproc(:symbolize_keys)
- map = Transproc(:map_hash, user_name: :name, user_email: :email)
+ symbolize_keys = t(:symbolize_keys)
+ map = t(:map_hash, user_name: :name, user_email: :email)
- transformation = symbolize_keys + map
+ transformation = symbolize_keys >> map
input = { 'user_name' => 'Jade', 'user_email' => 'jade@doe.org' }
output = { name: 'Jade', email: 'jade@doe.org' }
result = transformation[input]