valid_attribute 0.0.2 → 0.0.3

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/HISTORY CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.0.3
2
+ should_not fix for when the validation key doesn't exist yet
3
+ removed .message method. Not good BDD
4
+ will test all values instead of stopping at the first invalid value
5
+
1
6
  == 0.0.2
2
7
  Removed default value, allways required to pass value
3
8
  Changed #with to #when
data/README.markdown CHANGED
@@ -21,7 +21,7 @@ Instead of having validation specific matchers ValidAttribute only cares if the
21
21
 
22
22
  attr_accessor :email, :name, :password
23
23
 
24
- validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => 'invalid email format' }
24
+ validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
25
25
  validates :name, :length => { :minimum => 5 }
26
26
  validates :password, :confirmation => true, :presence => true
27
27
  end
@@ -29,7 +29,7 @@ Instead of having validation specific matchers ValidAttribute only cares if the
29
29
  describe User do
30
30
  # The .when method can take any number of values that you want to pass
31
31
  it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
32
- it { should_not have_valid(:email).when('fail', 123).message('invalid email format') }
32
+ it { should_not have_valid(:email).when('fail', 123) }
33
33
  it { should have_valid(:name).when('TestName')
34
34
  it { should_not have_valid(:name).when('Test')
35
35
 
@@ -46,8 +46,7 @@ Instead of having validation specific matchers ValidAttribute only cares if the
46
46
  As long as your model responds to the following methods:
47
47
 
48
48
  * valid? - only used to generate errors on the model
49
- * errors - should be a Hash of errors, the keys being the attribute with errors and the value for each key being an array of error messages
50
- * model_name - used in the failure messages. Class level method
49
+ * errors - should be a collection of attributes that have validation errors.
51
50
 
52
51
  Other than that everything should work!
53
52
 
@@ -1,3 +1,3 @@
1
1
  module ValidAttribute
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,8 +4,9 @@ module ValidAttribute
4
4
  #
5
5
  # examples:
6
6
  # it { should have_valid(:name).when('Brian') }
7
- # it { should_not have_valid(:name).message("can't be blank") }
7
+ # it { should_not have_valid(:name).when(nil) }
8
8
  # it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
9
+ # it { should_not have_valid(:email).when('abc', 123) }
9
10
  #
10
11
  # @param [Symbol]
11
12
  #
@@ -15,10 +16,12 @@ module ValidAttribute
15
16
  end
16
17
 
17
18
  class ValidAttributeMatcher
18
- attr_accessor :attr, :values, :validation_message, :subject, :failed_value
19
+ attr_accessor :attr, :values, :subject, :failed_values, :passed_values
19
20
 
20
21
  def initialize(attr)
21
- self.attr = attr
22
+ self.attr = attr
23
+ self.failed_values = []
24
+ self.passed_values = []
22
25
  end
23
26
 
24
27
  def when(*values)
@@ -26,30 +29,19 @@ module ValidAttribute
26
29
  self
27
30
  end
28
31
 
29
- def message(message)
30
- self.validation_message = message
31
- self
32
- end
33
-
34
- def negative_failure_message
35
- failure_message = " expected #{subject.class.model_name}##{attr} to not accept a value of #{value_message}"
36
-
37
- if validation_message
38
- failure_message = "#{failure_message} with a message of '#{validation_message}'"
39
- end
40
-
41
- failure_message
42
- end
43
-
44
32
  def failure_message
45
- " expected #{subject.class.model_name}##{attr} to accept a value of #{value_message}"
33
+ if failed_values.size == 1
34
+ " expected #{subject.class.model_name}##{attr} to accept the value: #{quote_values(failed_values)}"
35
+ else
36
+ " expected #{subject.class.model_name}##{attr} to accept the values: #{quote_values(failed_values)}"
37
+ end
46
38
  end
47
39
 
48
- def value_message
49
- if failed_value.is_a?(String)
50
- "'#{failed_value}'"
40
+ def negative_failure_message
41
+ if passed_values.size == 1
42
+ " expected #{subject.class.model_name}##{attr} to not accept the value: #{quote_values(passed_values)}"
51
43
  else
52
- failed_value
44
+ " expected #{subject.class.model_name}##{attr} to not accept the values: #{quote_values(passed_values)}"
53
45
  end
54
46
  end
55
47
 
@@ -63,18 +55,20 @@ module ValidAttribute
63
55
  values.each do |value|
64
56
  subject.send("#{attr}=", value)
65
57
  subject.valid?
66
-
67
- if subject.errors.include?(attr)
68
- self.failed_value = value
69
- if validation_message
70
- return !subject.errors[attr].include?(validation_message)
71
- else
72
- return false
73
- end
58
+ if subject.errors.key?(attr)
59
+ self.failed_values << value
60
+ else
61
+ self.passed_values << value
74
62
  end
75
63
  end
76
64
 
77
- true
65
+ failed_values.empty?
66
+ end
67
+
68
+ private
69
+
70
+ def quote_values(values)
71
+ values.map { |value| value.is_a?(String) ? "'#{value}'" : value }.join(', ')
78
72
  end
79
73
 
80
74
  end
@@ -6,6 +6,7 @@ end
6
6
 
7
7
  class User
8
8
  attr_accessor :name
9
+ attr_accessor :email
9
10
 
10
11
  def errors
11
12
  @error ||= {}
@@ -28,92 +29,51 @@ describe 'ValidAttribute' do
28
29
  @user.stubs(:valid?).returns(true)
29
30
  end
30
31
 
31
- it 'passes with no value set' do
32
- matcher = @should.have_valid(:name).when(nil)
33
- matcher.matches?(@user).should be_true
34
- end
35
-
36
- it 'passes with values set' do
32
+ it 'passes with values' do
37
33
  matcher = @should.have_valid(:name).when('Brian', 'Stephanie')
38
34
  matcher.matches?(@user).should be_true
39
35
  end
40
36
  end
41
37
 
42
- describe 'invalid data' do
38
+ describe 'data is first invalid then invalid' do
43
39
  before do
44
- @user.stubs(:valid?).returns(false)
45
- @user.errors[:name] = ['is not valid']
46
- end
47
-
48
- it 'returns false when no message passed' do
49
- matcher = @should.have_valid(:name).when(nil)
50
- matcher.matches?(@user).should be_false
51
- end
52
-
53
- it 'returns true when wrong message is passed' do
54
- matcher = @should.have_valid(:name).when(nil).message('wrong message')
55
- matcher.matches?(@user).should_not be_false
56
- end
57
-
58
- it 'returns false when correct message is passed' do
59
- matcher = @should.have_valid(:name).when(nil).message('is not valid')
60
- matcher.matches?(@user).should be_false
61
- end
62
- end
63
-
64
- describe 'data is first valid then invalid' do
65
- before do
66
- @user.stubs(:valid?).returns(true).then.returns(false)
67
- @user.errors[:name] = ['is not valid']
40
+ @user.stubs(:valid?).returns(false).then.returns(true).then.returns(false)
41
+ @user.stubs(:errors).returns({:name => []}).then.returns({})
42
+ @matcher = @should.have_valid(:name).when('abc', 123)
43
+ @result = @matcher.matches?(@user)
68
44
  end
69
45
 
70
46
  it 'returns false' do
71
- matcher = @should.have_valid(:name).when('true', 'false')
72
- matcher.matches?(@user).should be_false
47
+ @result.should be_false
73
48
  end
74
- end
75
49
 
76
- describe 'failure message' do
77
- before do
78
- @user.stubs(:valid?).returns(false)
79
- @user.errors[:name] = ['is not valid']
50
+ it 'has a failure message of the failed values' do
51
+ @matcher.failure_message.should == " expected User#name to accept the value: 'abc'"
80
52
  end
81
53
 
82
- it 'has a message for string values' do
83
- matcher = @should.have_valid(:name).when('Brian')
84
- matcher.matches?(@user)
85
- matcher.failure_message.should == " expected User#name to accept a value of 'Brian'"
86
- end
87
-
88
- it 'has a message for non string values' do
89
- matcher = @should.have_valid(:name).when(123)
90
- matcher.matches?(@user)
91
- matcher.failure_message.should == " expected User#name to accept a value of 123"
54
+ it 'has a negative failure message of the passed values' do
55
+ @matcher.negative_failure_message.should == " expected User#name to not accept the value: 123"
92
56
  end
93
57
  end
94
58
 
95
- describe 'negative failure message' do
59
+ describe 'data is first invalid then valid then invalid then valid' do
96
60
  before do
97
- @user.stubs(:valid?).returns(false)
98
- @user.errors[:name] = ['is not valid']
61
+ @user.stubs(:valid?).returns(false).then.returns(true).then.returns(false)
62
+ @user.stubs(:errors).returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({})
63
+ @matcher = @should.have_valid(:name).when('abc', 123, 456, 'def')
64
+ @result = @matcher.matches?(@user)
99
65
  end
100
66
 
101
- it 'has a message for string values' do
102
- matcher = @should.have_valid(:name).when('Brian')
103
- matcher.matches?(@user)
104
- matcher.negative_failure_message.should == " expected User#name to not accept a value of 'Brian'"
67
+ it 'returns false' do
68
+ @result.should be_false
105
69
  end
106
70
 
107
- it 'has a message for non string values' do
108
- matcher = @should.have_valid(:name).when(123)
109
- matcher.matches?(@user)
110
- matcher.negative_failure_message.should == " expected User#name to not accept a value of 123"
71
+ it 'has a failure message of the failed values' do
72
+ @matcher.failure_message.should == " expected User#name to accept the values: 'abc', 456"
111
73
  end
112
74
 
113
- it 'includes the validation message' do
114
- matcher = @should.have_valid(:name).when('Brian').message('is not valid')
115
- matcher.matches?(@user)
116
- matcher.negative_failure_message.should == " expected User#name to not accept a value of 'Brian' with a message of 'is not valid'"
75
+ it 'has a negative failure message of the passed values' do
76
+ @matcher.negative_failure_message.should == " expected User#name to not accept the values: 123, 'def'"
117
77
  end
118
78
  end
119
79
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: valid_attribute
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
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-04-05 00:00:00 -04:00
13
+ date: 2011-04-12 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency