virtus 0.0.10 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/.gitignore +33 -7
  2. data/.travis.yml +4 -74
  3. data/Changelog.md +11 -2
  4. data/Gemfile +10 -9
  5. data/README.md +85 -1
  6. data/TODO +18 -0
  7. data/config/flay.yml +2 -2
  8. data/config/flog.yml +1 -1
  9. data/config/roodi.yml +3 -3
  10. data/config/site.reek +8 -3
  11. data/lib/virtus.rb +5 -0
  12. data/lib/virtus/attribute.rb +137 -30
  13. data/lib/virtus/attribute/array.rb +17 -1
  14. data/lib/virtus/attribute/boolean.rb +8 -13
  15. data/lib/virtus/attribute/collection.rb +96 -0
  16. data/lib/virtus/attribute/default_value.rb +11 -7
  17. data/lib/virtus/attribute/embedded_value.rb +70 -0
  18. data/lib/virtus/attribute/set.rb +25 -0
  19. data/lib/virtus/attribute_set.rb +18 -16
  20. data/lib/virtus/attributes_accessor.rb +66 -0
  21. data/lib/virtus/class_methods.rb +50 -28
  22. data/lib/virtus/coercion/array.rb +23 -0
  23. data/lib/virtus/coercion/string.rb +10 -4
  24. data/lib/virtus/instance_methods.rb +38 -31
  25. data/lib/virtus/support/options.rb +6 -8
  26. data/lib/virtus/support/type_lookup.rb +4 -11
  27. data/lib/virtus/version.rb +1 -1
  28. data/spec/integration/collection_member_coercion_spec.rb +75 -0
  29. data/spec/integration/custom_attributes_spec.rb +49 -0
  30. data/spec/integration/default_values_spec.rb +32 -0
  31. data/spec/integration/defining_attributes_spec.rb +79 -0
  32. data/spec/integration/embedded_value_spec.rb +50 -0
  33. data/spec/integration/overriding_virtus_spec.rb +46 -0
  34. data/spec/integration/virtus/instance_level_attributes_spec.rb +23 -0
  35. data/spec/rcov.opts +1 -0
  36. data/spec/shared/constants_helpers.rb +9 -0
  37. data/spec/shared/options_class_method.rb +19 -0
  38. data/spec/spec_helper.rb +20 -7
  39. data/spec/unit/virtus/attribute/array/coerce_spec.rb +13 -0
  40. data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +85 -0
  41. data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +15 -0
  42. data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +97 -0
  43. data/spec/unit/virtus/attribute/boolean_spec.rb +2 -81
  44. data/spec/unit/virtus/attribute/class/coerce_spec.rb +13 -0
  45. data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +12 -0
  46. data/spec/unit/virtus/attribute/class_methods/build_spec.rb +37 -0
  47. data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +9 -0
  48. data/spec/unit/virtus/attribute/class_methods/default_spec.rb +9 -0
  49. data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +31 -1
  50. data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +11 -0
  51. data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +9 -0
  52. data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +9 -0
  53. data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +9 -0
  54. data/spec/unit/virtus/attribute/coerce_spec.rb +30 -0
  55. data/spec/unit/virtus/attribute/coercion_method_spec.rb +12 -0
  56. data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +40 -0
  57. data/spec/unit/virtus/attribute/collection/coerce_spec.rb +26 -0
  58. data/spec/unit/virtus/attribute/date/coerce_spec.rb +47 -0
  59. data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +46 -0
  60. data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +68 -0
  61. data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +117 -0
  62. data/spec/unit/virtus/attribute/default_spec.rb +32 -0
  63. data/spec/unit/virtus/attribute/default_value/attribute_spec.rb +11 -0
  64. data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +4 -2
  65. data/spec/unit/virtus/attribute/default_value/evaluate_spec.rb +51 -0
  66. data/spec/unit/virtus/attribute/default_value/instance_methods/evaluate_spec.rb +9 -6
  67. data/spec/unit/virtus/attribute/default_value/value_spec.rb +11 -0
  68. data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +26 -0
  69. data/spec/unit/virtus/attribute/define_reader_method_spec.rb +24 -0
  70. data/spec/unit/virtus/attribute/define_writer_method_spec.rb +24 -0
  71. data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +17 -0
  72. data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +50 -0
  73. data/spec/unit/virtus/attribute/float/coerce_spec.rb +117 -0
  74. data/spec/unit/virtus/attribute/get_spec.rb +80 -0
  75. data/spec/unit/virtus/attribute/inspect_spec.rb +27 -0
  76. data/spec/unit/virtus/attribute/instance_variable_name_spec.rb +12 -0
  77. data/spec/unit/virtus/attribute/integer/coerce_spec.rb +105 -0
  78. data/spec/unit/virtus/attribute/name_spec.rb +12 -0
  79. data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +1 -1
  80. data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +9 -0
  81. data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +9 -0
  82. data/spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb +9 -7
  83. data/spec/unit/virtus/attribute/options_spec.rb +14 -0
  84. data/spec/unit/virtus/attribute/public_reader_spec.rb +24 -0
  85. data/spec/unit/virtus/attribute/public_writer_spec.rb +24 -0
  86. data/spec/unit/virtus/attribute/reader_visibility_spec.rb +24 -0
  87. data/spec/unit/virtus/attribute/set/coerce_spec.rb +13 -0
  88. data/spec/unit/virtus/attribute/set_spec.rb +49 -0
  89. data/spec/unit/virtus/attribute/string/coerce_spec.rb +11 -0
  90. data/spec/unit/virtus/attribute/time/coerce_spec.rb +67 -0
  91. data/spec/unit/virtus/attribute/value_coerced_spec.rb +19 -0
  92. data/spec/unit/virtus/attribute/writer_visibility_spec.rb +24 -0
  93. data/spec/unit/virtus/attribute_set/append_spec.rb +12 -0
  94. data/spec/unit/virtus/attribute_set/element_reference_spec.rb +4 -0
  95. data/spec/unit/virtus/attribute_set/element_set_spec.rb +29 -9
  96. data/spec/unit/virtus/attributes_accessor/inspect_spec.rb +9 -0
  97. data/spec/unit/virtus/class_methods/attribute_spec.rb +23 -5
  98. data/spec/unit/virtus/class_methods/attributes_spec.rb +3 -5
  99. data/spec/unit/virtus/class_methods/const_missing_spec.rb +27 -0
  100. data/spec/unit/virtus/class_methods/inherited_spec.rb +21 -0
  101. data/spec/unit/virtus/coercion/array/to_set_spec.rb +12 -0
  102. data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +10 -0
  103. data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +10 -0
  104. data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +10 -3
  105. data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +10 -3
  106. data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +10 -3
  107. data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +8 -8
  108. data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +2 -2
  109. data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +1 -1
  110. data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +1 -1
  111. data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +13 -13
  112. data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +25 -1
  113. data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +25 -1
  114. data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +30 -1
  115. data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +13 -13
  116. data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +10 -0
  117. data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +1 -1
  118. data/spec/unit/virtus/instance_methods/attributes_spec.rb +77 -20
  119. data/spec/unit/virtus/instance_methods/element_reference_spec.rb +1 -1
  120. data/spec/unit/virtus/instance_methods/element_set_spec.rb +2 -2
  121. data/spec/unit/virtus/instance_methods/to_hash_spec.rb +23 -0
  122. data/spec/unit/virtus/options/accept_options_spec.rb +10 -11
  123. data/spec/unit/virtus/options/accepted_options_spec.rb +1 -1
  124. data/spec/unit/virtus/options/options_spec.rb +27 -4
  125. data/tasks/metrics/ci.rake +2 -1
  126. data/tasks/metrics/heckle.rake +207 -0
  127. data/tasks/spec.rake +14 -7
  128. data/virtus.gemspec +1 -1
  129. metadata +111 -97
  130. data/VERSION +0 -1
  131. data/examples/custom_coercion_spec.rb +0 -50
  132. data/examples/default_values_spec.rb +0 -21
  133. data/examples/override_attribute_methods_spec.rb +0 -40
  134. data/spec/integration/virtus/attributes/attribute/set_spec.rb +0 -36
  135. data/spec/integration/virtus/class_methods/attribute_spec.rb +0 -82
  136. data/spec/integration/virtus/class_methods/attributes_spec.rb +0 -22
  137. data/spec/integration/virtus/class_methods/const_missing_spec.rb +0 -44
  138. data/spec/unit/shared/attribute.rb +0 -7
  139. data/spec/unit/shared/attribute/accept_options.rb +0 -37
  140. data/spec/unit/shared/attribute/accepted_options.rb +0 -5
  141. data/spec/unit/shared/attribute/get.rb +0 -44
  142. data/spec/unit/shared/attribute/inspect.rb +0 -7
  143. data/spec/unit/shared/attribute/set.rb +0 -37
  144. data/spec/unit/virtus/attribute/array_spec.rb +0 -24
  145. data/spec/unit/virtus/attribute/class_spec.rb +0 -24
  146. data/spec/unit/virtus/attribute/date_spec.rb +0 -59
  147. data/spec/unit/virtus/attribute/date_time_spec.rb +0 -87
  148. data/spec/unit/virtus/attribute/decimal_spec.rb +0 -109
  149. data/spec/unit/virtus/attribute/float_spec.rb +0 -109
  150. data/spec/unit/virtus/attribute/hash_spec.rb +0 -11
  151. data/spec/unit/virtus/attribute/integer_spec.rb +0 -99
  152. data/spec/unit/virtus/attribute/string_spec.rb +0 -21
  153. data/spec/unit/virtus/attribute/time_spec.rb +0 -82
  154. data/spec/unit/virtus/class_methods/new_spec.rb +0 -41
@@ -1,11 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Virtus::ClassMethods, '.attribute' do
4
- let(:described_class) do
5
- Class.new { include Virtus }
3
+ describe Virtus::ClassMethods, '#attribute' do
4
+ subject { object.attribute(name, type) }
5
+
6
+ let(:object) { Class.new { include Virtus } }
7
+ let(:descendant) { Class.new(object) }
8
+ let(:name) { :name }
9
+ let(:type) { Virtus::Attribute::String }
10
+
11
+ def assert_attribute_added(klass, name, attribute_class)
12
+ attributes = klass.attributes
13
+ attributes[name].should be_nil
14
+ subject
15
+ attribute = attributes[name]
16
+ attribute.name.should be(name)
17
+ attribute.class.should be(attribute_class)
18
+ end
19
+
20
+ it { should be(object) }
21
+
22
+ it 'adds the attribute to the class' do
23
+ assert_attribute_added(object, name, type)
6
24
  end
7
25
 
8
- it 'returns self' do
9
- described_class.attribute(:name, String).should be(described_class)
26
+ it 'adds the attribute to the descendant' do
27
+ assert_attribute_added(descendant, name, type)
10
28
  end
11
29
  end
@@ -1,11 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Virtus::ClassMethods, '.attributes' do
4
- subject { described_class.attributes }
3
+ describe Virtus::ClassMethods, '#attributes' do
4
+ subject { object.attributes }
5
5
 
6
- let(:described_class) do
7
- Class.new { extend Virtus::ClassMethods }
8
- end
6
+ let(:object) { Class.new { extend Virtus::ClassMethods } }
9
7
 
10
8
  it { should be_instance_of(Virtus::AttributeSet) }
11
9
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::ClassMethods, '#const_missing' do
4
+ let(:object) { Class.new { extend Virtus::ClassMethods } }
5
+
6
+ context 'with a constant name that is known' do
7
+ subject { object.class_eval 'String' }
8
+
9
+ it 'returns the constant' do
10
+ subject.should be(String)
11
+ end
12
+ end
13
+
14
+ context 'with a constant name that corresponds to a Virtus::Attribute' do
15
+ subject { object.class_eval 'Boolean' }
16
+
17
+ it 'determines the correct type' do
18
+ subject.should be(Virtus::Attribute::Boolean)
19
+ end
20
+ end
21
+
22
+ context 'with a constant name that is unknown' do
23
+ subject { object.class_eval 'Unknown' }
24
+
25
+ specify { expect { subject }.to raise_error(NameError) }
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::ClassMethods, '#inherited' do
4
+ subject { Class.new(object) }
5
+
6
+ let(:object) { Class.new { extend Virtus::ClassMethods } }
7
+
8
+ it 'includes an AttributesAccessor module' do
9
+ descendant = subject
10
+
11
+ # return the descendant's attribute accessor modules (superclass + own)
12
+ modules = descendant.ancestors.grep(Virtus::AttributesAccessor)
13
+ modules.size.should be(2)
14
+
15
+ # remove the superclass' attribute accessor module
16
+ modules -= object.ancestors.grep(Virtus::AttributesAccessor)
17
+
18
+ # the descendant should have it's own attribute accessor module
19
+ modules.size.should be(1)
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Coercion::Array, '.to_set' do
4
+ subject { object.to_set(array) }
5
+
6
+ let(:object) { described_class }
7
+ let(:array) { [ 'a', 1, 'b', 2, 'b', 1, 'a', 2 ] }
8
+
9
+ it { should be_instance_of(Set) }
10
+
11
+ it { should eql(Set[ 'a', 1, 'b', 2 ]) }
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Coercion::Date, '.to_date' do
4
+ subject { object.to_date(date) }
5
+
6
+ let(:object) { described_class }
7
+ let(:date) { Date.new(2012, 1, 1) }
8
+
9
+ it { should equal(date) }
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::Coercion::DateTime, '.to_datetime' do
4
+ subject { object.to_datetime(date_time) }
5
+
6
+ let(:object) { described_class }
7
+ let(:date_time) { DateTime.new(2012, 1, 1) }
8
+
9
+ it { should equal(date_time) }
10
+ end
@@ -10,8 +10,7 @@ describe Virtus::Coercion::Hash, '.to_date' do
10
10
  let(:hash) { {} }
11
11
 
12
12
  before do
13
- now = time_now
14
- Time.stub!(:now).and_return(now) # freeze time
13
+ Time.stub!(:now).and_return(time_now) # freeze time
15
14
  end
16
15
 
17
16
  it { should be_instance_of(Date) }
@@ -21,11 +20,19 @@ describe Virtus::Coercion::Hash, '.to_date' do
21
20
  end
22
21
  end
23
22
 
24
- context 'when time segments are populated' do
23
+ context 'when time segments are integers' do
25
24
  let(:hash) { { :year => 2011, :month => 1, :day => 1 } }
26
25
 
27
26
  it { should be_instance_of(Date) }
28
27
 
29
28
  it { should eql(Date.new(2011, 1, 1)) }
30
29
  end
30
+
31
+ context 'when time segments are strings' do
32
+ let(:hash) { { :year => '2011', :month => '1', :day => '1' } }
33
+
34
+ it { should be_instance_of(Date) }
35
+
36
+ it { should eql(Date.new(2011, 1, 1)) }
37
+ end
31
38
  end
@@ -10,8 +10,7 @@ describe Virtus::Coercion::Hash, '.to_datetime' do
10
10
  let(:hash) { {} }
11
11
 
12
12
  before do
13
- now = time_now
14
- Time.stub!(:now).and_return(now) # freeze time
13
+ Time.stub!(:now).and_return(time_now) # freeze time
15
14
  end
16
15
 
17
16
  it { should be_instance_of(DateTime) }
@@ -21,11 +20,19 @@ describe Virtus::Coercion::Hash, '.to_datetime' do
21
20
  end
22
21
  end
23
22
 
24
- context 'when time segments are populated' do
23
+ context 'when time segments are integers' do
25
24
  let(:hash) { { :year => 2011, :month => 1, :day => 1, :hour => 1, :min => 1, :sec => 1 } }
26
25
 
27
26
  it { should be_instance_of(DateTime) }
28
27
 
29
28
  it { should eql(DateTime.new(2011, 1, 1, 1, 1, 1)) }
30
29
  end
30
+
31
+ context 'when time segments are strings' do
32
+ let(:hash) { { :year => '2011', :month => '1', :day => '1', :hour => '1', :min => '1', :sec => '1' } }
33
+
34
+ it { should be_instance_of(DateTime) }
35
+
36
+ it { should eql(DateTime.new(2011, 1, 1, 1, 1, 1)) }
37
+ end
31
38
  end
@@ -10,8 +10,7 @@ describe Virtus::Coercion::Hash, '.to_time' do
10
10
  let(:hash) { {} }
11
11
 
12
12
  before do
13
- now = time_now
14
- Time.stub!(:now).and_return(now) # freeze time
13
+ Time.stub!(:now).and_return(time_now) # freeze time
15
14
  end
16
15
 
17
16
  it { should be_instance_of(Time) }
@@ -21,11 +20,19 @@ describe Virtus::Coercion::Hash, '.to_time' do
21
20
  end
22
21
  end
23
22
 
24
- context 'when time segments are populated' do
23
+ context 'when time segments are integers' do
25
24
  let(:hash) { { :year => 2011, :month => 1, :day => 1, :hour => 1, :min => 1, :sec => 1 } }
26
25
 
27
26
  it { should be_instance_of(Time) }
28
27
 
29
28
  it { should eql(Time.local(2011, 1, 1, 1, 1, 1)) }
30
29
  end
30
+
31
+ context 'when time segments are strings' do
32
+ let(:hash) { { :year => '2011', :month => '1', :day => '1', :hour => '1', :min => '1', :sec => '1' } }
33
+
34
+ it { should be_instance_of(Time) }
35
+
36
+ it { should eql(Time.local(2011, 1, 1, 1, 1, 1)) }
37
+ end
31
38
  end
@@ -1,20 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
+ shared_examples_for 'no method error' do
4
+ specify do
5
+ expect { subject }.to raise_error(NoMethodError)
6
+ end
7
+ end
8
+
3
9
  describe Virtus::Coercion::Object, '.method_missing' do
4
10
  subject { described_class.send(method_name, value) }
5
11
 
6
12
  let(:value) { '1' }
7
13
 
8
- shared_examples_for 'no method error' do
9
- specify do
10
- expect { subject }.to raise_error(NoMethodError)
11
- end
12
- end
13
-
14
14
  context 'with a non-typecast method' do
15
15
  let(:method_name) { 'not_here' }
16
16
 
17
- it_behaves_like 'no method error'
17
+ it_should_behave_like 'no method error'
18
18
  end
19
19
 
20
20
  Virtus::Attribute::Object.descendants.each do |attribute|
@@ -27,7 +27,7 @@ describe Virtus::Coercion::Object, '.method_missing' do
27
27
  context "with #{attribute.coercion_method.inspect} and without an input value" do
28
28
  subject { described_class.send(method_name) }
29
29
 
30
- it_behaves_like 'no method error'
30
+ it_should_behave_like 'no method error'
31
31
  end
32
32
  end
33
33
  end
@@ -5,7 +5,7 @@ describe Virtus::Coercion::String, '.to_boolean' do
5
5
 
6
6
  let(:object) { described_class }
7
7
 
8
- %w[ 1 t true T TRUE ].each do |value|
8
+ %w[ 1 on ON t true T TRUE y yes Y YES ].each do |value|
9
9
  context "with #{value.inspect}" do
10
10
  let(:string) { value }
11
11
 
@@ -13,7 +13,7 @@ describe Virtus::Coercion::String, '.to_boolean' do
13
13
  end
14
14
  end
15
15
 
16
- %w[ 0 f false F FALSE ].each do |value|
16
+ %w[ 0 off OFF f false F FALSE n no N NO ].each do |value|
17
17
  context "with #{value.inspect}" do
18
18
  let(:string) { value }
19
19
 
@@ -5,7 +5,7 @@ describe Virtus::Coercion::String, '.to_constant' do
5
5
 
6
6
  let(:object) { described_class }
7
7
 
8
- context "with a String" do
8
+ context 'with a String' do
9
9
  let(:string) { 'String' }
10
10
 
11
11
  it { should be(String) }
@@ -6,7 +6,7 @@ describe Virtus::Coercion::String, '.to_date' do
6
6
  let(:object) { described_class }
7
7
 
8
8
  context 'with a valid date string' do
9
- let(:string) { "July, 22th, 2011" }
9
+ let(:string) { 'July, 22th, 2011' }
10
10
 
11
11
  it { should be_instance_of(Date) }
12
12
 
@@ -1,21 +1,21 @@
1
1
  require 'spec_helper'
2
2
 
3
+ shared_examples_for 'a correct datetime object' do
4
+ it { should be_instance_of(DateTime) }
5
+
6
+ its(:year) { should == year }
7
+ its(:month) { should == month }
8
+ its(:day) { should == day }
9
+ its(:hour) { should == hour }
10
+ its(:min) { should == min }
11
+ its(:sec) { should == sec }
12
+ end
13
+
3
14
  describe Virtus::Coercion::String, '.to_datetime' do
4
15
  subject { object.to_datetime(string) }
5
16
 
6
17
  let(:object) { described_class }
7
18
 
8
- shared_examples_for 'a correct datetime object' do
9
- it { should be_instance_of(DateTime) }
10
-
11
- its(:year) { should == year }
12
- its(:month) { should == month }
13
- its(:day) { should == day }
14
- its(:hour) { should == hour }
15
- its(:min) { should == min }
16
- its(:sec) { should == sec }
17
- end
18
-
19
19
  context 'with a valid date string' do
20
20
  let(:year) { 2011 }
21
21
  let(:month) { 7 }
@@ -28,7 +28,7 @@ describe Virtus::Coercion::String, '.to_datetime' do
28
28
  let(:min) { 0 }
29
29
  let(:sec) { 0 }
30
30
 
31
- it_behaves_like 'a correct datetime object'
31
+ it_should_behave_like 'a correct datetime object'
32
32
  end
33
33
 
34
34
  context 'including time part' do
@@ -38,7 +38,7 @@ describe Virtus::Coercion::String, '.to_datetime' do
38
38
  let(:min) { 44 }
39
39
  let(:sec) { 50 }
40
40
 
41
- it_behaves_like 'a correct datetime object'
41
+ it_should_behave_like 'a correct datetime object'
42
42
  end
43
43
  end
44
44
 
@@ -5,7 +5,31 @@ describe Virtus::Coercion::String, '.to_decimal' do
5
5
 
6
6
  let(:object) { described_class }
7
7
 
8
- { '1' => BigDecimal('1.0'), '1.0' => BigDecimal('1.0'), '.1' => BigDecimal('0.1') }.each do |value, expected|
8
+ {
9
+ '1' => BigDecimal('1.0'),
10
+ '+1' => BigDecimal('1.0'),
11
+ '-1' => BigDecimal('-1.0'),
12
+ '1.0' => BigDecimal('1.0'),
13
+ '1.0e+1' => BigDecimal('10.0'),
14
+ '1.0e-1' => BigDecimal('0.1'),
15
+ '1.0E+1' => BigDecimal('10.0'),
16
+ '1.0E-1' => BigDecimal('0.1'),
17
+ '+1.0' => BigDecimal('1.0'),
18
+ '+1.0e+1' => BigDecimal('10.0'),
19
+ '+1.0e-1' => BigDecimal('0.1'),
20
+ '+1.0E+1' => BigDecimal('10.0'),
21
+ '+1.0E-1' => BigDecimal('0.1'),
22
+ '-1.0' => BigDecimal('-1.0'),
23
+ '-1.0e+1' => BigDecimal('-10.0'),
24
+ '-1.0e-1' => BigDecimal('-0.1'),
25
+ '-1.0E+1' => BigDecimal('-10.0'),
26
+ '-1.0E-1' => BigDecimal('-0.1'),
27
+ '.1' => BigDecimal('0.1'),
28
+ '.1e+1' => BigDecimal('1.0'),
29
+ '.1e-1' => BigDecimal('0.01'),
30
+ '.1E+1' => BigDecimal('1.0'),
31
+ '.1E-1' => BigDecimal('0.01'),
32
+ }.each do |value, expected|
9
33
  context "with #{value.inspect}" do
10
34
  let(:string) { value }
11
35
 
@@ -3,7 +3,31 @@ require 'spec_helper'
3
3
  describe Virtus::Coercion::String, '.to_float' do
4
4
  subject { described_class.to_float(string) }
5
5
 
6
- { '1' => 1.0, '1.0' => 1.0, '.1' => 0.1 }.each do |value, expected|
6
+ {
7
+ '1' => 1.0,
8
+ '+1' => 1.0,
9
+ '-1' => -1.0,
10
+ '1.0' => 1.0,
11
+ '1.0e+1' => 10.0,
12
+ '1.0e-1' => 0.1,
13
+ '1.0E+1' => 10.0,
14
+ '1.0E-1' => 0.1,
15
+ '+1.0' => 1.0,
16
+ '+1.0e+1' => 10.0,
17
+ '+1.0e-1' => 0.1,
18
+ '+1.0E+1' => 10.0,
19
+ '+1.0E-1' => 0.1,
20
+ '-1.0' => -1.0,
21
+ '-1.0e+1' => -10.0,
22
+ '-1.0e-1' => -0.1,
23
+ '-1.0E+1' => -10.0,
24
+ '-1.0E-1' => -0.1,
25
+ '.1' => 0.1,
26
+ '.1e+1' => 1.0,
27
+ '.1e-1' => 0.01,
28
+ '.1E+1' => 1.0,
29
+ '.1E-1' => 0.01,
30
+ }.each do |value, expected|
7
31
  context "with #{value.inspect}" do
8
32
  let(:string) { value }
9
33
 
@@ -3,7 +3,36 @@ require 'spec_helper'
3
3
  describe Virtus::Coercion::String, '.to_integer' do
4
4
  subject { described_class.to_integer(string) }
5
5
 
6
- { '1' => 1, '1.0' => 1, '.1' => 0 }.each do |value, expected|
6
+ min_float = Float::MIN
7
+ max_float = (Float::MAX / 10).to_s.to_f # largest float that can be parsed
8
+
9
+ {
10
+ '1' => 1,
11
+ '+1' => 1,
12
+ '-1' => -1,
13
+ '1.0' => 1,
14
+ '1.0e+1' => 10,
15
+ '1.0e-1' => 0,
16
+ '1.0E+1' => 10,
17
+ '1.0E-1' => 0,
18
+ '+1.0' => 1,
19
+ '+1.0e+1' => 10,
20
+ '+1.0e-1' => 0,
21
+ '+1.0E+1' => 10,
22
+ '+1.0E-1' => 0,
23
+ '-1.0' => -1,
24
+ '-1.0e+1' => -10,
25
+ '-1.0e-1' => 0,
26
+ '-1.0E+1' => -10,
27
+ '-1.0E-1' => 0,
28
+ '.1' => 0,
29
+ '.1e+1' => 1,
30
+ '.1e-1' => 0,
31
+ '.1E+1' => 1,
32
+ '.1E-1' => 0,
33
+ min_float.to_s => min_float.to_i,
34
+ max_float.to_s => max_float.to_i,
35
+ }.each do |value, expected|
7
36
  context "with #{value.inspect}" do
8
37
  let(:string) { value }
9
38