virtus 0.5.5 → 1.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -1
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -8
- data/Gemfile +8 -2
- data/Gemfile.devtools +24 -30
- data/Guardfile +17 -6
- data/README.md +215 -77
- data/TODO.md +6 -0
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/config/mutant.yml +12 -1
- data/config/reek.yml +57 -66
- data/lib/virtus.rb +126 -42
- data/lib/virtus/attribute.rb +52 -240
- data/lib/virtus/attribute/accessor.rb +91 -0
- data/lib/virtus/attribute/boolean.rb +11 -8
- data/lib/virtus/attribute/builder.rb +120 -0
- data/lib/virtus/attribute/coercer.rb +51 -0
- data/lib/virtus/attribute/coercible.rb +14 -0
- data/lib/virtus/attribute/collection.rb +44 -90
- data/lib/virtus/attribute/default_value/from_callable.rb +1 -1
- data/lib/virtus/attribute/default_value/from_clonable.rb +1 -6
- data/lib/virtus/attribute/default_value/from_symbol.rb +3 -3
- data/lib/virtus/attribute/embedded_value.rb +37 -28
- data/lib/virtus/attribute/hash.rb +69 -71
- data/lib/virtus/attribute/lazy_default.rb +18 -0
- data/lib/virtus/attribute/strict.rb +16 -0
- data/lib/virtus/attribute_set.rb +81 -9
- data/lib/virtus/class_inclusions.rb +24 -17
- data/lib/virtus/class_methods.rb +13 -31
- data/lib/virtus/configuration.rb +77 -0
- data/lib/virtus/const_missing_extensions.rb +16 -0
- data/lib/virtus/extensions.rb +86 -61
- data/lib/virtus/instance_methods.rb +107 -128
- data/lib/virtus/model.rb +68 -0
- data/lib/virtus/module_builder.rb +184 -0
- data/lib/virtus/module_extensions.rb +27 -15
- data/lib/virtus/support/equalizer.rb +2 -3
- data/lib/virtus/support/type_lookup.rb +1 -1
- data/lib/virtus/value_object.rb +24 -31
- data/lib/virtus/version.rb +1 -1
- data/spec/integration/building_module_spec.rb +68 -0
- data/spec/integration/custom_attributes_spec.rb +8 -15
- data/spec/integration/custom_collection_attributes_spec.rb +101 -0
- data/spec/integration/default_values_spec.rb +9 -2
- data/spec/integration/defining_attributes_spec.rb +2 -2
- data/spec/integration/using_modules_spec.rb +5 -3
- data/spec/shared/freeze_method_behavior.rb +2 -2
- data/spec/shared/options_class_method.rb +1 -1
- data/spec/spec_helper.rb +25 -5
- data/spec/unit/virtus/attribute/boolean/value_coerced_predicate_spec.rb +25 -0
- data/spec/unit/virtus/attribute/class_methods/build_spec.rb +115 -22
- data/spec/unit/virtus/attribute/class_methods/coerce_spec.rb +32 -0
- data/spec/unit/virtus/attribute/coerce_spec.rb +38 -21
- data/spec/unit/virtus/attribute/coercible_predicate_spec.rb +20 -0
- data/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb +84 -0
- data/spec/unit/virtus/attribute/collection/coerce_spec.rb +37 -24
- data/spec/unit/virtus/attribute/custom_collection_spec.rb +23 -0
- data/spec/unit/virtus/attribute/embedded_value/class_methods/build_spec.rb +37 -0
- data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +46 -10
- data/spec/unit/virtus/attribute/get_spec.rb +15 -63
- data/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb +80 -0
- data/spec/unit/virtus/attribute/hash/coerce_spec.rb +75 -13
- data/spec/unit/virtus/attribute/lazy_predicate_spec.rb +20 -0
- data/spec/unit/virtus/attribute/set_default_value_spec.rb +74 -0
- data/spec/unit/virtus/attribute/set_spec.rb +16 -36
- data/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb +19 -0
- data/spec/unit/virtus/attribute_set/append_spec.rb +3 -5
- data/spec/unit/virtus/attribute_set/define_reader_method_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/define_writer_method_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/each_spec.rb +3 -3
- data/spec/unit/virtus/attribute_set/element_reference_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/element_set_spec.rb +4 -4
- data/spec/unit/virtus/attribute_set/merge_spec.rb +10 -16
- data/spec/unit/virtus/attribute_set/reset_spec.rb +6 -10
- data/spec/unit/virtus/attribute_spec.rb +222 -0
- data/spec/unit/virtus/attributes_reader_spec.rb +41 -0
- data/spec/unit/virtus/attributes_writer_spec.rb +51 -0
- data/spec/unit/virtus/class_methods/new_spec.rb +39 -0
- data/spec/unit/virtus/config_spec.rb +13 -0
- data/spec/unit/virtus/element_reader_spec.rb +21 -0
- data/spec/unit/virtus/element_writer_spec.rb +19 -0
- data/spec/unit/virtus/freeze_spec.rb +21 -0
- data/spec/unit/virtus/model_spec.rb +134 -0
- data/spec/unit/virtus/module_spec.rb +76 -0
- data/spec/unit/virtus/set_default_attributes_spec.rb +25 -0
- data/spec/unit/virtus/value_object_spec.rb +105 -0
- data/virtus.gemspec +4 -2
- metadata +82 -191
- data/.rvmrc +0 -1
- data/TODO +0 -1
- data/config/roodi.yml +0 -17
- data/lib/virtus/attribute/array.rb +0 -24
- data/lib/virtus/attribute/class.rb +0 -21
- data/lib/virtus/attribute/date.rb +0 -34
- data/lib/virtus/attribute/date_time.rb +0 -36
- data/lib/virtus/attribute/decimal.rb +0 -21
- data/lib/virtus/attribute/embedded_value/from_open_struct.rb +0 -19
- data/lib/virtus/attribute/embedded_value/from_struct.rb +0 -19
- data/lib/virtus/attribute/float.rb +0 -30
- data/lib/virtus/attribute/integer.rb +0 -27
- data/lib/virtus/attribute/numeric.rb +0 -11
- data/lib/virtus/attribute/object.rb +0 -11
- data/lib/virtus/attribute/set.rb +0 -24
- data/lib/virtus/attribute/string.rb +0 -24
- data/lib/virtus/attribute/symbol.rb +0 -21
- data/lib/virtus/attribute/time.rb +0 -36
- data/lib/virtus/coercion.rb +0 -32
- data/lib/virtus/coercion/array.rb +0 -23
- data/lib/virtus/coercion/date.rb +0 -26
- data/lib/virtus/coercion/date_time.rb +0 -26
- data/lib/virtus/coercion/decimal.rb +0 -40
- data/lib/virtus/coercion/false_class.rb +0 -24
- data/lib/virtus/coercion/float.rb +0 -39
- data/lib/virtus/coercion/hash.rb +0 -71
- data/lib/virtus/coercion/integer.rb +0 -83
- data/lib/virtus/coercion/numeric.rb +0 -66
- data/lib/virtus/coercion/object.rb +0 -125
- data/lib/virtus/coercion/string.rb +0 -218
- data/lib/virtus/coercion/symbol.rb +0 -24
- data/lib/virtus/coercion/time.rb +0 -40
- data/lib/virtus/coercion/time_coercions.rb +0 -86
- data/lib/virtus/coercion/true_class.rb +0 -24
- data/spec/unit/virtus/attribute/array/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +0 -85
- data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +0 -15
- data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +0 -97
- data/spec/unit/virtus/attribute/boolean_spec.rb +0 -19
- data/spec/unit/virtus/attribute/class/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +0 -12
- data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/default_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +0 -117
- data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +0 -11
- data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +0 -9
- data/spec/unit/virtus/attribute/coercion_method_spec.rb +0 -12
- data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +0 -40
- data/spec/unit/virtus/attribute/collection/coerce_and_append_member_spec.rb +0 -16
- data/spec/unit/virtus/attribute/collection/member_coercion/coerce_and_append_member_spec.rb +0 -18
- data/spec/unit/virtus/attribute/collection/member_type_spec.rb +0 -17
- data/spec/unit/virtus/attribute/collection/new_collection_spec.rb +0 -9
- data/spec/unit/virtus/attribute/date/coerce_spec.rb +0 -47
- data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +0 -46
- data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +0 -80
- data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +0 -123
- data/spec/unit/virtus/attribute/default_spec.rb +0 -28
- data/spec/unit/virtus/attribute/default_value/call_spec.rb +0 -21
- data/spec/unit/virtus/attribute/default_value/class_methods/build_spec.rb +0 -23
- data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +0 -21
- data/spec/unit/virtus/attribute/default_value/from_callable/call_spec.rb +0 -19
- data/spec/unit/virtus/attribute/default_value/from_callable/class_methods/handle_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/from_clonable/call_spec.rb +0 -20
- data/spec/unit/virtus/attribute/default_value/from_clonable/class_methods/handle_spec.rb +0 -19
- data/spec/unit/virtus/attribute/default_value/from_symbol/call_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/from_symbol/class_methods/handle_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/value_spec.rb +0 -10
- data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +0 -26
- data/spec/unit/virtus/attribute/define_reader_method_spec.rb +0 -24
- data/spec/unit/virtus/attribute/define_writer_method_spec.rb +0 -24
- data/spec/unit/virtus/attribute/embedded_value/class_methods/determine_type_spec.rb +0 -23
- data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +0 -17
- data/spec/unit/virtus/attribute/embedded_value/from_open_struct/coerce_spec.rb +0 -34
- data/spec/unit/virtus/attribute/embedded_value/from_struct/coerce_spec.rb +0 -34
- data/spec/unit/virtus/attribute/float/coerce_spec.rb +0 -123
- data/spec/unit/virtus/attribute/hash/class_methods/merge_options_spec.rb +0 -33
- data/spec/unit/virtus/attribute/hash/key_type_spec.rb +0 -10
- data/spec/unit/virtus/attribute/hash/value_type_spec.rb +0 -10
- data/spec/unit/virtus/attribute/inspect_spec.rb +0 -27
- data/spec/unit/virtus/attribute/integer/coerce_spec.rb +0 -117
- data/spec/unit/virtus/attribute/name_spec.rb +0 -12
- data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +0 -15
- data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +0 -9
- data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +0 -9
- data/spec/unit/virtus/attribute/options_spec.rb +0 -14
- data/spec/unit/virtus/attribute/public_reader_spec.rb +0 -24
- data/spec/unit/virtus/attribute/public_writer_spec.rb +0 -24
- data/spec/unit/virtus/attribute/set/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/string/coerce_spec.rb +0 -11
- data/spec/unit/virtus/attribute/symbol/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/time/coerce_spec.rb +0 -67
- data/spec/unit/virtus/attribute/value_coerced_spec.rb +0 -19
- data/spec/unit/virtus/class_methods/attribute_set_spec.rb +0 -9
- data/spec/unit/virtus/class_methods/attribute_spec.rb +0 -30
- data/spec/unit/virtus/class_methods/attributes_spec.rb +0 -22
- data/spec/unit/virtus/class_methods/const_missing_spec.rb +0 -27
- data/spec/unit/virtus/class_methods/inherited_spec.rb +0 -21
- data/spec/unit/virtus/coercion/array/class_methods/to_set_spec.rb +0 -12
- data/spec/unit/virtus/coercion/class_methods/element_reference_spec.rb +0 -17
- data/spec/unit/virtus/coercion/class_methods/primitive_spec.rb +0 -13
- data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +0 -10
- data/spec/unit/virtus/coercion/date/class_methods/to_datetime_spec.rb +0 -30
- data/spec/unit/virtus/coercion/date/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date/class_methods/to_time_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date_time/class_methods/to_date_spec.rb +0 -30
- data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +0 -10
- data/spec/unit/virtus/coercion/date_time/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date_time/class_methods/to_time_spec.rb +0 -30
- data/spec/unit/virtus/coercion/decimal/class_methods/to_decimal_spec.rb +0 -9
- data/spec/unit/virtus/coercion/decimal/class_methods/to_float_spec.rb +0 -12
- data/spec/unit/virtus/coercion/decimal/class_methods/to_integer_spec.rb +0 -12
- data/spec/unit/virtus/coercion/decimal/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/false_class/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_float_spec.rb +0 -9
- data/spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +0 -38
- data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +0 -38
- data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +0 -38
- data/spec/unit/virtus/coercion/integer/class_methods/to_boolean_spec.rb +0 -25
- data/spec/unit/virtus/coercion/integer/class_methods/to_decimal_spec.rb +0 -12
- data/spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb +0 -12
- data/spec/unit/virtus/coercion/integer/class_methods/to_integer_spec.rb +0 -9
- data/spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/numeric/class_methods/to_decimal_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_float_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_integer_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +0 -33
- data/spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb +0 -51
- data/spec/unit/virtus/coercion/object/class_methods/to_hash_spec.rb +0 -22
- data/spec/unit/virtus/coercion/object/class_methods/to_integer_spec.rb +0 -22
- data/spec/unit/virtus/coercion/object/class_methods/to_string_spec.rb +0 -22
- data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +0 -29
- data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +0 -49
- data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +0 -23
- data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +0 -50
- data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +0 -47
- data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +0 -57
- data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +0 -68
- data/spec/unit/virtus/coercion/string/class_methods/to_symbol_spec.rb +0 -9
- data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +0 -50
- data/spec/unit/virtus/coercion/symbol/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/time/class_methods/to_integer_spec.rb +0 -10
- data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +0 -10
- data/spec/unit/virtus/coercion/time_coercions/to_date_spec.rb +0 -33
- data/spec/unit/virtus/coercion/time_coercions/to_datetime_spec.rb +0 -33
- data/spec/unit/virtus/coercion/time_coercions/to_string_spec.rb +0 -18
- data/spec/unit/virtus/coercion/time_coercions/to_time_spec.rb +0 -33
- data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/equalizer/append_spec.rb +0 -82
- data/spec/unit/virtus/equalizer/class_method/new_spec.rb +0 -111
- data/spec/unit/virtus/equalizer/methods/eql_spec.rb +0 -47
- data/spec/unit/virtus/equalizer/methods/equal_value_spec.rb +0 -57
- data/spec/unit/virtus/extensions/allowed_writer_methods_spec.rb +0 -25
- data/spec/unit/virtus/extensions/attribute_spec.rb +0 -26
- data/spec/unit/virtus/instance_methods/attributes_spec.rb +0 -140
- data/spec/unit/virtus/instance_methods/element_reference_spec.rb +0 -24
- data/spec/unit/virtus/instance_methods/element_set_spec.rb +0 -28
- data/spec/unit/virtus/instance_methods/freeze_spec.rb +0 -64
- data/spec/unit/virtus/instance_methods/initialize_spec.rb +0 -48
- data/spec/unit/virtus/instance_methods/to_hash_spec.rb +0 -23
- data/spec/unit/virtus/module_extensions/attribute_spec.rb +0 -31
- data/spec/unit/virtus/options/accept_options_spec.rb +0 -37
- data/spec/unit/virtus/options/accepted_options_spec.rb +0 -21
- data/spec/unit/virtus/options/options_spec.rb +0 -34
- data/spec/unit/virtus/type_lookup/class_methods/extended_spec.rb +0 -9
- data/spec/unit/virtus/type_lookup/determine_type_spec.rb +0 -81
- data/spec/unit/virtus/type_lookup/primitive_spec.rb +0 -9
- data/spec/unit/virtus/value_object/class_methods/allowed_writer_methods_spec.rb +0 -15
- data/spec/unit/virtus/value_object/class_methods/attribute_spec.rb +0 -50
- data/spec/unit/virtus/value_object/class_methods/equalizer_spec.rb +0 -24
- data/spec/unit/virtus/value_object/initialize_spec.rb +0 -19
- data/spec/unit/virtus/value_object/instance_methods/clone_spec.rb +0 -21
- data/spec/unit/virtus/value_object/instance_methods/with_spec.rb +0 -31
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::String, '.to_decimal' do
|
4
|
-
subject { object.to_decimal(string) }
|
5
|
-
|
6
|
-
let(:object) { described_class }
|
7
|
-
|
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|
|
33
|
-
context "with #{value.inspect}" do
|
34
|
-
let(:string) { value }
|
35
|
-
|
36
|
-
it { should be_instance_of(BigDecimal) }
|
37
|
-
|
38
|
-
it { should eql(expected) }
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'with an invalid decimal string' do
|
43
|
-
let(:string) { 'non-decimal' }
|
44
|
-
|
45
|
-
it { should equal(string) }
|
46
|
-
end
|
47
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::String, '.to_float' do
|
4
|
-
subject { described_class.to_float(string) }
|
5
|
-
|
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
|
-
'1e1' => 10.0,
|
31
|
-
'1E+1' => 10.0,
|
32
|
-
'+1e-1' => 0.1,
|
33
|
-
'-1E1' => -10.0,
|
34
|
-
'-1e-1' => -0.1,
|
35
|
-
}.each do |value, expected|
|
36
|
-
context "with #{value.inspect}" do
|
37
|
-
let(:string) { value }
|
38
|
-
|
39
|
-
it { should be_instance_of(Float) }
|
40
|
-
|
41
|
-
it { should eql(expected) }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'with an invalid float string' do
|
46
|
-
let(:string) { 'non-float' }
|
47
|
-
|
48
|
-
it { should equal(string) }
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'string starts with e' do
|
52
|
-
let(:string) { 'e1' }
|
53
|
-
|
54
|
-
# In further version it will raise exception
|
55
|
-
it { should == 'e1' }
|
56
|
-
end
|
57
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::String, '.to_integer' do
|
4
|
-
subject { described_class.to_integer(string) }
|
5
|
-
|
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
|
-
'1e1' => 10,
|
34
|
-
'1E+1' => 10,
|
35
|
-
'+1e-1' => 0,
|
36
|
-
'-1E1' => -10,
|
37
|
-
'-1e-1' => 0,
|
38
|
-
min_float.to_s => min_float.to_i,
|
39
|
-
max_float.to_s => max_float.to_i,
|
40
|
-
}.each do |value, expected|
|
41
|
-
context "with #{value.inspect}" do
|
42
|
-
let(:string) { value }
|
43
|
-
|
44
|
-
it { should be_kind_of(Integer) }
|
45
|
-
|
46
|
-
it { should eql(expected) }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with an invalid integer string' do
|
51
|
-
let(:string) { 'non-integer' }
|
52
|
-
|
53
|
-
it { should equal(string) }
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'when integer string is big' do
|
57
|
-
let(:string) { '334490140000101135' }
|
58
|
-
|
59
|
-
it { should == 334490140000101135 }
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'string starts with e' do
|
63
|
-
let(:string) { 'e1' }
|
64
|
-
|
65
|
-
# In further version it will raise exception
|
66
|
-
it { should == 'e1' }
|
67
|
-
end
|
68
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
shared_examples_for 'a correct time object' do
|
4
|
-
it { should be_instance_of(Time) }
|
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
|
-
|
14
|
-
describe Virtus::Coercion::String, '.to_time' do
|
15
|
-
subject { object.to_time(string) }
|
16
|
-
|
17
|
-
let(:object) { described_class }
|
18
|
-
|
19
|
-
context 'with a valid time string' do
|
20
|
-
let(:year) { 2011 }
|
21
|
-
let(:month) { 7 }
|
22
|
-
let(:day) { 22 }
|
23
|
-
|
24
|
-
context 'not including time part' do
|
25
|
-
let(:string) { "July, #{day}th, #{year}" }
|
26
|
-
|
27
|
-
let(:hour) { 0 }
|
28
|
-
let(:min) { 0 }
|
29
|
-
let(:sec) { 0 }
|
30
|
-
|
31
|
-
it_should_behave_like 'a correct time object'
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'including time part' do
|
35
|
-
let(:string) { "July, #{day}, #{year}, #{hour}:#{min}:#{sec}" }
|
36
|
-
|
37
|
-
let(:hour) { 13 }
|
38
|
-
let(:min) { 44 }
|
39
|
-
let(:sec) { 50 }
|
40
|
-
|
41
|
-
it_should_behave_like 'a correct time object'
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'with an invalid date time string' do
|
46
|
-
let(:string) { '2999' }
|
47
|
-
|
48
|
-
it { should equal(string) }
|
49
|
-
end
|
50
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::Symbol, '.to_string' do
|
4
|
-
subject { object.to_string(symbol) }
|
5
|
-
|
6
|
-
let(:object) { described_class }
|
7
|
-
let(:symbol) { :piotr }
|
8
|
-
|
9
|
-
it { should be_instance_of(String) }
|
10
|
-
|
11
|
-
it { should eql('piotr') }
|
12
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::TimeCoercions, '.to_date' do
|
4
|
-
subject { object.to_date(value) }
|
5
|
-
|
6
|
-
let(:object) { Class.new(Virtus::Coercion::Object) }
|
7
|
-
let(:value) { mock('value') }
|
8
|
-
|
9
|
-
context 'when the value responds to #to_date' do
|
10
|
-
before do
|
11
|
-
object.extend Virtus::Coercion::TimeCoercions
|
12
|
-
|
13
|
-
value.should_receive(:to_date).and_return(Date.new(2011, 1, 1))
|
14
|
-
end
|
15
|
-
|
16
|
-
it { should be_instance_of(Date) }
|
17
|
-
|
18
|
-
it { should eql(Date.new(2011, 1, 1)) }
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when the value does not respond to #to_date' do
|
22
|
-
before do
|
23
|
-
object.extend Virtus::Coercion::TimeCoercions
|
24
|
-
|
25
|
-
# use a string that Date.parse can handle
|
26
|
-
value.should_receive(:to_s).and_return('2011-01-01')
|
27
|
-
end
|
28
|
-
|
29
|
-
it { should be_instance_of(Date) }
|
30
|
-
|
31
|
-
it { should eql(Date.new(2011, 1, 1)) }
|
32
|
-
end
|
33
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::TimeCoercions, '.to_datetime' do
|
4
|
-
subject { object.to_datetime(value) }
|
5
|
-
|
6
|
-
let(:object) { Class.new(Virtus::Coercion::Object) }
|
7
|
-
let(:value) { mock('value') }
|
8
|
-
|
9
|
-
context 'when the value responds to #to_datetime' do
|
10
|
-
before do
|
11
|
-
object.extend Virtus::Coercion::TimeCoercions
|
12
|
-
|
13
|
-
value.should_receive(:to_datetime).and_return(DateTime.new(2011, 1, 1, 0, 0, 0))
|
14
|
-
end
|
15
|
-
|
16
|
-
it { should be_instance_of(DateTime) }
|
17
|
-
|
18
|
-
it { should eql(DateTime.new(2011, 1, 1, 0, 0, 0)) }
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when the value does not respond to #to_datetime' do
|
22
|
-
before do
|
23
|
-
object.extend Virtus::Coercion::TimeCoercions
|
24
|
-
|
25
|
-
# use a string that DateTime.parse can handle
|
26
|
-
value.should_receive(:to_s).and_return('2011-01-01T00:00:00+00:00')
|
27
|
-
end
|
28
|
-
|
29
|
-
it { should be_instance_of(DateTime) }
|
30
|
-
|
31
|
-
it { should eql(DateTime.new(2011, 1, 1, 0, 0, 0)) }
|
32
|
-
end
|
33
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::TimeCoercions, '.to_string' do
|
4
|
-
subject { object.to_string(value) }
|
5
|
-
|
6
|
-
let(:object) { Class.new(Virtus::Coercion::Object) }
|
7
|
-
let(:value) { mock('value') }
|
8
|
-
|
9
|
-
before do
|
10
|
-
object.extend Virtus::Coercion::TimeCoercions
|
11
|
-
|
12
|
-
value.should_receive(:to_s).and_return('2011-01-01')
|
13
|
-
end
|
14
|
-
|
15
|
-
it { should be_instance_of(String) }
|
16
|
-
|
17
|
-
it { should eql('2011-01-01') }
|
18
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::TimeCoercions, '.to_time' do
|
4
|
-
subject { object.to_time(value) }
|
5
|
-
|
6
|
-
let(:object) { Class.new(Virtus::Coercion::Object) }
|
7
|
-
let(:value) { mock('value') }
|
8
|
-
|
9
|
-
context 'when the value responds to #to_time' do
|
10
|
-
before do
|
11
|
-
object.extend Virtus::Coercion::TimeCoercions
|
12
|
-
|
13
|
-
value.should_receive(:to_time).and_return(Time.utc(2011, 1, 1))
|
14
|
-
end
|
15
|
-
|
16
|
-
it { should be_instance_of(Time) }
|
17
|
-
|
18
|
-
it { should eql(Time.utc(2011, 1, 1)) }
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when the value does not respond to #to_time' do
|
22
|
-
before do
|
23
|
-
object.extend Virtus::Coercion::TimeCoercions
|
24
|
-
|
25
|
-
# use a string that Time.parse can handle
|
26
|
-
value.should_receive(:to_s).and_return('Sat Jan 01 00:00:00 UTC 2011')
|
27
|
-
end
|
28
|
-
|
29
|
-
it { should be_instance_of(Time) }
|
30
|
-
|
31
|
-
it { should eql(Time.utc(2011, 1, 1)) }
|
32
|
-
end
|
33
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Coercion::TrueClass, '.to_string' do
|
4
|
-
subject { object.to_string(true_class) }
|
5
|
-
|
6
|
-
let(:object) { described_class }
|
7
|
-
let(:true_class) { true }
|
8
|
-
|
9
|
-
it { should be_instance_of(String) }
|
10
|
-
|
11
|
-
it { should eql('true') }
|
12
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Equalizer, '#<<' do
|
4
|
-
subject { object << :last_name }
|
5
|
-
|
6
|
-
let(:object) { described_class.new(name, attributes) }
|
7
|
-
let(:name) { 'User' }
|
8
|
-
let(:attributes) { [ :first_name ].freeze }
|
9
|
-
let(:first_name) { 'John' }
|
10
|
-
let(:last_name) { 'Doe' }
|
11
|
-
|
12
|
-
let(:klass) do
|
13
|
-
klass = Class.new { attr_accessor :first_name, :last_name }
|
14
|
-
klass.send(:include, object)
|
15
|
-
klass
|
16
|
-
end
|
17
|
-
|
18
|
-
let(:instance) do
|
19
|
-
klass.new.tap do |instance|
|
20
|
-
instance.first_name = first_name
|
21
|
-
instance.last_name = last_name
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#eql?' do
|
26
|
-
context 'when the objects are similar' do
|
27
|
-
let(:other) { instance.dup }
|
28
|
-
|
29
|
-
it 'adds a key to the comparison' do
|
30
|
-
expect { subject }.to_not change { instance.eql?(other) }.from(true)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when the objects are different' do
|
35
|
-
let(:other) { instance.dup }
|
36
|
-
|
37
|
-
before do
|
38
|
-
other.last_name = 'Smith'
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'adds a key to the comparison' do
|
42
|
-
expect { subject }.to change { instance.eql?(other) }.
|
43
|
-
from(true).to(false)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe '#==' do
|
49
|
-
context 'when the objects are similar' do
|
50
|
-
let(:other) { instance.dup }
|
51
|
-
|
52
|
-
it 'adds a key to the comparison' do
|
53
|
-
expect { subject }.to_not change { instance == other }.from(true)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'when the objects are different' do
|
58
|
-
let(:other) { instance.dup }
|
59
|
-
|
60
|
-
before do
|
61
|
-
other.last_name = 'Smith'
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'adds a key to the comparison' do
|
65
|
-
expect { subject }.to change { instance == other }.
|
66
|
-
from(true).to(false)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'adds a new to #hash' do
|
72
|
-
expect { subject }.to change(instance, :hash).
|
73
|
-
from(klass.hash ^ first_name.hash).
|
74
|
-
to(klass.hash ^ first_name.hash ^ last_name.hash)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'adds a new to #inspect' do
|
78
|
-
expect { subject }.to change(instance, :inspect).
|
79
|
-
from('#<User first_name="John">').
|
80
|
-
to('#<User first_name="John" last_name="Doe">')
|
81
|
-
end
|
82
|
-
end
|