wardrobe 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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +10 -0
- data/.gitignore +19 -0
- data/.ruby-version +1 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +18 -0
- data/Guardfile +42 -0
- data/LICENSE +19 -0
- data/README.md +227 -0
- data/Rakefile +20 -0
- data/examples/new_york_times_article.rb +55 -0
- data/lib/wardrobe/attribute.rb +76 -0
- data/lib/wardrobe/attribute_store.rb +16 -0
- data/lib/wardrobe/block_setup.rb +124 -0
- data/lib/wardrobe/boolean.rb +6 -0
- data/lib/wardrobe/class_methods.rb +127 -0
- data/lib/wardrobe/getter_setter.rb +48 -0
- data/lib/wardrobe/instance_methods.rb +56 -0
- data/lib/wardrobe/module_methods.rb +51 -0
- data/lib/wardrobe/option.rb +30 -0
- data/lib/wardrobe/option_store.rb +22 -0
- data/lib/wardrobe/plugin.rb +39 -0
- data/lib/wardrobe/plugin_store.rb +28 -0
- data/lib/wardrobe/plugins/alias_setters.rb +35 -0
- data/lib/wardrobe/plugins/coercible/refinements/array.rb +63 -0
- data/lib/wardrobe/plugins/coercible/refinements/boolean.rb +36 -0
- data/lib/wardrobe/plugins/coercible/refinements/date.rb +31 -0
- data/lib/wardrobe/plugins/coercible/refinements/date_time.rb +29 -0
- data/lib/wardrobe/plugins/coercible/refinements/float.rb +23 -0
- data/lib/wardrobe/plugins/coercible/refinements/hash.rb +65 -0
- data/lib/wardrobe/plugins/coercible/refinements/integer.rb +23 -0
- data/lib/wardrobe/plugins/coercible/refinements/object.rb +15 -0
- data/lib/wardrobe/plugins/coercible/refinements/set.rb +33 -0
- data/lib/wardrobe/plugins/coercible/refinements/string.rb +21 -0
- data/lib/wardrobe/plugins/coercible/refinements/symbol.rb +21 -0
- data/lib/wardrobe/plugins/coercible/refinements/time.rb +30 -0
- data/lib/wardrobe/plugins/coercible/refinements/unsupported_error.rb +11 -0
- data/lib/wardrobe/plugins/coercible.rb +52 -0
- data/lib/wardrobe/plugins/configurable/configurable_store.rb +26 -0
- data/lib/wardrobe/plugins/configurable.rb +40 -0
- data/lib/wardrobe/plugins/default.rb +33 -0
- data/lib/wardrobe/plugins/dirty_tracker.rb +66 -0
- data/lib/wardrobe/plugins/equality.rb +19 -0
- data/lib/wardrobe/plugins/html_initializer.rb +38 -0
- data/lib/wardrobe/plugins/immutable.rb +164 -0
- data/lib/wardrobe/plugins/ivy_presenter.rb +42 -0
- data/lib/wardrobe/plugins/json_initializer.rb +41 -0
- data/lib/wardrobe/plugins/nil_if_empty.rb +27 -0
- data/lib/wardrobe/plugins/nil_if_zero.rb +19 -0
- data/lib/wardrobe/plugins/optional_getter.rb +21 -0
- data/lib/wardrobe/plugins/optional_setter.rb +24 -0
- data/lib/wardrobe/plugins/presenter/refinements/array.rb +15 -0
- data/lib/wardrobe/plugins/presenter/refinements/hash.rb +15 -0
- data/lib/wardrobe/plugins/presenter/refinements/object.rb +15 -0
- data/lib/wardrobe/plugins/presenter/refinements/time.rb +19 -0
- data/lib/wardrobe/plugins/presenter/refinements.rb +6 -0
- data/lib/wardrobe/plugins/presenter.rb +25 -0
- data/lib/wardrobe/plugins/validation/block_handler.rb +83 -0
- data/lib/wardrobe/plugins/validation/class_methods.rb +13 -0
- data/lib/wardrobe/plugins/validation/deep_merge.rb +27 -0
- data/lib/wardrobe/plugins/validation/error_store.rb +27 -0
- data/lib/wardrobe/plugins/validation/instance_methods.rb +39 -0
- data/lib/wardrobe/plugins/validation/refinements/_size.rb +30 -0
- data/lib/wardrobe/plugins/validation/refinements/array.rb +26 -0
- data/lib/wardrobe/plugins/validation/refinements/date.rb +15 -0
- data/lib/wardrobe/plugins/validation/refinements/hash.rb +27 -0
- data/lib/wardrobe/plugins/validation/refinements/integer.rb +21 -0
- data/lib/wardrobe/plugins/validation/refinements/nil_class.rb +19 -0
- data/lib/wardrobe/plugins/validation/refinements/numeric.rb +31 -0
- data/lib/wardrobe/plugins/validation/refinements/object.rb +35 -0
- data/lib/wardrobe/plugins/validation/refinements/set.rb +25 -0
- data/lib/wardrobe/plugins/validation/refinements/string.rb +55 -0
- data/lib/wardrobe/plugins/validation/refinements/symbol.rb +26 -0
- data/lib/wardrobe/plugins/validation/refinements.rb +13 -0
- data/lib/wardrobe/plugins/validation/validation.rb +63 -0
- data/lib/wardrobe/plugins/validation/validation_error.rb +19 -0
- data/lib/wardrobe/plugins/validation/validaton_runner.rb +38 -0
- data/lib/wardrobe/plugins/validation/validator.rb +119 -0
- data/lib/wardrobe/plugins/validation.rb +28 -0
- data/lib/wardrobe/root_config.rb +31 -0
- data/lib/wardrobe/store.rb +62 -0
- data/lib/wardrobe/stores.rb +106 -0
- data/lib/wardrobe/version.rb +5 -0
- data/lib/wardrobe.rb +34 -0
- data/sandbox/Gemfile +5 -0
- data/sandbox/Gemfile.lock +52 -0
- data/sandbox/bench_mutable_coercion.rb +91 -0
- data/sandbox/bench_test_1.rb +50 -0
- data/sandbox/bench_test_2.rb +48 -0
- data/sandbox/bench_test_3.rb +92 -0
- data/sandbox/cnn_article_find.rb +20 -0
- data/sandbox/define_method_procs.rb +139 -0
- data/sandbox/hanami_validations.rb +40 -0
- data/sandbox/middleware_bench_test.rb +97 -0
- data/sandbox/middleware_bench_v2_test.rb +130 -0
- data/test/assertions.rb +22 -0
- data/test/test_helper.rb +37 -0
- data/test/unit/atrs_config_test.rb +33 -0
- data/test/unit/atrs_root_module_test.rb +19 -0
- data/test/unit/attribute_test.rb +44 -0
- data/test/unit/block_runner/array_test.rb +29 -0
- data/test/unit/block_setup_test.rb +41 -0
- data/test/unit/class_methods_test.rb +92 -0
- data/test/unit/create_class_from_hash_test.rb +28 -0
- data/test/unit/define_getter_test.rb +94 -0
- data/test/unit/define_setter_test.rb +0 -0
- data/test/unit/disable_coercion_test.rb +19 -0
- data/test/unit/inheritance_composition_test.rb +36 -0
- data/test/unit/method_override_test.rb +18 -0
- data/test/unit/method_polution_test.rb +48 -0
- data/test/unit/option_test.rb +17 -0
- data/test/unit/plugin_test.rb +12 -0
- data/test/unit/plugins/alias_setters_test.rb +24 -0
- data/test/unit/plugins/coercible/array_test.rb +79 -0
- data/test/unit/plugins/coercible/boolean_test.rb +68 -0
- data/test/unit/plugins/coercible/custom_class_test.rb +61 -0
- data/test/unit/plugins/coercible/date_test.rb +38 -0
- data/test/unit/plugins/coercible/date_time_test.rb +44 -0
- data/test/unit/plugins/coercible/float_test.rb +42 -0
- data/test/unit/plugins/coercible/hash_test.rb +62 -0
- data/test/unit/plugins/coercible/integer_test.rb +42 -0
- data/test/unit/plugins/coercible/nested_wardrobe_test.rb +63 -0
- data/test/unit/plugins/coercible/set_test.rb +49 -0
- data/test/unit/plugins/coercible/string_test.rb +36 -0
- data/test/unit/plugins/coercible/symbol_test.rb +30 -0
- data/test/unit/plugins/coercible/time_test.rb +43 -0
- data/test/unit/plugins/configurable_test.rb +64 -0
- data/test/unit/plugins/default_value_test.rb +78 -0
- data/test/unit/plugins/dirty_tracker_test.rb +78 -0
- data/test/unit/plugins/equality_test.rb +36 -0
- data/test/unit/plugins/html_initializer_test.rb +19 -0
- data/test/unit/plugins/immutable_test.rb +149 -0
- data/test/unit/plugins/ivy_presenter_test.rb +57 -0
- data/test/unit/plugins/json_initializer_test.rb +67 -0
- data/test/unit/plugins/nil_if_empty_test.rb +39 -0
- data/test/unit/plugins/nil_if_zero_test.rb +26 -0
- data/test/unit/plugins/presenter_test.rb +68 -0
- data/test/unit/plugins/validate/inheritance_test.rb +36 -0
- data/test/unit/plugins/validate/nested_model_test.rb +40 -0
- data/test/unit/plugins/validate/predicates/each_key_each_value_test.rb +34 -0
- data/test/unit/plugins/validate/predicates/each_test.rb +33 -0
- data/test/unit/plugins/validate/predicates/empty_test.rb +37 -0
- data/test/unit/plugins/validate/predicates/even_test.rb +33 -0
- data/test/unit/plugins/validate/predicates/excluded_from_test.rb +55 -0
- data/test/unit/plugins/validate/predicates/filled_test.rb +49 -0
- data/test/unit/plugins/validate/predicates/format_test.rb +28 -0
- data/test/unit/plugins/validate/predicates/gt_test.rb +36 -0
- data/test/unit/plugins/validate/predicates/gteq_test.rb +36 -0
- data/test/unit/plugins/validate/predicates/included_in_test.rb +55 -0
- data/test/unit/plugins/validate/predicates/lt_test.rb +36 -0
- data/test/unit/plugins/validate/predicates/lteq_test.rb +36 -0
- data/test/unit/plugins/validate/predicates/max_size_test.rb +43 -0
- data/test/unit/plugins/validate/predicates/min_size_test.rb +43 -0
- data/test/unit/plugins/validate/predicates/none_test.rb +49 -0
- data/test/unit/plugins/validate/predicates/odd_test.rb +33 -0
- data/test/unit/plugins/validate/predicates/size_range_test.rb +43 -0
- data/test/unit/plugins/validate/predicates/size_test.rb +43 -0
- data/test/unit/plugins/validate/predicates/type_test.rb +77 -0
- data/test/unit/plugins/validate/predicates_test.rb +73 -0
- data/test/unit/plugins/validate/validate_bang_test.rb +30 -0
- data/test/unit/store_test.rb +15 -0
- data/test/unit/stores_test.rb +18 -0
- data/wardrobe.gemspec +26 -0
- metadata +344 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesEmptyTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :string, String, validates { empty? }
|
8
|
+
attribute :symbol, Symbol, validates { empty? }
|
9
|
+
attribute :array, Array, validates { empty? }
|
10
|
+
attribute :hash, Hash, validates { empty? }
|
11
|
+
attribute :set, Set, validates { empty? }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
errors = Foo.new(
|
16
|
+
string: nil, symbol: nil, array: nil, hash: nil, set: nil
|
17
|
+
)._validation_errors
|
18
|
+
|
19
|
+
refute errors.has_key?(:string)
|
20
|
+
refute errors.has_key?(:symbol)
|
21
|
+
refute errors.has_key?(:array)
|
22
|
+
refute errors.has_key?(:hash)
|
23
|
+
refute errors.has_key?(:set)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_with_content
|
27
|
+
errors = Foo.new(
|
28
|
+
string: 'Bar', symbol: :'Bar', array: ['Bar'], hash: {foo: 'Bar'}, set: Set.new(['Bar'])
|
29
|
+
)._validation_errors
|
30
|
+
|
31
|
+
assert_equal 'must be empty', errors[:string][0]
|
32
|
+
assert_equal 'must be empty', errors[:symbol][0]
|
33
|
+
assert_equal 'must be empty', errors[:array][0]
|
34
|
+
assert_equal 'must be empty', errors[:hash][0]
|
35
|
+
assert_equal 'must be empty', errors[:set][0]
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesEvenTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { even? }
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_nil
|
11
|
+
assert_raises(NoMethodError) {
|
12
|
+
Foo.new(
|
13
|
+
integer: nil
|
14
|
+
)._validation_errors
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_fail
|
19
|
+
errors = Foo.new(
|
20
|
+
integer: 1
|
21
|
+
)._validation_errors
|
22
|
+
|
23
|
+
assert_equal 'must be even', errors[:integer][0]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_success
|
27
|
+
errors = Foo.new(
|
28
|
+
integer: 2
|
29
|
+
)._validation_errors
|
30
|
+
|
31
|
+
refute errors.has_key?(:integer)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesExludedFromTest < MiniTest::Test
|
4
|
+
TIME = Time.now
|
5
|
+
class Foo
|
6
|
+
include Wardrobe(coerce: false)
|
7
|
+
plugin :validation
|
8
|
+
attribute :string, String, validates { excluded_from?(['test']) }
|
9
|
+
attribute :symbol, Symbol, validates { excluded_from?([:test]) }
|
10
|
+
attribute :integer, Integer, validates { excluded_from?([1]) }
|
11
|
+
attribute :float, Float, validates { excluded_from?([1.1]) }
|
12
|
+
attribute :boolean, Wardrobe::Boolean, validates { excluded_from?([true]) }
|
13
|
+
attribute :date, Date, validates { excluded_from?([TIME.to_date]) }
|
14
|
+
attribute :time, Time, validates { excluded_from?([TIME]) }
|
15
|
+
attribute :date_time, DateTime, validates { excluded_from?([TIME.to_datetime]) }
|
16
|
+
attribute :array, Array, validates { excluded_from?([[1]]) }
|
17
|
+
attribute :hash, Hash, validates { excluded_from?([{one: 1}]) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_nil
|
21
|
+
errors = Foo.new(
|
22
|
+
string: nil, symbol: nil, integer: nil, float: nil, boolean: nil,
|
23
|
+
date: nil, time: nil, date_time: nil, array: nil, hash: nil
|
24
|
+
)._validation_errors
|
25
|
+
|
26
|
+
refute errors.has_key? :string
|
27
|
+
refute errors.has_key? :symbol
|
28
|
+
refute errors.has_key? :integer
|
29
|
+
refute errors.has_key? :float
|
30
|
+
refute errors.has_key? :boolean
|
31
|
+
refute errors.has_key? :date
|
32
|
+
refute errors.has_key? :time
|
33
|
+
refute errors.has_key? :date_time
|
34
|
+
refute errors.has_key? :array
|
35
|
+
refute errors.has_key? :hash
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_values
|
39
|
+
errors = Foo.new(
|
40
|
+
string: 'test', symbol: :test, integer: 1, float: 1.1, boolean: true,
|
41
|
+
date: TIME.to_date, time: TIME, date_time: TIME.to_datetime, array: [1], hash: {one: 1}
|
42
|
+
)._validation_errors
|
43
|
+
|
44
|
+
assert_equal 'must not be one of: "test"', errors[:string][0]
|
45
|
+
assert_equal 'must not be one of: :test', errors[:symbol][0]
|
46
|
+
assert_equal 'must not be one of: 1', errors[:integer][0]
|
47
|
+
assert_equal 'must not be one of: 1.1', errors[:float][0]
|
48
|
+
assert_equal 'must not be one of: true', errors[:boolean][0]
|
49
|
+
assert_equal "must not be one of: #{TIME.to_date.to_s}", errors[:date][0]
|
50
|
+
assert_equal "must not be one of: #{TIME.to_s}", errors[:time][0]
|
51
|
+
assert_equal "must not be one of: #{TIME.to_datetime.to_s}", errors[:date_time][0]
|
52
|
+
assert_equal 'must not be one of: [1]', errors[:array][0]
|
53
|
+
assert_equal 'must not be one of: {:one=>1}', errors[:hash][0]
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesFilledTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :string, String, validates { filled? }
|
8
|
+
attribute :symbol, Symbol, validates { filled? }
|
9
|
+
attribute :array, Array, validates { filled? }
|
10
|
+
attribute :hash, Hash, validates { filled? }
|
11
|
+
attribute :set, Set, validates { filled? }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
errors = Foo.new(
|
16
|
+
string: nil, symbol: nil, array: nil, hash: nil, set: nil
|
17
|
+
)._validation_errors
|
18
|
+
|
19
|
+
assert_equal 'must be filled', errors[:string][0]
|
20
|
+
assert_equal 'must be filled', errors[:symbol][0]
|
21
|
+
assert_equal 'must be filled', errors[:array][0]
|
22
|
+
assert_equal 'must be filled', errors[:hash][0]
|
23
|
+
assert_equal 'must be filled', errors[:set][0]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_empty
|
27
|
+
errors = Foo.new(
|
28
|
+
string: '', symbol: :'', array: [], hash: {}, set: Set.new
|
29
|
+
)._validation_errors
|
30
|
+
|
31
|
+
assert_equal 'must be filled', errors[:string][0]
|
32
|
+
assert_equal 'must be filled', errors[:symbol][0]
|
33
|
+
assert_equal 'must be filled', errors[:array][0]
|
34
|
+
assert_equal 'must be filled', errors[:hash][0]
|
35
|
+
assert_equal 'must be filled', errors[:set][0]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_content
|
39
|
+
errors = Foo.new(
|
40
|
+
string: 'Bar', symbol: :'Bar', array: ['Bar'], hash: {foo: 'Bar'}, set: Set.new(['Bar'])
|
41
|
+
)._validation_errors
|
42
|
+
|
43
|
+
refute errors.has_key?(:string)
|
44
|
+
refute errors.has_key?(:symbol)
|
45
|
+
refute errors.has_key?(:array)
|
46
|
+
refute errors.has_key?(:hash)
|
47
|
+
refute errors.has_key?(:set)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesFormatTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe(coerce: false)
|
6
|
+
plugin :validation
|
7
|
+
attribute :string, String, validates { format?(/^abc$/) }
|
8
|
+
attribute :symbol, Symbol, validates { format?(/^abc$/) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_failin_regex
|
12
|
+
errors = Foo.new(
|
13
|
+
string: 'no', symbol: :no
|
14
|
+
)._validation_errors
|
15
|
+
|
16
|
+
assert_equal 'must match /^abc$/', errors[:string][0]
|
17
|
+
assert_equal 'must match /^abc$/', errors[:symbol][0]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_matching_regex
|
21
|
+
errors = Foo.new(
|
22
|
+
string: 'abc', symbol: :abc
|
23
|
+
)._validation_errors
|
24
|
+
|
25
|
+
refute errors.has_key?(:string)
|
26
|
+
refute errors.has_key?(:symbol)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesGreaterThanTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { gt?(1) }
|
8
|
+
attribute :float, Float, validates { gt?(1.0) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nil
|
12
|
+
assert_raises(NoMethodError) {
|
13
|
+
Foo.new(
|
14
|
+
integer: nil, float: nil
|
15
|
+
)._validation_errors
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_fail
|
20
|
+
errors = Foo.new(
|
21
|
+
integer: 0, float: 0.1
|
22
|
+
)._validation_errors
|
23
|
+
|
24
|
+
assert_equal 'must be greater than 1', errors[:integer][0]
|
25
|
+
assert_equal 'must be greater than 1.0', errors[:float][0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_success
|
29
|
+
errors = Foo.new(
|
30
|
+
integer: 2, float: 1.2
|
31
|
+
)._validation_errors
|
32
|
+
|
33
|
+
refute errors.has_key?(:integer)
|
34
|
+
refute errors.has_key?(:float)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesGreaterThanOrEqualToTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { gteq?(1) }
|
8
|
+
attribute :float, Float, validates { gteq?(1.0) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nil
|
12
|
+
assert_raises(NoMethodError) {
|
13
|
+
Foo.new(
|
14
|
+
integer: nil, float: nil
|
15
|
+
)._validation_errors
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_fail
|
20
|
+
errors = Foo.new(
|
21
|
+
integer: 0, float: 0.9
|
22
|
+
)._validation_errors
|
23
|
+
|
24
|
+
assert_equal 'must be greater than or equal to 1', errors[:integer][0]
|
25
|
+
assert_equal 'must be greater than or equal to 1.0', errors[:float][0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_success
|
29
|
+
errors = Foo.new(
|
30
|
+
integer: 1, float: 1.0
|
31
|
+
)._validation_errors
|
32
|
+
|
33
|
+
refute errors.has_key?(:integer)
|
34
|
+
refute errors.has_key?(:float)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesIncludedInTest < MiniTest::Test
|
4
|
+
TIME = Time.now
|
5
|
+
class Foo
|
6
|
+
include Wardrobe(coerce: false)
|
7
|
+
plugin :validation
|
8
|
+
attribute :string, String, validates { included_in?(['test']) }
|
9
|
+
attribute :symbol, Symbol, validates { included_in?([:test]) }
|
10
|
+
attribute :integer, Integer, validates { included_in?([1]) }
|
11
|
+
attribute :float, Float, validates { included_in?([1.1]) }
|
12
|
+
attribute :boolean, Wardrobe::Boolean, validates { included_in?([true]) }
|
13
|
+
attribute :date, Date, validates { included_in?([TIME.to_date]) }
|
14
|
+
attribute :time, Time, validates { included_in?([TIME]) }
|
15
|
+
attribute :date_time, DateTime, validates { included_in?([TIME.to_datetime]) }
|
16
|
+
attribute :array, Array, validates { included_in?([[1]]) }
|
17
|
+
attribute :hash, Hash, validates { included_in?([{one: 1}]) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_nil
|
21
|
+
errors = Foo.new(
|
22
|
+
string: nil, symbol: nil, integer: nil, float: nil, boolean: nil,
|
23
|
+
date: nil, time: nil, date_time: nil, array: nil, hash: nil
|
24
|
+
)._validation_errors
|
25
|
+
|
26
|
+
assert_equal 'must be one of: "test"', errors[:string][0]
|
27
|
+
assert_equal 'must be one of: :test', errors[:symbol][0]
|
28
|
+
assert_equal 'must be one of: 1', errors[:integer][0]
|
29
|
+
assert_equal 'must be one of: 1.1', errors[:float][0]
|
30
|
+
assert_equal 'must be one of: true', errors[:boolean][0]
|
31
|
+
assert_equal "must be one of: #{TIME.to_date.to_s}", errors[:date][0]
|
32
|
+
assert_equal "must be one of: #{TIME.to_s}", errors[:time][0]
|
33
|
+
assert_equal "must be one of: #{TIME.to_datetime.to_s}", errors[:date_time][0]
|
34
|
+
assert_equal 'must be one of: [1]', errors[:array][0]
|
35
|
+
assert_equal 'must be one of: {:one=>1}', errors[:hash][0]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_values
|
39
|
+
errors = Foo.new(
|
40
|
+
string: 'test', symbol: :test, integer: 1, float: 1.1, boolean: true,
|
41
|
+
date: TIME.to_date, time: TIME, date_time: TIME.to_datetime, array: [1], hash: {one: 1}
|
42
|
+
)._validation_errors
|
43
|
+
|
44
|
+
refute errors.has_key? :string
|
45
|
+
refute errors.has_key? :symbol
|
46
|
+
refute errors.has_key? :integer
|
47
|
+
refute errors.has_key? :float
|
48
|
+
refute errors.has_key? :boolean
|
49
|
+
refute errors.has_key? :date
|
50
|
+
refute errors.has_key? :time
|
51
|
+
refute errors.has_key? :date_time
|
52
|
+
refute errors.has_key? :array
|
53
|
+
refute errors.has_key? :hash
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesLessThanTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { lt?(1) }
|
8
|
+
attribute :float, Float, validates { lt?(1.0) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nil
|
12
|
+
assert_raises(NoMethodError) {
|
13
|
+
Foo.new(
|
14
|
+
integer: nil, float: nil
|
15
|
+
)._validation_errors
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_fail
|
20
|
+
errors = Foo.new(
|
21
|
+
integer: 2, float: 1.1
|
22
|
+
)._validation_errors
|
23
|
+
|
24
|
+
assert_equal 'must be less than 1', errors[:integer][0]
|
25
|
+
assert_equal 'must be less than 1.0', errors[:float][0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_success
|
29
|
+
errors = Foo.new(
|
30
|
+
integer: 0, float: 0.9
|
31
|
+
)._validation_errors
|
32
|
+
|
33
|
+
refute errors.has_key?(:integer)
|
34
|
+
refute errors.has_key?(:float)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesLessThanOrEqualToTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { lteq?(1) }
|
8
|
+
attribute :float, Float, validates { lteq?(1.0) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nil
|
12
|
+
assert_raises(NoMethodError) {
|
13
|
+
Foo.new(
|
14
|
+
integer: nil, float: nil
|
15
|
+
)._validation_errors
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_fail
|
20
|
+
errors = Foo.new(
|
21
|
+
integer: 2, float: 1.1
|
22
|
+
)._validation_errors
|
23
|
+
|
24
|
+
assert_equal 'must be less than or equal to 1', errors[:integer][0]
|
25
|
+
assert_equal 'must be less than or equal to 1.0', errors[:float][0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_success
|
29
|
+
errors = Foo.new(
|
30
|
+
integer: 1, float: 1.0
|
31
|
+
)._validation_errors
|
32
|
+
|
33
|
+
refute errors.has_key?(:integer)
|
34
|
+
refute errors.has_key?(:float)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesMaxSizeTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
|
8
|
+
attribute :array, Array, validates { max_size?(1) }
|
9
|
+
attribute :hash, Hash, validates { max_size?(1) }
|
10
|
+
attribute :set, Set, validates { max_size?(1) }
|
11
|
+
attribute :string, String, validates { max_size?(1) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
assert_raises(NoMethodError) {
|
16
|
+
Foo.new(
|
17
|
+
array: nil, hash: nil, set: nil, string: nil
|
18
|
+
)._validation_errors
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_fail
|
23
|
+
errors = Foo.new(
|
24
|
+
array: [1, 2], hash: { 1 => 2, 3 => 4 }, set: Set.new([1, 2]), string: '12'
|
25
|
+
)._validation_errors
|
26
|
+
|
27
|
+
assert_equal 'size cannot be greater than 1', errors[:array][0]
|
28
|
+
assert_equal 'size cannot be greater than 1', errors[:hash][0]
|
29
|
+
assert_equal 'size cannot be greater than 1', errors[:set][0]
|
30
|
+
assert_equal 'size cannot be greater than 1', errors[:string][0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_success
|
34
|
+
errors = Foo.new(
|
35
|
+
array: [], hash: {}, set: Set.new, string: ''
|
36
|
+
)._validation_errors
|
37
|
+
|
38
|
+
refute errors.has_key?(:array)
|
39
|
+
refute errors.has_key?(:hash)
|
40
|
+
refute errors.has_key?(:set)
|
41
|
+
refute errors.has_key?(:string)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesMinSizeTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
|
8
|
+
attribute :array, Array, validates { min_size?(1) }
|
9
|
+
attribute :hash, Hash, validates { min_size?(1) }
|
10
|
+
attribute :set, Set, validates { min_size?(1) }
|
11
|
+
attribute :string, String, validates { min_size?(1) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
assert_raises(NoMethodError) {
|
16
|
+
Foo.new(
|
17
|
+
array: nil, hash: nil, set: nil, string: nil
|
18
|
+
)._validation_errors
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_fail
|
23
|
+
errors = Foo.new(
|
24
|
+
array: [], hash: {}, set: Set.new, string: ''
|
25
|
+
)._validation_errors
|
26
|
+
|
27
|
+
assert_equal 'size cannot be less than 1', errors[:array][0]
|
28
|
+
assert_equal 'size cannot be less than 1', errors[:hash][0]
|
29
|
+
assert_equal 'size cannot be less than 1', errors[:set][0]
|
30
|
+
assert_equal 'size cannot be less than 1', errors[:string][0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_success
|
34
|
+
errors = Foo.new(
|
35
|
+
array: [1], hash: { 1 => 2 }, set: Set.new([1]), string: '1'
|
36
|
+
)._validation_errors
|
37
|
+
|
38
|
+
refute errors.has_key?(:array)
|
39
|
+
refute errors.has_key?(:hash)
|
40
|
+
refute errors.has_key?(:set)
|
41
|
+
refute errors.has_key?(:string)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# require 'test_helper'
|
2
|
+
#
|
3
|
+
# class ValidationPredicatesNoneTest < MiniTest::Test
|
4
|
+
# class Foo
|
5
|
+
# include Wardrobe
|
6
|
+
# plugin :validation
|
7
|
+
# attribute :string, String, validates { none? }
|
8
|
+
# attribute :symbol, Symbol, validates { none? }
|
9
|
+
# attribute :array, Array, validates { none? }
|
10
|
+
# attribute :hash, Hash, validates { none? }
|
11
|
+
# attribute :set, Set, validates { none? }
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# def test_nil
|
15
|
+
# errors = Foo.new(
|
16
|
+
# string: nil, symbol: nil, array: nil, hash: nil, set: nil
|
17
|
+
# )._validation_errors
|
18
|
+
#
|
19
|
+
# assert_equal 'must be filled', errors[:string][0]
|
20
|
+
# assert_equal 'must be filled', errors[:symbol][0]
|
21
|
+
# assert_equal 'must be filled', errors[:array][0]
|
22
|
+
# assert_equal 'must be filled', errors[:hash][0]
|
23
|
+
# assert_equal 'must be filled', errors[:set][0]
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def test_empty
|
27
|
+
# errors = Foo.new(
|
28
|
+
# string: '', symbol: :'', array: [], hash: {}, set: Set.new
|
29
|
+
# )._validation_errors
|
30
|
+
#
|
31
|
+
# assert_equal 'must be filled', errors[:string][0]
|
32
|
+
# assert_equal 'must be filled', errors[:symbol][0]
|
33
|
+
# assert_equal 'must be filled', errors[:array][0]
|
34
|
+
# assert_equal 'must be filled', errors[:hash][0]
|
35
|
+
# assert_equal 'must be filled', errors[:set][0]
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# def test_with_content
|
39
|
+
# errors = Foo.new(
|
40
|
+
# string: 'Bar', symbol: :'Bar', array: ['Bar'], hash: {foo: 'Bar'}, set: Set.new(['Bar'])
|
41
|
+
# )._validation_errors
|
42
|
+
#
|
43
|
+
# refute errors.has_key?(:string)
|
44
|
+
# refute errors.has_key?(:symbol)
|
45
|
+
# refute errors.has_key?(:array)
|
46
|
+
# refute errors.has_key?(:hash)
|
47
|
+
# refute errors.has_key?(:set)
|
48
|
+
# end
|
49
|
+
# end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesOddTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
attribute :integer, Integer, validates { odd? }
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_nil
|
11
|
+
assert_raises(NoMethodError) {
|
12
|
+
Foo.new(
|
13
|
+
integer: nil
|
14
|
+
)._validation_errors
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_fail
|
19
|
+
errors = Foo.new(
|
20
|
+
integer: 0
|
21
|
+
)._validation_errors
|
22
|
+
|
23
|
+
assert_equal 'must be odd', errors[:integer][0]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_success
|
27
|
+
errors = Foo.new(
|
28
|
+
integer: 1
|
29
|
+
)._validation_errors
|
30
|
+
|
31
|
+
refute errors.has_key?(:integer)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesSizeRangeTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
|
8
|
+
attribute :array, Array, validates { size?(1..3) }
|
9
|
+
attribute :hash, Hash, validates { size?(1..3) }
|
10
|
+
attribute :set, Set, validates { size?(1..3) }
|
11
|
+
attribute :string, String, validates { size?(1..3) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
assert_raises(NoMethodError) {
|
16
|
+
Foo.new(
|
17
|
+
array: nil, hash: nil, set: nil, string: nil
|
18
|
+
)._validation_errors
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_fail
|
23
|
+
errors = Foo.new(
|
24
|
+
array: [], hash: {}, set: Set.new, string: ''
|
25
|
+
)._validation_errors
|
26
|
+
|
27
|
+
assert_equal 'size must be within 1 - 3', errors[:array][0]
|
28
|
+
assert_equal 'size must be within 1 - 3', errors[:hash][0]
|
29
|
+
assert_equal 'size must be within 1 - 3', errors[:set][0]
|
30
|
+
assert_equal 'size must be within 1 - 3', errors[:string][0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_success
|
34
|
+
errors = Foo.new(
|
35
|
+
array: [1], hash: { 1 => 2 }, set: Set.new([1]), string: '1'
|
36
|
+
)._validation_errors
|
37
|
+
|
38
|
+
refute errors.has_key?(:array)
|
39
|
+
refute errors.has_key?(:hash)
|
40
|
+
refute errors.has_key?(:set)
|
41
|
+
refute errors.has_key?(:string)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationPredicatesSizeTest < MiniTest::Test
|
4
|
+
class Foo
|
5
|
+
include Wardrobe
|
6
|
+
plugin :validation
|
7
|
+
|
8
|
+
attribute :array, Array, validates { size?(1) }
|
9
|
+
attribute :hash, Hash, validates { size?(1) }
|
10
|
+
attribute :set, Set, validates { size?(1) }
|
11
|
+
attribute :string, String, validates { size?(1) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_nil
|
15
|
+
assert_raises(NoMethodError) {
|
16
|
+
Foo.new(
|
17
|
+
array: nil, hash: nil, set: nil, string: nil
|
18
|
+
)._validation_errors
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_fail
|
23
|
+
errors = Foo.new(
|
24
|
+
array: [], hash: {}, set: Set.new, string: ''
|
25
|
+
)._validation_errors
|
26
|
+
|
27
|
+
assert_equal 'size must be 1', errors[:array][0]
|
28
|
+
assert_equal 'size must be 1', errors[:hash][0]
|
29
|
+
assert_equal 'size must be 1', errors[:set][0]
|
30
|
+
assert_equal 'size must be 1', errors[:string][0]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_success
|
34
|
+
errors = Foo.new(
|
35
|
+
array: [1], hash: { 1 => 2 }, set: Set.new([1]), string: '1'
|
36
|
+
)._validation_errors
|
37
|
+
|
38
|
+
refute errors.has_key?(:array)
|
39
|
+
refute errors.has_key?(:hash)
|
40
|
+
refute errors.has_key?(:set)
|
41
|
+
refute errors.has_key?(:string)
|
42
|
+
end
|
43
|
+
end
|