1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
require 'spec_helper'
module Hashie
module Extensions
describe DeepFetch do
subject { Class.new(Hash) { include Hashie::Extensions::DeepFetch } }
let(:hash) do
{
library: {
books: [
{ title: 'Call of the Wild' },
{ title: 'Moby Dick' }
],
shelves: nil,
location: {
address: '123 Library St.'
}
}
}
end
let(:instance) { subject.new.update(hash) }
describe '#deep_fetch' do
it 'extracts a value from a nested hash' do
expect(instance.deep_fetch(:library, :location, :address)).to eq('123 Library St.')
end
it 'extracts a value from a nested array' do
expect(instance.deep_fetch(:library, :books, 1, :title)).to eq('Moby Dick')
end
context 'when one of the keys is not present' do
context 'when a block is provided' do
it 'returns the value of the block' do
value = instance.deep_fetch(:library, :unknown_key, :location) { 'block value' }
expect(value).to eq('block value')
end
end
context 'when a block is not provided' do
context 'when the nested object is an array' do
it 'raises an UndefinedPathError' do
expect do
instance.deep_fetch(:library, :books, 2)
end.to(
raise_error(
DeepFetch::UndefinedPathError,
'Could not fetch path (library > books > 2) at 2'
)
)
end
end
context 'when the nested object is a hash' do
it 'raises a UndefinedPathError' do
expect do
instance.deep_fetch(:library, :location, :unknown_key)
end.to(
raise_error(
DeepFetch::UndefinedPathError,
'Could not fetch path (library > location > unknown_key) at unknown_key'
)
)
end
end
context 'when the nested object is missing' do
it 'raises an UndefinedPathError' do
expect do
instance.deep_fetch(:library, :unknown_key, :books)
end.to(
raise_error(
DeepFetch::UndefinedPathError,
'Could not fetch path (library > unknown_key > books) at unknown_key'
)
)
end
end
context 'when the nested object is nil' do
it 'raises an UndefinedPathError' do
expect do
instance.deep_fetch(:library, :shelves, :address)
end.to(
raise_error(
DeepFetch::UndefinedPathError,
'Could not fetch path (library > shelves > address) at address'
)
)
end
end
end
end
end
end
end
end
|