valid_attribute 1.1.0 → 1.2.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/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - rbx
5
+ - rbx-2.0
6
+ - ree
7
+ - jruby
data/HISTORY CHANGED
@@ -1,3 +1,6 @@
1
+ == 1.2.0
2
+ MiniTest::Spec support [wojtekmach]
3
+
1
4
  == 1.1.0
2
5
  #when is not necessary [davisre]
3
6
 
data/README.markdown CHANGED
@@ -1,5 +1,7 @@
1
1
  # ValidAttribute #
2
2
 
3
+ [![Build Status](http://travis-ci.org/bcardarella/valid_attribute.png)](http://travis-ci.org/bcardarella/valid_attribute)
4
+
3
5
  ValidAttribute is a minimalist matcher for validation BDD.
4
6
 
5
7
  Supported ORMs
@@ -34,6 +36,17 @@ require 'shoulda-context'
34
36
  require 'valid_attribute'
35
37
  ```
36
38
 
39
+ If you want to you use it with `MiniTest::Spec` you can use either `shoulda-context` (see above) or [minitest-matchers](https://github.com/zenspider/minitest-matchers):
40
+
41
+ ```ruby
42
+ # Gemfile
43
+ gem 'minitest-matchers'
44
+
45
+ # test_helper.rb
46
+ require 'minitest/matchers'
47
+ require 'valid_attribute'
48
+ ```
49
+
37
50
  ## Usage ##
38
51
 
39
52
  Instead of having validation specific matchers `ValidAttribute` only cares if the attribute is valid under certain circumstances
@@ -87,6 +100,27 @@ class UserTest < Test::Unit::TestCase
87
100
  # Using .when is optional. Without it, the existing value is examined.
88
101
  should_not have_valid(:email)
89
102
  end
103
+
104
+ # Minitest::Matchers
105
+ require 'minitest/matchers'
106
+ describe User do
107
+ subject { User.new }
108
+
109
+ # The .when method can take any number of values that you want to pass
110
+ must { have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
111
+ wont { have_valid(:email).when('fail', 123) }
112
+ must { have_valid(:name).when('TestName') }
113
+ wont { have_valid(:name).when('Test') }
114
+
115
+ describe 'password' do
116
+ subject { User.new(:password_confirmation => 'password') }
117
+ must { have_valid(:password).when('password') }
118
+ wont { have_valid(:password).when(nil) }
119
+ end
120
+
121
+ # Using .when is optional. Without it, the existing value is examined.
122
+ wont have_valid(:email)
123
+ end
90
124
  ```
91
125
 
92
126
  ## Custom Models ##
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler'
2
+ require 'rspec/core/rake_task'
2
3
  Bundler::GemHelper.install_tasks
4
+
5
+ RSpec::Core::RakeTask.new('default') do |t|
6
+ t.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
@@ -7,6 +7,8 @@ end
7
7
 
8
8
  if defined?(RSpec)
9
9
  require 'valid_attribute/rspec'
10
+ elsif defined?(MiniTest::Matchers)
11
+ require 'valid_attribute/minitest'
10
12
  else
11
13
  require 'valid_attribute/test_unit'
12
14
  end
@@ -28,7 +28,7 @@ module ValidAttribute
28
28
  end
29
29
 
30
30
  def description
31
- "be valid when: #{quote_values(values)}"
31
+ "be valid when #{attr} is: #{quote_values(values)}"
32
32
  end
33
33
 
34
34
  def matches?(subject)
@@ -48,36 +48,32 @@ module ValidAttribute
48
48
  self.failed_values = []
49
49
  self.passed_values = []
50
50
 
51
- if values
52
- check_specified_values
53
- else
54
- check_existing_value
55
- end
56
- end
57
-
58
- def check_specified_values
59
51
  values.each do |value|
60
52
  check_value value
61
53
  end
62
54
  end
63
55
 
64
- def check_existing_value
65
- check_value subject.send("#{attr}")
56
+ def values
57
+ unless @values
58
+ @values = [subject.send(attr)]
59
+ end
60
+ @values
66
61
  end
67
62
 
68
63
  def check_value(value)
69
- subject.send("#{attr}=", value)
70
- subject.valid?
64
+ cloned_subject = subject.clone
65
+ cloned_subject.send("#{attr}=", value)
66
+ cloned_subject.valid?
71
67
 
72
- if invalid_attribute?(subject, attr)
68
+ if invalid_attribute?(cloned_subject, attr)
73
69
  self.failed_values << value
74
70
  else
75
71
  self.passed_values << value
76
72
  end
77
73
  end
78
74
 
79
- def invalid_attribute?(subject, attr)
80
- errors = subject.errors[attr]
75
+ def invalid_attribute?(cloned_subject, attr)
76
+ errors = cloned_subject.errors[attr]
81
77
  errors.respond_to?(:empty?) ? !errors.empty? : !!errors
82
78
  end
83
79
 
@@ -0,0 +1,3 @@
1
+ class MiniTest::Spec
2
+ extend ::ValidAttribute::Method
3
+ end
@@ -1,3 +1,3 @@
1
1
  module ValidAttribute
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'minitest/spec'
3
+ require 'valid_attribute/minitest'
4
+
5
+ describe 'MiniTest::Spec' do
6
+ it '.have_valid' do
7
+ MiniTest::Spec.have_valid(:name).should be_instance_of(ValidAttribute::Matcher)
8
+ end
9
+ end
10
+
@@ -29,7 +29,7 @@ describe 'ValidAttribute' do
29
29
  end
30
30
 
31
31
  describe 'messages' do
32
- it '#negative_failue_message' do
32
+ it '#negative_failure_message' do
33
33
  @matcher.matches?(@user)
34
34
  @matcher.negative_failure_message.should == " expected User#name to reject the values: \"abc\", 123"
35
35
  end
@@ -52,7 +52,7 @@ describe 'ValidAttribute' do
52
52
  end
53
53
 
54
54
  describe 'messages' do
55
- it '#failue_message' do
55
+ it '#failure_message' do
56
56
  @matcher.matches?(@user)
57
57
  @matcher.failure_message.should == " expected User#name to accept the values: :abc, nil"
58
58
  end
@@ -86,12 +86,27 @@ describe 'ValidAttribute' do
86
86
  end
87
87
 
88
88
  it '#description' do
89
- @matcher.description.should == "be valid when: \"abc\", 123"
89
+ @matcher.description.should == "be valid when name is: \"abc\", 123"
90
90
  end
91
91
  end
92
92
  end
93
93
 
94
94
  context 'no values are specified with .when' do
95
+ context 'data is not set' do
96
+ before do
97
+ @user.stubs(:valid?).returns(true)
98
+ @user.stubs(:name).returns(nil)
99
+ @matcher = @should.have_valid(:name)
100
+ end
101
+
102
+ subject do
103
+ @matcher.matches?(@user)
104
+ @matcher
105
+ end
106
+
107
+ its(:description) { should eq('be valid when name is: nil') }
108
+ end
109
+
95
110
  context 'data is valid' do
96
111
  before do
97
112
  @user.stubs(:valid?).returns(true)
@@ -108,7 +123,7 @@ describe 'ValidAttribute' do
108
123
  end
109
124
 
110
125
  describe 'messages' do
111
- it '#negative_failue_message' do
126
+ it '#negative_failure_message' do
112
127
  @matcher.matches?(@user)
113
128
  @matcher.negative_failure_message.should == " expected User#name to reject the value: :abc"
114
129
  end
@@ -132,7 +147,7 @@ describe 'ValidAttribute' do
132
147
  end
133
148
 
134
149
  describe 'messages' do
135
- it '#failue_message' do
150
+ it '#failure_message' do
136
151
  @matcher.matches?(@user)
137
152
  @matcher.failure_message.should == " expected User#name to accept the value: :abc"
138
153
  end
@@ -21,8 +21,11 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'bourne'
24
+ s.add_development_dependency 'minitest-matchers'
24
25
  if RUBY_VERSION >= '1.9'
25
- s.add_development_dependency 'ruby-debug19'
26
+ if RUBY_VERSION <= '1.9.2'
27
+ s.add_development_dependency 'ruby-debug19'
28
+ end
26
29
  else
27
30
  s.add_development_dependency 'ruby-debug'
28
31
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: valid_attribute
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.0
5
+ version: 1.2.0
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-07-08 00:00:00 -04:00
13
+ date: 2011-09-05 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -36,7 +36,7 @@ dependencies:
36
36
  type: :development
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
- name: ruby-debug19
39
+ name: minitest-matchers
40
40
  prerelease: false
41
41
  requirement: &id003 !ruby/object:Gem::Requirement
42
42
  none: false
@@ -46,6 +46,17 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: ruby-debug19
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
49
60
  description: Minimalist validation matcher
50
61
  email:
51
62
  - bcardarella@gmail.com
@@ -59,6 +70,7 @@ files:
59
70
  - .gitignore
60
71
  - .rspec
61
72
  - .rvmrc
73
+ - .travis.yml
62
74
  - Gemfile
63
75
  - HISTORY
64
76
  - README.markdown
@@ -66,9 +78,11 @@ files:
66
78
  - lib/valid_attribute.rb
67
79
  - lib/valid_attribute/matcher.rb
68
80
  - lib/valid_attribute/method.rb
81
+ - lib/valid_attribute/minitest.rb
69
82
  - lib/valid_attribute/rspec.rb
70
83
  - lib/valid_attribute/test_unit.rb
71
84
  - lib/valid_attribute/version.rb
85
+ - spec/minitest_spec.rb
72
86
  - spec/spec_helper.rb
73
87
  - spec/test_unit_spec.rb
74
88
  - spec/user.rb
@@ -98,11 +112,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
112
  requirements: []
99
113
 
100
114
  rubyforge_project: valid_attribute
101
- rubygems_version: 1.5.3
115
+ rubygems_version: 1.3.9.2
102
116
  signing_key:
103
117
  specification_version: 3
104
118
  summary: Minimalist validation matcher
105
119
  test_files:
120
+ - spec/minitest_spec.rb
106
121
  - spec/spec_helper.rb
107
122
  - spec/test_unit_spec.rb
108
123
  - spec/user.rb