spec/compel/coercion_spec.rb in compel-0.1.3 vs spec/compel/coercion_spec.rb in compel-0.2.0
- old
+ new
@@ -21,11 +21,11 @@
it 'should not coerce' do
expect(Compel::Coercion.valid?('123abc', Integer)).to eq(false)
expect { Compel::Coercion.coerce!('123abc', Integer) }.to \
- raise_error Compel::ParamTypeError, "'123abc' is not a valid Integer"
+ raise_error Compel::TypeError, "'123abc' is not a valid Integer"
end
end
context 'Float' do
@@ -38,11 +38,11 @@
it 'should not coerce' do
expect(Compel::Coercion.valid?('1.a233', Float)).to eq(false)
expect { Compel::Coercion.coerce!('1.a233', Float) }.to \
- raise_error Compel::ParamTypeError, "'1.a233' is not a valid Float"
+ raise_error Compel::TypeError, "'1.a233' is not a valid Float"
end
end
context 'String' do
@@ -57,45 +57,95 @@
value = 1.2
expect(Compel::Coercion.valid?(value, String)).to eq(false)
expect { Compel::Coercion.coerce!(value, String) }.to \
- raise_error Compel::ParamTypeError, "'#{value}' is not a valid String"
+ raise_error Compel::TypeError, "'#{value}' is not a valid String"
end
end
context 'Date' do
- it 'should coerce' do
+ it 'should coerce with default format' do
value = Compel::Coercion.coerce!('2015-12-22', Date)
expect(value).to eq(Date.parse('2015-12-22'))
end
+ it 'should coerce with format' do
+ value = Compel::Coercion.coerce!('22-12-2015', Date, { format: '%d-%m-%Y'})
+
+ expect(value).to eq(Date.strptime('22-12-2015', '%d-%m-%Y'))
+ end
+
it 'should not coerce' do
value = '2015-0'
expect(Compel::Coercion.valid?(value, Date)).to eq(false)
expect { Compel::Coercion.coerce!(value, Date) }.to \
- raise_error Compel::ParamTypeError, "'#{value}' is not a valid Date"
+ raise_error \
+ Compel::TypeError,
+ "'#{value}' is not a parsable date with format: %Y-%m-%d"
end
+ it 'should not coerce 1' do
+ default_format = '%Y-%m-%d'
+ value = '22-12-2015'
+
+ expect { Compel::Coercion.coerce!(value, Date) }.to \
+ raise_error \
+ Compel::TypeError,
+ "'#{value}' is not a parsable date with format: #{default_format}"
+ end
+
end
- context 'DateTime' do
+ context 'DateTime & Time' do
it 'should coerce' do
- value = Compel::Coercion.coerce!('2015-12-22', DateTime)
+ [DateTime, Time].each do |time_klass|
+ value = Compel::Coercion.coerce!('2015-12-22', time_klass, format: '%Y-%m-%d')
- expect(value).to be_a DateTime
- expect(value.year).to eq(2015)
- expect(value.month).to eq(12)
- expect(value.day).to eq(22)
+ expect(value).to be_a time_klass
+ expect(value.year).to eq(2015)
+ expect(value.month).to eq(12)
+ expect(value.day).to eq(22)
+ end
+
end
+ it 'should coerce iso8601 format' do
+ [DateTime, Time].each do |time_klass|
+ value = Compel::Coercion.coerce!('2015-12-22T09:30:10', time_klass)
+
+ expect(value).to be_a time_klass
+ expect(value.year).to eq(2015)
+ expect(value.month).to eq(12)
+ expect(value.day).to eq(22)
+ expect(value.hour).to eq(9)
+ expect(value.min).to eq(30)
+ expect(value.sec).to eq(10)
+ end
+ end
+
+ it 'should not coerce' do
+ default_format = '%FT%T'
+ value = '22-12-2015'
+
+ [DateTime, Time].each do |time_klass|
+ type_down_cased = "#{time_klass}".downcase
+
+ expect { Compel::Coercion.coerce!('22-12-2015', time_klass) }.to \
+ raise_error \
+ Compel::TypeError,
+ "'#{value}' is not a parsable #{type_down_cased} with format: #{default_format}"
+ end
+
+ end
+
end
context 'Hash' do
it 'should coerce' do
@@ -134,21 +184,21 @@
})
end
it 'should not coerce' do
expect { Compel::Coercion.coerce!(123, Hash) }.to \
- raise_error Compel::ParamTypeError, "'123' is not a valid Hash"
+ raise_error Compel::TypeError, "'123' is not a valid Hash"
end
it 'should not coerce 1' do
expect { Compel::Coercion.coerce!('hash', Hash) }.to \
- raise_error Compel::ParamTypeError, "'hash' is not a valid Hash"
+ raise_error Compel::TypeError, "'hash' is not a valid Hash"
end
it 'should not coerce 2' do
expect { Compel::Coercion.coerce!(['hash'], Hash) }.to \
- raise_error Compel::ParamTypeError, "'[\"hash\"]' is not a valid Hash"
+ raise_error Compel::TypeError, "'[\"hash\"]' is not a valid Hash"
end
end
context 'JSON' do
@@ -165,19 +215,36 @@
end
context 'Boolean' do
- it 'should coerce' do
- value = Compel::Coercion.coerce!('f', Compel::Boolean)
+ it 'should coerce false' do
+ value = Compel::Coercion.coerce!('f', Compel::Coercion::Boolean)
expect(value).to eq(false)
end
- it 'should coerce' do
- value = Compel::Coercion.coerce!('0', Compel::Boolean)
+ it 'should coerce false 1' do
+ value = Compel::Coercion.coerce!('0', Compel::Coercion::Boolean)
expect(value).to eq(false)
+ end
+
+ it 'should coerce true' do
+ value = Compel::Coercion.coerce!('t', Compel::Coercion::Boolean)
+
+ expect(value).to eq(true)
+ end
+
+ it 'should coerce true 1' do
+ value = Compel::Coercion.coerce!('true', Compel::Coercion::Boolean)
+
+ expect(value).to eq(true)
+ end
+
+ it 'should not coerce' do
+ expect{ Compel::Coercion.coerce!('trye', Compel::Coercion::Boolean) }.to \
+ raise_error Compel::TypeError, "'trye' is not a valid Boolean"
end
end
end