validation_matcher 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/validation_matcher/version.rb +1 -1
- data/lib/validation_matcher.rb +28 -2
- data/spec/spec_helper.rb +8 -0
- data/spec/thing_spec.rb +37 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2256f5aae95d9a85c63454d483aa53d63b920579
|
4
|
+
data.tar.gz: 9b7f1c07918b26a57646055107d523b95330016c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39cdb61a3f6be45f76d7676eed2addaa6b6d4d24cbc7c8a455a684709deb43d82a2bfbd22397ecb28c7f8ba6b91cb6fc24d692cf50ad5c41b8005a649a06a259
|
7
|
+
data.tar.gz: dd45b03f8fee1a7acdd8809485f7ed75b9c2ed7c43d28394a38bc19ca5d8c1bdcdf852c692e80d0f7eb5b45aacd7d0ef95380d209034b663b3f99ebc7e368f9a
|
data/lib/validation_matcher.rb
CHANGED
@@ -3,6 +3,23 @@ require 'rspec/expectations'
|
|
3
3
|
|
4
4
|
module ValidationMatcher
|
5
5
|
|
6
|
+
class UnexpectedOptions < RuntimeError
|
7
|
+
|
8
|
+
attr_reader :validator
|
9
|
+
|
10
|
+
def initialze validator
|
11
|
+
@validator = validator
|
12
|
+
end
|
13
|
+
|
14
|
+
def message
|
15
|
+
<<-MSG
|
16
|
+
Cannot validate options with custom validator.
|
17
|
+
Use `should validate #{ validator.inspect }` (with no options) instead."
|
18
|
+
MSG
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
6
23
|
RSpec::Matchers.define :validate do |*expected|
|
7
24
|
attr_reader :expected_attribute, :expected_options, :expected_validator
|
8
25
|
|
@@ -13,7 +30,17 @@ module ValidationMatcher
|
|
13
30
|
@expected_options ||= {}
|
14
31
|
@expected_validator = expected.first
|
15
32
|
|
16
|
-
validator?
|
33
|
+
validator? or callback?
|
34
|
+
end
|
35
|
+
|
36
|
+
def callback_methods
|
37
|
+
actual._validate_callbacks.select { |c| c.filter == expected_validator }
|
38
|
+
end
|
39
|
+
|
40
|
+
def callback?
|
41
|
+
callback = callback_methods.any?
|
42
|
+
fail UnexpectedOptions if callback and expected_options.any?
|
43
|
+
callback
|
17
44
|
end
|
18
45
|
|
19
46
|
def attribute_validators
|
@@ -25,7 +52,6 @@ module ValidationMatcher
|
|
25
52
|
validator.kind == expected_validator && validator.options == expected_options
|
26
53
|
end
|
27
54
|
end
|
28
|
-
|
29
55
|
end
|
30
56
|
|
31
57
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -37,4 +37,12 @@ class Thing
|
|
37
37
|
validates :field_b, presence: true
|
38
38
|
validates :field_c, numericality: { allow_nil: false, only_integer: true }
|
39
39
|
|
40
|
+
validate :custom_validator
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def custom_validator
|
45
|
+
errors[:base] << 'Field A should be false-y' if field_a
|
46
|
+
end
|
47
|
+
|
40
48
|
end
|
data/spec/thing_spec.rb
CHANGED
@@ -2,11 +2,43 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Thing do
|
4
4
|
|
5
|
-
|
6
|
-
it { should validate(:presence).of :field_b }
|
5
|
+
describe 'each validators' do
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
describe 'without options' do
|
8
|
+
|
9
|
+
it { should_not validate(:presence).of :field_a }
|
10
|
+
it { should validate(:presence).of :field_b }
|
11
|
+
|
12
|
+
it { should_not validate(:numericality).of :field_c }
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'with options' do
|
17
|
+
|
18
|
+
it { should_not validate(:numericality).of(:field_c).with only_integer: true }
|
19
|
+
|
20
|
+
it { should validate(:numericality).of(:field_c).with only_integer: true, allow_nil: false }
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'custom validations' do
|
27
|
+
|
28
|
+
describe 'without options' do
|
29
|
+
|
30
|
+
it { should validate :custom_validator }
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'with options' do
|
35
|
+
|
36
|
+
it 'is not supported ' do
|
37
|
+
-> { should validate(:custom_validator).with on: :update }.should raise_exception ValidationMatcher::UnexpectedOptions
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
11
43
|
|
12
44
|
end
|