valid_attribute 0.2.1 → 1.0.0.rc.1
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/README.markdown +84 -53
- data/lib/valid_attribute/matcher.rb +7 -1
- data/lib/valid_attribute/version.rb +1 -1
- data/spec/user.rb +2 -2
- data/spec/valid_attribute_spec.rb +2 -2
- data/valid_attribute.gemspec +2 -2
- metadata +8 -8
data/README.markdown
CHANGED
@@ -1,79 +1,110 @@
|
|
1
1
|
# ValidAttribute #
|
2
2
|
|
3
|
-
ValidAttribute is a minimalist
|
3
|
+
ValidAttribute is a minimalist matcher for validation BDD.
|
4
|
+
|
5
|
+
Supported ORMs
|
6
|
+
|
7
|
+
* ActiveModel based (ActiveRecord >= 3.0, Mongoid >= 2.0, MongoMapper >= 0.9
|
8
|
+
* ActiveRecord <= 2.3
|
9
|
+
* DataMapper
|
10
|
+
* Custom (with compatible API, see below)
|
4
11
|
|
5
12
|
## Installation ##
|
6
13
|
|
7
14
|
If you're using `RSpec` just add the `valid_attribute` to your `Gemfile`
|
8
15
|
|
9
|
-
|
16
|
+
```ruby
|
17
|
+
gem 'valid_attribute'
|
18
|
+
```
|
10
19
|
|
11
20
|
Then add it to your `spec_helper.rb`
|
12
21
|
|
13
|
-
|
22
|
+
```ruby
|
23
|
+
require 'valid_attribute'
|
24
|
+
```
|
14
25
|
|
15
26
|
or if you're using `Test::Unit`, you must use [Thoughtbot](http://thoughtbot.com)'s [shoulda-context](https://github.com/thoughtbot/shoulda-context)
|
16
27
|
|
17
|
-
|
18
|
-
|
28
|
+
```ruby
|
29
|
+
# Gemfile
|
30
|
+
gem 'shoulda-context'
|
19
31
|
|
20
|
-
|
21
|
-
|
22
|
-
|
32
|
+
# test_helper.rb
|
33
|
+
require 'shoulda-context'
|
34
|
+
require 'valid_attribute'
|
35
|
+
```
|
23
36
|
|
24
37
|
## Usage ##
|
25
38
|
|
26
39
|
Instead of having validation specific matchers `ValidAttribute` only cares if the attribute is valid under certain circumstances
|
27
40
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
41
|
+
```ruby
|
42
|
+
class User
|
43
|
+
include ActiveModel::Validations
|
44
|
+
|
45
|
+
attr_accessor :email, :name, :password
|
46
|
+
|
47
|
+
validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
|
48
|
+
validates :name, :length => { :minimum => 5 }
|
49
|
+
validates :password, :confirmation => true, :presence => true
|
50
|
+
end
|
51
|
+
|
52
|
+
# RSpec
|
53
|
+
describe User do
|
54
|
+
# The .when method can take any number of values that you want to pass
|
55
|
+
it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
|
56
|
+
it { should_not have_valid(:email).when('fail', 123) }
|
57
|
+
it { should have_valid(:name).when('TestName') }
|
58
|
+
it { should_not have_valid(:name).when('Test') }
|
59
|
+
|
60
|
+
# Because 'should' works off the the 'subject' in RSpec we can set other values if necessary for a given validation test
|
61
|
+
describe 'password' do
|
62
|
+
before { subject.password_confirmation = 'password' }
|
63
|
+
it { should have_valid(:password).when('password') }
|
64
|
+
it { should_not have_valid(:password).when(nil) }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# TestUnit
|
69
|
+
require 'should/context'
|
70
|
+
class UserTest < Test::Unit::TestCase
|
71
|
+
# The .when method can take any number of values that you want to pass
|
72
|
+
should have_valid(:email).when('test@test.com', 'test+spam@gmail.com')
|
73
|
+
should_not have_valid(:email).when('fail', 123)
|
74
|
+
should have_valid(:name).when('TestName')
|
75
|
+
should_not have_valid(:name).when('Test')
|
76
|
+
|
77
|
+
# Because 'shoulda-context' works off the the 'subject' we can set other values if necessary for a given validation test
|
78
|
+
context 'password' do
|
79
|
+
subject { User.new(:password_confirmation => 'password') }
|
80
|
+
should have_valid(:password).when('password')
|
81
|
+
should_not have_valid(:password).when(nil)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Custom Models ##
|
72
87
|
|
73
88
|
Your model should respond to the following methods:
|
74
89
|
|
75
90
|
* `valid?` - only used to generate errors on the model
|
76
|
-
* `errors` - should be a
|
91
|
+
* `errors` - should be a Hash of attributes that have the invalid attributes as keys.
|
92
|
+
|
93
|
+
The following would be a compatible (albeit unrealistic) model:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class User
|
97
|
+
attr_accessor :name
|
98
|
+
|
99
|
+
def errors
|
100
|
+
{:name => ["can't be blank"]}
|
101
|
+
end
|
102
|
+
|
103
|
+
def valid?
|
104
|
+
false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
77
108
|
|
78
109
|
Other than that everything should work!
|
79
110
|
|
@@ -55,7 +55,8 @@ module ValidAttribute
|
|
55
55
|
values.each do |value|
|
56
56
|
subject.send("#{attr}=", value)
|
57
57
|
subject.valid?
|
58
|
-
|
58
|
+
|
59
|
+
if invalid_attribute?(subject, attr)
|
59
60
|
self.failed_values << value
|
60
61
|
else
|
61
62
|
self.passed_values << value
|
@@ -63,6 +64,11 @@ module ValidAttribute
|
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
67
|
+
def invalid_attribute?(subject, attr)
|
68
|
+
errors = subject.errors[attr]
|
69
|
+
errors.respond_to?(:empty?) ? !errors.empty? : !!errors
|
70
|
+
end
|
71
|
+
|
66
72
|
def quote_values(values)
|
67
73
|
values.map { |value| value.inspect }.join(', ')
|
68
74
|
end
|
data/spec/user.rb
CHANGED
@@ -39,7 +39,7 @@ describe 'ValidAttribute' do
|
|
39
39
|
context 'data is invalid' do
|
40
40
|
before do
|
41
41
|
@user.stubs(:valid?).returns(false)
|
42
|
-
@user.stubs(:errors).returns({:name => []})
|
42
|
+
@user.stubs(:errors).returns({:name => ["can't be blank"]})
|
43
43
|
@matcher = @should.have_valid(:name).when(:abc, nil)
|
44
44
|
end
|
45
45
|
|
@@ -62,7 +62,7 @@ describe 'ValidAttribute' do
|
|
62
62
|
context 'data is valid then invalid' do
|
63
63
|
before do
|
64
64
|
@user.stubs(:valid?).returns(true).then.returns(false)
|
65
|
-
@user.stubs(:errors).returns({}).then.returns({:name => []})
|
65
|
+
@user.stubs(:errors).returns({}).then.returns({:name => ["can't be blank"]})
|
66
66
|
@matcher = @should.have_valid(:name).when('abc', 123)
|
67
67
|
end
|
68
68
|
|
data/valid_attribute.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Brian Cardarella"]
|
10
10
|
s.email = ["bcardarella@gmail.com"]
|
11
11
|
s.homepage = "https://github.com/bcardarella/valid_attribute"
|
12
|
-
s.summary = %q{Minimalist validation matcher
|
13
|
-
s.description = %q{Minimalist validation matcher
|
12
|
+
s.summary = %q{Minimalist validation matcher}
|
13
|
+
s.description = %q{Minimalist validation matcher}
|
14
14
|
|
15
15
|
s.rubyforge_project = "valid_attribute"
|
16
16
|
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valid_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version: 0.
|
4
|
+
prerelease: 6
|
5
|
+
version: 1.0.0.rc.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Cardarella
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-06-06 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -46,7 +46,7 @@ dependencies:
|
|
46
46
|
version: "0"
|
47
47
|
type: :development
|
48
48
|
version_requirements: *id003
|
49
|
-
description: Minimalist validation matcher
|
49
|
+
description: Minimalist validation matcher
|
50
50
|
email:
|
51
51
|
- bcardarella@gmail.com
|
52
52
|
executables: []
|
@@ -92,16 +92,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
none: false
|
94
94
|
requirements:
|
95
|
-
- - "
|
95
|
+
- - ">"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version:
|
97
|
+
version: 1.3.1
|
98
98
|
requirements: []
|
99
99
|
|
100
100
|
rubyforge_project: valid_attribute
|
101
|
-
rubygems_version: 1.
|
101
|
+
rubygems_version: 1.3.8
|
102
102
|
signing_key:
|
103
103
|
specification_version: 3
|
104
|
-
summary: Minimalist validation matcher
|
104
|
+
summary: Minimalist validation matcher
|
105
105
|
test_files:
|
106
106
|
- spec/spec_helper.rb
|
107
107
|
- spec/test_unit_spec.rb
|