jnunemaker-validatable 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/README.rdoc +116 -0
  2. data/Rakefile +29 -0
  3. data/VERSION.yml +5 -0
  4. data/lib/validatable.rb +28 -0
  5. data/lib/validatable/child_validation.rb +15 -0
  6. data/lib/validatable/errors.rb +90 -0
  7. data/lib/validatable/included_validation.rb +9 -0
  8. data/lib/validatable/macros.rb +314 -0
  9. data/lib/validatable/object_extension.rb +21 -0
  10. data/lib/validatable/requireable.rb +26 -0
  11. data/lib/validatable/understandable.rb +31 -0
  12. data/lib/validatable/validatable_class_methods.rb +87 -0
  13. data/lib/validatable/validatable_instance_methods.rb +106 -0
  14. data/lib/validatable/validations/validates_acceptance_of.rb +14 -0
  15. data/lib/validatable/validations/validates_associated.rb +13 -0
  16. data/lib/validatable/validations/validates_confirmation_of.rb +23 -0
  17. data/lib/validatable/validations/validates_each.rb +14 -0
  18. data/lib/validatable/validations/validates_exclusion_of.rb +17 -0
  19. data/lib/validatable/validations/validates_format_of.rb +16 -0
  20. data/lib/validatable/validations/validates_inclusion_of.rb +17 -0
  21. data/lib/validatable/validations/validates_length_of.rb +30 -0
  22. data/lib/validatable/validations/validates_numericality_of.rb +27 -0
  23. data/lib/validatable/validations/validates_presence_of.rb +17 -0
  24. data/lib/validatable/validations/validates_true_for.rb +13 -0
  25. data/lib/validatable/validations/validation_base.rb +91 -0
  26. data/test/functional/test_validatable.rb +519 -0
  27. data/test/functional/test_validates_acceptance_of.rb +16 -0
  28. data/test/functional/test_validates_associated.rb +41 -0
  29. data/test/functional/test_validates_confirmation_of.rb +56 -0
  30. data/test/functional/test_validates_each.rb +14 -0
  31. data/test/functional/test_validates_exclusion_of.rb +29 -0
  32. data/test/functional/test_validates_format_of.rb +34 -0
  33. data/test/functional/test_validates_inclusion_of.rb +29 -0
  34. data/test/functional/test_validates_length_of.rb +64 -0
  35. data/test/functional/test_validates_numericality_of.rb +57 -0
  36. data/test/functional/test_validates_presence_of.rb +16 -0
  37. data/test/functional/test_validates_true_for.rb +27 -0
  38. data/test/test_helper.rb +32 -0
  39. data/test/unit/test_errors.rb +70 -0
  40. data/test/unit/test_understandable.rb +19 -0
  41. data/test/unit/test_validatable.rb +38 -0
  42. data/test/unit/test_validates_acceptance_of.rb +45 -0
  43. data/test/unit/test_validates_associated.rb +29 -0
  44. data/test/unit/test_validates_confirmation_of.rb +76 -0
  45. data/test/unit/test_validates_exclusion_of.rb +23 -0
  46. data/test/unit/test_validates_format_of.rb +44 -0
  47. data/test/unit/test_validates_inclusion_of.rb +23 -0
  48. data/test/unit/test_validates_length_of.rb +80 -0
  49. data/test/unit/test_validates_numericality_of.rb +76 -0
  50. data/test/unit/test_validates_presence_of.rb +35 -0
  51. data/test/unit/test_validates_true_for.rb +29 -0
  52. data/test/unit/test_validation_base.rb +52 -0
  53. metadata +133 -0
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect [:c, :b, :a] do
5
+ a = Class.new do
6
+ include Validatable::Understandable
7
+ understands :a
8
+ end
9
+ b = Class.new(a) do
10
+ include Validatable::Understandable
11
+ understands :b
12
+ end
13
+ c = Class.new(b) do
14
+ include Validatable::Understandable
15
+ understands :c
16
+ end
17
+ c.all_understandings
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = stub_everything(:should_validate? => true, :attribute => "attribute", :level => 1, :groups => [])
6
+ klass = Class.new do
7
+ include Validatable
8
+ validations << validation
9
+ end
10
+ klass.new.valid?
11
+ end
12
+
13
+ expect true do
14
+ klass = Class.new do
15
+ include Validatable
16
+ end
17
+ instance = klass.new
18
+ instance.errors.add(:attribute, "message")
19
+ instance.valid?
20
+ instance.errors.empty?
21
+ end
22
+
23
+ expect false do
24
+ klass = Class.new do
25
+ include Validatable
26
+ end
27
+ klass.validation_keys_include?("anything")
28
+ end
29
+
30
+ expect true do
31
+ validation = stub_everything(:key => "key")
32
+ klass = Class.new do
33
+ include Validatable
34
+ validations << validation
35
+ end
36
+ klass.validation_keys_include?("key")
37
+ end
38
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect true do
5
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
6
+ instance = stub(:acceptance=>'1')
7
+ validation.valid?(instance)
8
+ end
9
+
10
+ expect true do
11
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
12
+ instance = stub(:acceptance=>'t')
13
+ validation.valid?(instance)
14
+ end
15
+
16
+ expect true do
17
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
18
+ instance = stub(:acceptance=>'true')
19
+ validation.valid?(instance)
20
+ end
21
+
22
+ expect true do
23
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance, :allow_nil => true
24
+ instance = stub(:acceptance=>nil)
25
+ validation.valid?(instance)
26
+ end
27
+
28
+ expect true do
29
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance, :allow_blank => true
30
+ instance = stub(:acceptance=>'')
31
+ validation.valid?(instance)
32
+ end
33
+
34
+ expect false do
35
+ validation = Validatable::ValidatesAcceptanceOf.new stub_everything, :acceptance
36
+ instance = stub(:acceptance=>'0')
37
+ validation.valid?(instance)
38
+ end
39
+
40
+ expect true do
41
+ options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil}
42
+ Validatable::ValidatesAcceptanceOf.new(stub_everything, :test).must_understand(options)
43
+ end
44
+
45
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ parent = stub('parent', :child => stub(:valid? => false))
7
+ validation = Validatable::ValidatesAssociated.new stub_everything, :child
8
+ validation.valid?(parent)
9
+ end
10
+
11
+ expect true do
12
+ parent = stub('parent', :child => stub(:valid? => true))
13
+ validation = Validatable::ValidatesAssociated.new stub_everything, :child
14
+ validation.valid?(parent)
15
+ end
16
+
17
+ expect false do
18
+ parent = stub('parent', :children => [stub(:valid? => false)])
19
+ validation = Validatable::ValidatesAssociated.new stub_everything, :children
20
+ validation.valid?(parent)
21
+ end
22
+
23
+ expect true do
24
+ parent = stub('parent', :children => [stub(:valid? => true)])
25
+ validation = Validatable::ValidatesAssociated.new stub_everything, :children
26
+ validation.valid?(parent)
27
+ end
28
+
29
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect true do
6
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username
7
+ instance = stub(:username=>"username", :username_confirmation=>"username")
8
+ validation.valid?(instance)
9
+ end
10
+
11
+ expect false do
12
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username
13
+ instance = stub(:username=>"username", :username_confirmation=>"usessrname")
14
+ validation.valid?(instance)
15
+ end
16
+
17
+ expect true do
18
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :allow_nil => true
19
+ instance = stub(:username=>"username", :username_confirmation=>nil)
20
+ validation.valid?(instance)
21
+ end
22
+
23
+ expect true do
24
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :allow_blank => true
25
+ instance = stub(:username=>"username", :username_confirmation=>'')
26
+ validation.valid?(instance)
27
+ end
28
+
29
+ expect true do
30
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
31
+ instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
32
+ validation.valid?(instance)
33
+ end
34
+
35
+ expect false do
36
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
37
+ instance = stub(:username=>"username", :username_confirmation=>"USERNAME")
38
+ validation.valid?(instance)
39
+ end
40
+
41
+ expect false do
42
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
43
+ validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
44
+ end
45
+
46
+ expect false do
47
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
48
+ validation.valid?(stub(:username => 'something', :username_confirmation => nil))
49
+ end
50
+
51
+ expect true do
52
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => true
53
+ validation.valid?(stub(:username => nil, :username_confirmation => nil))
54
+ end
55
+
56
+ expect false do
57
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
58
+ validation.valid?(stub(:username => nil, :username_confirmation => 'something'))
59
+ end
60
+
61
+ expect false do
62
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
63
+ validation.valid?(stub(:username => 'something', :username_confirmation => nil))
64
+ end
65
+
66
+ expect true do
67
+ validation = Validatable::ValidatesConfirmationOf.new stub_everything, :username, :case_sensitive => false
68
+ validation.valid?(stub(:username => nil, :username_confirmation => nil))
69
+ end
70
+
71
+ expect true do
72
+ options = { :message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :case_sensitive => nil }
73
+ Validatable::ValidatesConfirmationOf.new(stub_everything, :test).must_understand(options)
74
+ end
75
+
76
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
6
+ validation.valid?(stub(:state => 'in'))
7
+ end
8
+
9
+ expect true do
10
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
11
+ validation.valid?(stub(:state => 'foo'))
12
+ end
13
+
14
+ expect true do
15
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
+ validation.valid?(stub(:state => ''))
17
+ end
18
+
19
+ expect true do
20
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
+ validation.valid?(stub(:state => nil))
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/
7
+ validation.valid?(stub_everything)
8
+ end
9
+
10
+ expect true do
11
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/
12
+ validation.valid?(stub(:name=>"book"))
13
+ end
14
+
15
+ expect true do
16
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/, :allow_nil => true
17
+ validation.valid?(stub(:name=>nil))
18
+ end
19
+
20
+ expect true do
21
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :name, :with => /book/, :allow_blank => true
22
+ validation.valid?(stub(:name=>''))
23
+ end
24
+
25
+ expect true do
26
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :age, :with => /14/
27
+ validation.valid?(stub(:age=>14))
28
+ end
29
+
30
+ expect ArgumentError do
31
+ validation = Validatable::ValidatesFormatOf.new stub_everything, :age
32
+ end
33
+
34
+ expect true do
35
+ options = [:message, :if, :times, :level, :groups, :with, :key]
36
+ Validatable::ValidatesFormatOf.new(stub_everything, :test, options.to_blank_options_hash).must_understand(options.to_blank_options_hash)
37
+ end
38
+
39
+ expect true do
40
+ options = [:with]
41
+ Validatable::ValidatesFormatOf.new(stub_everything, :name, options.to_blank_options_hash).requires(options.to_blank_options_hash)
42
+ end
43
+
44
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect true do
5
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
6
+ validation.valid?(stub(:state => 'in'))
7
+ end
8
+
9
+ expect false do
10
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
11
+ validation.valid?(stub(:state => 'foo'))
12
+ end
13
+
14
+ expect true do
15
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
16
+ validation.valid?(stub(:state => ''))
17
+ end
18
+
19
+ expect true do
20
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
21
+ validation.valid?(stub(:state => nil))
22
+ end
23
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+ expect false do
5
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :maximum => 8
6
+ validation.valid?(stub(:username=>"usernamefdfd"))
7
+ end
8
+
9
+ expect false do
10
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :minimum => 2
11
+ instance = stub(:username=>"u")
12
+ validation.valid?(instance)
13
+ end
14
+
15
+ expect true do
16
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :minimum => 2, :maximum => 8
17
+ instance = stub(:username=>"udfgdf")
18
+ validation.valid?(instance)
19
+ end
20
+
21
+ expect false do
22
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :is => 2
23
+ instance = stub(:username=>"u")
24
+ validation.valid?(instance)
25
+ end
26
+
27
+ expect true do
28
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :is => 2
29
+ instance = stub(:username=>"uu")
30
+ validation.valid?(instance)
31
+ end
32
+
33
+ expect true do
34
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
35
+ instance = stub(:username => "aa")
36
+ validation.valid?(instance)
37
+ end
38
+
39
+ expect false do
40
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
41
+ instance = stub(:username => "a")
42
+ validation.valid?(instance)
43
+ end
44
+
45
+ expect true do
46
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
47
+ instance = stub(:username => "aaaa")
48
+ validation.valid?(instance)
49
+ end
50
+
51
+ expect false do
52
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
53
+ instance = stub(:username => "aaaaa")
54
+ validation.valid?(instance)
55
+ end
56
+
57
+ expect false do
58
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4
59
+ instance = stub(:username => nil)
60
+ validation.valid?(instance)
61
+ end
62
+
63
+ expect true do
64
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4, :allow_nil => true
65
+ instance = stub(:username => nil)
66
+ validation.valid?(instance)
67
+ end
68
+
69
+ expect true do
70
+ validation = Validatable::ValidatesLengthOf.new stub_everything, :username, :within => 2..4, :allow_blank => true
71
+ instance = stub(:username => '')
72
+ validation.valid?(instance)
73
+ end
74
+
75
+ expect true do
76
+ options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
77
+ Validatable::ValidatesLengthOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
78
+ end
79
+
80
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ Expectations do
4
+
5
+ expect false do
6
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :nothing
7
+ instance = stub(:nothing => nil)
8
+ validation.valid?(instance)
9
+ end
10
+
11
+ expect true do
12
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
13
+ instance = stub(:some_int => 50)
14
+ validation.valid?(instance)
15
+ end
16
+
17
+ expect false do
18
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
19
+ instance = stub(:some_int => 0, :some_int_before_typecast => 'string')
20
+ validation.valid?(instance)
21
+ end
22
+
23
+ expect true do
24
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int
25
+ instance = stub(:some_int => 21, :some_int_before_typecast => '21')
26
+ validation.valid?(instance)
27
+ end
28
+
29
+ expect true do
30
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int, :allow_nil => true
31
+ instance = stub(:some_int => nil)
32
+ validation.valid?(instance)
33
+ end
34
+
35
+ expect true do
36
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_int, :allow_blank => true
37
+ instance = stub(:some_int => '')
38
+ validation.valid?(instance)
39
+ end
40
+
41
+ expect true do
42
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_decimal
43
+ instance = stub(:some_decimal => 1.23)
44
+ validation.valid?(instance)
45
+ end
46
+
47
+ expect false do
48
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_decimal, :only_integer => true
49
+ instance = stub(:some_decimal => 1.23)
50
+ validation.valid?(instance)
51
+ end
52
+
53
+ expect true do
54
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_negative_number, :only_integer => true
55
+ instance = stub(:some_negative_number => "-1")
56
+ validation.valid?(instance)
57
+ end
58
+
59
+ expect false do
60
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :some_non_numeric
61
+ instance = stub(:some_non_numeric => "50F")
62
+ validation.valid?(instance)
63
+ end
64
+
65
+ expect false do
66
+ validation = Validatable::ValidatesNumericalityOf.new stub_everything, :multiple_dots
67
+ instance = stub(:multiple_dots => "50.0.0")
68
+ validation.valid?(instance)
69
+ end
70
+
71
+ expect true do
72
+ options = [:message, :if, :times, :level, :groups, :only_integer]
73
+ Validatable::ValidatesNumericalityOf.new(stub_everything, :test).must_understand(options.to_blank_options_hash)
74
+ end
75
+
76
+ end