blob: 84a662dcc0f7c4c492fc2f5ec328344cc7681f2e (
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
|
require 'spec_helper'
describe Hashie::Extensions::Coercion do
class Initializable
def initialize(obj, coerced = false)
@coerced = coerced
@value = obj.class.to_s
end
def coerced?; @coerced end
attr_reader :value
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 { subject.should be_respond_to(:coerce_key) }
it 'should run through coerce on a specified key' do
subject.coerce_key :foo, Coercable
instance[:foo] = "bar"
instance[:foo].should be_coerced
end
it 'should just call #new if no coerce method is available' do
subject.coerce_key :foo, Initializable
instance[:foo] = "bar"
instance[:foo].value.should == "String"
instance[:foo].should_not be_coerced
end
it "should coerce when the merge initializer is used" do
subject.coerce_key :foo, Coercable
instance = subject.new(:foo => "bar")
instance[:foo].should be_coerced
end
end
describe '.coerce_value' do
context 'with :strict => true' do
it 'should coerce any value of the exact right class' do
subject.coerce_value String, Coercable
instance[:foo] = "bar"
instance[:bar] = "bax"
instance[:foo].should be_coerced
instance[:bar].should be_coerced
end
it 'should not coerce superclasses' do
klass = Class.new(String)
subject.coerce_value klass, Coercable
instance[:foo] = "bar"
instance[:foo].should_not be_kind_of(Coercable)
instance[:foo] = klass.new
instance[:foo].should be_kind_of(Coercable)
end
end
end
after(:each) do
Object.send(:remove_const, :ExampleCoercableHash)
end
end
|