summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Lincoln <max@devopsy.com>2014-08-08 00:00:34 -0400
committerMax Lincoln <max@devopsy.com>2014-08-08 00:52:58 -0400
commita17d15888b11f12fc015573f8a2d1901d1994ce7 (patch)
treee1aef9a77dad39f0f71a9c6cdf39b02594a08098
parent2adedfc98d1d66e19fe5e0449d3395274692695d (diff)
downloadhashie-a17d15888b11f12fc015573f8a2d1901d1994ce7.tar.gz
Add exception handling and coerce_value tests
-rw-r--r--lib/hashie/extensions/coercion.rb2
-rw-r--r--spec/hashie/extensions/coercion_spec.rb42
2 files changed, 43 insertions, 1 deletions
diff --git a/lib/hashie/extensions/coercion.rb b/lib/hashie/extensions/coercion.rb
index 6c49868..681a0af 100644
--- a/lib/hashie/extensions/coercion.rb
+++ b/lib/hashie/extensions/coercion.rb
@@ -33,6 +33,8 @@ module Hashie
end
set_value_without_coercion(key, value)
+ rescue NoMethodError, TypeError => e
+ raise TypeError, "Cannot coerce property #{key.inspect} from #{value.class} to #{into}: #{e.message}"
end
def coerce_or_init(type)
diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb
index 09ea18d..15c8ac8 100644
--- a/spec/hashie/extensions/coercion_spec.rb
+++ b/spec/hashie/extensions/coercion_spec.rb
@@ -156,7 +156,7 @@ describe Hashie::Extensions::Coercion do
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')
+ expect { instance[:foo] = true }.to raise_error(TypeError, /TrueClass is not a coercable type/)
end
end
@@ -315,6 +315,46 @@ describe Hashie::Extensions::Coercion do
expect(instance[:foo]).to be_kind_of(Coercable)
end
end
+
+ context 'core types' do
+ it 'coerces String to Integer when possible' do
+ subject.coerce_value String, Integer
+
+ instance[:foo] = '2'
+ instance[:bar] = '2.7'
+ instance[:hi] = 'hi'
+ expect(instance[:foo]).to be_a(Integer)
+ expect(instance[:foo]).to eq(2)
+ expect(instance[:bar]).to be_a(Integer)
+ expect(instance[:bar]).to eq(2)
+ expect(instance[:hi]).to be_a(Integer)
+ expect(instance[:hi]).to eq(0) # not what I expected...
+ end
+
+ it 'coerces non-numeric from String to Integer' do
+ # This was surprising, but I guess it's "correct"
+ # unless there is a stricter `to_i` alternative
+ subject.coerce_value String, Integer
+ instance[:hi] = 'hi'
+ expect(instance[:hi]).to be_a(Integer)
+ expect(instance[:hi]).to eq(0)
+ end
+
+ it 'raises a TypeError when coercion is not possible' do
+ subject.coerce_value Fixnum, Symbol
+ expect { instance[:hi] = 1 }.to raise_error(TypeError, /Cannot coerce property :hi from Fixnum to Symbol/)
+ end
+
+ pending 'coerces Integer to String' do
+ # This isn't possible because coerce_value doesn't handle superclasses
+ instance[:foo] = 2
+ instance[:bar] = 2.7
+ expect(instance[:foo]).to be_a(String)
+ expect(instance[:foo]).to eq('2')
+ expect(instance[:bar]).to be_a(String)
+ expect(instance[:bar]).to eq('2.0')
+ end
+ end
end
after(:each) do