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
|
require 'spec_helper'
describe Immutable::Hash do
describe '#update_in' do
let(:hash) {
Immutable::Hash[
'A' => 'aye',
'B' => Immutable::Hash['C' => 'see', 'D' => Immutable::Hash['E' => 'eee']],
'F' => Immutable::Vector['G', Immutable::Hash['H' => 'eitch'], 'I']
]
}
context 'with one level on existing key' do
it 'passes the value to the block' do
hash.update_in('A') { |value| value.should == 'aye' }
end
it 'replaces the value with the result of the block' do
result = hash.update_in('A') { |value| 'FLIBBLE' }
result.get('A').should == 'FLIBBLE'
end
it 'should preserve the original' do
result = hash.update_in('A') { |value| 'FLIBBLE' }
hash.get('A').should == 'aye'
end
end
context 'with multi-level on existing keys' do
it 'passes the value to the block' do
hash.update_in('B', 'D', 'E') { |value| value.should == 'eee' }
end
it 'replaces the value with the result of the block' do
result = hash.update_in('B', 'D', 'E') { |value| 'FLIBBLE' }
result['B']['D']['E'].should == 'FLIBBLE'
end
it 'should preserve the original' do
result = hash.update_in('B', 'D', 'E') { |value| 'FLIBBLE' }
hash['B']['D']['E'].should == 'eee'
end
end
context "with multi-level creating sub-hashes when keys don't exist" do
it 'passes nil to the block' do
hash.update_in('B', 'X', 'Y') { |value| value.should be_nil }
end
it 'creates subhashes on the way to set the value' do
result = hash.update_in('B', 'X', 'Y') { |value| 'NEWVALUE' }
result['B']['X']['Y'].should == 'NEWVALUE'
result['B']['D']['E'].should == 'eee'
end
end
context 'with multi-level including vector with existing keys' do
it 'passes the value to the block' do
hash.update_in('F', 1, 'H') { |value| value.should == 'eitch' }
end
it 'replaces the value with the result of the block' do
result = hash.update_in('F', 1, 'H') { |value| 'FLIBBLE' }
result['F'][1]['H'].should == 'FLIBBLE'
end
it 'should preserve the original' do
result = hash.update_in('F', 1, 'H') { |value| 'FLIBBLE' }
hash['F'][1]['H'].should == 'eitch'
end
end
context 'with empty key_path' do
it 'raises ArguemntError' do
expect { hash.update_in() { |v| 42 } }.to raise_error(ArgumentError)
end
end
end
end
|