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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
require_relative '../spec_helper'
require 'recursive_open_struct'
describe RecursiveOpenStruct do
let(:value) { 'foo' }
let(:symbol) { :bar }
let(:new_value) { 'bar' }
let(:new_symbol) { :foo }
describe 'indifferent access' do
let(:hash) { {:foo => value, 'bar' => symbol} }
let(:hash_ros_opts) { {} }
subject(:hash_ros) { RecursiveOpenStruct.new(hash, hash_ros_opts) }
context 'setting value with method' do
before(:each) do
subject.foo = value
end
it('allows getting with method') { expect(subject.foo).to be value }
it('allows getting with symbol') { expect(subject[:foo]).to be value }
it('allows getting with string') { expect(subject['foo']).to be value }
end
context 'setting value with symbol' do
before(:each) do
subject[:foo] = value
end
it('allows getting with method') { expect(subject.foo).to be value }
it('allows getting with symbol') { expect(subject[:foo]).to be value }
it('allows getting with string') { expect(subject['foo']).to be value }
end
context 'setting value with string' do
before(:each) do
subject['foo'] = value
end
it('allows getting with method') { expect(subject.foo).to be value }
it('allows getting with symbol') { expect(subject[:foo]).to be value }
it('allows getting with string') { expect(subject['foo']).to be value }
end
context 'overwriting values' do
context 'set with method' do
before(:each) do
subject.foo = value
end
it('overrides with symbol') do
subject[:foo] = new_value
expect(subject.foo).to be new_value
end
it('overrides with string') do
subject['foo'] = new_value
expect(subject.foo).to be new_value
end
end
context 'set with symbol' do
before(:each) do
subject[:foo] = value
end
it('overrides with method') do
subject.foo = new_value
expect(subject[:foo]).to be new_value
end
it('overrides with string') do
subject['foo'] = new_value
expect(subject[:foo]).to be new_value
end
end
context 'set with string' do
before(:each) do
subject['foo'] = value
end
it('overrides with method') do
subject.foo = new_value
expect(subject['foo']).to be new_value
end
it('overrides with symbol') do
subject[:foo] = new_value
expect(subject['foo']).to be new_value
end
end
context 'set with hash' do
it('overrides with method') do
hash_ros.foo = new_value
expect(hash_ros[:foo]).to be new_value
hash_ros.bar = new_symbol
expect(hash_ros['bar']).to be new_symbol
end
it('overrides with symbol') do
hash_ros[:bar] = new_symbol
expect(hash_ros['bar']).to be new_symbol
end
it('overrides with string') do
hash_ros['foo'] = new_value
expect(hash_ros[:foo]).to be new_value
end
end
context 'when preserve_original_keys is not enabled' do
context 'transforms original keys to symbols' do
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true) }
let(:recursive_hash) { {:foo => [ {'bar' => [ { 'foo' => :bar} ] } ] } }
let(:symbolized_recursive_hash) { {:foo => [ {:bar => [ { :foo => :bar} ] } ] } }
let(:symbolized_modified_hash) { {:foo => [ {:bar => [ { :foo => :foo} ] } ] } }
let(:symbolized_hash) { Hash[hash.map{|(k,v)| [k.to_sym,v]}] }
specify 'after initialization' do
expect(hash_ros.to_h).to eq symbolized_hash
end
specify 'in recursive hashes' do
expect(recursive.to_h).to eq symbolized_recursive_hash
end
specify 'after resetting value' do
recursive.foo.first[:bar].first[:foo] = :foo
expect(recursive.to_h).to eq symbolized_modified_hash
end
end
end
context 'when preserve_original_keys is enabled' do
context 'preserves the original keys' do
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true, preserve_original_keys: true) }
let(:recursive_hash) { {:foo => [ {'bar' => [ { 'foo' => :bar} ] } ] } }
let(:modified_hash) { {:foo => [ {'bar' => [ { 'foo' => :foo} ] } ] } }
let(:hash_ros_opts) { { preserve_original_keys: true }}
specify 'after initialization' do
expect(hash_ros.to_h).to eq hash
end
specify 'in recursive hashes' do
expect(recursive.to_h).to eq recursive_hash
end
specify 'after resetting value' do
recursive.foo.first[:bar].first[:foo] = :foo
expect(recursive.to_h).to eq modified_hash
end
end
end
end
end
end
|