validation_matcher 3.0.1 → 3.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.
- checksums.yaml +4 -4
- data/lib/validation_matcher/version.rb +1 -1
- data/lib/validation_matcher.rb +13 -46
- data/spec/spec_helper.rb +18 -0
- metadata +2 -3
- data/lib/validation_matcher/hash_diff.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16e7a986b13eb0f10dea0235be0abcaf46b97519
|
4
|
+
data.tar.gz: f97b1d7823440f7190b64933975039199d98b42c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f304d374fbac5c2fe71d24720f151d9c3422aa7ef34918381db4aea93aa62954fd173669db069f983e70915f328cd13a3ac2433e337a9b4878478a21d112926
|
7
|
+
data.tar.gz: ea3d90d4556bbb05ae2e4005bd5dc412a515dbd0df4f585ea257632135672e0a3961b2f59db041904e221683e0ee6788e9ac57cf27b243b9533a335a0a07a88b
|
data/lib/validation_matcher.rb
CHANGED
@@ -1,62 +1,29 @@
|
|
1
1
|
require 'validation_matcher/version'
|
2
|
-
require 'validation_matcher/hash_diff'
|
3
2
|
require 'rspec/expectations'
|
4
3
|
|
5
4
|
module ValidationMatcher
|
6
5
|
|
7
|
-
|
6
|
+
RSpec::Matchers.define :validate do |*expected|
|
7
|
+
attr_reader :expected_attribute, :expected_options, :expected_validator
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
chain(:of) { |field| @field = field }
|
9
|
+
chain(:of) { |field| @expected_attribute = field }
|
12
10
|
chain(:with) { |hash| @expected_options = hash }
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@expected_options = nil # for some reason this appears to be cached between runs?
|
18
|
-
msg
|
19
|
-
end
|
20
|
-
|
21
|
-
failure_message do
|
22
|
-
"Expected #{ described_class } to validate the #{ kind } of #{ @field.inspect } #{ diff_with }"
|
23
|
-
end
|
24
|
-
|
25
|
-
failure_message_when_negated do
|
26
|
-
"Expected #{ described_class } not to validate the #{ kind } of #{ @field.inspect } #{ diff_with }"
|
27
|
-
end
|
28
|
-
|
29
|
-
def expected_options
|
30
|
-
@expected_options || {}
|
31
|
-
end
|
32
|
-
|
33
|
-
def validator
|
34
|
-
@validator ||= actual.class.validators_on(@field).detect { |v| expected == v.kind }
|
35
|
-
end
|
36
|
-
|
37
|
-
def validator_options
|
38
|
-
@validator_options ||= validator.options rescue {}
|
39
|
-
end
|
40
|
-
|
41
|
-
def diff_with?
|
42
|
-
# if validator_options is empty and @expected_options is not, there is a problem
|
43
|
-
# if @expected_options is empty and validator_options is not, there is a problem
|
44
|
-
# if neither is empty diff_with them, if there's a diff_with, there is a problem
|
12
|
+
match do |actual|
|
13
|
+
@expected_options ||= {}
|
14
|
+
@expected_validator = expected.first
|
45
15
|
|
46
|
-
|
47
|
-
return true if validator_options.diff_with(expected_options).present?
|
48
|
-
false
|
16
|
+
validator?
|
49
17
|
end
|
50
18
|
|
51
|
-
def
|
52
|
-
|
53
|
-
str = "\n expected options: #{ expected_options.inspect }"
|
54
|
-
str << "\n actual options: #{ validator_options.inspect }"
|
55
|
-
str
|
19
|
+
def attribute_validators
|
20
|
+
expected_attribute ? actual.class.validators_on(expected_attribute) : []
|
56
21
|
end
|
57
22
|
|
58
|
-
|
59
|
-
|
23
|
+
def validator?
|
24
|
+
attribute_validators.any? do |validator|
|
25
|
+
validator.kind == expected_validator && validator.options == expected_options
|
26
|
+
end
|
60
27
|
end
|
61
28
|
|
62
29
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,6 +8,24 @@ libs = %w[
|
|
8
8
|
|
9
9
|
libs.each { |lib| require lib }
|
10
10
|
|
11
|
+
RSpec.configure do |config|
|
12
|
+
Kernel.srand config.seed
|
13
|
+
|
14
|
+
config.filter_run :focus
|
15
|
+
config.order = :random
|
16
|
+
config.run_all_when_everything_filtered = true
|
17
|
+
|
18
|
+
config.expect_with :rspec do |expectations|
|
19
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
20
|
+
expectations.syntax = :should
|
21
|
+
end
|
22
|
+
|
23
|
+
config.mock_with :rspec do |mocks|
|
24
|
+
mocks.verify_partial_doubles = true
|
25
|
+
mocks.syntax = :should
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
11
29
|
class Thing
|
12
30
|
|
13
31
|
extend ActiveModel::Naming
|
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: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- BM5k
|
@@ -109,7 +109,6 @@ files:
|
|
109
109
|
- README.md
|
110
110
|
- Rakefile
|
111
111
|
- lib/validation_matcher.rb
|
112
|
-
- lib/validation_matcher/hash_diff.rb
|
113
112
|
- lib/validation_matcher/version.rb
|
114
113
|
- spec/spec_helper.rb
|
115
114
|
- spec/thing_spec.rb
|
@@ -134,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
133
|
version: '0'
|
135
134
|
requirements: []
|
136
135
|
rubyforge_project: validation_matcher
|
137
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.8
|
138
137
|
signing_key:
|
139
138
|
specification_version: 4
|
140
139
|
summary: RSpec matcher for ActiveModel validations
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module HashDiff
|
2
|
-
|
3
|
-
refine Hash do
|
4
|
-
# Stolen from Rails 4.0.5 since this has been removed!
|
5
|
-
#
|
6
|
-
# Returns a hash that represents the diff_witherence between two hashes.
|
7
|
-
#
|
8
|
-
# {1 => 2}.diff_with(1 => 2) # => {}
|
9
|
-
# {1 => 2}.diff_with(1 => 3) # => {1 => 2}
|
10
|
-
# {}.diff_with(1 => 2) # => {1 => 2}
|
11
|
-
# {1 => 2, 3 => 4}.diff_with(1 => 2) # => {3 => 4}
|
12
|
-
def diff_with other
|
13
|
-
dup.delete_if { |k, v| other[k] == v }.merge!(other.dup.delete_if { |k, v| has_key?(k) })
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|