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
|
require 'spec_helper'
describe Hashie::Extensions::IndifferentAccess do
class IndifferentHashWithMergeInitializer < Hash
include Hashie::Extensions::MergeInitializer
include Hashie::Extensions::IndifferentAccess
class << self
alias_method :build, :new
end
end
class IndifferentHashWithArrayInitializer < Hash
include Hashie::Extensions::IndifferentAccess
class << self
alias_method :build, :[]
end
end
class IndifferentHashWithTryConvertInitializer < Hash
include Hashie::Extensions::IndifferentAccess
class << self
alias_method :build, :try_convert
end
end
shared_examples_for 'hash with indifferent access' do
it 'is able to access via string or symbol' do
h = subject.build(abc: 123)
expect(h[:abc]).to eq 123
expect(h['abc']).to eq 123
end
describe '#values_at' do
it 'indifferently finds values' do
h = subject.build(:foo => 'bar', 'baz' => 'qux')
expect(h.values_at('foo', :baz)).to eq %w(bar qux)
end
end
describe '#fetch' do
it 'works like normal fetch, but indifferent' do
h = subject.build(foo: 'bar')
expect(h.fetch(:foo)).to eq h.fetch('foo')
expect(h.fetch(:foo)).to eq 'bar'
end
end
describe '#delete' do
it 'deletes indifferently' do
h = subject.build(:foo => 'bar', 'baz' => 'qux')
h.delete('foo')
h.delete(:baz)
expect(h).to be_empty
end
end
describe '#key?' do
let(:h) { subject.build(foo: 'bar') }
it 'finds it indifferently' do
expect(h).to be_key(:foo)
expect(h).to be_key('foo')
end
%w(include? member? has_key?).each do |key_alias|
it "is aliased as #{key_alias}" do
expect(h.send(key_alias.to_sym, :foo)).to be(true)
expect(h.send(key_alias.to_sym, 'foo')).to be(true)
end
end
end
describe '#update' do
let(:h) { subject.build(foo: 'bar') }
it 'allows keys to be indifferent still' do
h.update(baz: 'qux')
expect(h['foo']).to eq 'bar'
expect(h['baz']).to eq 'qux'
end
it 'recursively injects indifference into sub-hashes' do
h.update(baz: { qux: 'abc' })
expect(h['baz']['qux']).to eq 'abc'
end
it 'does not change the ancestors of the injected object class' do
h.update(baz: { qux: 'abc' })
expect(Hash.new).not_to be_respond_to(:indifferent_access?)
end
end
describe '#replace' do
let(:h) { subject.build(foo: 'bar').replace(bar: 'baz', hi: 'bye') }
it 'returns self' do
expect(h).to be_a(subject)
end
it 'removes old keys' do
[:foo, 'foo'].each do |k|
expect(h[k]).to be_nil
expect(h.key?(k)).to be_falsy
end
end
it 'creates new keys with indifferent access' do
[:bar, 'bar', :hi, 'hi'].each { |k| expect(h.key?(k)).to be_truthy }
expect(h[:bar]).to eq 'baz'
expect(h['bar']).to eq 'baz'
expect(h[:hi]).to eq 'bye'
expect(h['hi']).to eq 'bye'
end
end
describe '#try_convert' do
describe 'with conversion' do
let(:h) { subject.try_convert(foo: 'bar') }
it 'is a subject' do
expect(h).to be_a(subject)
end
end
describe 'without conversion' do
let(:h) { subject.try_convert('{ :foo => bar }') }
it 'is nil' do
expect(h).to be_nil
end
end
end
end
describe 'with merge initializer' do
subject { IndifferentHashWithMergeInitializer }
it_should_behave_like 'hash with indifferent access'
end
describe 'with array initializer' do
subject { IndifferentHashWithArrayInitializer }
it_should_behave_like 'hash with indifferent access'
end
describe 'with try convert initializer' do
subject { IndifferentHashWithTryConvertInitializer }
it_should_behave_like 'hash with indifferent access'
end
end
|