boof-not-naughty 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,267 @@
1
+ require "#{ File.dirname(__FILE__) }/spec_helper.rb"
2
+
3
+ ::NotNaughty::Validation.load(
4
+ :acceptance, :confirmation, :format,
5
+ :length, :numericality, :presence
6
+ )
7
+
8
+ describe subject::LengthValidation do
9
+
10
+ before(:each) { @receiver, @errors = mock('Receiver'), mock('Errors') }
11
+
12
+ it "should return the 'precise' block" do
13
+ validation = subject::LengthValidation.new :is => 8, :within => 10..12
14
+
15
+ probe = mock 'Probe', :length => 8, :nil? => false
16
+ validation.call @receiver, :probe, probe
17
+
18
+ @receiver.should_receive(:errors).and_return(@errors)
19
+ @errors.should_receive(:add).with(:probe, an_instance_of(String))
20
+
21
+ probe = mock 'Probe', :length => 11, :nil? => false
22
+ validation.call @receiver, :probe, probe
23
+ end
24
+ it "should return the 'range' block" do
25
+ validation = subject::LengthValidation.
26
+ new :within => 10..12, :maximum => 9
27
+
28
+ probe = mock 'Probe', :length => 10, :nil? => false
29
+ validation.call @receiver, :probe, probe
30
+
31
+ @receiver.should_receive(:errors).and_return(@errors)
32
+ @errors.should_receive(:add).with(:probe, an_instance_of(String))
33
+
34
+ probe = mock 'Probe', :length => 9, :nil? => false
35
+ validation.call @receiver, :probe, probe
36
+ end
37
+ it "should return the 'maximum' block" do
38
+ validation = subject::LengthValidation.
39
+ new :maximum => 9
40
+
41
+ probe = mock 'Probe', :length => 9, :nil? => false
42
+ validation.call @receiver, :probe, probe
43
+
44
+ @receiver.should_receive(:errors).and_return(@errors)
45
+ @errors.should_receive(:add).with(:probe, an_instance_of(String))
46
+
47
+ probe = mock 'Probe', :length => 10, :nil? => false
48
+ validation.call @receiver, :probe, probe
49
+ end
50
+ it "should return the 'minimum' block" do
51
+ validation = subject::LengthValidation.
52
+ new :minimum => 9
53
+
54
+ probe = mock 'Probe', :length => 9, :nil? => false
55
+ validation.call @receiver, :probe, probe
56
+
57
+ @receiver.should_receive(:errors).and_return(@errors)
58
+ @errors.should_receive(:add).with(:probe, an_instance_of(String))
59
+
60
+ probe = mock 'Probe', :length => 8, :nil? => false
61
+ validation.call @receiver, :probe, probe
62
+ end
63
+ it "should raise an ArgumentError" do
64
+ lambda { subject::LengthValidation.new }.
65
+ should raise_error(ArgumentError)
66
+ end
67
+
68
+ end
69
+
70
+ LengthExample = Struct.new(:name).extend(subject)
71
+ describe LengthExample do
72
+
73
+ before(:each) { @example = LengthExample.clone }
74
+
75
+ it "should always allow nil " do
76
+ @example.validates_length_of :name, :is => 1, :allow_nil => false
77
+ @example.new(nil).should be_valid
78
+ @example.new('').should_not be_valid
79
+ @example.new('a').should be_valid
80
+ @example.new('ab').should_not be_valid
81
+ end
82
+ it "should allow blank" do
83
+ @example.validates_length_of :name, :is => 1, :allow_blank => true
84
+ @example.new(nil).should be_valid
85
+ @example.new('').should be_valid
86
+ @example.new('a').should be_valid
87
+ @example.new('ab').should_not be_valid
88
+ end
89
+
90
+ end
91
+
92
+ FormatExample = Struct.new(:email).extend(subject)
93
+ describe FormatExample do
94
+
95
+ before(:each) { @example = FormatExample.clone }
96
+
97
+ it "claims to match 99.9% of all e-mail addresses out there..." do
98
+ # Regexp was taken from: http://www.regular-expressions.info/email.html
99
+ @example.validates_format_of :email,
100
+ :with => :email
101
+ @example.new('"Foo Bar" <foo@bar.com>').should be_valid
102
+ @example.new('foo@bar.com').should be_valid
103
+ @example.new('foobarcom').should_not be_valid
104
+ @example.new(nil).should_not be_valid
105
+ @example.new('').should_not be_valid
106
+ end
107
+ it "should allow nil e-mail addresses" do
108
+ @example.validates_format_of :email, :allow_nil => true,
109
+ :with => /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
110
+ @example.new('"Foo Bar" <foo@bar.com>').should be_valid
111
+ @example.new('foo@bar.com').should be_valid
112
+ @example.new('foobarcom').should_not be_valid
113
+ @example.new(nil).should be_valid
114
+ @example.new('').should_not be_valid
115
+ end
116
+ it "should allow blank e-mail addresses" do
117
+ @example.validates_format_of :email, :allow_blank => true,
118
+ :with => /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
119
+ @example.new('"Foo Bar" <foo@bar.com>').should be_valid
120
+ @example.new('foo@bar.com').should be_valid
121
+ @example.new('foobarcom').should_not be_valid
122
+ @example.new(nil).should be_valid
123
+ @example.new('').should be_valid
124
+ end
125
+ it "should raise an ArgumentError if format does not respond to :match" do
126
+ lambda { @example.validates_format_of :email }.
127
+ should raise_error(ArgumentError)
128
+ lambda { @example.validates_format_of :email, :with => 1 }.
129
+ should raise_error(ArgumentError)
130
+ lambda { @example.validates_format_of :email, :with => '' }.
131
+ should_not raise_error(ArgumentError)
132
+ lambda { @example.validates_format_of :email, :with => // }.
133
+ should_not raise_error(ArgumentError)
134
+ end
135
+
136
+ end
137
+
138
+ PresenceExample = Struct.new(:name).extend(subject)
139
+ describe PresenceExample do
140
+
141
+ before(:each) { @example = PresenceExample.clone }
142
+
143
+ it "should be present" do
144
+ @example.validates_presence_of :name
145
+ @example.new(0).should be_valid
146
+ @example.new([0]).should be_valid
147
+ @example.new('0').should be_valid
148
+ end
149
+ it "should not be present" do
150
+ @example.validates_presence_of :name
151
+ @example.new(nil).should_not be_valid
152
+ @example.new([]).should_not be_valid
153
+ @example.new('').should_not be_valid
154
+ end
155
+
156
+ end
157
+
158
+ AcceptanceExample = Struct.new(:conditions).extend(subject)
159
+ describe AcceptanceExample do
160
+
161
+ before(:each) { @example = AcceptanceExample.clone }
162
+
163
+ it "should accept '1' and allows nil by default" do
164
+ @example.validates_acceptance_of :conditions
165
+ @example.new(nil).should be_valid
166
+ @example.new('').should_not be_valid
167
+ @example.new(true).should_not be_valid
168
+ @example.new(false).should_not be_valid
169
+ @example.new('0').should_not be_valid
170
+ @example.new('1').should be_valid
171
+ end
172
+ it "should accept true and allows nil by default" do
173
+ @example.validates_acceptance_of :conditions, :accept => true
174
+ @example.new(nil).should be_valid
175
+ @example.new('').should_not be_valid
176
+ @example.new(true).should be_valid
177
+ @example.new(false).should_not be_valid
178
+ @example.new('0').should_not be_valid
179
+ @example.new('1').should_not be_valid
180
+ end
181
+ it "should accept '1' and disallows nil" do
182
+ @example.validates_acceptance_of :conditions, :accept => true,
183
+ :allow_nil => false
184
+
185
+ @example.new(nil).should_not be_valid
186
+ @example.new('').should_not be_valid
187
+ @example.new(true).should be_valid
188
+ @example.new(false).should_not be_valid
189
+ @example.new('0').should_not be_valid
190
+ @example.new('1').should_not be_valid
191
+ end
192
+ it "should accept '1' and allow blank" do
193
+ @example.validates_acceptance_of :conditions, :accept => true,
194
+ :allow_blank => true
195
+
196
+ @example.new(nil).should be_valid
197
+ @example.new('').should be_valid
198
+ @example.new(true).should be_valid
199
+ @example.new(false).should be_valid
200
+ @example.new('0').should_not be_valid
201
+ @example.new('1').should_not be_valid
202
+ end
203
+
204
+ end
205
+
206
+ ConfirmationExample = Struct.new(:name, :name_confirmation).extend(subject)
207
+ describe ConfirmationExample do
208
+
209
+ before(:each) { @example = ConfirmationExample.clone }
210
+
211
+ it "should be confirmed without allowing neither :nil nor :blank" do
212
+ @example.validates_confirmation_of :name
213
+
214
+ @example.new(nil, 'foo').should_not be_valid
215
+ @example.new('', 'foo').should_not be_valid
216
+
217
+ @example.new('foo', 'foo').should be_valid
218
+ @example.new('foo', 'bar').should_not be_valid
219
+ end
220
+ it "should be confirmed with allowing :nil" do
221
+ @example.validates_confirmation_of :name, :allow_nil => true
222
+
223
+ @example.new(nil, 'foo').should be_valid
224
+ @example.new('', 'foo').should_not be_valid
225
+
226
+ @example.new('foo', 'foo').should be_valid
227
+ @example.new('foo', 'bar').should_not be_valid
228
+ end
229
+ it "should be confirmed with allowing :blank" do
230
+ @example.validates_confirmation_of :name, :allow_blank => true
231
+
232
+ @example.new(nil, 'foo').should be_valid
233
+ @example.new('', 'foo').should be_valid
234
+
235
+ @example.new('foo', 'foo').should be_valid
236
+ @example.new('foo', 'bar').should_not be_valid
237
+ end
238
+
239
+ end
240
+
241
+ NumericalityExample = Struct.new(:weight).extend(subject)
242
+ describe NumericalityExample do
243
+
244
+ before(:each) { @example = NumericalityExample.clone }
245
+
246
+ it "should be matched with number pattern" do
247
+ @example.validates_numericality_of :weight
248
+
249
+ @example.new('-123.56').should be_valid
250
+
251
+ @example.new('+123').should be_valid
252
+ @example.new('-123').should be_valid
253
+ @example.new('123').should be_valid
254
+ @example.new('abc').should_not be_valid
255
+ end
256
+ it "should be matched with integer pattern" do
257
+ @example.validates_numericality_of :weight, :only_integer => true
258
+
259
+ @example.new('-123.45').should_not be_valid
260
+
261
+ @example.new('+123').should be_valid
262
+ @example.new('-123').should be_valid
263
+ @example.new('123').should be_valid
264
+ @example.new('abc').should_not be_valid
265
+ end
266
+
267
+ end
@@ -0,0 +1,132 @@
1
+ require "#{ File.dirname(__FILE__) }/spec_helper.rb"
2
+
3
+ describe subject::Validator, 'with default state' do
4
+
5
+ before(:each) { @validator = subject::Validator.new }
6
+
7
+ it "should have atleast the default state" do
8
+ @validator.instance_variable_get(:@states).keys.should include(:default)
9
+ end
10
+ it "should shoult return default state" do
11
+ @validator.get_state(nil).name.should == :default
12
+ end
13
+ it "should have an errors handler" do
14
+ @validator.error_handler.should be_kind_of(NotNaughty::ErrorHandler)
15
+ end
16
+ it "should clone the error handler" do
17
+ rnd = rand(10)
18
+
19
+ @validator.error_handler.should_receive(:clone).and_return(rnd)
20
+ @validator.clone.error_handler.should == rnd
21
+ end
22
+
23
+ end
24
+
25
+ describe NotNaughty::Validator, 'with custom states' do
26
+
27
+ before(:each) do
28
+ @states = [:create, :update]
29
+ @validator = subject::Validator.new(*@states)
30
+ end
31
+
32
+ it "should assign states dynamically" do
33
+ @validator.states.keys.should include(*@states)
34
+ end
35
+ it "should have an initial state" do
36
+ @validator.instance_variable_get(:@initial_state).name.
37
+ should == @states[0]
38
+ end
39
+ it "should add validations to all states" do
40
+ @validator.add_validation :firstname, :lastname
41
+
42
+ @validator.states.each do |name, state|
43
+ state.validations.should include(:firstname, :lastname)
44
+ end
45
+ end
46
+ it "should add validations to :create state" do
47
+ @validator.add_validation :firstname, :lastname, :on => :create
48
+
49
+ @validator.states[:create].validations.keys.
50
+ should include(:firstname, :lastname)
51
+ @validator.states[:update].validations.keys.
52
+ should_not include(:firstname, :lastname)
53
+ end
54
+ it "should add validations to :create and :update states" do
55
+ @validator.add_validation :firstname, :lastname, :on => [:create, :update]
56
+
57
+ @validator.states.each do |name, state|
58
+ state.validations.should include(:firstname, :lastname)
59
+ end
60
+ end
61
+ it "should return initial state" do
62
+ @validator.get_state(nil).name.should == :create
63
+ end
64
+ it "should not have validations" do
65
+ @validator.should_not have_validations
66
+ end
67
+ it "should have validations" do
68
+ @validator.add_validation :firstname, :lastname
69
+ @validator.should have_validations
70
+ end
71
+ it "should have validations on initial state" do
72
+ @validator.add_validation :firstname, :lastname, :on => :create
73
+ @validator.should have_validations('')
74
+ end
75
+ it "should not have validations on initial state" do
76
+ @validator.add_validation :firstname, :lastname, :on => :update
77
+ @validator.should_not have_validations('')
78
+ end
79
+ it "should send attributes to probe if invoked" do
80
+ block = proc {|o, a, v|}
81
+
82
+ probe = mock 'Probe'
83
+ probe.should_receive(:send).with(:firstname)
84
+ probe.should_receive(:send).with(:lastname)
85
+
86
+ @validator.add_validation :firstname, :lastname, &block
87
+ @validator.invoke probe
88
+ end
89
+ it "should call validations with object, attribute and value if invoked" do
90
+ block = proc {|o, a, v|}
91
+
92
+ probe = mock 'Probe'
93
+ value = mock 'Value'
94
+ probe.stub!(:send).and_return(value)
95
+
96
+ @validator.add_validation :firstname, :lastname, &block
97
+ @validator.get_state.validations
98
+ @validator.invoke probe
99
+ end
100
+ it "should clone states as well" do
101
+ validator_clone = @validator.clone
102
+ validator_clone.states.length == @validator.states.length
103
+ validator_clone.states.should_not != @validator.states
104
+ end
105
+
106
+ end
107
+
108
+ describe NotNaughty::Validator::State do
109
+
110
+ before(:each) { @state = NotNaughty::Validator::State.new }
111
+
112
+ it "should initialize with name and validations" do
113
+ @state.name.should == :default
114
+ @state.validations.should be_an_instance_of(Hash)
115
+
116
+ @state = NotNaughty::Validator::State.new :foo
117
+ @state.name.should == :foo
118
+ end
119
+ it "should add validation" do
120
+ @state.add_validation(:firstname, :lastname, :on => :default) {|o, a, v|}
121
+ @state.validations.keys.should include(:firstname, :lastname)
122
+ end
123
+ it "should return validation for an attribute" do
124
+ @state.validations[:foo] = :bar
125
+ @state[:foo].should == :bar
126
+ end
127
+ it "should have validations" do
128
+ @state.validations[:foo] = [:bar]
129
+ @state.should have_validations
130
+ end
131
+
132
+ end
@@ -0,0 +1,36 @@
1
+ require "#{ File.dirname(__FILE__) }/spec_helper.rb"
2
+
3
+ describe subject::Violation do
4
+
5
+ before(:each) { @errors = subject::Violation.new }
6
+
7
+ it "should add errors in a hash" do
8
+ @errors.add :attribute, 'Message'
9
+
10
+ @errors.instance_variable_get(:@errors)[:attribute].
11
+ should include('Message')
12
+ end
13
+ it "should return full messages" do
14
+ @errors.add :attribute, 'Message #{"%s".capitalize}'
15
+
16
+ @errors.full_messages.
17
+ should == ['Message Attribute']
18
+ end
19
+ it "should be kind of RuntimeError" do
20
+ @errors.should be_kind_of(RuntimeError)
21
+ end
22
+ it "should have methods delegated" do
23
+ probe = mock 'Probe'
24
+ methods = [:empty?, :clear, :[], :each]
25
+
26
+ methods.each { |m| probe.should_receive m }
27
+ @errors.instance_variable_set :@errors, probe
28
+
29
+ methods.each { |m| @errors.send m }
30
+ end
31
+ it "should return evaluated errors messages" do
32
+ @errors.add :attribute, 'Message #{"%s".capitalize}'
33
+ @errors.on(:attribute).should == ['Message Attribute']
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boof-not-naughty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.2
5
+ platform: ruby
6
+ authors:
7
+ - "Florian A\xC3\x9Fmann"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-31 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubytree
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.5.2
23
+ version:
24
+ description: Heavily armed validation framework.
25
+ email: boof@monkey-patch.me
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ - CHANGELOG.rdoc
33
+ - COPYING
34
+ files:
35
+ - COPYING
36
+ - README.rdoc
37
+ - Rakefile
38
+ - lib/core_extensions.rb
39
+ - lib/not_naughty
40
+ - lib/not_naughty/class_methods.rb
41
+ - lib/not_naughty/error_handler.rb
42
+ - lib/not_naughty/instance_methods.rb
43
+ - lib/not_naughty/validation.rb
44
+ - lib/not_naughty/validations
45
+ - lib/not_naughty/validations/acceptance_validation.rb
46
+ - lib/not_naughty/validations/confirmation_validation.rb
47
+ - lib/not_naughty/validations/format_validation.rb
48
+ - lib/not_naughty/validations/length_validation.rb
49
+ - lib/not_naughty/validations/numericality_validation.rb
50
+ - lib/not_naughty/validations/presence_validation.rb
51
+ - lib/not_naughty/validator.rb
52
+ - lib/not_naughty/violation.rb
53
+ - lib/not_naughty.rb
54
+ - CHANGELOG.rdoc
55
+ has_rdoc: true
56
+ homepage: http://monkey-patch.me/p/not-naughty
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --quiet
60
+ - --title
61
+ - "NotNaughty: The Validation Framework"
62
+ - --opname
63
+ - index.html
64
+ - --inline-source
65
+ - --line-numbers
66
+ - --main
67
+ - README.rdoc
68
+ - --inline-source
69
+ - --charset
70
+ - utf-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.6
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.2.0
89
+ signing_key:
90
+ specification_version: 2
91
+ summary: Heavily armed validation framework.
92
+ test_files:
93
+ - spec/class_methods_spec.rb
94
+ - spec/error_handler_spec.rb
95
+ - spec/not_naughty_spec.rb
96
+ - spec/rcov.opts
97
+ - spec/spec.opts
98
+ - spec/spec_helper.rb
99
+ - spec/validation_spec.rb
100
+ - spec/validations_spec.rb
101
+ - spec/validator_spec.rb
102
+ - spec/violation_spec.rb