easy-matchers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +8 -0
- data/.rubocop.yml +12 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +17 -0
- data/easy-matchers.gemspec +27 -0
- data/lib/easy-matchers.rb +7 -0
- data/lib/easy/version.rb +5 -0
- data/lib/matchers/validations.rb +76 -0
- data/lib/matchers/validations/absence_of.rb +66 -0
- data/lib/matchers/validations/acceptance_of.rb +77 -0
- data/lib/matchers/validations/base_validation_matcher.rb +57 -0
- data/lib/matchers/validations/confirmation_of.rb +69 -0
- data/lib/matchers/validations/length_of.rb +113 -0
- data/lib/matchers/validations/numericality_of.rb +111 -0
- data/lib/matchers/validations/presence_of.rb +66 -0
- data/mlsdev-logo.png +0 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0245d697cce0fbd8bf28805a1b2d8aa7d7323c5
|
4
|
+
data.tar.gz: 079b1507f271cf07673a1de11614bf1cc108daee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d20087c77bc3e33b085a677b563167f3be720aa61a4848fcb1ed0486d8c5b46a33f77a3f95c2d6fe3f02a05933e800f21f08e5359753b181edac23f8844a34c
|
7
|
+
data.tar.gz: c05719749f54633839f789db57b7b7574a9857ffb2123b18ae2e02c7ceed1a0414e17e953d0d1646bbbda73566e082fd97470f66a72ab66a2e7b86497aef4d54
|
data/.codeclimate.yml
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright © 2015 Igor Zubkov
|
4
|
+
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Easy Matchers
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/MLSDev/easy-matchers/badges/gpa.svg)](https://codeclimate.com/github/MLSDev/easy-matchers)
|
4
|
+
|
5
|
+
Easy matchers provides RSpec matchers for common Rails functionality.
|
6
|
+
|
7
|
+
----
|
8
|
+
|
9
|
+
### ActiveModel::Validations matchers
|
10
|
+
|
11
|
+
* validate_absence_of
|
12
|
+
* validate_acceptance_of
|
13
|
+
* validate_confirmation_of
|
14
|
+
* validate_length_of
|
15
|
+
* validate_numericality_of
|
16
|
+
* validate_presence_of
|
17
|
+
|
18
|
+
## Install and configure
|
19
|
+
|
20
|
+
`gem install easy-matchers`
|
21
|
+
|
22
|
+
or via bundler:
|
23
|
+
|
24
|
+
`gem 'easy-matchers'`
|
25
|
+
|
26
|
+
spec/support/easy-matchers.rb:
|
27
|
+
```
|
28
|
+
RSpec.configure do |config|
|
29
|
+
config.include Easy::Matchers::Validations, type: :model
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
This work is licensed under a MIT License.
|
36
|
+
|
37
|
+
## About MLSDev
|
38
|
+
|
39
|
+
[<img src="/mlsdev-logo.png" alt="MLSDev.com">][mlsdev]
|
40
|
+
|
41
|
+
Easy-Matcher are maintained by MLSDev, Inc. We specialize in providing all-in-one solution in mobile and web development. Our team follows Lean principles and works according to agile methodologies to deliver the best results reducing the budget for development and its timeline.
|
42
|
+
|
43
|
+
Find out more [here][mlsdev] and don't hesitate to [contact us][contact]!
|
44
|
+
|
45
|
+
[mlsdev]: http://mlsdev.com
|
46
|
+
[contact]: http://mlsdev.com/contact_us
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
11
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
15
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
16
|
+
spec.rcov = true
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'easy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'easy-matchers'
|
8
|
+
spec.version = Easy::Matchers::VERSION
|
9
|
+
spec.authors = ['Igor Zubkov']
|
10
|
+
spec.email = ['igor.zubkov@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Extra RSpec matchers for Rails'
|
13
|
+
spec.description = 'Extra RSpec matches for Rails (ActiveModel::Validators, ActiveModel::Model)'
|
14
|
+
spec.homepage = 'https://github.com/MLSDev/easy-matchers'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|bin)/}) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.required_ruby_version = '>= 2.0'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
|
25
|
+
spec.add_runtime_dependency 'rspec', '~> 3.4'
|
26
|
+
spec.add_runtime_dependency 'activemodel', '~> 4.2'
|
27
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'matchers/validations/base_validation_matcher'
|
2
|
+
require 'matchers/validations/absence_of'
|
3
|
+
require 'matchers/validations/acceptance_of'
|
4
|
+
require 'matchers/validations/confirmation_of'
|
5
|
+
require 'matchers/validations/length_of'
|
6
|
+
require 'matchers/validations/numericality_of'
|
7
|
+
require 'matchers/validations/presence_of'
|
data/lib/easy/version.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
class HaveValidationMatcher
|
5
|
+
def initialize(field, type)
|
6
|
+
@field = field
|
7
|
+
@type = type
|
8
|
+
@options = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(actual)
|
12
|
+
@klass = actual.is_a?(Class) ? actual : actual.class
|
13
|
+
|
14
|
+
@validator = @klass.validators_on(@field).detect{ |v|
|
15
|
+
v.kind == @type && (!v.options[:on] || on_options_matches?(v))
|
16
|
+
}
|
17
|
+
|
18
|
+
if @validator
|
19
|
+
@negative_result_message = "#{ @type.inspect } validator on #{ @field.inspect }"
|
20
|
+
@positive_result_message = "#{ @type.inspect } validator on #{ @field.inspect }"
|
21
|
+
else
|
22
|
+
@negative_result_message = "no #{ @type.inspect } validator on #{ @field.inspect }"
|
23
|
+
return false
|
24
|
+
end
|
25
|
+
@result = true
|
26
|
+
check_on if @options[:on]
|
27
|
+
@result
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure_message
|
31
|
+
"Expected #{ @klass.inspect } to #{ description }; instead got #{ @negative_result_message }"
|
32
|
+
end
|
33
|
+
|
34
|
+
def failure_message_when_negated
|
35
|
+
"Expected #{ @klass.inspect } to not #{ description }; instead got #{ @positive_result_message }"
|
36
|
+
end
|
37
|
+
|
38
|
+
def description
|
39
|
+
desc = "have #{ @type } validator on #{ @field }"
|
40
|
+
desc << " on #{ @options[:on] }" if @options[:on]
|
41
|
+
desc
|
42
|
+
end
|
43
|
+
|
44
|
+
def on(*on_method)
|
45
|
+
@options[:on] = on_method.flatten
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def check_on
|
50
|
+
validator_on_methods = [@validator.options[:on]].flatten
|
51
|
+
|
52
|
+
if validator_on_methods.any?
|
53
|
+
message = " on methods: #{ validator_on_methods }"
|
54
|
+
|
55
|
+
if on_options_covered_by?(@validator)
|
56
|
+
@positive_result_message << message
|
57
|
+
else
|
58
|
+
@negative_result_message << message
|
59
|
+
@result = false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def on_options_matches?(validator)
|
67
|
+
@options[:on] && validator.options[:on] && on_options_covered_by?(validator)
|
68
|
+
end
|
69
|
+
|
70
|
+
def on_options_covered_by?(validator)
|
71
|
+
([@options[:on]].flatten - [validator.options[:on]].flatten).empty?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_absence_of(*attributes)
|
5
|
+
ValidateAbsenceOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidateAbsenceOfMatcher < BaseValidationMatcher
|
9
|
+
def initialize(attributes)
|
10
|
+
super(attributes, :absence)
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(subject)
|
14
|
+
super(subject)
|
15
|
+
|
16
|
+
matched_validator = class_name.validators.detect do |validator|
|
17
|
+
validator.kind == type &&
|
18
|
+
validator.attributes.sort == attributes.sort &&
|
19
|
+
validator.options.sort == options.sort
|
20
|
+
end
|
21
|
+
|
22
|
+
true if matched_validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
"require #{ attributes.join(', ') } to not be set"
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_message(message)
|
30
|
+
options[:message] = message
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def if(symbol_name)
|
35
|
+
options[:if] = symbol_name
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def unless(symbol_name)
|
40
|
+
options[:unless] = symbol_name
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def on(context)
|
45
|
+
options[:on] = context
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def allow_nil(value = true)
|
50
|
+
options[:allow_nil] = value
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def allow_blank(value = true)
|
55
|
+
options[:allow_blank] = value
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def strict(value)
|
60
|
+
options[:strict] = value
|
61
|
+
self
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_acceptance_of(*attributes)
|
5
|
+
ValidateAcceptanceOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidateAcceptanceOfMatcher < BaseValidationMatcher
|
9
|
+
def initialize(attributes)
|
10
|
+
super(attributes, :acceptance)
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(subject)
|
14
|
+
super(subject)
|
15
|
+
|
16
|
+
add_allow_nil_and_accept_to_options
|
17
|
+
|
18
|
+
matched_validator = class_name.validators.detect do |validator|
|
19
|
+
validator.kind == type &&
|
20
|
+
validator.attributes.sort == attributes.sort &&
|
21
|
+
validator.options.sort == options.sort
|
22
|
+
end
|
23
|
+
|
24
|
+
true if matched_validator
|
25
|
+
end
|
26
|
+
|
27
|
+
def description
|
28
|
+
"require #{ attributes.join(', ') } to be accepted"
|
29
|
+
end
|
30
|
+
|
31
|
+
def with_message(message)
|
32
|
+
options[:message] = message
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def if(symbol_name)
|
37
|
+
options[:if] = symbol_name
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def unless(symbol_name)
|
42
|
+
options[:unless] = symbol_name
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def on(context)
|
47
|
+
options[:on] = context
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def allow_nil(value = true)
|
52
|
+
options[:allow_nil] = value
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def allow_blank(value = true)
|
57
|
+
options[:allow_blank] = value
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def strict(value)
|
62
|
+
options[:strict] = value
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
# TODO: check :allow_nil and :accept before setting
|
69
|
+
# NOTE: Rails adds this to options by defaults
|
70
|
+
def add_allow_nil_and_accept_to_options
|
71
|
+
options[:allow_nil] = true
|
72
|
+
options[:accept] = '1'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
class BaseValidationMatcher
|
4
|
+
attr_reader :attributes,
|
5
|
+
:failure_message,
|
6
|
+
:failure_message_when_negated,
|
7
|
+
:subject,
|
8
|
+
:type,
|
9
|
+
:options
|
10
|
+
|
11
|
+
def initialize(attributes, type)
|
12
|
+
@attributes = attributes
|
13
|
+
@failure_message = nil
|
14
|
+
@failure_message_when_negated = nil
|
15
|
+
@type = type
|
16
|
+
@options = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def matches?(subject)
|
20
|
+
@subject = subject
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def class_name
|
25
|
+
subject.class
|
26
|
+
end
|
27
|
+
|
28
|
+
def description
|
29
|
+
raise NotImplementedError, 'Subclasses must implement a description method.'
|
30
|
+
end
|
31
|
+
|
32
|
+
def if
|
33
|
+
raise NotImplementedError, 'Subclasses must implement a if method.'
|
34
|
+
end
|
35
|
+
|
36
|
+
def unless
|
37
|
+
raise NotImplementedError, 'Subclasses must implement a unless method.'
|
38
|
+
end
|
39
|
+
|
40
|
+
def on(context)
|
41
|
+
raise NotImplementedError, 'Subclasses must implement a on method.'
|
42
|
+
end
|
43
|
+
|
44
|
+
def allow_nil
|
45
|
+
raise NotImplementedError, 'Subclasses must implement a allow_nil method.'
|
46
|
+
end
|
47
|
+
|
48
|
+
def allow_blank
|
49
|
+
raise NotImplementedError, 'Subclasses must implement a allow_blank method.'
|
50
|
+
end
|
51
|
+
|
52
|
+
def strict
|
53
|
+
raise NotImplementedError, 'Subclasses must implement a strict method.'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_confirmation_of(*attributes)
|
5
|
+
ValidateConfirmationOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidateConfirmationOfMatcher < BaseValidationMatcher
|
9
|
+
attr_reader :confirmation_attributes
|
10
|
+
|
11
|
+
def initialize(attributes)
|
12
|
+
super(attributes, :confirmation)
|
13
|
+
@confirmation_attributes = attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(subject)
|
17
|
+
super(subject)
|
18
|
+
|
19
|
+
matched_validator = class_name.validators.detect do |validator|
|
20
|
+
validator.kind == type &&
|
21
|
+
validator.attributes.sort == attributes.sort &&
|
22
|
+
validator.options.sort == options.sort
|
23
|
+
end
|
24
|
+
|
25
|
+
true if matched_validator
|
26
|
+
end
|
27
|
+
|
28
|
+
def description
|
29
|
+
"require #{ confirmation_attributes.join(', ') } to match #{ attributes.join(', ') }"
|
30
|
+
end
|
31
|
+
|
32
|
+
def with_message(message)
|
33
|
+
options[:message] = message
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def if(symbol_name)
|
38
|
+
options[:if] = symbol_name
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def unless(symbol_name)
|
43
|
+
options[:unless] = symbol_name
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def on(context)
|
48
|
+
options[:on] = context
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def allow_nil(value = true)
|
53
|
+
options[:allow_nil] = value
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def allow_blank(value = true)
|
58
|
+
options[:allow_blank] = value
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def strict(value)
|
63
|
+
options[:strict] = value
|
64
|
+
self
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_length_of(*attributes)
|
5
|
+
ValidateLengthOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidateLengthOfMatcher < BaseValidationMatcher
|
9
|
+
def initialize(attributes)
|
10
|
+
super(attributes, :length)
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(subject)
|
14
|
+
super(subject)
|
15
|
+
|
16
|
+
matched_validator = class_name.validators.detect do |validator|
|
17
|
+
validator.kind == type &&
|
18
|
+
validator.attributes.sort == attributes.sort &&
|
19
|
+
validator.options.sort == options.sort
|
20
|
+
end
|
21
|
+
|
22
|
+
true if matched_validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_message(message)
|
26
|
+
options[:message] = message
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def if(symbol_name)
|
31
|
+
options[:if] = symbol_name
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def unless(symbol_name)
|
36
|
+
options[:unless] = symbol_name
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def on(context)
|
41
|
+
options[:on] = context
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def allow_nil(value = true)
|
46
|
+
options[:allow_nil] = value
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def allow_blank(value = true)
|
51
|
+
options[:allow_blank] = value
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def strict(value)
|
56
|
+
options[:strict] = value
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def is_at_most(value)
|
61
|
+
options[:maximum] = value
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def is_at_least(value)
|
66
|
+
options[:minimum] = value
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
# def is_equal_to
|
71
|
+
# @is = value
|
72
|
+
# self
|
73
|
+
# end
|
74
|
+
|
75
|
+
def description
|
76
|
+
description = "ensure #{ attributes.join(', ') } has a length "
|
77
|
+
# options_desc = []
|
78
|
+
# options_desc << "with minimum of #{ @minimum }" if @minimum
|
79
|
+
# options_desc << "with maximum of #{ @maximum }" if @maximum
|
80
|
+
# # options_desc << "within the range of #{ @within }" if @within
|
81
|
+
# # options_desc << "as exactly #{ @is }" if @is
|
82
|
+
# options_desc << "with message '#{ @expected_message }'" if @expected_message
|
83
|
+
# super << " #{ options_desc.to_sentence }"
|
84
|
+
description
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
# # def check_exact
|
90
|
+
# # if actual_is == @is
|
91
|
+
# # @positive_result_message << " as exactly #{@is}"
|
92
|
+
# # else
|
93
|
+
# # @negative_result_message << " as exactly #{actual_is}"
|
94
|
+
# # @result = false
|
95
|
+
# # end
|
96
|
+
# # end
|
97
|
+
#
|
98
|
+
# def check_expected_message
|
99
|
+
# actual_message = @validator.options[:message]
|
100
|
+
# if actual_message.nil?
|
101
|
+
# @negative_result_message << ' with no custom message'
|
102
|
+
# @result = false
|
103
|
+
# elsif actual_message == @expected_message
|
104
|
+
# @positive_result_message << " with custom message '#{ @expected_message }'"
|
105
|
+
# else
|
106
|
+
# @negative_result_message << " got message '#{ actual_message }'"
|
107
|
+
# @result = false
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_numericality_of(*attributes)
|
5
|
+
ValidateNumericalityOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidateNumericalityOfMatcher < BaseValidationMatcher
|
9
|
+
def initialize(attributes)
|
10
|
+
super(attributes, :numericality)
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(subject)
|
14
|
+
super(subject)
|
15
|
+
|
16
|
+
matched_validator = class_name.validators.detect do |validator|
|
17
|
+
validator.kind == type &&
|
18
|
+
validator.attributes.sort == attributes.sort &&
|
19
|
+
validator.options.sort == options.sort
|
20
|
+
end
|
21
|
+
|
22
|
+
true if matched_validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
"require #{ attributes.join(', ') } to be numeric"
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_message(message)
|
30
|
+
options[:message] = message
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def if(symbol_name)
|
35
|
+
options[:if] = symbol_name
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def unless(symbol_name)
|
40
|
+
options[:unless] = symbol_name
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def on(context)
|
45
|
+
options[:on] = context
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def allow_nil(value = true)
|
50
|
+
options[:allow_nil] = value
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def allow_blank(value = true)
|
55
|
+
options[:allow_blank] = value
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def strict(value)
|
60
|
+
options[:strict] = value
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def only_integer(value = true)
|
65
|
+
options[:only_integer] = value
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def is_greater_than(value)
|
70
|
+
options[:greater_than] = value
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def is_greater_than_or_equal_to(value)
|
75
|
+
options[:greater_than_or_equal_to] = value
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def is_equal_to(value)
|
80
|
+
options[:equal_to] = value
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def is_less_than(value)
|
85
|
+
options[:less_than] = value
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
89
|
+
def is_less_than_or_equal_to(value)
|
90
|
+
options[:less_than_or_equal_to] = value
|
91
|
+
self
|
92
|
+
end
|
93
|
+
|
94
|
+
def is_other_than(value)
|
95
|
+
options[:other_than] = value
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
def even(value = true)
|
100
|
+
options[:even] = value
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def odd(value = true)
|
105
|
+
options[:odd] = value
|
106
|
+
self
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Easy
|
2
|
+
module Matchers
|
3
|
+
module Validations
|
4
|
+
def validate_presence_of(*attributes)
|
5
|
+
ValidatePresenceOfMatcher.new(attributes)
|
6
|
+
end
|
7
|
+
|
8
|
+
class ValidatePresenceOfMatcher < BaseValidationMatcher
|
9
|
+
def initialize(attributes)
|
10
|
+
super(attributes, :presence)
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(subject)
|
14
|
+
super(subject)
|
15
|
+
|
16
|
+
matched_validator = class_name.validators.detect do |validator|
|
17
|
+
validator.kind == type &&
|
18
|
+
validator.attributes.sort == attributes.sort &&
|
19
|
+
validator.options.sort == options.sort
|
20
|
+
end
|
21
|
+
|
22
|
+
true if matched_validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def description
|
26
|
+
"require #{ attributes.join(', ') } to be set"
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_message(message)
|
30
|
+
options[:message] = message
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def if(symbol_name)
|
35
|
+
options[:if] = symbol_name
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def unless(symbol_name)
|
40
|
+
options[:unless] = symbol_name
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def on(context)
|
45
|
+
options[:on] = context
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def allow_nil(value = true)
|
50
|
+
options[:allow_nil] = value
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def allow_blank(value = true)
|
55
|
+
options[:allow_blank] = value
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def strict(value)
|
60
|
+
options[:strict] = value
|
61
|
+
self
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/mlsdev-logo.png
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy-matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Zubkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.2'
|
69
|
+
description: Extra RSpec matches for Rails (ActiveModel::Validators, ActiveModel::Model)
|
70
|
+
email:
|
71
|
+
- igor.zubkov@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".codeclimate.yml"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- Gemfile
|
79
|
+
- MIT-LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- easy-matchers.gemspec
|
83
|
+
- lib/easy-matchers.rb
|
84
|
+
- lib/easy/version.rb
|
85
|
+
- lib/matchers/validations.rb
|
86
|
+
- lib/matchers/validations/absence_of.rb
|
87
|
+
- lib/matchers/validations/acceptance_of.rb
|
88
|
+
- lib/matchers/validations/base_validation_matcher.rb
|
89
|
+
- lib/matchers/validations/confirmation_of.rb
|
90
|
+
- lib/matchers/validations/length_of.rb
|
91
|
+
- lib/matchers/validations/numericality_of.rb
|
92
|
+
- lib/matchers/validations/presence_of.rb
|
93
|
+
- mlsdev-logo.png
|
94
|
+
homepage: https://github.com/MLSDev/easy-matchers
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '2.0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.4.8
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Extra RSpec matchers for Rails
|
118
|
+
test_files: []
|