whyvalidationssuckin96 1.0.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.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.md +121 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/doc/ActiveRecord/RecordInvalid.html +258 -0
- data/doc/ActiveRecord.html +93 -0
- data/doc/FalseClass.html +87 -0
- data/doc/NilClass.html +87 -0
- data/doc/Numeric.html +87 -0
- data/doc/Object.html +79 -0
- data/doc/String.html +87 -0
- data/doc/TrueClass.html +87 -0
- data/doc/WhyValidationsSuckIn96/ActiveRecord/InstanceMethods.html +156 -0
- data/doc/WhyValidationsSuckIn96/ActiveRecord.html +192 -0
- data/doc/WhyValidationsSuckIn96/AttributeBasedValidation.html +464 -0
- data/doc/WhyValidationsSuckIn96/SkippableValidation.html +194 -0
- data/doc/WhyValidationsSuckIn96/ValidatesAcceptance.html +254 -0
- data/doc/WhyValidationsSuckIn96/ValidatesAssociated.html +250 -0
- data/doc/WhyValidationsSuckIn96/ValidatesConfirmation.html +251 -0
- data/doc/WhyValidationsSuckIn96/ValidatesExclusion.html +388 -0
- data/doc/WhyValidationsSuckIn96/ValidatesFormat.html +387 -0
- data/doc/WhyValidationsSuckIn96/ValidatesInclusion.html +388 -0
- data/doc/WhyValidationsSuckIn96/ValidatesLength.html +469 -0
- data/doc/WhyValidationsSuckIn96/ValidatesNumericality.html +267 -0
- data/doc/WhyValidationsSuckIn96/ValidatesPresence.html +244 -0
- data/doc/WhyValidationsSuckIn96/ValidatesUniqueness.html +289 -0
- data/doc/WhyValidationsSuckIn96/Validation.html +934 -0
- data/doc/WhyValidationsSuckIn96/ValidationBuilder.html +391 -0
- data/doc/WhyValidationsSuckIn96/ValidationSupport/ClassMethods.html +249 -0
- data/doc/WhyValidationsSuckIn96/ValidationSupport/InstanceMethods.html +484 -0
- data/doc/WhyValidationsSuckIn96/ValidationSupport.html +168 -0
- data/doc/WhyValidationsSuckIn96.html +97 -0
- data/doc/_index.html +346 -0
- data/doc/class_list.html +293 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +23 -0
- data/doc/css/style.css +263 -0
- data/doc/file.README.html +173 -0
- data/doc/file_list.html +29 -0
- data/doc/index.html +173 -0
- data/doc/js/app.js +91 -0
- data/doc/js/full_list.js +39 -0
- data/doc/js/jquery.js +19 -0
- data/doc/method_list.html +462 -0
- data/doc/top-level-namespace.html +81 -0
- data/lib/whyvalidationssuckin96/attribute_based_validation.rb +46 -0
- data/lib/whyvalidationssuckin96/constants.rb +3 -0
- data/lib/whyvalidationssuckin96/ext/blank.rb +47 -0
- data/lib/whyvalidationssuckin96/macros/validates_acceptance.rb +36 -0
- data/lib/whyvalidationssuckin96/macros/validates_associated.rb +33 -0
- data/lib/whyvalidationssuckin96/macros/validates_confirmation.rb +40 -0
- data/lib/whyvalidationssuckin96/macros/validates_exclusion.rb +38 -0
- data/lib/whyvalidationssuckin96/macros/validates_format.rb +38 -0
- data/lib/whyvalidationssuckin96/macros/validates_inclusion.rb +38 -0
- data/lib/whyvalidationssuckin96/macros/validates_length.rb +98 -0
- data/lib/whyvalidationssuckin96/macros/validates_numericality.rb +56 -0
- data/lib/whyvalidationssuckin96/macros/validates_presence.rb +30 -0
- data/lib/whyvalidationssuckin96/macros.rb +9 -0
- data/lib/whyvalidationssuckin96/rails/active_record.rb +94 -0
- data/lib/whyvalidationssuckin96/rails/macros/validates_uniqueness.rb +87 -0
- data/lib/whyvalidationssuckin96/rails/macros.rb +1 -0
- data/lib/whyvalidationssuckin96/skippable_validation.rb +59 -0
- data/lib/whyvalidationssuckin96/validation.rb +88 -0
- data/lib/whyvalidationssuckin96/validation_builder.rb +56 -0
- data/lib/whyvalidationssuckin96/validation_support.rb +74 -0
- data/lib/whyvalidationssuckin96.rb +4 -0
- data/test/attribute_based_validation_test.rb +58 -0
- data/test/macros/validates_acceptance_test.rb +64 -0
- data/test/macros/validates_associated_test.rb +60 -0
- data/test/macros/validates_confirmation_test.rb +63 -0
- data/test/macros/validates_exclusion_test.rb +37 -0
- data/test/macros/validates_format_test.rb +43 -0
- data/test/macros/validates_inclusion_test.rb +37 -0
- data/test/macros/validates_length_test.rb +179 -0
- data/test/macros/validates_numericality_test.rb +129 -0
- data/test/macros/validates_presence_test.rb +31 -0
- data/test/rails/active_record_test.rb +187 -0
- data/test/rails/active_record_test_helper.rb +90 -0
- data/test/rails/macros/validates_uniqueness_test.rb +153 -0
- data/test/skippable_validation_test.rb +102 -0
- data/test/teststrap.rb +4 -0
- data/test/validation_builder_test.rb +62 -0
- data/test/validation_support_test.rb +209 -0
- data/test/validation_test.rb +101 -0
- data/whyvalidationssuckin96.gemspec +153 -0
- metadata +189 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates confirmation" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_confirmation_of')
|
8
|
+
|
9
|
+
context "with some default options" do
|
10
|
+
setup do
|
11
|
+
WhyValidationsSuckIn96::ValidatesConfirmation.new(Object.new, :attribute => :password)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have a message accessor with a default message" do
|
15
|
+
topic.message
|
16
|
+
end.equals("does not match the confirmation")
|
17
|
+
end # with some default options
|
18
|
+
|
19
|
+
context "validating an object" do
|
20
|
+
|
21
|
+
context "without a confirmation field" do
|
22
|
+
validatable = Class.new do
|
23
|
+
def password
|
24
|
+
"foo"
|
25
|
+
end
|
26
|
+
end.new
|
27
|
+
|
28
|
+
setup do
|
29
|
+
WhyValidationsSuckIn96::ValidatesConfirmation.new(validatable, :attribute => :password)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "raise if a confirmation field isn't available" do
|
33
|
+
topic.validates?
|
34
|
+
end.raises(NoMethodError)
|
35
|
+
|
36
|
+
end # without a confirmation field
|
37
|
+
|
38
|
+
context "with a confirmation field" do
|
39
|
+
validatable = OpenStruct.new(:password => "foo")
|
40
|
+
|
41
|
+
setup do
|
42
|
+
WhyValidationsSuckIn96::ValidatesConfirmation.new(validatable, :attribute => :password)
|
43
|
+
end
|
44
|
+
|
45
|
+
should "fail if field does not match the confirmation" do
|
46
|
+
validatable.password = "foo"
|
47
|
+
validatable.password_confirmation = "bleh"
|
48
|
+
topic.validates?
|
49
|
+
end.equals(false)
|
50
|
+
|
51
|
+
should "pass if the field matches the confirmation" do
|
52
|
+
validatable.password = validatable.password_confirmation = "foo"
|
53
|
+
topic.validates?
|
54
|
+
end
|
55
|
+
|
56
|
+
should "skip the validation if the confirmation is nil" do
|
57
|
+
validatable.password_confirmation = nil
|
58
|
+
topic.validates?
|
59
|
+
topic.has_run?
|
60
|
+
end.equals(false)
|
61
|
+
end # with a confirmation field
|
62
|
+
end # validating an object
|
63
|
+
end # validates confirmation
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates exclusion" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_exclusion_of')
|
8
|
+
|
9
|
+
should "raise if an :in option is not given" do
|
10
|
+
WhyValidationsSuckIn96::ValidatesExclusion.new(Object.new, :attribute => :colour)
|
11
|
+
end.raises(ArgumentError, "Collection to check for exclusion against should be specified with :in")
|
12
|
+
|
13
|
+
context "with some default options" do
|
14
|
+
setup do
|
15
|
+
WhyValidationsSuckIn96::ValidatesExclusion.new(Object.new, :attribute => :colour, :in => %w[1 2 3])
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have a message accessor with a default message" do
|
19
|
+
topic.message
|
20
|
+
end.equals("is in the excluded collection")
|
21
|
+
end # with some default options
|
22
|
+
|
23
|
+
context "validating an object" do
|
24
|
+
|
25
|
+
should "fail if the attribute is in the excluded set" do
|
26
|
+
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
27
|
+
:in => %w[red blue])
|
28
|
+
validation.validates?
|
29
|
+
end.equals(false)
|
30
|
+
|
31
|
+
should "pass if the attribute is out of the excluded set" do
|
32
|
+
validation = WhyValidationsSuckIn96::ValidatesExclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
33
|
+
:in => %w[green blue])
|
34
|
+
validation.validates?
|
35
|
+
end
|
36
|
+
end # validating an object
|
37
|
+
end # validates exclusion
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates format" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_format_of')
|
8
|
+
|
9
|
+
should "raise if a :with option is not given" do
|
10
|
+
WhyValidationsSuckIn96::ValidatesFormat.new(Object.new, :attribute => :colour)
|
11
|
+
end.raises(ArgumentError, "Regular expression to check against must be given as :with")
|
12
|
+
|
13
|
+
context "with some default options" do
|
14
|
+
setup do
|
15
|
+
WhyValidationsSuckIn96::ValidatesFormat.new(Object.new, :attribute => :colour, :with => /\d{3}/)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have a message accessor with a default message" do
|
19
|
+
topic.message
|
20
|
+
end.equals("does not match the given format")
|
21
|
+
end # with some default options
|
22
|
+
|
23
|
+
context "validating an object" do
|
24
|
+
|
25
|
+
should "fail if given attribute does not match the regular expression" do
|
26
|
+
validation = WhyValidationsSuckIn96::ValidatesFormat.new(OpenStruct.new(:number => "1"), :attribute => :number,
|
27
|
+
:with => /\d{3}/)
|
28
|
+
validation.validates?
|
29
|
+
end.equals(false)
|
30
|
+
|
31
|
+
should "pass if given attribute matches the regular expression" do
|
32
|
+
validation = WhyValidationsSuckIn96::ValidatesFormat.new(OpenStruct.new(:number => "111"), :attribute => :number,
|
33
|
+
:with => /\d{3}/)
|
34
|
+
validation.validates?
|
35
|
+
end
|
36
|
+
|
37
|
+
should "to_s the attribute when checking validity" do
|
38
|
+
validation = WhyValidationsSuckIn96::ValidatesFormat.new(OpenStruct.new(:number => 333), :attribute => :number,
|
39
|
+
:with => /\d{3}/)
|
40
|
+
validation.validates?
|
41
|
+
end
|
42
|
+
end # validating an object
|
43
|
+
end # validates format
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates inclusion" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_inclusion_of')
|
8
|
+
|
9
|
+
should "raise if an :in option is not given" do
|
10
|
+
WhyValidationsSuckIn96::ValidatesInclusion.new(Object.new, :attribute => :colour)
|
11
|
+
end.raises(ArgumentError, "Collection to check for inclusion against should be specified with :in")
|
12
|
+
|
13
|
+
context "with some default options" do
|
14
|
+
setup do
|
15
|
+
WhyValidationsSuckIn96::ValidatesInclusion.new(Object.new, :attribute => :colour, :in => %w[1 2 3])
|
16
|
+
end
|
17
|
+
|
18
|
+
should "have a message accessor with a default message" do
|
19
|
+
topic.message
|
20
|
+
end.equals("is not in the acceptable collection")
|
21
|
+
end # with some default options
|
22
|
+
|
23
|
+
context "validating an object" do
|
24
|
+
|
25
|
+
should "fail if the attribute is not in the given set" do
|
26
|
+
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
27
|
+
:in => %w[blue green])
|
28
|
+
validation.validates?
|
29
|
+
end.equals(false)
|
30
|
+
|
31
|
+
should "pass if the attribute is in the given set" do
|
32
|
+
validation = WhyValidationsSuckIn96::ValidatesInclusion.new(OpenStruct.new(:colour => "red"), :attribute => :colour,
|
33
|
+
:in => %w[red green blue])
|
34
|
+
validation.validates?
|
35
|
+
end
|
36
|
+
end # validating an object
|
37
|
+
end # validates inclusion
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates length" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_length_of')
|
8
|
+
|
9
|
+
should "raise if an no options are given" do
|
10
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text)
|
11
|
+
end.raises(ArgumentError, "Length options must be given as :minimum, :maximum, :is, or :in")
|
12
|
+
|
13
|
+
should "have a message accessor with a default message" do
|
14
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :is => 3).message
|
15
|
+
end.equals("does not meet the given length restrictions")
|
16
|
+
|
17
|
+
context "when specifying the :is option" do
|
18
|
+
should "not raise if solely :is is given" do
|
19
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :is => 3)
|
20
|
+
end
|
21
|
+
|
22
|
+
should "raise if :minimum is given" do
|
23
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :is => 3, :minimum => 4)
|
24
|
+
end.raises(ArgumentError, "Option :is can not be mixed with :minimum")
|
25
|
+
|
26
|
+
should "raise if :maximum is given" do
|
27
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :is => 3, :maximum => 2)
|
28
|
+
end.raises(ArgumentError, "Option :is can not be mixed with :maximum")
|
29
|
+
|
30
|
+
should "raise if :in is given" do
|
31
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :is => 3, :in => 5..10)
|
32
|
+
end.raises(ArgumentError, "Option :in can not be mixed with :is")
|
33
|
+
|
34
|
+
should "fail if the attribute length isn't exactly the given value" do
|
35
|
+
validation = WhyValidationsSuckIn96::ValidatesLength.new(OpenStruct.new(:text => "testing"), :attribute => :text, :is => 4)
|
36
|
+
validation.validates?
|
37
|
+
end.equals(false)
|
38
|
+
|
39
|
+
should "pass if the attribute length is exactly the given value" do
|
40
|
+
validation = WhyValidationsSuckIn96::ValidatesLength.new(OpenStruct.new(:text => "test"), :attribute => :text, :is => 4)
|
41
|
+
validation.validates?
|
42
|
+
end
|
43
|
+
end # when specifying the :is option
|
44
|
+
|
45
|
+
context "when specifying the :in option" do
|
46
|
+
|
47
|
+
should "not raise if solely :in is given" do
|
48
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :in => 1..10)
|
49
|
+
end
|
50
|
+
|
51
|
+
should "raise if :minimum is given" do
|
52
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :in => 1..10, :minimum => 4)
|
53
|
+
end.raises(ArgumentError, "Option :in can not be mixed with :minimum")
|
54
|
+
|
55
|
+
should "raise if :maximum is given" do
|
56
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :in => 1..10, :maximum => 4)
|
57
|
+
end.raises(ArgumentError, "Option :in can not be mixed with :maximum")
|
58
|
+
|
59
|
+
should "raise if :is is given" do
|
60
|
+
WhyValidationsSuckIn96::ValidatesLength.new(Object.new, :attribute => :text, :in => 1..10, :is => 3)
|
61
|
+
end.raises(ArgumentError, "Option :in can not be mixed with :is")
|
62
|
+
|
63
|
+
context "using a two dot range" do
|
64
|
+
validatable = OpenStruct.new(:text => "test")
|
65
|
+
|
66
|
+
setup do
|
67
|
+
WhyValidationsSuckIn96::ValidatesLength.new(validatable, :attribute => :text, :in => 2..10)
|
68
|
+
end
|
69
|
+
|
70
|
+
should "fail if the value is less than the beginning of the range" do
|
71
|
+
validatable.text = "a"
|
72
|
+
topic.validates?
|
73
|
+
end.equals(false)
|
74
|
+
|
75
|
+
should "fail if the value is more than the end of the range" do
|
76
|
+
validatable.text = "a" * 12
|
77
|
+
topic.validates?
|
78
|
+
end.equals(false)
|
79
|
+
|
80
|
+
should "pass if the value is exactly at the end of the range" do
|
81
|
+
validatable.text = "a" * 10
|
82
|
+
topic.validates?
|
83
|
+
end
|
84
|
+
|
85
|
+
should "pass if the value falls inside the range" do
|
86
|
+
validatable.text = "test"
|
87
|
+
topic.validates?
|
88
|
+
end
|
89
|
+
end # using a two dot range
|
90
|
+
|
91
|
+
context "using a three dot range" do
|
92
|
+
validatable = OpenStruct.new(:text => "test")
|
93
|
+
|
94
|
+
setup do
|
95
|
+
WhyValidationsSuckIn96::ValidatesLength.new(validatable, :attribute => :text, :in => 2...10)
|
96
|
+
end
|
97
|
+
|
98
|
+
should "fail if the value is less than the beginning of the range" do
|
99
|
+
validatable.text = "a"
|
100
|
+
topic.validates?
|
101
|
+
end.equals(false)
|
102
|
+
|
103
|
+
should "fail if the value is more than the end of the range" do
|
104
|
+
validatable.text = "a" * 12
|
105
|
+
topic.validates?
|
106
|
+
end.equals(false)
|
107
|
+
|
108
|
+
should "fail if the value is exactly at the end of the range" do
|
109
|
+
validatable.text = "a" * 10
|
110
|
+
topic.validates?
|
111
|
+
end.equals(false)
|
112
|
+
|
113
|
+
should "pass if the value falls inside the range" do
|
114
|
+
validatable.text = "test"
|
115
|
+
topic.validates?
|
116
|
+
end
|
117
|
+
end # using a three dot range
|
118
|
+
|
119
|
+
end # when specifying the :in option
|
120
|
+
|
121
|
+
context "when specifying the :minimum option" do
|
122
|
+
validatable = OpenStruct.new(:text => "test")
|
123
|
+
|
124
|
+
setup do
|
125
|
+
WhyValidationsSuckIn96::ValidatesLength.new(validatable, :attribute => :text, :minimum => 4)
|
126
|
+
end
|
127
|
+
|
128
|
+
should "pass if the attribute length is above the minimum value" do
|
129
|
+
validatable.text = "a" * 5
|
130
|
+
topic.validates?
|
131
|
+
end
|
132
|
+
|
133
|
+
should "pass if the attribute length is equal to the minimum value" do
|
134
|
+
validatable.text = "a" * 4
|
135
|
+
topic.validates?
|
136
|
+
end
|
137
|
+
|
138
|
+
should "fail if the attribute length is less than the minimum value" do
|
139
|
+
validatable.text = "a" * 2
|
140
|
+
topic.validates?
|
141
|
+
end.equals(false)
|
142
|
+
end # when specifying the :minimum option
|
143
|
+
|
144
|
+
context "when specifying the :maximum option" do
|
145
|
+
validatable = OpenStruct.new(:text => "test")
|
146
|
+
|
147
|
+
setup do
|
148
|
+
WhyValidationsSuckIn96::ValidatesLength.new(validatable, :attribute => :text, :maximum => 10)
|
149
|
+
end
|
150
|
+
|
151
|
+
should "pass if the attribute length is below the maximum value" do
|
152
|
+
validatable.text = "a" * 9
|
153
|
+
topic.validates?
|
154
|
+
end
|
155
|
+
|
156
|
+
should "pass if the attribute length is equal to the maximum value" do
|
157
|
+
validatable.text = "a" * 10
|
158
|
+
topic.validates?
|
159
|
+
end
|
160
|
+
|
161
|
+
should "fail if the attribute length is more than the maximum value" do
|
162
|
+
validatable.text = "a" * 15
|
163
|
+
topic.validates?
|
164
|
+
end.equals(false)
|
165
|
+
end # when specifying the :maximum option
|
166
|
+
|
167
|
+
context "when specifying the :minimum and :maximum options" do
|
168
|
+
validatable = OpenStruct.new(:text => "test")
|
169
|
+
|
170
|
+
setup do
|
171
|
+
WhyValidationsSuckIn96::ValidatesLength.new(validatable, :attribute => :text, :minimum => 3, :maximum => 10)
|
172
|
+
end
|
173
|
+
|
174
|
+
should "pass if the attribute length falls into the min/max range" do
|
175
|
+
validatable.text = "a" * 5
|
176
|
+
topic.validates?
|
177
|
+
end
|
178
|
+
end # when specifying the :minimum and :maximum options
|
179
|
+
end # validates length
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates numericality" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_numericality_of')
|
8
|
+
|
9
|
+
context "with some default options" do
|
10
|
+
setup do
|
11
|
+
WhyValidationsSuckIn96::ValidatesNumericality.new(Object.new, :attribute => :number)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have a message accessor with a default message" do
|
15
|
+
topic.message
|
16
|
+
end.equals("is not a numerical value")
|
17
|
+
end # with some default options
|
18
|
+
|
19
|
+
context "validating an object" do
|
20
|
+
|
21
|
+
context "with the default option of :only_integer being false" do
|
22
|
+
validatable = OpenStruct.new(:number => 123)
|
23
|
+
|
24
|
+
setup do
|
25
|
+
WhyValidationsSuckIn96::ValidatesNumericality.new(validatable, :attribute => :number)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "pass for an actual integer" do
|
29
|
+
validatable.number = 30000
|
30
|
+
topic.validates?
|
31
|
+
end
|
32
|
+
|
33
|
+
should "pass for a regular old integer string" do
|
34
|
+
validatable.number = "123"
|
35
|
+
topic.validates?
|
36
|
+
end
|
37
|
+
|
38
|
+
should "pass for a valid integer with spaces" do
|
39
|
+
validatable.number = "123 456"
|
40
|
+
topic.validates?
|
41
|
+
end
|
42
|
+
|
43
|
+
should "pass for a valid integer with commas" do
|
44
|
+
validatable.number = "123,456"
|
45
|
+
topic.validates?
|
46
|
+
end
|
47
|
+
|
48
|
+
should "pass for a valid integer with a positive sign" do
|
49
|
+
validatable.number = "+1234"
|
50
|
+
topic.validates?
|
51
|
+
end
|
52
|
+
|
53
|
+
should "pass for a valid integer with a negative sign" do
|
54
|
+
validatable.number "-1234"
|
55
|
+
topic.validates?
|
56
|
+
end
|
57
|
+
|
58
|
+
should "fail for an integer with letters" do
|
59
|
+
validatable.number = "a1234"
|
60
|
+
topic.validates?
|
61
|
+
end.equals(false)
|
62
|
+
|
63
|
+
should "fail for an integer with other punctuation" do
|
64
|
+
validatable.number = "!1234"
|
65
|
+
topic.validates?
|
66
|
+
end.equals(false)
|
67
|
+
|
68
|
+
should "pass for an actual float" do
|
69
|
+
validatable.number = 123.45
|
70
|
+
topic.validates?
|
71
|
+
end
|
72
|
+
|
73
|
+
should "pass for a regular old float" do
|
74
|
+
validatable.number = "123.456"
|
75
|
+
topic.validates?
|
76
|
+
end
|
77
|
+
|
78
|
+
should "pass for a valid float with spaces" do
|
79
|
+
validatable.number = "123 456.10"
|
80
|
+
topic.validates?
|
81
|
+
end
|
82
|
+
|
83
|
+
should "pass for a valid float with commas" do
|
84
|
+
validatable.number = "123,456.10"
|
85
|
+
topic.validates?
|
86
|
+
end
|
87
|
+
|
88
|
+
should "pass for a valid float with a positive sign" do
|
89
|
+
validatable.number = "+123456.10"
|
90
|
+
topic.validates?
|
91
|
+
end
|
92
|
+
|
93
|
+
should "pass for a valid float with a negative sign" do
|
94
|
+
validatable.number = "-123456.10"
|
95
|
+
topic.validates?
|
96
|
+
end
|
97
|
+
|
98
|
+
should "fail for a float with letters other than the exponent" do
|
99
|
+
validatable.number = "123z456.10"
|
100
|
+
topic.validates?
|
101
|
+
end.equals(false)
|
102
|
+
|
103
|
+
should "fail for a float with other punctuation" do
|
104
|
+
validatable.number = "$123456.10"
|
105
|
+
topic.validates?
|
106
|
+
end.equals(false)
|
107
|
+
|
108
|
+
end # with the default option of :only_integer being false
|
109
|
+
|
110
|
+
context "with the :only_integer option being true" do
|
111
|
+
validatable = OpenStruct.new(:number => 123)
|
112
|
+
|
113
|
+
setup do
|
114
|
+
WhyValidationsSuckIn96::ValidatesNumericality.new(validatable, :attribute => :number, :only_integer => true)
|
115
|
+
end
|
116
|
+
|
117
|
+
should "not allow float values" do
|
118
|
+
validatable.number = 123.456
|
119
|
+
topic.validates?
|
120
|
+
end.equals(false)
|
121
|
+
|
122
|
+
should "allow integer values" do
|
123
|
+
validatable.number = 123
|
124
|
+
topic.validates?
|
125
|
+
end
|
126
|
+
|
127
|
+
end # with the :only_integer option being true
|
128
|
+
end # validating an object
|
129
|
+
end # validates numericality
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "validates presence" do
|
4
|
+
|
5
|
+
should "add a validation macro" do
|
6
|
+
WhyValidationsSuckIn96::ValidationBuilder.instance_methods
|
7
|
+
end.includes('validates_presence_of')
|
8
|
+
|
9
|
+
context "validating an object" do
|
10
|
+
validatable = OpenStruct.new(:message => "foo")
|
11
|
+
|
12
|
+
setup do
|
13
|
+
WhyValidationsSuckIn96::ValidatesPresence.new(validatable, :attribute => :message)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have a message accessor with a default message" do
|
17
|
+
topic.message
|
18
|
+
end.equals("is not present")
|
19
|
+
|
20
|
+
should "fail if the attribute is blank" do
|
21
|
+
validatable.message = ""
|
22
|
+
topic.validates?
|
23
|
+
end.equals(false)
|
24
|
+
|
25
|
+
should "pass if the attribute is non blank" do
|
26
|
+
validatable.message = "blah"
|
27
|
+
topic.validates?
|
28
|
+
end
|
29
|
+
|
30
|
+
end # validating an object
|
31
|
+
end # validates presence
|