veto 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -94
- data/lib/veto/blocks/block.rb +25 -0
- data/lib/veto/blocks/checker.rb +28 -0
- data/lib/veto/blocks/conditional_block.rb +26 -0
- data/lib/veto/blocks/validate_block.rb +18 -0
- data/lib/veto/blocks/validates_block.rb +51 -0
- data/lib/veto/blocks/with_options_block.rb +7 -0
- data/lib/veto/check_context_object.rb +10 -0
- data/lib/veto/checks/attribute_check.rb +19 -0
- data/lib/veto/checks/check.rb +7 -0
- data/lib/veto/checks/check_factory.rb +18 -0
- data/lib/veto/{validators/exact_length_validator.rb → checks/exact_length_check.rb} +5 -4
- data/lib/veto/{validators/format_validator.rb → checks/format_check.rb} +5 -4
- data/lib/veto/{validators/greater_than_validator.rb → checks/greater_than_check.rb} +2 -4
- data/lib/veto/{validators/greater_than_or_equal_to_validator.rb → checks/greater_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/inclusion_validator.rb → checks/inclusion_check.rb} +2 -4
- data/lib/veto/{validators/integer_validator.rb → checks/integer_check.rb} +2 -4
- data/lib/veto/{validators/length_range_validator.rb → checks/length_range_check.rb} +2 -4
- data/lib/veto/{validators/less_than_validator.rb → checks/less_than_check.rb} +2 -4
- data/lib/veto/{validators/less_than_or_equal_to_validator.rb → checks/less_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/max_length_validator.rb → checks/max_length_check.rb} +2 -4
- data/lib/veto/checks/method_check.rb +17 -0
- data/lib/veto/{validators/min_length_validator.rb → checks/min_length_check.rb} +2 -4
- data/lib/veto/{validators/not_null_validator.rb → checks/not_null_check.rb} +2 -4
- data/lib/veto/{validators/numeric_validator.rb → checks/numeric_check.rb} +2 -4
- data/lib/veto/{validators/presence_validator.rb → checks/presence_check.rb} +2 -4
- data/lib/veto/conditions/condition.rb +7 -0
- data/lib/veto/conditions/condition_factory.rb +18 -0
- data/lib/veto/conditions/conditions.rb +21 -0
- data/lib/veto/conditions/context_method_condition.rb +11 -0
- data/lib/veto/conditions/entity_eval_condition.rb +11 -0
- data/lib/veto/conditions/if_conditions.rb +8 -0
- data/lib/veto/conditions/if_unless_conditions.rb +21 -0
- data/lib/veto/conditions/passing_condition.rb +7 -0
- data/lib/veto/conditions/primative_condition.rb +11 -0
- data/lib/veto/conditions/proc_condition.rb +11 -0
- data/lib/veto/conditions/unless_conditions.rb +8 -0
- data/lib/veto/configuration.rb +38 -37
- data/lib/veto/errors.rb +27 -27
- data/lib/veto/exceptions.rb +13 -3
- data/lib/veto/model.rb +33 -35
- data/lib/veto/validator.rb +68 -169
- data/lib/veto/version.rb +1 -1
- data/lib/veto.rb +90 -49
- data/spec/blocks/block_spec.rb +31 -0
- data/spec/blocks/check_block_spec.rb +46 -0
- data/spec/blocks/conditional_block_spec.rb +36 -0
- data/spec/blocks/validate_block_spec.rb +18 -0
- data/spec/blocks/validates_block_spec.rb +22 -0
- data/spec/checks/exact_length_check_spec.rb +37 -0
- data/spec/checks/format_validator_spec.rb +37 -0
- data/spec/checks/greater_than_check_spec.rb +48 -0
- data/spec/checks/greater_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/inclusion_check_spec.rb +37 -0
- data/spec/checks/integer_check_spec.rb +37 -0
- data/spec/checks/length_range_check_spec.rb +37 -0
- data/spec/checks/less_than_check_spec.rb +48 -0
- data/spec/checks/less_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/max_length_check_spec.rb +48 -0
- data/spec/checks/method_check_spec.rb +13 -0
- data/spec/checks/min_length_check_spec.rb +48 -0
- data/spec/checks/not_null_check_spec.rb +37 -0
- data/spec/checks/numeric_check_spec.rb +37 -0
- data/spec/checks/presence_check_spec.rb +48 -0
- data/spec/conditions/condition_factory_spec.rb +31 -0
- data/spec/conditions/conditions_spec.rb +25 -0
- data/spec/conditions/context_method_condition_spec.rb +25 -0
- data/spec/conditions/entity_eval_condition_spec.rb +21 -0
- data/spec/conditions/if_conditions_spec.rb +65 -0
- data/spec/conditions/if_unless_conditions_spec.rb +33 -0
- data/spec/conditions/primative_condition_spec.rb +22 -0
- data/spec/conditions/proc_condition_spec.rb +25 -0
- data/spec/conditions/unless_conditions_spec.rb +65 -0
- data/spec/configuration/message_spec.rb +18 -23
- data/spec/spec_helper.rb +2 -1
- data/spec/validator_spec.rb +224 -115
- metadata +96 -61
- data/lib/veto/attribute_validator_factory.rb +0 -36
- data/lib/veto/builder.rb +0 -65
- data/lib/veto/conditions.rb +0 -23
- data/lib/veto/conditions_evaluator.rb +0 -30
- data/lib/veto/validators/abstract_validator.rb +0 -15
- data/lib/veto/validators/attribute_validator.rb +0 -22
- data/lib/veto/validators/custom_method_validator.rb +0 -19
- data/spec/attribute_validator_factory_spec.rb +0 -72
- data/spec/builder_spec.rb +0 -38
- data/spec/conditions_evaluator_spec.rb +0 -90
- data/spec/conditions_spec.rb +0 -16
- data/spec/configuration_spec.rb +0 -6
- data/spec/errors_spec.rb +0 -22
- data/spec/model_spec.rb +0 -67
- data/spec/system/validator_spec.rb +0 -530
- data/spec/validators/exact_length_validator_spec.rb +0 -37
- data/spec/validators/format_validator_spec.rb +0 -32
- data/spec/validators/inclusion_validator_spec.rb +0 -45
- data/spec/validators/integer_validator_spec.rb +0 -42
- data/spec/validators/length_range_validator_spec.rb +0 -55
- data/spec/validators/max_length_validator_spec.rb +0 -32
- data/spec/validators/min_length_validator_spec.rb +0 -32
- data/spec/validators/not_null_validator_spec.rb +0 -27
- data/spec/validators/numeric_validator_spec.rb +0 -42
- data/spec/validators/presence_validator_spec.rb +0 -47
- data/spec/veto_spec.rb +0 -24
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::Block do
|
4
|
+
it 'can have children assigned' do
|
5
|
+
block = new_block
|
6
|
+
child = stub
|
7
|
+
block << child
|
8
|
+
block.children.must_equal([child])
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'can :call each child' do
|
12
|
+
block = new_block
|
13
|
+
|
14
|
+
child1 = stub
|
15
|
+
child2 = stub
|
16
|
+
|
17
|
+
block << child1
|
18
|
+
block << child2
|
19
|
+
|
20
|
+
args = stub
|
21
|
+
|
22
|
+
child1.expects(:call).with(args).once
|
23
|
+
child2.expects(:call).with(args).once
|
24
|
+
|
25
|
+
block.call(args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def new_block
|
29
|
+
Veto::Block.new
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::ValidatesBlock::CheckBlock do
|
4
|
+
it 'gets attribute_name' do
|
5
|
+
block = new_block(:presence, :first_name, true)
|
6
|
+
block.attribute_name.must_equal(:first_name)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'gets type' do
|
10
|
+
block = new_block(:presence, :first_name, true)
|
11
|
+
block.type.must_equal(:presence)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'converts true value to hash' do
|
15
|
+
block = new_block(:presence, :first_name, true)
|
16
|
+
block.options.must_equal({})
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'converts range value to hash' do
|
20
|
+
block = new_block(:presence, :first_name, 1..9)
|
21
|
+
block.options.must_equal(:in => 1..9)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'converts array value to hash' do
|
25
|
+
block = new_block(:presence, :first_name, ['cat', 'dog'])
|
26
|
+
block.options.must_equal(:in => ['cat', 'dog'])
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'converts other value to hash' do
|
30
|
+
block = new_block(:presence, :first_name, 23)
|
31
|
+
block.options.must_equal(:with => 23)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'can build structure and assign check child' do
|
35
|
+
block = build_block(:presence, :first_name, true)
|
36
|
+
block.children.map{|c| c.class.name}.must_equal(["Veto::PresenceCheck"])
|
37
|
+
end
|
38
|
+
|
39
|
+
def new_block(*args)
|
40
|
+
Veto::ValidatesBlock::CheckBlock.new(*args)
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_block(*args)
|
44
|
+
Veto::ValidatesBlock::CheckBlock.build(*args)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::ConditionalBlock do
|
4
|
+
it 'exposes options with conditions removed' do
|
5
|
+
block = new_block(:if => 'blah', :opt1 => 'blahblah')
|
6
|
+
block.options.must_equal(:opt1 => 'blahblah')
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'when conditions pass' do
|
10
|
+
it 'will :call children' do
|
11
|
+
block = new_block(:if => true, :opt1 => 'blahblah')
|
12
|
+
child = stub
|
13
|
+
|
14
|
+
block << child
|
15
|
+
|
16
|
+
child.expects(:call).once
|
17
|
+
block.call
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when conditions fail' do
|
22
|
+
it 'will not :call children' do
|
23
|
+
block = new_block(:if => false, :opt1 => 'blahblah')
|
24
|
+
child = stub
|
25
|
+
|
26
|
+
block << child
|
27
|
+
|
28
|
+
child.expects(:call).never
|
29
|
+
block.call
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def new_block(*args)
|
34
|
+
Veto::ConditionalBlock.new(*args)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::ValidateBlock do
|
4
|
+
it 'can build itself and assign check child' do
|
5
|
+
block = Veto::ValidateBlock.build(:good_method?, :if => true)
|
6
|
+
block.children.size.must_equal(1)
|
7
|
+
block.children[0].must_be_instance_of(Veto::MethodCheck)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'has method_name' do
|
11
|
+
block = new_block(:good_method?)
|
12
|
+
block.method_name.must_equal(:good_method?)
|
13
|
+
end
|
14
|
+
|
15
|
+
def new_block(*args)
|
16
|
+
Veto::ValidateBlock.new(*args)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::ValidatesBlock do
|
4
|
+
it 'gets attribute_name' do
|
5
|
+
block = new_block(:first_name, :presence => true, :if => true)
|
6
|
+
block.attribute_name.must_equal(:first_name)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'can build nested check structure' do
|
10
|
+
block = build_block(:first_name, :presence => true, :format => /[a-z]/, :if => true)
|
11
|
+
block.children.size.must_equal(2)
|
12
|
+
block.children[0].must_be_instance_of(Veto::ValidatesBlock::CheckBlock)
|
13
|
+
end
|
14
|
+
|
15
|
+
def new_block(*args)
|
16
|
+
Veto::ValidatesBlock.new(*args)
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_block(*args)
|
20
|
+
Veto::ValidatesBlock.build(*args)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::ExactLengthCheck do
|
4
|
+
context 'when value is exact length' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcde')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 5)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is not exact length' do
|
16
|
+
it 'assigns errors' do
|
17
|
+
entity = stub(:name => 'abcde')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 4)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(['is not 4 characters'])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::ExactLengthCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::FormatCheck do
|
4
|
+
context 'when value matches format' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcde')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => /^[a-z]+$/)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value does not match format' do
|
16
|
+
it 'assigns errors' do
|
17
|
+
entity = stub(:name => 'abcde1')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => /^[a-z]+$/)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["is not valid"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::FormatCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::GreaterThanCheck do
|
4
|
+
context 'when value is greater than' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 24)
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 23)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is equal' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 23)
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 23)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["must be greater than 23"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is less than' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 22)
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 23)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["must be greater than 23"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::GreaterThanCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::GreaterThanOrEqualToCheck do
|
4
|
+
context 'when value is equal' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 23)
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 23)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is greater than' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 24)
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 23)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is less than' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 22)
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 23)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["must be greater than or equal to 23"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::GreaterThanOrEqualToCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::InclusionCheck do
|
4
|
+
context 'when value is included' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'cat')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :in => %w(cat dog mouse))
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is not included' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 'cat')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :in => %w(dog mouse))
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["is not in set: [\"dog\", \"mouse\"]"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::InclusionCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::IntegerCheck do
|
4
|
+
context 'when value is an integer' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 23)
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is not an integer' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 'abc')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["is not a number"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::IntegerCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::LengthRangeCheck do
|
4
|
+
context 'when value in length range' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcdef')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :in => 1..10)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value not in length range' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 'abcdef')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :in => 99..199)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["is too short or too long"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::LengthRangeCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::LessThanCheck do
|
4
|
+
context 'when value is less than' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 22)
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 23)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is equal' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 23)
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 23)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["must be less than 23"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is greater than' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 24)
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 23)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["must be less than 23"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::LessThanCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::LessThanOrEqualToCheck do
|
4
|
+
context 'when value is equal' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 23)
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 23)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is less than' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 22)
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 23)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is greater than' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 24)
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 23)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["must be less than or equal to 23"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::LessThanOrEqualToCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::MaxLengthCheck do
|
4
|
+
context 'when value is less than max length' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcdefgh')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 99)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value equals max length' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 'abcde')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 5)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is greater than max length' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 'abcdefgh')
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 5)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["is longer than 5 characters"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::MaxLengthCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::MethodCheck do
|
4
|
+
it 'delegates to method call on context' do
|
5
|
+
check = Veto::MethodCheck.new(:name)
|
6
|
+
entity = stub
|
7
|
+
context = stub(:name => true)
|
8
|
+
cco = stub(:entity => entity, :context => context)
|
9
|
+
|
10
|
+
context.expects(:name).with(entity).once
|
11
|
+
check.call(cco)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::MinLengthCheck do
|
4
|
+
context 'when value is greater than min length' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcdefgh')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name, :with => 5)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value equals min length' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => 'abcde')
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name, :with => 5)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_be_nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when value is less than min length' do
|
27
|
+
it 'assigns no errors' do
|
28
|
+
entity = stub(:name => 'abcdefgh')
|
29
|
+
errors = new_errors
|
30
|
+
check = new_check(:name, :with => 99)
|
31
|
+
|
32
|
+
check.call(new_cto(entity, errors))
|
33
|
+
errors[:name].must_equal(["is shorter than 99 characters"])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def new_cto(entity, errors)
|
38
|
+
stub(:entity => entity, :errors => errors)
|
39
|
+
end
|
40
|
+
|
41
|
+
def new_check(*args)
|
42
|
+
Veto::MinLengthCheck.new(*args)
|
43
|
+
end
|
44
|
+
|
45
|
+
def new_errors
|
46
|
+
Veto::Errors.new
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Veto::NotNullCheck do
|
4
|
+
context 'when value is not null' do
|
5
|
+
it 'assigns no errors' do
|
6
|
+
entity = stub(:name => 'abcde')
|
7
|
+
errors = new_errors
|
8
|
+
check = new_check(:name)
|
9
|
+
|
10
|
+
check.call(new_cto(entity, errors))
|
11
|
+
errors[:name].must_be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when value is null' do
|
16
|
+
it 'assigns no errors' do
|
17
|
+
entity = stub(:name => nil)
|
18
|
+
errors = new_errors
|
19
|
+
check = new_check(:name)
|
20
|
+
|
21
|
+
check.call(new_cto(entity, errors))
|
22
|
+
errors[:name].must_equal(["is not present"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_cto(entity, errors)
|
27
|
+
stub(:entity => entity, :errors => errors)
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_check(*args)
|
31
|
+
Veto::NotNullCheck.new(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_errors
|
35
|
+
Veto::Errors.new
|
36
|
+
end
|
37
|
+
end
|