Skip to content

Instantly share code, notes, and snippets.

@Ted0-F
Created October 23, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ted0-F/ec2c457d91dab0bcf000834ca030eb2f to your computer and use it in GitHub Desktop.
Save Ted0-F/ec2c457d91dab0bcf000834ca030eb2f to your computer and use it in GitHub Desktop.
Ruby task02 tests
describe 'Hash#fetch_deep' do
input = {meal: 'musaka'}
order = {
dessert: {
type: 'cake',
variant: 'chocolate',
rating: 10,
"test" => {
t1: 15,
t2: 25
},
comments: [
{text: 'So sweet!'},
{text: 'A perfect blend of milk chocolate and cookies. With a cherry on top.'}
],
hints: ["Batman", "Maynard Keenan"]
}
}
output = { :type=>"cake",
:variant=>"chocolate",
:rating=>10,
"test" => {
t1: 15,
t2: 25
},
:comments=>[
{:text=>"So sweet!"},
{:text=>"A perfect blend of milk chocolate and cookies. With a cherry on top."}
],
hints: ["Batman", "Maynard Keenan"]
}
it 'can look up simple values' do
expect(input.fetch_deep('meal')).to eq 'musaka'
end
it 'can look up abscent values' do
expect(input.fetch_deep('bla.bla.bla')).to eq nil
end
it 'can look up more complicated values' do
expect(order.fetch_deep('dessert')).to eq output
end
it 'can look up two levels deep' do
expect(order.fetch_deep('dessert.variant')).to eq 'chocolate'
end
it 'can lookup string values' do
expect(order.fetch_deep('dessert.test.t2')).to eq 25
end
it 'can lookup in arrays' do
expect(order.fetch_deep('dessert.comments.0.text')).to eq 'So sweet!'
end
it 'can lookup if the last element is array' do
expect(order.fetch_deep('dessert.hints.0')).to eq 'Batman'
end
it 'can look up in nested objects' do
input = { menu: {
breakfast: 'banana',
"lunch" => 'musaka',
dinner: ["rakia", "mandja","torta"]
}
}
expect(input.fetch_deep('menu.breakfast')).to eq 'banana'
expect(input.fetch_deep('menu.lunch')).to eq 'musaka'
expect(input.fetch_deep('menu.dinner.0')).to eq 'rakia'
expect(input.fetch_deep('menu.dinner.1')).to eq 'mandja'
expect(input.fetch_deep('menu.dinner.2')).to eq 'torta'
end
it 'can look up in other nested objects' do
input = { menu: {
breakfast: [:banana],
:lunch => :musaka,
"dinner": ["rakia", "mandja", :torta]
}
}
expect(input.fetch_deep('menu.breakfast.0')).to eq :banana
expect(input.fetch_deep('menu.lunch')).to eq :musaka
expect(input.fetch_deep('menu.dinner.0')).to eq 'rakia'
expect(input.fetch_deep('menu.dinner.1')).to eq 'mandja'
expect(input.fetch_deep('menu.dinner.2')).to eq :torta
end
it 'can look up in really complicated objects' do
input = { menu: {
breakfast: [:banana],
:lunch => :musaka,
"dinner": [{
"pre": {
"soup": "chicken",
salad: :caesar
}
}, "mandja", :torta]
}
}
expect(input.fetch_deep('menu.dinner.0.pre.soup')).to eq "chicken"
expect(input.fetch_deep('menu.dinner.0.pre.salad')).to eq :caesar
end
end
describe 'Hash#reshape' do
order = {
dessert: {
type: 'cake',
variant: 'chocolate'
}
}
it 'can rename fields' do
input = {name: 'Georgi'}
shape = {first_name: 'name'}
output = {first_name: 'Georgi'}
expect(input.reshape(shape)).to eq output
end
it 'can rename string of fields' do
shape = {
food: 'dessert.type',
taste: 'dessert.variant'
}
result = {
food: 'cake',
taste: 'chocolate'
}
expect(order.reshape(shape)).to eq result
end
it 'can go deep' do
shape = {
food: {
type: 'dessert.type',
taste: 'dessert.variant'
}
}
result = {
food: {
type: 'cake',
taste: 'chocolate'
}
}
expect(order.reshape(shape)).to eq result
end
it 'can reshape complex objects' do
input = {dessert: {type: 'cake', variant: 'chocolate'}}
shape = {food: 'dessert.type', taste: 'dessert.variant'}
output = {food: 'cake', taste: 'chocolate'}
expect(input.reshape(shape)).to eq output
end
it 'can reshape an array/hash mix' do
order = { dessert: ['cake', 'chocolate'] }
shape = { food: {type: 'dessert.0', taste: 'dessert.1'} }
output = {:food=>{:type=>"cake", :taste=>"chocolate"}}
expect(order.reshape(shape)).to eq output
end
end
describe 'Array#reshape' do
it 'can rename fields in each element' do
input = [
{item: 'musaka'}
]
shape = {meal: 'item'}
expect(input.reshape(shape)).to eq [
{meal: 'musaka'}
]
end
it 'can reshape complex arrays' do
input = [
{item: {type: 'musaka', price: 4.0, quantity: 30}},
{item: {type: 'cake', price: 3.5, quantity: 20}}
]
shape = {food: 'item.type', price: 'item.price'}
expect(input.reshape(shape)).to eq [
{food: 'musaka', price: 4.0},
{food: 'cake', price: 3.5}
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment