validation_matcher 3.1.0 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16e7a986b13eb0f10dea0235be0abcaf46b97519
4
- data.tar.gz: f97b1d7823440f7190b64933975039199d98b42c
3
+ metadata.gz: 2256f5aae95d9a85c63454d483aa53d63b920579
4
+ data.tar.gz: 9b7f1c07918b26a57646055107d523b95330016c
5
5
  SHA512:
6
- metadata.gz: 2f304d374fbac5c2fe71d24720f151d9c3422aa7ef34918381db4aea93aa62954fd173669db069f983e70915f328cd13a3ac2433e337a9b4878478a21d112926
7
- data.tar.gz: ea3d90d4556bbb05ae2e4005bd5dc412a515dbd0df4f585ea257632135672e0a3961b2f59db041904e221683e0ee6788e9ac57cf27b243b9533a335a0a07a88b
6
+ metadata.gz: 39cdb61a3f6be45f76d7676eed2addaa6b6d4d24cbc7c8a455a684709deb43d82a2bfbd22397ecb28c7f8ba6b91cb6fc24d692cf50ad5c41b8005a649a06a259
7
+ data.tar.gz: dd45b03f8fee1a7acdd8809485f7ed75b9c2ed7c43d28394a38bc19ca5d8c1bdcdf852c692e80d0f7eb5b45aacd7d0ef95380d209034b663b3f99ebc7e368f9a
@@ -1,3 +1,3 @@
1
1
  module ValidationMatcher
2
- VERSION = '3.1.0'
2
+ VERSION = '3.2.0'
3
3
  end
@@ -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
- it { should_not validate(:presence).of :field_a }
6
- it { should validate(:presence).of :field_b }
5
+ describe 'each validators' do
7
6
 
8
- it { should_not validate(:numericality).of :field_c }
9
- it { should_not validate(:numericality).of(:field_c).with only_integer: true }
10
- it { should validate(:numericality).of(:field_c).with only_integer: true, allow_nil: false }
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
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.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BM5k