Skip to content

Commit

Permalink
Updated String .to_integer, .to_float and .to_decimal specs to test m…
Browse files Browse the repository at this point in the history
…ore cases
  • Loading branch information
dkubb committed Jul 23, 2011
1 parent aa9cf01 commit b53ab73
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 22 deletions.
17 changes: 11 additions & 6 deletions spec/unit/virtus/typecast/string/class_methods/to_boolean_spec.rb
@@ -1,24 +1,29 @@
require 'spec_helper'

describe Virtus::Typecast::String, '.to_boolean' do
subject { described_class.to_boolean(string) }
subject { object.to_boolean(string) }

described_class::TRUE_VALUES.each do |value|
let(:object) { described_class }

%w[ 1 t true T TRUE ].each do |value|
context "with #{value.inspect}" do
let(:string) { value }

it { should be(true) }
end
end

described_class::FALSE_VALUES.each do |value|
%w[ 0 f false F FALSE ].each do |value|
context "with #{value.inspect}" do
let(:string) { value }

it { should be(false) }
end
end

context 'with an unknown string' do
let(:string) { 'something' }
it { should == string }
context 'with an invalid boolean string' do
let(:string) { 'non-boolean' }

it { should equal(string) }
end
end
@@ -1,7 +1,9 @@
require 'spec_helper'

describe Virtus::Typecast::String, '.to_date' do
subject { described_class.to_date(string) }
subject { object.to_date(string) }

let(:object) { described_class }

context 'with a valid date string' do
let(:string) { "July, 22th, 2011" }
Expand All @@ -15,6 +17,7 @@

context 'with an invalid date string' do
let(:string) { 'non-date' }
it { should == string }

it { should equal(string) }
end
end
@@ -1,7 +1,9 @@
require 'spec_helper'

describe Virtus::Typecast::String, '.to_datetime' do
subject { described_class.to_datetime(string) }
subject { object.to_datetime(string) }

let(:object) { described_class }

shared_examples_for 'a correct datetime object' do
it { should be_instance_of(DateTime) }
Expand Down Expand Up @@ -43,6 +45,6 @@
context 'with an invalid date time string' do
let(:string) { 'non-datetime' }

it { should == string }
it { should equal(string) }
end
end
21 changes: 17 additions & 4 deletions spec/unit/virtus/typecast/string/class_methods/to_decimal_spec.rb
@@ -1,10 +1,23 @@
require 'spec_helper'

describe Virtus::Typecast::String, '.to_decimal' do
subject { described_class.to_decimal(string) }
subject { object.to_decimal(string) }

let(:string) { '1.0' }
let(:decimal) { BigDecimal('1.0') }
let(:object) { described_class }

it { should == decimal }
{ '1' => BigDecimal('1.0'), '1.0' => BigDecimal('1.0'), '.1' => BigDecimal('0.1') }.each do |value, expected|
context "with #{value.inspect}" do
let(:string) { value }

it { should be_instance_of(BigDecimal) }

it { should eql(expected) }
end
end

context 'with an invalid decimal string' do
let(:string) { 'non-decimal' }

it { should equal(string) }
end
end
17 changes: 14 additions & 3 deletions spec/unit/virtus/typecast/string/class_methods/to_float_spec.rb
Expand Up @@ -3,8 +3,19 @@
describe Virtus::Typecast::String, '.to_float' do
subject { described_class.to_float(string) }

let(:string) { '1' }
let(:float) { 1.0 }
{ '1' => 1.0, '1.0' => 1.0, '.1' => 0.1 }.each do |value, expected|
context "with #{value.inspect}" do
let(:string) { value }

it { should == float }
it { should be_instance_of(Float) }

it { should eql(expected) }
end
end

context 'with an invalid float string' do
let(:string) { 'non-float' }

it { should equal(string) }
end
end
17 changes: 14 additions & 3 deletions spec/unit/virtus/typecast/string/class_methods/to_integer_spec.rb
Expand Up @@ -3,8 +3,19 @@
describe Virtus::Typecast::String, '.to_integer' do
subject { described_class.to_integer(string) }

let(:string) { '1.0' }
let(:integer) { 1 }
{ '1' => 1, '1.0' => 1, '.1' => 0 }.each do |value, expected|
context "with #{value.inspect}" do
let(:string) { value }

it { should == integer }
it { should be_kind_of(Integer) }

it { should eql(expected) }
end
end

context 'with an invalid integer string' do
let(:string) { 'non-integer' }

it { should equal(string) }
end
end
@@ -1,7 +1,9 @@
require 'spec_helper'

describe Virtus::Typecast::String, '.to_time' do
subject { described_class.to_time(string) }
subject { object.to_time(string) }

let(:object) { described_class }

shared_examples_for 'a correct time object' do
it { should be_instance_of(Time) }
Expand Down Expand Up @@ -43,6 +45,6 @@
context 'with an invalid date time string' do
let(:string) { '2999' }

it { should == string }
it { should equal(string) }
end
end
1 change: 1 addition & 0 deletions virtus.gemspec
Expand Up @@ -128,6 +128,7 @@ Gem::Specification.new do |s|
"spec/unit/virtus/typecast/hash/class_methods/to_date_spec.rb",
"spec/unit/virtus/typecast/hash/class_methods/to_datetime_spec.rb",
"spec/unit/virtus/typecast/hash/class_methods/to_time_spec.rb",
"spec/unit/virtus/typecast/object/class_methods/method_missing.rb",
"spec/unit/virtus/typecast/object/class_methods/method_missing_spec.rb",
"spec/unit/virtus/typecast/string/class_methods/to_boolean_spec.rb",
"spec/unit/virtus/typecast/string/class_methods/to_date_spec.rb",
Expand Down

0 comments on commit b53ab73

Please sign in to comment.