|
4 | 4 | require 'recursive_open_struct' |
5 | 5 |
|
6 | 6 | describe RecursiveOpenStruct do |
7 | | - let(:value) { 'foo' } |
8 | | - let(:symbol) { :bar } |
9 | | - let(:new_value) { 'bar' } |
10 | | - let(:new_symbol) { :foo } |
11 | | - |
12 | 7 | describe 'indifferent access' do |
13 | | - let(:hash) { { :foo => value, 'bar' => symbol } } |
| 8 | + subject(:hash_ros) { described_class.new(hash, hash_ros_opts) } |
| 9 | + |
| 10 | + let(:hash) { { :foo => value, 'bar' => :bar } } |
14 | 11 | let(:hash_ros_opts) { {} } |
15 | | - subject(:hash_ros) { RecursiveOpenStruct.new(hash, hash_ros_opts) } |
16 | 12 |
|
17 | | - context 'setting value with method' do |
18 | | - before(:each) do |
19 | | - subject.foo = value |
| 13 | + describe 'setting value with method' do |
| 14 | + let(:value) { 'foo' } |
| 15 | + |
| 16 | + before do |
| 17 | + hash_ros.foo = value |
20 | 18 | end |
21 | 19 |
|
22 | | - it('allows getting with method') { expect(subject.foo).to be value } |
23 | | - it('allows getting with symbol') { expect(subject[:foo]).to be value } |
24 | | - it('allows getting with string') { expect(subject['foo']).to be value } |
| 20 | + it('allows getting with method') { expect(hash_ros.foo).to be value } |
| 21 | + it('allows getting with symbol') { expect(hash_ros[:foo]).to be value } |
| 22 | + it('allows getting with string') { expect(hash_ros['foo']).to be value } |
25 | 23 | end |
26 | 24 |
|
27 | | - context 'setting value with symbol' do |
28 | | - before(:each) do |
29 | | - subject[:foo] = value |
| 25 | + describe 'setting value with symbol' do |
| 26 | + let(:value) { 'foo' } |
| 27 | + |
| 28 | + before do |
| 29 | + hash_ros[:foo] = value |
30 | 30 | end |
31 | 31 |
|
32 | | - it('allows getting with method') { expect(subject.foo).to be value } |
33 | | - it('allows getting with symbol') { expect(subject[:foo]).to be value } |
34 | | - it('allows getting with string') { expect(subject['foo']).to be value } |
| 32 | + it('allows getting with method') { expect(hash_ros.foo).to be value } |
| 33 | + it('allows getting with symbol') { expect(hash_ros[:foo]).to be value } |
| 34 | + it('allows getting with string') { expect(hash_ros['foo']).to be value } |
35 | 35 | end |
36 | 36 |
|
37 | | - context 'setting value with string' do |
38 | | - before(:each) do |
39 | | - subject['foo'] = value |
| 37 | + describe 'setting value with string' do |
| 38 | + let(:value) { 'foo' } |
| 39 | + |
| 40 | + before do |
| 41 | + hash_ros['foo'] = value |
40 | 42 | end |
41 | 43 |
|
42 | | - it('allows getting with method') { expect(subject.foo).to be value } |
43 | | - it('allows getting with symbol') { expect(subject[:foo]).to be value } |
44 | | - it('allows getting with string') { expect(subject['foo']).to be value } |
| 44 | + it('allows getting with method') { expect(hash_ros.foo).to be value } |
| 45 | + it('allows getting with symbol') { expect(hash_ros[:foo]).to be value } |
| 46 | + it('allows getting with string') { expect(hash_ros['foo']).to be value } |
45 | 47 | end |
46 | 48 |
|
47 | | - context 'overwriting values' do |
48 | | - context 'set with method' do |
49 | | - before(:each) do |
50 | | - subject.foo = value |
| 49 | + describe 'overwriting values' do |
| 50 | + let(:value) { 'foo' } |
| 51 | + let(:new_value) { 'bar' } |
| 52 | + |
| 53 | + describe 'set with method' do |
| 54 | + before do |
| 55 | + hash_ros.foo = value |
51 | 56 | end |
52 | 57 |
|
53 | 58 | it('overrides with symbol') do |
54 | | - subject[:foo] = new_value |
55 | | - expect(subject.foo).to be new_value |
| 59 | + hash_ros[:foo] = new_value |
| 60 | + expect(hash_ros.foo).to be new_value |
56 | 61 | end |
57 | 62 |
|
58 | 63 | it('overrides with string') do |
59 | | - subject['foo'] = new_value |
60 | | - expect(subject.foo).to be new_value |
| 64 | + hash_ros['foo'] = new_value |
| 65 | + expect(hash_ros.foo).to be new_value |
61 | 66 | end |
62 | 67 | end |
63 | 68 |
|
64 | | - context 'set with symbol' do |
65 | | - before(:each) do |
66 | | - subject[:foo] = value |
| 69 | + describe 'set with symbol' do |
| 70 | + before do |
| 71 | + hash_ros[:foo] = value |
67 | 72 | end |
68 | 73 |
|
69 | 74 | it('overrides with method') do |
70 | | - subject.foo = new_value |
71 | | - expect(subject[:foo]).to be new_value |
| 75 | + hash_ros.foo = new_value |
| 76 | + expect(hash_ros[:foo]).to be new_value |
72 | 77 | end |
73 | 78 |
|
74 | 79 | it('overrides with string') do |
75 | | - subject['foo'] = new_value |
76 | | - expect(subject[:foo]).to be new_value |
| 80 | + hash_ros['foo'] = new_value |
| 81 | + expect(hash_ros[:foo]).to be new_value |
77 | 82 | end |
78 | 83 | end |
79 | 84 |
|
80 | | - context 'set with string' do |
81 | | - before(:each) do |
82 | | - subject['foo'] = value |
| 85 | + describe 'set with string' do |
| 86 | + before do |
| 87 | + hash_ros['foo'] = value |
83 | 88 | end |
84 | 89 |
|
85 | 90 | it('overrides with method') do |
86 | | - subject.foo = new_value |
87 | | - expect(subject['foo']).to be new_value |
| 91 | + hash_ros.foo = new_value |
| 92 | + expect(hash_ros['foo']).to be new_value |
88 | 93 | end |
89 | 94 |
|
90 | 95 | it('overrides with symbol') do |
91 | | - subject[:foo] = new_value |
92 | | - expect(subject['foo']).to be new_value |
| 96 | + hash_ros[:foo] = new_value |
| 97 | + expect(hash_ros['foo']).to be new_value |
93 | 98 | end |
94 | 99 | end |
95 | 100 |
|
96 | | - context 'set with hash' do |
| 101 | + describe 'set with hash' do |
97 | 102 | it('overrides with method') do |
98 | 103 | hash_ros.foo = new_value |
99 | 104 | expect(hash_ros[:foo]).to be new_value |
100 | | - |
| 105 | + new_symbol = :foo |
101 | 106 | hash_ros.bar = new_symbol |
102 | 107 | expect(hash_ros['bar']).to be new_symbol |
103 | 108 | end |
104 | 109 |
|
105 | 110 | it('overrides with symbol') do |
| 111 | + new_symbol = :foo |
106 | 112 | hash_ros[:bar] = new_symbol |
107 | 113 | expect(hash_ros['bar']).to be new_symbol |
108 | 114 | end |
|
114 | 120 | end |
115 | 121 |
|
116 | 122 | context 'when preserve_original_keys is not enabled' do |
117 | | - context 'transforms original keys to symbols' do |
118 | | - subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true) } |
| 123 | + # rubocop:disable RSpec/MultipleMemoizedHelpers |
| 124 | + describe 'transforms original keys to symbols' do |
| 125 | + subject(:recursive) { described_class.new(recursive_hash, recurse_over_arrays: true) } |
| 126 | + |
119 | 127 | let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } } |
120 | 128 | let(:symbolized_recursive_hash) { { foo: [{ bar: [{ foo: :bar }] }] } } |
121 | 129 | let(:symbolized_modified_hash) { { foo: [{ bar: [{ foo: :foo }] }] } } |
|
134 | 142 | expect(recursive.to_h).to eq symbolized_modified_hash |
135 | 143 | end |
136 | 144 | end |
| 145 | + # rubocop:enable RSpec/MultipleMemoizedHelpers |
137 | 146 | end |
138 | 147 |
|
139 | 148 | context 'when preserve_original_keys is enabled' do |
140 | | - context 'preserves the original keys' do |
| 149 | + # rubocop:disable RSpec/MultipleMemoizedHelpers |
| 150 | + describe 'preserves the original keys' do |
141 | 151 | subject(:recursive) do |
142 | | - RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true, preserve_original_keys: true) |
| 152 | + described_class.new(recursive_hash, recurse_over_arrays: true, preserve_original_keys: true) |
143 | 153 | end |
| 154 | + |
144 | 155 | let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } } |
145 | 156 | let(:modified_hash) { { foo: [{ 'bar' => [{ 'foo' => :foo }] }] } } |
146 | 157 |
|
|
159 | 170 | expect(recursive.to_h).to eq modified_hash |
160 | 171 | end |
161 | 172 | end |
| 173 | + # rubocop:enable RSpec/MultipleMemoizedHelpers |
162 | 174 | end |
163 | 175 |
|
164 | 176 | context 'when undefined method' do |
165 | 177 | context 'when raise_on_missing is enabled' do |
166 | | - subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, raise_on_missing: true) } |
| 178 | + subject(:recursive) { described_class.new(recursive_hash, raise_on_missing: true) } |
| 179 | + |
167 | 180 | let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } } |
168 | 181 |
|
169 | 182 | specify 'raises NoMethodError' do |
|
174 | 187 | end |
175 | 188 |
|
176 | 189 | context 'when raise_on_missing is disabled' do |
177 | | - context 'preserves the original keys' do |
178 | | - subject(:recursive) { RecursiveOpenStruct.new(recursive_hash) } |
179 | | - let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } } |
| 190 | + subject(:recursive) { described_class.new(recursive_hash) } |
| 191 | + |
| 192 | + let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } } |
180 | 193 |
|
181 | | - specify 'returns nil' do |
182 | | - expect(recursive.undefined_method).to be_nil |
183 | | - end |
| 194 | + specify 'returns nil' do |
| 195 | + expect(recursive.undefined_method).to be_nil |
184 | 196 | end |
185 | 197 | end |
186 | 198 | end |
|
0 commit comments