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
|
require 'spec_helper'
describe Hashie::Extensions::Coercion do
class Initializable
attr_reader :coerced, :value
def initialize(obj, coerced = false)
@coerced = coerced
@value = obj.class.to_s
end
def coerced?
!!@coerced
end
end
class Coercable < Initializable
def self.coerce(obj)
new(obj, true)
end
end
before(:each) do
class ExampleCoercableHash < Hash
include Hashie::Extensions::Coercion
include Hashie::Extensions::MergeInitializer
end
end
subject { ExampleCoercableHash }
let(:instance) { subject.new }
describe '#coerce_key' do
it { expect(subject).to be_respond_to(:coerce_key) }
it 'runs through coerce on a specified key' do
subject.coerce_key :foo, Coercable
instance[:foo] = 'bar'
expect(instance[:foo]).to be_coerced
end
it 'skips unnecessary coercions' do
subject.coerce_key :foo, Coercable
instance[:foo] = Coercable.new('bar')
expect(instance[:foo]).to_not be_coerced
end
it 'supports an array of keys' do
subject.coerce_keys :foo, :bar, Coercable
instance[:foo] = 'bar'
instance[:bar] = 'bax'
expect(instance[:foo]).to be_coerced
expect(instance[:bar]).to be_coerced
end
it 'supports coercion for Array' do
subject.coerce_key :foo, Array[Coercable]
instance[:foo] = %w('bar', 'bar2')
expect(instance[:foo]).to all(be_coerced)
expect(instance[:foo]).to be_a(Array)
end
it 'supports coercion for Set' do
subject.coerce_key :foo, Set[Coercable]
instance[:foo] = Set.new(%w('bar', 'bar2'))
expect(instance[:foo]).to all(be_coerced)
expect(instance[:foo]).to be_a(Set)
end
it 'supports coercion for Set of primitive' do
subject.coerce_key :foo, Set[Initializable]
instance[:foo] = %w('bar', 'bar2')
expect(instance[:foo].map(&:value)).to all(eq 'String')
expect(instance[:foo]).to be_none { |v| v.coerced? }
expect(instance[:foo]).to be_a(Set)
end
it 'supports coercion for Hash' do
subject.coerce_key :foo, Hash[Coercable => Coercable]
instance[:foo] = { 'bar_key' => 'bar_value', 'bar2_key' => 'bar2_value' }
expect(instance[:foo].keys).to all(be_coerced)
expect(instance[:foo].values).to all(be_coerced)
expect(instance[:foo]).to be_a(Hash)
end
it 'supports coercion for Hash with primitive as value' do
subject.coerce_key :foo, Hash[Coercable => Initializable]
instance[:foo] = { 'bar_key' => '1', 'bar2_key' => '2' }
expect(instance[:foo].values.map(&:value)).to all(eq 'String')
expect(instance[:foo].keys).to all(be_coerced)
end
context 'coercing core types' do
def test_coercion(literal, target_type, coerce_method)
subject.coerce_key :foo, target_type
instance[:foo] = literal
expect(instance[:foo]).to be_a(target_type)
expect(instance[:foo]).to eq(literal.send(coerce_method))
end
RSpec.shared_examples 'coerces from numeric types' do |target_type, coerce_method|
it "coerces from String to #{target_type} via #{coerce_method}" do
test_coercion '2.0', target_type, coerce_method
end
it "coerces from Integer to #{target_type} via #{coerce_method}" do
# Fixnum
test_coercion 2, target_type, coerce_method
# Bignum
test_coercion 12_345_667_890_987_654_321, target_type, coerce_method
end
it "coerces from Rational to #{target_type} via #{coerce_method}" do
test_coercion Rational(2, 3), target_type, coerce_method
end
end
RSpec.shared_examples 'coerces from alphabetical types' do |target_type, coerce_method|
it "coerces from String to #{target_type} via #{coerce_method}" do
test_coercion 'abc', target_type, coerce_method
end
it "coerces from Symbol to #{target_type} via #{coerce_method}" do
test_coercion :abc, target_type, coerce_method
end
end
include_examples 'coerces from numeric types', Integer, :to_i
include_examples 'coerces from numeric types', Float, :to_f
include_examples 'coerces from numeric types', String, :to_s
include_examples 'coerces from alphabetical types', String, :to_s
include_examples 'coerces from alphabetical types', Symbol, :to_sym
it 'checks boolean types' do
subject.coerce_key :true, :boolean
subject.coerce_key :false, :boolean
instance[:true] = true
instance[:false] = false
expect(instance[:true]).to be_a(TrueClass)
expect(instance[:false]).to be_a(FalseClass)
expect { instance[:true] = 'true' }.to raise_error(NotImplementedError, 'Boolean coercion is not supported')
expect { instance[:false] = 'false' }.to raise_error(NotImplementedError, 'Boolean coercion is not supported')
end
it 'raises errors for non-coercable types' do
subject.coerce_key :foo, TrueClass
expect { instance[:foo] = true }.to raise_error(TypeError, 'TrueClass is not a coercable type')
end
end
it 'does not coerce unnecessarily' do
subject.coerce_key :foo, Float
instance[:foo] = 2.0
expect(instance[:foo]).to be_a(Float)
expect(instance[:foo]).to eq(2.0)
end
it 'calls #new if no coerce method is available' do
subject.coerce_key :foo, Initializable
instance[:foo] = 'bar'
expect(instance[:foo].value).to eq 'String'
expect(instance[:foo]).not_to be_coerced
end
it 'coerces when the merge initializer is used' do
subject.coerce_key :foo, Coercable
instance = subject.new(foo: 'bar')
expect(instance[:foo]).to be_coerced
end
context 'when #replace is used' do
before { subject.coerce_key :foo, :bar, Coercable }
let(:instance) do
subject.new(foo: 'bar').replace(foo: 'foz', bar: 'baz', hi: 'bye')
end
it 'coerces relevant keys' do
expect(instance[:foo]).to be_coerced
expect(instance[:bar]).to be_coerced
expect(instance[:hi]).not_to respond_to(:coerced?)
end
it 'sets correct values' do
expect(instance[:hi]).to eq 'bye'
end
end
context 'when used with a Mash' do
class UserMash < Hashie::Mash
end
class TweetMash < Hashie::Mash
include Hashie::Extensions::Coercion
coerce_key :user, UserMash
end
it 'coerces with instance initialization' do
tweet = TweetMash.new(user: { email: 'foo@bar.com' })
expect(tweet[:user]).to be_a(UserMash)
end
it 'coerces when setting with attribute style' do
tweet = TweetMash.new
tweet.user = { email: 'foo@bar.com' }
expect(tweet[:user]).to be_a(UserMash)
end
it 'coerces when setting with string index' do
tweet = TweetMash.new
tweet['user'] = { email: 'foo@bar.com' }
expect(tweet['user']).to be_a(UserMash)
end
it 'coerces when setting with symbol index' do
tweet = TweetMash.new
tweet[:user] = { email: 'foo@bar.com' }
expect(tweet[:user]).to be_a(UserMash)
end
end
context 'when used with a Trash' do
class UserTrash < Hashie::Trash
property :email
end
class TweetTrash < Hashie::Trash
include Hashie::Extensions::Coercion
property :user, from: :user_data
coerce_key :user, UserTrash
end
it 'coerces with instance initialization' do
tweet = TweetTrash.new(user_data: { email: 'foo@bar.com' })
expect(tweet[:user]).to be_a(UserTrash)
end
end
context 'when used with IndifferentAccess to coerce a Mash' do
class MyHash < Hash
include Hashie::Extensions::Coercion
include Hashie::Extensions::IndifferentAccess
include Hashie::Extensions::MergeInitializer
end
class UserHash < MyHash
end
class TweetHash < MyHash
coerce_key :user, UserHash
end
it 'coerces with instance initialization' do
tweet = TweetHash.new(user: Hashie::Mash.new(email: 'foo@bar.com'))
expect(tweet[:user]).to be_a(UserHash)
end
it 'coerces when setting with string index' do
tweet = TweetHash.new
tweet['user'] = Hashie::Mash.new(email: 'foo@bar.com')
expect(tweet[:user]).to be_a(UserHash)
end
it 'coerces when setting with symbol index' do
tweet = TweetHash.new
tweet[:user] = Hashie::Mash.new(email: 'foo@bar.com')
expect(tweet[:user]).to be_a(UserHash)
end
end
end
describe '#coerce_value' do
context 'with strict: true' do
it 'coerces any value of the exact right class' do
subject.coerce_value String, Coercable
instance[:foo] = 'bar'
instance[:bar] = 'bax'
instance[:hi] = :bye
expect(instance[:foo]).to be_coerced
expect(instance[:bar]).to be_coerced
expect(instance[:hi]).not_to respond_to(:coerced?)
end
it 'coerces values from a #replace call' do
subject.coerce_value String, Coercable
instance[:foo] = :bar
instance.replace(foo: 'bar', bar: 'bax')
expect(instance[:foo]).to be_coerced
expect(instance[:bar]).to be_coerced
end
it 'does not coerce superclasses' do
klass = Class.new(String)
subject.coerce_value klass, Coercable
instance[:foo] = 'bar'
expect(instance[:foo]).not_to be_kind_of(Coercable)
instance[:foo] = klass.new
expect(instance[:foo]).to be_kind_of(Coercable)
end
end
end
after(:each) do
Object.send(:remove_const, :ExampleCoercableHash)
end
end
|