diff options
author | Max Lincoln <max@devopsy.com> | 2014-08-07 23:36:20 -0400 |
---|---|---|
committer | Max Lincoln <max@devopsy.com> | 2014-08-07 23:38:27 -0400 |
commit | 2adedfc98d1d66e19fe5e0449d3395274692695d (patch) | |
tree | 3cba883d07bf1e4dc8d98f47ea236d9393eaa8e1 /spec/hashie/extensions/coercion_spec.rb | |
parent | a8e29ffc492621ca725af2832df01055ea1517b1 (diff) | |
download | hashie-2adedfc98d1d66e19fe5e0449d3395274692695d.tar.gz |
core type coercion
Diffstat (limited to 'spec/hashie/extensions/coercion_spec.rb')
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index 50b359e..09ea18d 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -99,12 +99,65 @@ describe Hashie::Extensions::Coercion do expect(instance[:foo].keys).to all(be_coerced) end - pending 'supports coercion for Float' do - subject.coerce_key :foo, Float + 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 - instance[:foo] = '2.0' - expect(instance[:foo]).to be_a(Float) - expect(instance[:foo]).to eq(2.0) + 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 |