validation_matcher 0.0.1 → 0.1.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 +5 -0
- data/README.md +22 -9
- data/lib/validation_matcher/version.rb +1 -1
- data/lib/validation_matcher.rb +29 -15
- data/spec/spec_helper.rb +11 -3
- data/spec/thing_spec.rb +3 -2
- data/validation_matcher.gemspec +4 -2
- metadata +53 -17
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,8 +1,17 @@
|
|
1
|
-
Validation Matcher
|
2
|
-
|
1
|
+
Validation Matcher [![build status][status-image]][ci]
|
2
|
+
======================================================
|
3
|
+
|
4
|
+
About
|
5
|
+
-----
|
3
6
|
|
4
7
|
Use Rails 3 validation reflection to test validations.
|
5
8
|
|
9
|
+
URLs:
|
10
|
+
|
11
|
+
- Github: https://github.com/bm5k/validation_matcher/
|
12
|
+
- Documentation: http://rubydoc.info/github/BM5k/validation_matcher/master/frames
|
13
|
+
- RubyGems: https://rubygems.org/gems/validation_matcher
|
14
|
+
|
6
15
|
Installation
|
7
16
|
------------
|
8
17
|
|
@@ -26,12 +35,16 @@ Usage
|
|
26
35
|
|
27
36
|
require 'spec_helper'
|
28
37
|
|
29
|
-
describe
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
its(:field_c) { should validate :uniqueness, { case_insensitive: false }}
|
35
|
-
|
38
|
+
describe Foo do
|
39
|
+
it { should validate(:presence).of(:field_a) }
|
40
|
+
it { should validate(:presence).of(:field_b) }
|
41
|
+
it { should validate(:presence).of(:field_c).with(case_insensitive: false) }
|
42
|
+
it { should validate(:uniqueness).of(:field_b).with(options) }
|
36
43
|
end
|
37
44
|
```
|
45
|
+
|
46
|
+
<!-- links -->
|
47
|
+
[ci]: http://travis-ci.org/BM5k/validation_matcher "build status"
|
48
|
+
|
49
|
+
<!-- images -->
|
50
|
+
[status-image]: https://secure.travis-ci.org/BM5k/validation_matcher.png?branch=master
|
data/lib/validation_matcher.rb
CHANGED
@@ -2,35 +2,49 @@ require 'validation_matcher/version'
|
|
2
2
|
|
3
3
|
module ValidationMatcher
|
4
4
|
|
5
|
-
RSpec::Matchers.define :validate do |kind
|
5
|
+
RSpec::Matchers.define :validate do |kind|
|
6
6
|
|
7
|
-
field
|
8
|
-
|
9
|
-
options = validator.options rescue {}
|
7
|
+
chain(:of) { |field| @field = field }
|
8
|
+
chain(:with) { |hash| @options = hash }
|
10
9
|
|
11
|
-
|
10
|
+
# simplify access to the described_class
|
11
|
+
def described_class
|
12
|
+
@described_class ||= actual.class
|
13
|
+
end
|
14
|
+
|
15
|
+
# Attempts to find the selected validator
|
16
|
+
#
|
17
|
+
# @param [Symbol] kind the type of validator we're looking for
|
18
|
+
def find_validator kind
|
19
|
+
described_class.validators_on(@field).detect { |v| v.kind == kind }
|
20
|
+
end
|
12
21
|
|
13
22
|
description do
|
14
|
-
msg = "validate the #{ kind } of #{ field.inspect }"
|
15
|
-
msg
|
23
|
+
msg = "validate the #{ kind } of #{ @field.inspect }"
|
24
|
+
msg << " with options: #{ @options.inspect }" if @options
|
16
25
|
msg
|
17
26
|
end
|
18
27
|
|
19
28
|
failure_message_for_should do
|
20
|
-
msg = "Expected #{ described_class } to validate the #{ kind } of #{ field.inspect }"
|
21
|
-
|
22
|
-
msg += "\n with options: #{ expected_options.inspect }"
|
23
|
-
msg += "\n actual options: #{ options.inspect }"
|
24
|
-
end
|
29
|
+
msg = "Expected #{ described_class } to validate the #{ kind } of #{ @field.inspect }"
|
30
|
+
msg << "\n with options: #{ @options.inspect }" if @options.present?
|
25
31
|
msg
|
26
32
|
end
|
27
33
|
|
28
34
|
failure_message_for_should_not do
|
29
|
-
"
|
35
|
+
msg = "Expected #{ described_class } not to validate the #{ kind } of #{ @field.inspect }"
|
36
|
+
msg << "\n with options: #{ @options.inspect }" if @options.present?
|
37
|
+
msg
|
30
38
|
end
|
31
39
|
|
32
|
-
|
33
|
-
|
40
|
+
match do |actual|
|
41
|
+
validator = find_validator kind
|
42
|
+
options = validator.options rescue {}
|
43
|
+
diff = @options ? options.diff(@options) : nil
|
44
|
+
@options = nil # for some reason this appears to be cached between runs?
|
45
|
+
|
46
|
+
validator.present? and diff.blank?
|
47
|
+
end
|
34
48
|
|
35
49
|
end
|
36
50
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
%w[ validation_matcher
|
1
|
+
%w[ validation_matcher active_support/core_ext active_model pry pry-nav ].each { |lib| require lib }
|
2
|
+
|
3
|
+
class Thing
|
4
|
+
|
5
|
+
extend ActiveModel::Naming
|
6
|
+
include ActiveModel::Conversion
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
attr_accessor :field_a, :field_b, :field_c
|
2
10
|
|
3
|
-
class Thing < SuperModel::Base
|
4
|
-
attr_accessor :field_a, :field_b
|
5
11
|
validates :field_b, presence: true
|
12
|
+
validates :field_c, numericality: { allow_nil: false, only_integer: true }
|
13
|
+
|
6
14
|
end
|
data/spec/thing_spec.rb
CHANGED
@@ -2,7 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Thing do
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
it { should_not validate(:presence).of(:field_a) }
|
6
|
+
it { should validate(:presence).of(:field_b) }
|
7
|
+
it { should validate(:numericality).of(:field_c).with(only_integer: true, allow_nil: false) }
|
7
8
|
|
8
9
|
end
|
data/validation_matcher.gemspec
CHANGED
@@ -18,9 +18,11 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ['lib']
|
20
20
|
|
21
|
-
s.add_dependency 'rspec', '~> 2
|
21
|
+
s.add_dependency 'rspec', '~> 2'
|
22
|
+
|
23
|
+
s.add_dependency 'activemodel', '~> 3'
|
24
|
+
s.add_dependency 'activesupport', '~> 3'
|
22
25
|
|
23
|
-
s.add_development_dependency 'supermodel'
|
24
26
|
s.add_development_dependency 'pry'
|
25
27
|
s.add_development_dependency 'pry-nav'
|
26
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,33 +9,59 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 2
|
21
|
+
version: '2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement:
|
31
|
+
name: activemodel
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
|
-
- -
|
35
|
+
- - ~>
|
31
36
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
33
|
-
type: :
|
37
|
+
version: '3'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3'
|
54
|
+
type: :runtime
|
34
55
|
prerelease: false
|
35
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
36
62
|
- !ruby/object:Gem::Dependency
|
37
63
|
name: pry
|
38
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
39
65
|
none: false
|
40
66
|
requirements:
|
41
67
|
- - ! '>='
|
@@ -43,10 +69,15 @@ dependencies:
|
|
43
69
|
version: '0'
|
44
70
|
type: :development
|
45
71
|
prerelease: false
|
46
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
47
78
|
- !ruby/object:Gem::Dependency
|
48
79
|
name: pry-nav
|
49
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
50
81
|
none: false
|
51
82
|
requirements:
|
52
83
|
- - ! '>='
|
@@ -54,7 +85,12 @@ dependencies:
|
|
54
85
|
version: '0'
|
55
86
|
type: :development
|
56
87
|
prerelease: false
|
57
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
58
94
|
description: Use reflection to spec ActiveModel validations
|
59
95
|
email:
|
60
96
|
- me@bm5k.com
|
@@ -64,6 +100,7 @@ extra_rdoc_files: []
|
|
64
100
|
files:
|
65
101
|
- .gitignore
|
66
102
|
- .rspec
|
103
|
+
- .travis.yml
|
67
104
|
- Gemfile
|
68
105
|
- README.md
|
69
106
|
- Rakefile
|
@@ -93,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
130
|
version: '0'
|
94
131
|
requirements: []
|
95
132
|
rubyforge_project: validation_matcher
|
96
|
-
rubygems_version: 1.8.
|
133
|
+
rubygems_version: 1.8.23
|
97
134
|
signing_key:
|
98
135
|
specification_version: 3
|
99
136
|
summary: RSpec matcher for ActiveModel validations
|
@@ -101,4 +138,3 @@ test_files:
|
|
101
138
|
- spec/matchers_spec.rb
|
102
139
|
- spec/spec_helper.rb
|
103
140
|
- spec/thing_spec.rb
|
104
|
-
has_rdoc:
|