File: recursion_spec.rb

package info (click to toggle)
ruby-recursive-open-struct 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: ruby: 912; makefile: 6
file content (336 lines) | stat: -rw-r--r-- 10,827 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
require_relative '../spec_helper'
require 'recursive_open_struct'

describe RecursiveOpenStruct do

  describe "recursive behavior" do
    let(:h) { { :blah => { :another => 'value' } } }
    subject(:ros) { RecursiveOpenStruct.new(h) }

    it "can convert the entire hash tree back into a hash" do
      blank_obj = Object.new
      h = {:asdf => 'John Smith', :foo => [{:bar => blank_obj}, {:baz => nil}]}
      ros = RecursiveOpenStruct.new(h)

      expect(ros.to_h).to eq h
      expect(ros.to_hash).to eq h
    end

    it "returns accessed hashes as RecursiveOpenStructs instead of hashes" do
      expect(subject.blah.another).to eq 'value'
    end

    it "handles subscript notation the same way as dotted notation" do
      expect(subject.blah.another).to eq subject[:blah].another
    end

    it "uses #key_as_a_hash to return key as a Hash" do
      expect(subject.blah_as_a_hash).to eq({ :another => 'value' })
    end

    it "handles sub-element replacement with dotted notation before member setup" do
      expect(ros[:blah][:another]).to eql 'value'
      expect(ros.methods).not_to include(:blah)

      ros.blah = { changed: 'backing' }

      expect(ros.blah.changed).to eql 'backing'
    end

    describe "handling loops in the original Hashes" do
      let(:h1) { { :a => 'a'} }
      let(:h2) { { :a => 'b', :h1 => h1 } }
      before(:each) { h1[:h2] = h2 }

      subject { RecursiveOpenStruct.new(h2) }

      it { expect(subject.h1.a).to eq 'a' }
      it { expect(subject.h1.h2.a).to eq 'b' }
      it { expect(subject.h1.h2.h1.a).to eq 'a' }
      it { expect(subject.h1.h2.h1.h2.a).to eq 'b' }
      it { expect(subject.h1).to eq subject.h1.h2.h1 }
      it { expect(subject.h1).to_not eq subject.h1.h2 }
    end # describe handling loops in the origin Hashes

    it "can modify a key of a sub-element" do
      h = {
        :blah => {
          :blargh => 'Brad'
        }
      }
      ros = RecursiveOpenStruct.new(h)
      ros.blah.blargh = "Janet"

      expect(ros.blah.blargh).to eq "Janet"
    end

    describe 'subscript mutation notation' do
      it 'handles the basic case' do
        subject[:blah] = 12345
        expect(subject.blah).to eql 12345
      end

      it 'recurses properly' do
        subject[:blah][:another] = 'abc'
        expect(subject.blah.another).to eql 'abc'
        expect(subject.blah_as_a_hash).to eql({ :another => 'abc' })
      end

      let(:diff){ { :different => 'thing' } }

      it 'can replace the entire hash' do
        expect(subject.to_h).to eql(h)
        subject[:blah] = diff
        expect(subject.to_h).to eql({ :blah => diff })
      end

      it 'updates sub-element cache' do
        expect(subject.blah.different).to be_nil
        subject[:blah] = diff
        expect(subject.blah.different).to eql 'thing'
        expect(subject.blah_as_a_hash).to eql(diff)
      end
    end

    context "after a sub-element has been modified" do
      let(:hash) do
        { :blah => { :blargh => "Brad" }, :some_array => [ 1, 2, 3] }
      end
      let(:updated_hash) do
        { :blah => { :blargh => "Janet" }, :some_array => [ 1, 2, 3] }
      end

      subject { RecursiveOpenStruct.new(hash) }

      before(:each) { subject.blah.blargh = "Janet" }

      describe ".to_h" do
        it "returns a hash tree that contains those modifications" do
          expect(subject.to_h).to eq updated_hash
        end

        specify "modifying the returned hash tree does not modify the ROS" do
          subject.to_h[:blah][:blargh] = "Dr Scott"

          expect(subject.blah.blargh).to eq "Janet"
        end
      end

      it "does not mutate the original hash tree passed to the constructor" do
        expect(hash[:blah][:blargh]).to eq 'Brad'
      end

      it "limits the deep-copy to the initial hash tree" do
        subject.some_array[0] = 4

        expect(hash[:some_array][0]).to eq 4
      end

      describe "#dup" do
        let(:duped_subject) { subject.dup }

        it "preserves sub-element modifications" do
          expect(duped_subject.blah.blargh).to eq subject.blah.blargh
        end

        it "allows the copy's sub-elements to be modified independently from the original's" do
          expect(subject.blah.blargh).to eq "Janet"

          duped_subject.blah.blargh = "Dr. Scott"

          expect(subject.blah.blargh).to eq "Janet"
          expect(duped_subject.blah.blargh).to eq "Dr. Scott"
        end
      end
    end

    context "when memoizing and then modifying entire recursive structures" do
      subject do
        RecursiveOpenStruct.new(
          { :blah => original_blah }, :recurse_over_arrays => true)
      end

      before(:each) { subject.blah } # enforce memoization

      context "when modifying an entire Hash" do
        let(:original_blah) { { :a => 'A', :b => 'B' } }
        let(:new_blah) { { :something_new => "C" } }

        before(:each) { subject.blah = new_blah }

        it "returns the modified value instead of the memoized one" do
          expect(subject.blah.something_new).to eq "C"
        end

        specify "the old value no longer exists" do
          expect(subject.blah.a).to be_nil
        end
      end

      context "when modifying an entire Array" do
        let(:original_blah) { [1, 2, 3] }

        it "returns the modified value instead of the memoized one" do
          new_blah = [4, 5, 6]
          subject.blah = new_blah
          expect(subject.blah).to eq new_blah
        end
      end
    end

    describe 'recursing over arrays' do
      let(:blah_list) { [ { :foo => '1' }, { :foo => '2' }, 'baz' ] }
      let(:h) { { :blah => blah_list } }

      context "when recursing over arrays is enabled" do
        subject { RecursiveOpenStruct.new(h, :recurse_over_arrays => true) }

        it { expect(subject.blah.length).to eq 3 }
        it { expect(subject.blah[0].foo).to eq '1' }
        it { expect(subject.blah[1].foo).to eq '2' }
        it { expect(subject.blah_as_a_hash).to eq blah_list }
        it { expect(subject.blah[2]).to eq 'baz' }

        context "when an inner value changes" do
          let(:updated_blah_list) { [ { :foo => '1' }, { :foo => 'Dr Scott' }, 'baz' ] }
          let(:updated_h) { { :blah => updated_blah_list } }

          before(:each) { subject.blah[1].foo = "Dr Scott" }

          it "Retains changes across Array lookups" do
            expect(subject.blah[1].foo).to eq "Dr Scott"
          end

          it "propagates the changes through to .to_h across Array lookups" do
            expect(subject.to_h).to eq({
              :blah => [ { :foo => '1' }, { :foo => "Dr Scott" }, 'baz' ]
            })
          end

          it "deep-copies hashes within Arrays" do
            subject.to_h[:blah][1][:foo] = "Rocky"

            expect(subject.blah[1].foo).to eq "Dr Scott"
          end

          it "does not mutate the input hash passed to the constructor" do
            expect(h[:blah][1][:foo]).to eq '2'
          end

          it "the deep copy recurses over Arrays as well" do
            expect(h[:blah][1][:foo]).to eq '2'
          end

          describe "#dup" do
            let(:duped_subject) { subject.dup }

            it "preserves sub-element modifications" do
              expect(duped_subject.blah[1].foo).to eq subject.blah[1].foo
            end

            it "allows the copy's sub-elements to be modified independently from the original's" do
              duped_subject.blah[1].foo = "Rocky"

              expect(duped_subject.blah[1].foo).to eq "Rocky"
              expect(subject.blah[1].foo).to eq "Dr Scott"
            end
          end
        end

        context "when array is nested deeper" do
          let(:deep_hash) { { :foo => { :blah => blah_list } } }
          subject { RecursiveOpenStruct.new(deep_hash, :recurse_over_arrays => true) }

          it { expect(subject.foo.blah.length).to eq 3 }
          it "Retains changes across Array lookups" do
            subject.foo.blah[1].foo = "Dr Scott"
            expect(subject.foo.blah[1].foo).to eq "Dr Scott"
          end

        end

        context "when array is in an array" do
          let(:haah) { { :blah => [ blah_list ] } }
          subject { RecursiveOpenStruct.new(haah, :recurse_over_arrays => true) }

          it { expect(subject.blah.length).to eq 1 }
          it { expect(subject.blah[0].length).to eq 3 }
          it "Retains changes across Array lookups" do
            subject.blah[0][1].foo = "Dr Scott"

            expect(subject.blah[0][1].foo).to eq "Dr Scott"
          end

        end

      end # when recursing over arrays is enabled

      context "when recursing over arrays is disabled" do
        subject { RecursiveOpenStruct.new(h) }

        it { expect(subject.blah.length).to eq 3 }
        it { expect(subject.blah[0]).to eq({ :foo => '1' }) }
        it { expect(subject.blah[0][:foo]).to eq '1' }
      end # when recursing over arrays is disabled

      describe 'modifying an array and recursing over it' do
        let(:h) { {} }
        subject { RecursiveOpenStruct.new(h, recurse_over_arrays: true) }

        context 'when adding an array with hashes into the tree' do
          before(:each) do
            subject.mystery = {}
            subject.mystery.science = [{ theatre: 9000 }]
          end

          it "ROS's it" do
            expect(subject.mystery.science[0].theatre).to eq 9000
          end
        end

        context 'when appending a hash to an array' do
          before(:each) do
            subject.mystery = {}
            subject.mystery.science = []
            subject.mystery.science << { theatre: 9000 }
          end

          it "ROS's it" do
            expect(subject.mystery.science[0].theatre).to eq 9000
          end

          specify "the changes show up in .to_h" do
            expect(subject.to_h).to eq({ mystery: { science: [{theatre: 9000}]}})
          end
        end

        context 'after appending a hash to an array' do
          before(:each) do
            subject.mystery = {}
            subject.mystery.science = []
            subject.mystery.science[0] = {}
          end

          it "can have new values be set" do
            expect do
              subject.mystery.science[0].theatre = 9000
            end.to_not raise_error

            expect(subject.mystery.science[0].theatre).to eq 9000
          end
        end
      end # modifying an array and then recursing
    end # recursing over arrays

    describe 'nested nil values' do
      let(:h) { { foo: { bar: nil }} }
      it 'returns nil' do
        expect(subject.foo.bar).to be_nil
      end

      it 'returns a hash with the key and a nil value' do
        expect(subject.to_hash).to eq({ foo: { bar: nil }})
      end
    end # nested nil values
  end # recursive behavior
end