Skip to content

Commit

Permalink
dry up cast specs
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLasseigne committed Apr 30, 2014
1 parent 9e1719f commit e47a8fa
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 63 deletions.
12 changes: 7 additions & 5 deletions spec/active_interaction/filters/array_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@
end

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with an Array' do
let(:value) { [] }

it 'returns the Array' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with a heterogenous Array' do
let(:value) { [[], false, 0.0, {}, 0, '', :''] }

it 'returns the Array' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

Expand All @@ -59,15 +61,15 @@
let(:value) { [] }

it 'returns the Array' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with an Array of Arrays' do
let(:value) { [[]] }

it 'returns the Array' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

Expand All @@ -76,7 +78,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
18 changes: 10 additions & 8 deletions spec/active_interaction/filters/date_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
end

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with a Date' do
let(:value) { Date.new }

it 'returns the Date' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with a String' do
let(:value) { '2011-12-13' }

it 'returns a Date' do
expect(filter.cast(value)).to eql Date.parse(value)
expect(result).to eql Date.parse(value)
end

context 'with format' do
Expand All @@ -36,7 +38,7 @@
let(:value) { '13/12/2011' }

it 'returns a Date' do
expect(filter.cast(value)).to eql Date.strptime(value, format)
expect(result).to eql Date.strptime(value, format)
end
end
end
Expand All @@ -46,7 +48,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end

Expand All @@ -55,7 +57,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand All @@ -74,7 +76,7 @@
end

it 'returns a Date' do
expect(filter.cast(value)).to eql Date.new(year, month, day)
expect(result).to eql Date.new(year, month, day)
end
end

Expand All @@ -84,7 +86,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand All @@ -98,7 +100,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
18 changes: 10 additions & 8 deletions spec/active_interaction/filters/date_time_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
end

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with a Datetime' do
let(:value) { DateTime.new }

it 'returns the DateTime' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with a String' do
let(:value) { '2011-12-13T14:15:16+17:18' }

it 'returns a DateTime' do
expect(filter.cast(value)).to eql DateTime.parse(value)
expect(result).to eql DateTime.parse(value)
end

context 'with format' do
Expand All @@ -36,7 +38,7 @@
let(:value) { '13/12/2011 14:15:16 +17:18' }

it 'returns a DateTime' do
expect(filter.cast(value)).to eql DateTime.strptime(value, format)
expect(result).to eql DateTime.strptime(value, format)
end
end
end
Expand All @@ -46,7 +48,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end

Expand All @@ -55,7 +57,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand All @@ -81,7 +83,7 @@

it 'returns a DateTime' do
expect(
filter.cast(value)
result
).to eql DateTime.new(year, month, day, hour, min, sec)
end
end
Expand All @@ -92,7 +94,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand All @@ -106,7 +108,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
12 changes: 7 additions & 5 deletions spec/active_interaction/filters/decimal_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
end

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with a Float' do
let(:value) { rand }

it 'returns the BigDecimal' do
expect(filter.cast(value)).to eql BigDecimal.new(value, 0)
expect(result).to eql BigDecimal.new(value, 0)
end

context 'with :digits option' do
Expand All @@ -28,7 +30,7 @@
let(:value) { 1.23456789 }

it 'returns BigDecimal with given digits' do
expect(filter.cast(value)).to eql BigDecimal.new('1.235')
expect(result).to eql BigDecimal.new('1.235')
end
end
end
Expand All @@ -37,15 +39,15 @@
let(:value) { rand(1 << 16) }

it 'returns a BigDecimal' do
expect(filter.cast(value)).to eql BigDecimal.new(value)
expect(result).to eql BigDecimal.new(value)
end
end

context 'with a String' do
let(:value) { rand.to_s }

it 'returns a BigDecimal' do
expect(filter.cast(value)).to eql BigDecimal.new(value)
expect(result).to eql BigDecimal.new(value)
end
end

Expand All @@ -54,7 +56,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
8 changes: 5 additions & 3 deletions spec/active_interaction/filters/file_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
it_behaves_like 'a filter'

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with a File' do
let(:value) { File.new(__FILE__) }

it 'returns the File' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with a Tempfile' do
let(:value) { Tempfile.new(SecureRandom.hex) }

it 'returns the Tempfile' do
expect(filter.cast(value)).to eq value
expect(result).to eq value
end
end

context 'with an object that responds to #tempfile' do
let(:value) { double(tempfile: Tempfile.new(SecureRandom.hex)) }

it 'returns the Tempfile' do
expect(filter.cast(value)).to eq value.tempfile
expect(result).to eq value.tempfile
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions spec/active_interaction/filters/float_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
it_behaves_like 'a filter'

describe '#cast' do
let(:result) { filter.cast(value) }

context 'with a Float' do
let(:value) { rand }

it 'returns the Float' do
expect(filter.cast(value)).to eql value
expect(result).to eql value
end
end

context 'with a Numeric' do
let(:value) { rand(1 << 16) }

it 'returns a Float' do
expect(filter.cast(value)).to eql value.to_f
expect(result).to eql value.to_f
end
end

context 'with a String' do
let(:value) { rand.to_s }

it 'returns a Float' do
expect(filter.cast(value)).to eql Float(value)
expect(result).to eql Float(value)
end
end

Expand All @@ -36,7 +38,7 @@

it 'raises an error' do
expect do
filter.cast(value)
result
end.to raise_error ActiveInteraction::InvalidValueError
end
end
Expand Down
Loading

0 comments on commit e47a8fa

Please sign in to comment.