summaryrefslogtreecommitdiff
path: root/spec/hashie/extensions/deep_find_spec.rb
blob: cd13d7fc82f37bdc8f73b5c1b39e465d334f2f42 (plain)
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
require 'spec_helper'
require 'active_support/core_ext/hash/indifferent_access'

describe Hashie::Extensions::DeepFind do
  subject { Class.new(Hash) { include Hashie::Extensions::DeepFind } }
  let(:hash) do
    {
      library: {
        books: [
          { title: 'Call of the Wild' },
          { title: 'Moby Dick' }
        ],
        shelves: nil,
        location: {
          address: '123 Library St.',
          title: 'Main Library'
        }
      }
    }
  end
  let(:instance) { subject.new.update(hash) }

  describe '#deep_find' do
    it 'detects a value from a nested hash' do
      expect(instance.deep_find(:address)).to eq('123 Library St.')
    end

    it 'detects a value from a nested array' do
      expect(instance.deep_find(:title)).to eq('Call of the Wild')
    end

    it 'returns nil if it does not find a match' do
      expect(instance.deep_find(:wahoo)).to be_nil
    end
  end

  describe '#deep_find_all' do
    it 'detects all values from a nested hash' do
      expect(instance.deep_find_all(:title))
        .to eq(['Call of the Wild', 'Moby Dick', 'Main Library'])
    end

    it 'returns nil if it does not find any matches' do
      expect(instance.deep_find_all(:wahoo)).to be_nil
    end

    context 'when match value is hash itself' do
      let(:hash) do
        {
          title: {
            type: :string
          },
          library: {
            books: [
              { title: 'Call of the Wild' },
              { title: 'Moby Dick' }
            ],
            shelves: nil,
            location: {
              address: '123 Library St.',
              title: 'Main Library'
            }
          }
        }
      end

      it 'detects all values from a nested hash' do
        expect(instance.deep_find_all(:title))
          .to eq([{ type: :string }, 'Call of the Wild', 'Moby Dick', 'Main Library'])
      end
    end
  end

  context 'on an ActiveSupport::HashWithIndifferentAccess' do
    subject(:instance) { hash.with_indifferent_access.extend(Hashie::Extensions::DeepFind) }

    describe '#deep_find' do
      it 'indifferently detects a value from a nested hash' do
        expect(instance.deep_find(:address)).to eq('123 Library St.')
        expect(instance.deep_find('address')).to eq('123 Library St.')
      end

      it 'indifferently detects a value from a nested array' do
        expect(instance.deep_find(:title)).to eq('Call of the Wild')
        expect(instance.deep_find('title')).to eq('Call of the Wild')
      end

      it 'indifferently returns nil if it does not find a match' do
        expect(instance.deep_find(:wahoo)).to be_nil
        expect(instance.deep_find('wahoo')).to be_nil
      end
    end

    describe '#deep_find_all' do
      it 'indifferently detects all values from a nested hash' do
        expect(instance.deep_find_all(:title))
          .to eq(['Call of the Wild', 'Moby Dick', 'Main Library'])
        expect(instance.deep_find_all('title'))
          .to eq(['Call of the Wild', 'Moby Dick', 'Main Library'])
      end

      it 'indifferently returns nil if it does not find any matches' do
        expect(instance.deep_find_all(:wahoo)).to be_nil
        expect(instance.deep_find_all('wahoo')).to be_nil
      end
    end
  end

  context 'on a Hash including Hashie::Extensions::IndifferentAccess' do
    let(:klass) { Class.new(Hash) { include Hashie::Extensions::IndifferentAccess } }
    subject(:instance) { klass[hash.dup].extend(Hashie::Extensions::DeepFind) }

    describe '#deep_find' do
      it 'indifferently detects a value from a nested hash' do
        expect(instance.deep_find(:address)).to eq('123 Library St.')
        expect(instance.deep_find('address')).to eq('123 Library St.')
      end

      it 'indifferently detects a value from a nested array' do
        expect(instance.deep_find(:title)).to eq('Call of the Wild')
        expect(instance.deep_find('title')).to eq('Call of the Wild')
      end

      it 'indifferently returns nil if it does not find a match' do
        expect(instance.deep_find(:wahoo)).to be_nil
        expect(instance.deep_find('wahoo')).to be_nil
      end
    end

    describe '#deep_find_all' do
      it 'indifferently detects all values from a nested hash' do
        expect(instance.deep_find_all(:title))
          .to eq(['Call of the Wild', 'Moby Dick', 'Main Library'])
        expect(instance.deep_find_all('title'))
          .to eq(['Call of the Wild', 'Moby Dick', 'Main Library'])
      end

      it 'indifferently returns nil if it does not find any matches' do
        expect(instance.deep_find_all(:wahoo)).to be_nil
        expect(instance.deep_find_all('wahoo')).to be_nil
      end
    end
  end
end