File: wrapping_spec.rb

package info (click to toggle)
ruby-recursive-open-struct 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: ruby: 1,050; makefile: 6
file content (70 lines) | stat: -rw-r--r-- 1,919 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
require_relative '../spec_helper'
require 'recursive_open_struct'

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

    it 'can convert the entire hash tree back into a hash' do
      expect(ros.to_h).to eq h
    end

    it 'can access the flat keys' do
      expect(ros.blah).to be_a(RecursiveOpenStruct)
    end

    it 'can access the nested keys' do
      expect(ros.blah.another).to eql('value')
    end

    it 'can be inspected' do
      expect(ros.inspect).to \
        match(/#<RecursiveOpenStruct blah={:?another(: |=>)"value"}>/)
    end
  end

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

    it 'can convert the entire hash tree back into a hash' do
      expect(ros.to_h).to eq h
    end

    it 'can access the flat keys' do
      expect(ros.blah).to be_a(RecursiveOpenStruct)
    end

    it 'can access the nested keys' do
      expect(ros.blah.another).to eql('value')
    end

    it 'can be inspected' do
      expect(ros.inspect).to \
        match(/#<RecursiveOpenStruct blah={:?another(: |=>)"value"}>/)
    end
  end

  describe 'wrapping a subclass' do
    let(:h) { { :blah => { :another => 'value' } } }
    let(:subclass) { Class.new(RecursiveOpenStruct) }
    subject(:ros) { subclass.new(subclass.new(h)) }

    it 'can convert the entire hash tree back into a hash' do
      expect(ros.to_h).to eq h
    end

    it 'can access the flat keys' do
      expect(ros.blah).to be_a(RecursiveOpenStruct)
    end

    it 'can access the nested keys' do
      expect(ros.blah.another).to eql('value')
    end

    it 'can be inspected' do
      expect(ros.inspect).to match(/ blah={:?another(: |=>)"value"}>$/)
    end
  end
end