hashie 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/lib/hashie/hash.rb +13 -3
- data/lib/hashie/version.rb +1 -1
- data/spec/hashie/clash_spec.rb +11 -11
- data/spec/hashie/dash_spec.rb +69 -60
- data/spec/hashie/extensions/coercion_spec.rb +22 -22
- data/spec/hashie/extensions/deep_fetch_spec.rb +7 -7
- data/spec/hashie/extensions/deep_merge_spec.rb +2 -2
- data/spec/hashie/extensions/indifferent_access_spec.rb +24 -24
- data/spec/hashie/extensions/key_conversion_spec.rb +14 -14
- data/spec/hashie/extensions/merge_initializer_spec.rb +4 -4
- data/spec/hashie/extensions/method_access_spec.rb +22 -22
- data/spec/hashie/hash_spec.rb +19 -7
- data/spec/hashie/mash_spec.rb +122 -123
- data/spec/hashie/rash_spec.rb +13 -13
- data/spec/hashie/trash_spec.rb +30 -30
- data/spec/hashie/version_spec.rb +1 -1
- metadata +2 -2
data/spec/hashie/mash_spec.rb
CHANGED
@@ -5,123 +5,123 @@ describe Hashie::Mash do
|
|
5
5
|
subject { Hashie::Mash.new }
|
6
6
|
|
7
7
|
it 'inherits from Hash' do
|
8
|
-
subject.is_a?(Hash).
|
8
|
+
expect(subject.is_a?(Hash)).to be_true
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'sets hash values through method= calls' do
|
12
12
|
subject.test = 'abc'
|
13
|
-
subject['test'].
|
13
|
+
expect(subject['test']).to eq 'abc'
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'retrieves set values through method calls' do
|
17
17
|
subject['test'] = 'abc'
|
18
|
-
subject.test.
|
18
|
+
expect(subject.test).to eq 'abc'
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'retrieves set values through blocks' do
|
22
22
|
subject['test'] = 'abc'
|
23
23
|
value = nil
|
24
24
|
subject.[]('test') { |v| value = v }
|
25
|
-
value.
|
25
|
+
expect(value).to eq 'abc'
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'retrieves set values through blocks with method calls' do
|
29
29
|
subject['test'] = 'abc'
|
30
30
|
value = nil
|
31
31
|
subject.test { |v| value = v }
|
32
|
-
value.
|
32
|
+
expect(value).to eq 'abc'
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'tests for already set values when passed a ? method' do
|
36
|
-
subject.test
|
36
|
+
expect(subject.test?).to be_false
|
37
37
|
subject.test = 'abc'
|
38
|
-
subject.test
|
38
|
+
expect(subject.test?).to be_true
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'returns false on a ? method if a value has been set to nil or false' do
|
42
42
|
subject.test = nil
|
43
|
-
subject.
|
43
|
+
expect(subject).not_to be_test
|
44
44
|
subject.test = false
|
45
|
-
subject.
|
45
|
+
expect(subject).not_to be_test
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'makes all [] and []= into strings for consistency' do
|
49
49
|
subject['abc'] = 123
|
50
|
-
subject.key?('abc').
|
51
|
-
subject['abc'].
|
50
|
+
expect(subject.key?('abc')).to be_true
|
51
|
+
expect(subject['abc']).to eq 123
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'has a to_s that is identical to its inspect' do
|
55
55
|
subject.abc = 123
|
56
|
-
subject.to_s.
|
56
|
+
expect(subject.to_s).to eq subject.inspect
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'returns nil instead of raising an error for attribute-esque method calls' do
|
60
|
-
subject.abc.
|
60
|
+
expect(subject.abc).to be_nil
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'returns the default value if set like Hash' do
|
64
64
|
subject.default = 123
|
65
|
-
subject.abc.
|
65
|
+
expect(subject.abc).to eq 123
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'gracefully handles being accessed with arguments' do
|
69
|
-
subject.abc('foobar').
|
69
|
+
expect(subject.abc('foobar')).to eq nil
|
70
70
|
subject.abc = 123
|
71
|
-
subject.abc('foobar').
|
71
|
+
expect(subject.abc('foobar')).to eq 123
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'returns a Hashie::Mash when passed a bang method to a non-existenct key' do
|
75
|
-
subject.abc!.is_a?(Hashie::Mash).
|
75
|
+
expect(subject.abc!.is_a?(Hashie::Mash)).to be_true
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'returns the existing value when passed a bang method for an existing key' do
|
79
79
|
subject.name = 'Bob'
|
80
|
-
subject.name
|
80
|
+
expect(subject.name!).to eq 'Bob'
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'returns a Hashie::Mash when passed an under bang method to a non-existenct key' do
|
84
|
-
subject.abc_.is_a?(Hashie::Mash).
|
84
|
+
expect(subject.abc_.is_a?(Hashie::Mash)).to be_true
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'returns the existing value when passed an under bang method for an existing key' do
|
88
88
|
subject.name = 'Bob'
|
89
|
-
subject.name_.
|
89
|
+
expect(subject.name_).to eq 'Bob'
|
90
90
|
end
|
91
91
|
|
92
92
|
it '#initializing_reader returns a Hashie::Mash when passed a non-existent key' do
|
93
|
-
subject.initializing_reader(:abc).is_a?(Hashie::Mash).
|
93
|
+
expect(subject.initializing_reader(:abc).is_a?(Hashie::Mash)).to be_true
|
94
94
|
end
|
95
95
|
|
96
96
|
it 'allows for multi-level assignment through bang methods' do
|
97
97
|
subject.author!.name = 'Michael Bleigh'
|
98
|
-
subject.author.
|
98
|
+
expect(subject.author).to eq Hashie::Mash.new(name: 'Michael Bleigh')
|
99
99
|
subject.author!.website!.url = 'http://www.mbleigh.com/'
|
100
|
-
subject.author.website.
|
100
|
+
expect(subject.author.website).to eq Hashie::Mash.new(url: 'http://www.mbleigh.com/')
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'allows for multi-level under bang testing' do
|
104
|
-
subject.author_.website_.url.
|
105
|
-
subject.author_.website_.url
|
106
|
-
subject.author.
|
104
|
+
expect(subject.author_.website_.url).to be_nil
|
105
|
+
expect(subject.author_.website_.url?).to eq false
|
106
|
+
expect(subject.author).to be_nil
|
107
107
|
end
|
108
108
|
|
109
109
|
it 'does not call super if id is not a key' do
|
110
|
-
subject.id.
|
110
|
+
expect(subject.id).to eq nil
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'returns the value if id is a key' do
|
114
114
|
subject.id = 'Steve'
|
115
|
-
subject.id.
|
115
|
+
expect(subject.id).to eq 'Steve'
|
116
116
|
end
|
117
117
|
|
118
118
|
it 'does not call super if type is not a key' do
|
119
|
-
subject.type.
|
119
|
+
expect(subject.type).to eq nil
|
120
120
|
end
|
121
121
|
|
122
122
|
it 'returns the value if type is a key' do
|
123
123
|
subject.type = 'Steve'
|
124
|
-
subject.type.
|
124
|
+
expect(subject.type).to eq 'Steve'
|
125
125
|
end
|
126
126
|
|
127
127
|
context 'updating' do
|
@@ -138,10 +138,10 @@ describe Hashie::Mash do
|
|
138
138
|
describe '#deep_update' do
|
139
139
|
it 'recursively Hashie::Mash Hashie::Mashes and hashes together' do
|
140
140
|
subject.deep_update(details: { email: 'michael@intridea.com', city: 'Imagineton' })
|
141
|
-
subject.first_name.
|
142
|
-
subject.details.email.
|
143
|
-
subject.details.address.
|
144
|
-
subject.details.city.
|
141
|
+
expect(subject.first_name).to eq 'Michael'
|
142
|
+
expect(subject.details.email).to eq 'michael@intridea.com'
|
143
|
+
expect(subject.details.address).to eq 'Nowhere road'
|
144
|
+
expect(subject.details.city).to eq 'Imagineton'
|
145
145
|
end
|
146
146
|
|
147
147
|
it 'converts values only once' do
|
@@ -149,60 +149,59 @@ describe Hashie::Mash do
|
|
149
149
|
end
|
150
150
|
|
151
151
|
rhs = ConvertedMash.new(email: 'foo@bar.com')
|
152
|
-
subject.
|
152
|
+
expect(subject).to receive(:convert_value).exactly(1).times
|
153
153
|
subject.deep_update(rhs)
|
154
154
|
end
|
155
155
|
|
156
156
|
it 'makes #update deep by default' do
|
157
|
-
subject.update(details: { address: 'Fake street' }).
|
158
|
-
subject.details.address.
|
159
|
-
subject.details.email.
|
157
|
+
expect(subject.update(details: { address: 'Fake street' })).to eql(subject)
|
158
|
+
expect(subject.details.address).to eq 'Fake street'
|
159
|
+
expect(subject.details.email).to eq 'michael@asf.com'
|
160
160
|
end
|
161
161
|
|
162
162
|
it 'clones before a #deep_merge' do
|
163
163
|
duped = subject.deep_merge(details: { address: 'Fake street' })
|
164
|
-
duped.
|
165
|
-
duped.details.address.
|
166
|
-
subject.details.address.
|
167
|
-
duped.details.email.
|
164
|
+
expect(duped).not_to eql(subject)
|
165
|
+
expect(duped.details.address).to eq 'Fake street'
|
166
|
+
expect(subject.details.address).to eq 'Nowhere road'
|
167
|
+
expect(duped.details.email).to eq 'michael@asf.com'
|
168
168
|
end
|
169
169
|
|
170
170
|
it 'default #merge is deep' do
|
171
171
|
duped = subject.merge(details: { email: 'michael@intridea.com' })
|
172
|
-
duped.
|
173
|
-
duped.details.email.
|
174
|
-
duped.details.address.
|
172
|
+
expect(duped).not_to eql(subject)
|
173
|
+
expect(duped.details.email).to eq 'michael@intridea.com'
|
174
|
+
expect(duped.details.address).to eq 'Nowhere road'
|
175
175
|
end
|
176
176
|
|
177
177
|
# http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-update
|
178
178
|
it 'accepts a block' do
|
179
179
|
duped = subject.merge(details: { address: 'Pasadena CA' }) { |key, oldv, newv| [oldv, newv].join(', ') }
|
180
|
-
duped.details.address.
|
180
|
+
expect(duped.details.address).to eq 'Nowhere road, Pasadena CA'
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
184
|
describe 'shallow update' do
|
185
185
|
it 'shallowly Hashie::Mash Hashie::Mashes and hashes together' do
|
186
|
-
subject.shallow_update(details: {
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
subject.
|
191
|
-
subject.details.
|
192
|
-
subject.details.
|
193
|
-
subject.details.city.should eq 'Imagineton'
|
186
|
+
expect(subject.shallow_update(details: { email: 'michael@intridea.com',
|
187
|
+
city: 'Imagineton' })).to eql(subject)
|
188
|
+
|
189
|
+
expect(subject.first_name).to eq 'Michael'
|
190
|
+
expect(subject.details.email).to eq 'michael@intridea.com'
|
191
|
+
expect(subject.details.address).to be_nil
|
192
|
+
expect(subject.details.city).to eq 'Imagineton'
|
194
193
|
end
|
195
194
|
|
196
195
|
it 'clones before a #regular_merge' do
|
197
196
|
duped = subject.shallow_merge(details: { address: 'Fake street' })
|
198
|
-
duped.
|
197
|
+
expect(duped).not_to eql(subject)
|
199
198
|
end
|
200
199
|
|
201
200
|
it 'default #merge is shallow' do
|
202
201
|
duped = subject.shallow_merge(details: { address: 'Fake street' })
|
203
|
-
duped.details.address.
|
204
|
-
subject.details.address.
|
205
|
-
duped.details.email.
|
202
|
+
expect(duped.details.address).to eq 'Fake street'
|
203
|
+
expect(subject.details.address).to eq 'Nowhere road'
|
204
|
+
expect(duped.details.email).to be_nil
|
206
205
|
end
|
207
206
|
end
|
208
207
|
|
@@ -215,45 +214,45 @@ describe Hashie::Mash do
|
|
215
214
|
end
|
216
215
|
|
217
216
|
it 'returns self' do
|
218
|
-
subject.replace(foo: 'bar').to_hash.
|
217
|
+
expect(subject.replace(foo: 'bar').to_hash).to eq('foo' => 'bar')
|
219
218
|
end
|
220
219
|
|
221
220
|
it 'sets all specified keys to their corresponding values' do
|
222
|
-
subject.middle_name
|
223
|
-
subject.details
|
224
|
-
subject.middle_name.
|
225
|
-
subject.details.city
|
226
|
-
subject.details.city.
|
221
|
+
expect(subject.middle_name?).to be_true
|
222
|
+
expect(subject.details?).to be_true
|
223
|
+
expect(subject.middle_name).to eq 'Cain'
|
224
|
+
expect(subject.details.city?).to be_true
|
225
|
+
expect(subject.details.city).to eq 'Imagination'
|
227
226
|
end
|
228
227
|
|
229
228
|
it 'leaves only specified keys' do
|
230
|
-
subject.keys.sort.
|
231
|
-
subject.first_name
|
232
|
-
subject.
|
233
|
-
subject.last_name
|
234
|
-
subject.
|
229
|
+
expect(subject.keys.sort).to eq %w(details middle_name)
|
230
|
+
expect(subject.first_name?).to be_false
|
231
|
+
expect(subject).not_to respond_to(:first_name)
|
232
|
+
expect(subject.last_name?).to be_false
|
233
|
+
expect(subject).not_to respond_to(:last_name)
|
235
234
|
end
|
236
235
|
end
|
237
236
|
|
238
237
|
describe 'delete' do
|
239
238
|
it 'deletes with String key' do
|
240
239
|
subject.delete('details')
|
241
|
-
subject.details.
|
242
|
-
subject.
|
240
|
+
expect(subject.details).to be_nil
|
241
|
+
expect(subject).not_to be_respond_to :details
|
243
242
|
end
|
244
243
|
|
245
244
|
it 'deletes with Symbol key' do
|
246
245
|
subject.delete(:details)
|
247
|
-
subject.details.
|
248
|
-
subject.
|
246
|
+
expect(subject.details).to be_nil
|
247
|
+
expect(subject).not_to be_respond_to :details
|
249
248
|
end
|
250
249
|
end
|
251
250
|
end
|
252
251
|
|
253
252
|
it 'converts hash assignments into Hashie::Mashes' do
|
254
253
|
subject.details = { email: 'randy@asf.com', address: { state: 'TX' } }
|
255
|
-
subject.details.email.
|
256
|
-
subject.details.address.state.
|
254
|
+
expect(subject.details.email).to eq 'randy@asf.com'
|
255
|
+
expect(subject.details.address.state).to eq 'TX'
|
257
256
|
end
|
258
257
|
|
259
258
|
it 'does not convert the type of Hashie::Mashes childs to Hashie::Mash' do
|
@@ -262,7 +261,7 @@ describe Hashie::Mash do
|
|
262
261
|
|
263
262
|
record = MyMash.new
|
264
263
|
record.son = MyMash.new
|
265
|
-
record.son.class.
|
264
|
+
expect(record.son.class).to eq MyMash
|
266
265
|
end
|
267
266
|
|
268
267
|
it 'does not change the class of Mashes when converted' do
|
@@ -272,35 +271,35 @@ describe Hashie::Mash do
|
|
272
271
|
record = Hashie::Mash.new
|
273
272
|
son = SubMash.new
|
274
273
|
record['submash'] = son
|
275
|
-
record['submash'].
|
274
|
+
expect(record['submash']).to be_kind_of(SubMash)
|
276
275
|
end
|
277
276
|
|
278
277
|
it 'respects the class when passed a bang method for a non-existent key' do
|
279
278
|
record = Hashie::Mash.new
|
280
|
-
record.non_existent
|
279
|
+
expect(record.non_existent!).to be_kind_of(Hashie::Mash)
|
281
280
|
|
282
281
|
class SubMash < Hashie::Mash
|
283
282
|
end
|
284
283
|
|
285
284
|
son = SubMash.new
|
286
|
-
son.non_existent
|
285
|
+
expect(son.non_existent!).to be_kind_of(SubMash)
|
287
286
|
end
|
288
287
|
|
289
288
|
it 'respects the class when passed an under bang method for a non-existent key' do
|
290
289
|
record = Hashie::Mash.new
|
291
|
-
record.non_existent_.
|
290
|
+
expect(record.non_existent_).to be_kind_of(Hashie::Mash)
|
292
291
|
|
293
292
|
class SubMash < Hashie::Mash
|
294
293
|
end
|
295
294
|
|
296
295
|
son = SubMash.new
|
297
|
-
son.non_existent_.
|
296
|
+
expect(son.non_existent_).to be_kind_of(SubMash)
|
298
297
|
end
|
299
298
|
|
300
299
|
it 'respects the class when converting the value' do
|
301
300
|
record = Hashie::Mash.new
|
302
301
|
record.details = Hashie::Mash.new(email: 'randy@asf.com')
|
303
|
-
record.details.
|
302
|
+
expect(record.details).to be_kind_of(Hashie::Mash)
|
304
303
|
end
|
305
304
|
|
306
305
|
it 'respects another subclass when converting the value' do
|
@@ -311,84 +310,84 @@ describe Hashie::Mash do
|
|
311
310
|
|
312
311
|
son = SubMash.new(email: 'foo@bar.com')
|
313
312
|
record.details = son
|
314
|
-
record.details.
|
313
|
+
expect(record.details).to be_kind_of(SubMash)
|
315
314
|
end
|
316
315
|
|
317
316
|
describe '#respond_to?' do
|
318
317
|
it 'responds to a normal method' do
|
319
|
-
Hashie::Mash.new.
|
318
|
+
expect(Hashie::Mash.new).to be_respond_to(:key?)
|
320
319
|
end
|
321
320
|
|
322
321
|
it 'responds to a set key' do
|
323
|
-
Hashie::Mash.new(abc: 'def').
|
322
|
+
expect(Hashie::Mash.new(abc: 'def')).to be_respond_to(:abc)
|
324
323
|
end
|
325
324
|
|
326
325
|
it 'responds to a set key with a suffix' do
|
327
326
|
%w(= ? ! _).each do |suffix|
|
328
|
-
Hashie::Mash.new(abc: 'def').
|
327
|
+
expect(Hashie::Mash.new(abc: 'def')).to be_respond_to(:"abc#{suffix}")
|
329
328
|
end
|
330
329
|
end
|
331
330
|
|
332
331
|
it 'does not respond to an unknown key with a suffix' do
|
333
332
|
%w(= ? ! _).each do |suffix|
|
334
|
-
Hashie::Mash.new(abc: 'def').
|
333
|
+
expect(Hashie::Mash.new(abc: 'def')).not_to be_respond_to(:"xyz#{suffix}")
|
335
334
|
end
|
336
335
|
end
|
337
336
|
|
338
337
|
it 'does not respond to an unknown key without a suffix' do
|
339
|
-
Hashie::Mash.new(abc: 'def').
|
338
|
+
expect(Hashie::Mash.new(abc: 'def')).not_to be_respond_to(:xyz)
|
340
339
|
end
|
341
340
|
|
342
341
|
it 'does not respond to permitted?' do
|
343
|
-
Hashie::Mash.new.
|
342
|
+
expect(Hashie::Mash.new).not_to be_respond_to(:permitted?)
|
344
343
|
end
|
345
344
|
end
|
346
345
|
|
347
346
|
context '#initialize' do
|
348
347
|
it 'converts an existing hash to a Hashie::Mash' do
|
349
348
|
converted = Hashie::Mash.new(abc: 123, name: 'Bob')
|
350
|
-
converted.abc.
|
351
|
-
converted.name.
|
349
|
+
expect(converted.abc).to eq 123
|
350
|
+
expect(converted.name).to eq 'Bob'
|
352
351
|
end
|
353
352
|
|
354
353
|
it 'converts hashes recursively into Hashie::Mashes' do
|
355
354
|
converted = Hashie::Mash.new(a: { b: 1, c: { d: 23 } })
|
356
|
-
converted.a.is_a?(Hashie::Mash).
|
357
|
-
converted.a.b.
|
358
|
-
converted.a.c.d.
|
355
|
+
expect(converted.a.is_a?(Hashie::Mash)).to be_true
|
356
|
+
expect(converted.a.b).to eq 1
|
357
|
+
expect(converted.a.c.d).to eq 23
|
359
358
|
end
|
360
359
|
|
361
360
|
it 'converts hashes in arrays into Hashie::Mashes' do
|
362
361
|
converted = Hashie::Mash.new(a: [{ b: 12 }, 23])
|
363
|
-
converted.a.first.b.
|
364
|
-
converted.a.last.
|
362
|
+
expect(converted.a.first.b).to eq 12
|
363
|
+
expect(converted.a.last).to eq 23
|
365
364
|
end
|
366
365
|
|
367
366
|
it 'converts an existing Hashie::Mash into a Hashie::Mash' do
|
368
367
|
initial = Hashie::Mash.new(name: 'randy', address: { state: 'TX' })
|
369
368
|
copy = Hashie::Mash.new(initial)
|
370
|
-
initial.name.
|
371
|
-
initial.__id__.
|
372
|
-
copy.address.state.
|
369
|
+
expect(initial.name).to eq copy.name
|
370
|
+
expect(initial.__id__).not_to eq copy.__id__
|
371
|
+
expect(copy.address.state).to eq 'TX'
|
373
372
|
copy.address.state = 'MI'
|
374
|
-
initial.address.state.
|
375
|
-
copy.address.__id__.
|
373
|
+
expect(initial.address.state).to eq 'TX'
|
374
|
+
expect(copy.address.__id__).not_to eq initial.address.__id__
|
376
375
|
end
|
377
376
|
|
378
377
|
it 'accepts a default block' do
|
379
378
|
initial = Hashie::Mash.new { |h, i| h[i] = [] }
|
380
|
-
initial.default_proc.
|
381
|
-
initial.default.
|
382
|
-
initial.test.
|
383
|
-
initial.test
|
379
|
+
expect(initial.default_proc).not_to be_nil
|
380
|
+
expect(initial.default).to be_nil
|
381
|
+
expect(initial.test).to eq []
|
382
|
+
expect(initial.test?).to be_true
|
384
383
|
end
|
385
384
|
|
386
385
|
it 'converts Hashie::Mashes within Arrays back to Hashes' do
|
387
386
|
initial_hash = { 'a' => [{ 'b' => 12, 'c' => ['d' => 50, 'e' => 51] }, 23] }
|
388
387
|
converted = Hashie::Mash.new(initial_hash)
|
389
|
-
converted.to_hash['a'].first.is_a?(Hashie::Mash).
|
390
|
-
converted.to_hash['a'].first.is_a?(Hash).
|
391
|
-
converted.to_hash['a'].first['c'].first.is_a?(Hashie::Mash).
|
388
|
+
expect(converted.to_hash['a'].first.is_a?(Hashie::Mash)).to be_false
|
389
|
+
expect(converted.to_hash['a'].first.is_a?(Hash)).to be_true
|
390
|
+
expect(converted.to_hash['a'].first['c'].first.is_a?(Hashie::Mash)).to be_false
|
392
391
|
end
|
393
392
|
end
|
394
393
|
|
@@ -398,16 +397,16 @@ describe Hashie::Mash do
|
|
398
397
|
|
399
398
|
context 'when key exists' do
|
400
399
|
it 'returns the value' do
|
401
|
-
mash.fetch(:one).
|
400
|
+
expect(mash.fetch(:one)).to eql(1)
|
402
401
|
end
|
403
402
|
|
404
403
|
it 'returns the value even if the value is falsy' do
|
405
|
-
mash.fetch(:other).
|
404
|
+
expect(mash.fetch(:other)).to eql(false)
|
406
405
|
end
|
407
406
|
|
408
407
|
context 'when key has other than original but acceptable type' do
|
409
408
|
it 'returns the value' do
|
410
|
-
mash.fetch('one').
|
409
|
+
expect(mash.fetch('one')).to eql(1)
|
411
410
|
end
|
412
411
|
end
|
413
412
|
end
|
@@ -420,19 +419,19 @@ describe Hashie::Mash do
|
|
420
419
|
|
421
420
|
context 'with default value given' do
|
422
421
|
it 'returns default value' do
|
423
|
-
mash.fetch(:two, 8).
|
422
|
+
expect(mash.fetch(:two, 8)).to eql(8)
|
424
423
|
end
|
425
424
|
|
426
425
|
it 'returns default value even if it is falsy' do
|
427
|
-
mash.fetch(:two, false).
|
426
|
+
expect(mash.fetch(:two, false)).to eql(false)
|
428
427
|
end
|
429
428
|
end
|
430
429
|
|
431
430
|
context 'with block given' do
|
432
431
|
it 'returns default value' do
|
433
|
-
mash.fetch(:two) do |key|
|
432
|
+
expect(mash.fetch(:two) do |key|
|
434
433
|
'block default value'
|
435
|
-
end.
|
434
|
+
end).to eql('block default value')
|
436
435
|
end
|
437
436
|
end
|
438
437
|
end
|
@@ -443,26 +442,26 @@ describe Hashie::Mash do
|
|
443
442
|
let(:mash) { Hashie::Mash.new(hash) }
|
444
443
|
|
445
444
|
it 'returns a standard Hash' do
|
446
|
-
mash.to_hash.
|
445
|
+
expect(mash.to_hash).to be_a(::Hash)
|
447
446
|
end
|
448
447
|
|
449
448
|
it 'includes all keys' do
|
450
|
-
mash.to_hash.keys.
|
449
|
+
expect(mash.to_hash.keys).to eql(%w(outer testing))
|
451
450
|
end
|
452
451
|
|
453
452
|
it 'converts keys to symbols when symbolize_keys option is true' do
|
454
|
-
mash.to_hash(symbolize_keys: true).keys.
|
455
|
-
mash.to_hash(symbolize_keys: true).keys.
|
453
|
+
expect(mash.to_hash(symbolize_keys: true).keys).to include(:outer)
|
454
|
+
expect(mash.to_hash(symbolize_keys: true).keys).not_to include('outer')
|
456
455
|
end
|
457
456
|
|
458
457
|
it 'leaves keys as strings when symbolize_keys option is false' do
|
459
|
-
mash.to_hash(symbolize_keys: false).keys.
|
460
|
-
mash.to_hash(symbolize_keys: false).keys.
|
458
|
+
expect(mash.to_hash(symbolize_keys: false).keys).to include('outer')
|
459
|
+
expect(mash.to_hash(symbolize_keys: false).keys).not_to include(:outer)
|
461
460
|
end
|
462
461
|
|
463
462
|
it 'symbolizes keys recursively' do
|
464
|
-
mash.to_hash(symbolize_keys: true)[:outer].keys.
|
465
|
-
mash.to_hash(symbolize_keys: true)[:outer].keys.
|
463
|
+
expect(mash.to_hash(symbolize_keys: true)[:outer].keys).to include(:inner)
|
464
|
+
expect(mash.to_hash(symbolize_keys: true)[:outer].keys).not_to include('inner')
|
466
465
|
end
|
467
466
|
end
|
468
467
|
end
|
data/spec/hashie/rash_spec.rb
CHANGED
@@ -15,30 +15,30 @@ describe Hashie::Rash do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'finds strings' do
|
18
|
-
subject['other'].
|
19
|
-
subject['well hello there'].
|
20
|
-
subject['the world is round'].
|
21
|
-
subject.all('hello world').sort.
|
18
|
+
expect(subject['other']).to eq 'whee'
|
19
|
+
expect(subject['well hello there']).to eq 'hello'
|
20
|
+
expect(subject['the world is round']).to eq 'world'
|
21
|
+
expect(subject.all('hello world').sort).to eq %w(hello world)
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'finds regexps' do
|
25
|
-
subject[/other/].
|
25
|
+
expect(subject[/other/]).to eq 'whee'
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'finds other objects' do
|
29
|
-
subject[true].
|
30
|
-
subject[1].
|
29
|
+
expect(subject[true]).to eq false
|
30
|
+
expect(subject[1]).to eq 'awesome'
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'finds numbers from ranges' do
|
34
|
-
subject[250].
|
35
|
-
subject[999].
|
36
|
-
subject[1000].
|
37
|
-
subject[1001].
|
34
|
+
expect(subject[250]).to eq 'rangey'
|
35
|
+
expect(subject[999]).to eq 'rangey'
|
36
|
+
expect(subject[1000]).to eq 'rangey'
|
37
|
+
expect(subject[1001]).to be_nil
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'evaluates proc values' do
|
41
|
-
subject['abcdef'].
|
42
|
-
subject['ffffff'].
|
41
|
+
expect(subject['abcdef']).to eq 'bcd'
|
42
|
+
expect(subject['ffffff']).to be_nil
|
43
43
|
end
|
44
44
|
end
|