minitest-activemodel 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +24 -0
- data/CONTRIBUTING.md +7 -0
- data/Gemfile +3 -0
- data/LICENSE.md +22 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/lib/minitest-activemodel/matchers/validate_acceptance_matcher.rb +56 -0
- data/lib/minitest-activemodel/matchers/validate_confirmation_matcher.rb +12 -0
- data/lib/minitest-activemodel/matchers/validate_format_matcher.rb +82 -0
- data/lib/minitest-activemodel/matchers/validate_length_matcher.rb +146 -0
- data/lib/minitest-activemodel/matchers/validate_presence_matcher.rb +12 -0
- data/lib/minitest-activemodel/matchers/validation_matcher.rb +52 -0
- data/lib/minitest-activemodel/version.rb +7 -0
- data/lib/minitest-activemodel.rb +9 -0
- data/minitest-activemodel.gemspec +25 -0
- data/test/cases/validate_acceptance_matcher_test.rb +19 -0
- data/test/cases/validate_confirmation_matcher_test.rb +11 -0
- data/test/cases/validate_format_matcher_test.rb +29 -0
- data/test/cases/validate_length_matcher_test.rb +77 -0
- data/test/cases/validate_presence_matcher_test.rb +11 -0
- data/test/cases/validation_test.rb +46 -0
- data/test/models/user.rb +18 -0
- data/test/test_helper.rb +9 -0
- metadata +146 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# 0.0.1 - 10/13/12
|
2
|
+
|
3
|
+
+ Add `validate_presence_of` matcher.
|
4
|
+
+ Add `validate_acceptance_of` matcher.
|
5
|
+
+ Add `accepts_with(value)` option to `ValidateAcceptanceMatcher`.
|
6
|
+
+ Add `validate_confirmation_of` matcher.
|
7
|
+
+ Add `validate_length_of` matcher.
|
8
|
+
+ Add `validate_size_of` alias to `validate_length_of`.
|
9
|
+
+ Add `ensure_length_of` and `ensure_size_of` aliases to `validate_length_of`.
|
10
|
+
+ Add `with_minimum` option to `ValidateLengthMatcher`.
|
11
|
+
+ Add `with_min` and `is_at_least` aliases to `ValidateLengthMatcher#with_minimum`.
|
12
|
+
+ Add `with_maximum` option to `ValidateLengthMatcher`.
|
13
|
+
+ Add `with_max` and `is_at_most` aliases to `ValidateLengthMatcher#with_maximum`.
|
14
|
+
+ Add `within` option to `ValidateLengthMatcher`.
|
15
|
+
+ Add `in` alias to `ValidateLengthMatcher#within`.
|
16
|
+
+ Raise `ArgumentError` if the given value for `ValidateLengthMatcher#within`
|
17
|
+
is not a `Range`.
|
18
|
+
+ Add `is` option to `ValidateLengthMatcher`.
|
19
|
+
+ Add `is_equal_to` alias to `ValidateLengthMatcher#is`.
|
20
|
+
+ Add `validate_format_of` matcher.
|
21
|
+
+ Raise `ArgumentError` if no option is given for `validate_length_of` matcher.
|
22
|
+
+ Add `to_allow` and `to_not_allow` options to `ValidateFormatMatcher`.
|
23
|
+
+ Raise `ArgumentError` if no option is given for `validate_format_of` matcher.
|
24
|
+
+ Raise error if `to_allow` and `to_not_allow` options are called at once.
|
data/CONTRIBUTING.md
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Francesco Rodriguez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# minitest-activemodel
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/frodsan/minitest-activemodel.png?branch=master&.png)](http://travis-ci.org/frodsan/minitest-activemodel)
|
4
|
+
[![Dependency Status](https://gemnasium.com/frodsan/minitest-activemodel.png)](https://gemnasium.com/frodsan/minitest-activemodel)
|
5
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/frodsan/minitest-activemodel)
|
6
|
+
|
7
|
+
MiniTest matchers for ActiveModel::Validations.
|
8
|
+
|
9
|
+
## Support
|
10
|
+
|
11
|
+
This gem supports:
|
12
|
+
|
13
|
+
+ Ruby 1.9.3.
|
14
|
+
+ ActiveModel 3.2.x.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'minitest-activemodel', group: :test
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install minitest-activemodel
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
TODO: In this section, it must appear the Official Documentation link.
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Read [CONTRIBUTING](https://github.com/frodsan/minitest-activemodel/blob/master/CONTRIBUTING.md).
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
MIT License. Copyright 2012 Francesco Rodriguez. See [LICENSE](https://github.com/frodsan/minitest-activemodel/blob/master/LICENSE.md)
|
41
|
+
for more information.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
# Ensures that the model is not valid if the given attribute is not accepted.
|
5
|
+
#
|
6
|
+
# Options:
|
7
|
+
# * <tt>accept_with</tt> - value that is considered accepted.
|
8
|
+
#
|
9
|
+
# it { must validate_acceptance_of(:eula) }
|
10
|
+
# it { must validate_acceptance_of(:terms_of_service).accept_with(true) }
|
11
|
+
def validate_acceptance_of attr
|
12
|
+
ValidateAcceptanceMatcher.new attr
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
class ValidateAcceptanceMatcher < ValidationMatcher
|
18
|
+
def initialize attr
|
19
|
+
super attr, :acceptance
|
20
|
+
end
|
21
|
+
|
22
|
+
def accept_with value
|
23
|
+
@accepted = value
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def matches? subject
|
28
|
+
return false unless @result = super(subject)
|
29
|
+
|
30
|
+
check_accepted_value if @accepted
|
31
|
+
|
32
|
+
@result
|
33
|
+
end
|
34
|
+
|
35
|
+
def description
|
36
|
+
desc = ''
|
37
|
+
desc = " accept with #{@accepted.inspect}" if @accepted
|
38
|
+
super << desc
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def check_accepted_value
|
44
|
+
actual = @validator.options[:accept]
|
45
|
+
|
46
|
+
if actual == @accepted
|
47
|
+
@positive_message << " accept with #{actual.inspect}."
|
48
|
+
else
|
49
|
+
@negative_message << " accept with #{actual.inspect}."
|
50
|
+
@result = false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
# Ensures that the model's attribute matches confirmation.
|
5
|
+
#
|
6
|
+
# it { must validate_confirmation_of :password }
|
7
|
+
def validate_confirmation_of attr
|
8
|
+
ValidationMatcher.new attr, :confirmation
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
# Ensures that the model is not valid if the given attribute is not
|
5
|
+
# formatted correctly.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
# * <tt>to_allow</tt> - string to test against that it is valid.
|
9
|
+
# * <tt>to_not_allow</tt> - string to test against that it is not valid.
|
10
|
+
#
|
11
|
+
# it { must validate_format_of(:email).to_allow('foo@bar.com') }
|
12
|
+
# it { must validate_format_of(:email).to_not_allow('foo_bar_com') }
|
13
|
+
def validate_format_of attr
|
14
|
+
ValidateFormatMatcher.new attr
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
class ValidateFormatMatcher < ValidationMatcher
|
20
|
+
def initialize attr
|
21
|
+
super attr, :format
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_allow valid_value
|
25
|
+
raise 'You must not call both to_allow and to_not_allow' if @invalid
|
26
|
+
|
27
|
+
@valid = valid_value
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_not_allow invalid_value
|
32
|
+
raise 'You must not call both to_allow and to_not_allow' if @valid
|
33
|
+
|
34
|
+
@invalid = invalid_value
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def matches? subject
|
39
|
+
validate_invalid_options! @valid, @invalid
|
40
|
+
|
41
|
+
return false unless @result = super(subject)
|
42
|
+
|
43
|
+
check_valid_value if @valid
|
44
|
+
check_invalid_value if @invalid
|
45
|
+
|
46
|
+
@result
|
47
|
+
end
|
48
|
+
|
49
|
+
def description
|
50
|
+
desc = []
|
51
|
+
desc << " allowing the value #{@valid.inspect}" if @valid
|
52
|
+
desc << " not allowing the value #{@invalid.inspect}" if @invalid
|
53
|
+
super << desc.to_sentence
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def check_valid_value
|
59
|
+
if format =~ @valid
|
60
|
+
@positive_message << " with #{@valid.inspect} as a valid value"
|
61
|
+
else
|
62
|
+
@negative_message << " with #{@valid.inspect} as an invalid value"
|
63
|
+
@result = false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def check_invalid_value
|
68
|
+
if format !~ @invalid
|
69
|
+
@positive_message << " with #{@invalid.inspect} as a invalid value"
|
70
|
+
else
|
71
|
+
@negative_message << " with #{@invalid.inspect} as a valid value"
|
72
|
+
@result = false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def format
|
77
|
+
@with ||= @validator.options[:with]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
# Ensures that the length/size of the attribute is validated.
|
5
|
+
# You must supply at least one of the following options:
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
# * <tt>with_minimum</tt> - ensures the minimum length of the attribute.
|
9
|
+
# Aliased as: <tt>with_min</tt> and <tt>is_at_least</tt>.
|
10
|
+
# * <tt>with_maximum</tt> - ensures the maximum length of the attribute.
|
11
|
+
# Aliased as: <tt>with_max</tt> and <tt>is_at_most</tt>.
|
12
|
+
# * <tt>within</tt> - a range specifying the minimum and maximum length
|
13
|
+
# of the attribute. Aliased as: <tt>in</tt>.
|
14
|
+
# * <tt>is</tt> - ensures the exact length of the attribute.
|
15
|
+
# Aliased as: <tt>is_equal_to</tt>.
|
16
|
+
#
|
17
|
+
# it { must validate_length_of(:name).with_minimum(10) }
|
18
|
+
# it { must validate_length_of(:name).with_min(10) }
|
19
|
+
# it { must validate_length_of(:name).is_at_least(10) }
|
20
|
+
# it { must validate_length_of(:name).with_maximum(100) }
|
21
|
+
# it { must validate_length_of(:name).with_max(100) }
|
22
|
+
# it { must validate_length_of(:name).is_at_most(100) }
|
23
|
+
# it { must validate_length_of(:name).within(10..100) }
|
24
|
+
# it { must validate_length_of(:name).in(10..100) }
|
25
|
+
# it { must validate_length_of(:password).is(8) }
|
26
|
+
# it { must validate_length_of(:password).is_equal_to(8) }
|
27
|
+
#
|
28
|
+
# This matcher is also aliased as:
|
29
|
+
#
|
30
|
+
# * <tt>validate_size_of</tt>.
|
31
|
+
# * <tt>ensure_length_of</tt>.
|
32
|
+
# * <tt>ensure_size_of</tt>.
|
33
|
+
#
|
34
|
+
# So you can do something like:
|
35
|
+
#
|
36
|
+
# it { must ensure_length_of(:name).is_at_least(10) }
|
37
|
+
# it { must ensure_size_of(:name).is_at_most(100) }
|
38
|
+
# it { must validate_size_of(:name).in(10..100) }
|
39
|
+
def validate_length_of attr
|
40
|
+
ValidateLengthMatcher.new attr
|
41
|
+
end
|
42
|
+
alias :validate_size_of :validate_length_of
|
43
|
+
alias :ensure_length_of :validate_length_of
|
44
|
+
alias :ensure_size_of :validate_length_of
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
class ValidateLengthMatcher < ValidationMatcher
|
49
|
+
def initialize attr
|
50
|
+
super attr, :length
|
51
|
+
end
|
52
|
+
|
53
|
+
def with_minimum value
|
54
|
+
@minimum = value
|
55
|
+
self
|
56
|
+
end
|
57
|
+
alias :with_min :with_minimum
|
58
|
+
alias :is_at_least :with_minimum
|
59
|
+
|
60
|
+
def with_maximum value
|
61
|
+
@maximum = value
|
62
|
+
self
|
63
|
+
end
|
64
|
+
alias :with_max :with_maximum
|
65
|
+
alias :is_at_most :with_maximum
|
66
|
+
|
67
|
+
def within value
|
68
|
+
raise ArgumentError, 'within must be a Range' unless value.is_a? Range
|
69
|
+
@within = value
|
70
|
+
self
|
71
|
+
end
|
72
|
+
alias :in :within
|
73
|
+
|
74
|
+
def is value
|
75
|
+
@is = value
|
76
|
+
self
|
77
|
+
end
|
78
|
+
alias :is_equal_to :is
|
79
|
+
|
80
|
+
# TODO: add helper methods for :too_long, :too_short
|
81
|
+
# and :wrong_length options.
|
82
|
+
# See http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_length_of
|
83
|
+
|
84
|
+
def matches? subject
|
85
|
+
validate_invalid_options! @minimum, @maximum, @within, @is
|
86
|
+
|
87
|
+
return false unless @result = super(subject)
|
88
|
+
|
89
|
+
check :minimum if @minimum
|
90
|
+
check :maximum if @maximum
|
91
|
+
check_range if @within
|
92
|
+
check_precision if @is
|
93
|
+
|
94
|
+
@result
|
95
|
+
end
|
96
|
+
|
97
|
+
def description
|
98
|
+
desc = []
|
99
|
+
desc << " with minimum #{@minimum}" if @minimum
|
100
|
+
desc << " with maximum #{@maximum}" if @maximum
|
101
|
+
desc << " within range #{@within}" if @within
|
102
|
+
desc << " is equal to #{@is}" if @is
|
103
|
+
|
104
|
+
super << desc.to_sentence
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def check option
|
110
|
+
actual = @validator.options[option]
|
111
|
+
|
112
|
+
if actual == instance_variable_get("@#{option}")
|
113
|
+
@positive_message << " with #{option} of #{actual}"
|
114
|
+
else
|
115
|
+
@negative_message << " with #{option} of #{actual}"
|
116
|
+
@result = false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def check_range
|
121
|
+
min, max = @within.min, @within.max
|
122
|
+
actual_min = @validator.options[:minimum]
|
123
|
+
actual_max = @validator.options[:maximum]
|
124
|
+
|
125
|
+
if actual_min == min && actual_max == max
|
126
|
+
@positive_message << " with range #{@within}"
|
127
|
+
else
|
128
|
+
@negative_message << " with range #{actual_min..actual_max}"
|
129
|
+
@result = false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def check_precision
|
134
|
+
actual = @validator.options[:is]
|
135
|
+
|
136
|
+
if actual == @is
|
137
|
+
@positive_message << " is equal to #{@is}"
|
138
|
+
else
|
139
|
+
@negative_message << " is equal to #{actual}"
|
140
|
+
@result = false
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
# Ensures that the model is not valid if the given attribute is not present.
|
5
|
+
#
|
6
|
+
# it { must validate_presence_of(:name) }
|
7
|
+
def validate_presence_of attr
|
8
|
+
ValidationMatcher.new attr, :presence
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module MiniTest
|
2
|
+
module Matchers
|
3
|
+
module ActiveModel
|
4
|
+
class ValidationMatcher # :nodoc:
|
5
|
+
def initialize attr, type
|
6
|
+
@attr = attr
|
7
|
+
@type = type
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches? subject
|
11
|
+
@klass = subject.is_a?(Class) ? subject : subject.class
|
12
|
+
@validator = @klass.validators_on(@attr).find { |v| v.kind == @type }
|
13
|
+
@result = true
|
14
|
+
|
15
|
+
check_validator
|
16
|
+
|
17
|
+
@result
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message
|
21
|
+
"Expected #{@klass} to #{description}; instead got #{@negative_message}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def negative_failure_message
|
25
|
+
"Expected #{@klass} to not #{description}; instead got #{@positive_message}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def description
|
29
|
+
"validate #{@type} of #{@attr}"
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def check_validator
|
35
|
+
if @validator
|
36
|
+
@positive_message = "#{@type} validator for #{@attr}"
|
37
|
+
@negative_message = "#{@type} validator for #{@attr}"
|
38
|
+
else
|
39
|
+
@negative_message = "no #{@type} validator for #{@attr}"
|
40
|
+
@result = false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate_invalid_options! *options
|
45
|
+
if options.all? &:nil?
|
46
|
+
raise ArgumentError, 'You have to supply an option for this matcher'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'minitest-activemodel/version'
|
2
|
+
|
3
|
+
require 'minitest/matchers'
|
4
|
+
require 'minitest-activemodel/matchers/validation_matcher.rb'
|
5
|
+
require 'minitest-activemodel/matchers/validate_acceptance_matcher'
|
6
|
+
require 'minitest-activemodel/matchers/validate_confirmation_matcher'
|
7
|
+
require 'minitest-activemodel/matchers/validate_format_matcher'
|
8
|
+
require 'minitest-activemodel/matchers/validate_length_matcher'
|
9
|
+
require 'minitest-activemodel/matchers/validate_presence_matcher'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path '../lib/minitest-activemodel/version', __FILE__
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'minitest-activemodel'
|
6
|
+
gem.version = MiniTest::Matchers::ActiveModel::VERSION
|
7
|
+
|
8
|
+
gem.authors = ['Francesco Rodriguez']
|
9
|
+
gem.email = ['lrodriguezsanc@gmail.com']
|
10
|
+
gem.description = %q{MiniTest Matchers for ActiveModel::Validations}
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.homepage = 'https://github.com/frodsan/minitest-activemodel'
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split "\n"
|
15
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename f }
|
16
|
+
gem.test_files = `git ls-files -- test/*`.split "\n"
|
17
|
+
gem.extra_rdoc_files = ['CHANGELOG.md', 'LICENSE.md', 'README.md']
|
18
|
+
gem.rdoc_options = ['--charset=UTF-8']
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_dependency 'minitest', '>= 4.0.0'
|
22
|
+
gem.add_dependency 'minitest-matchers', '~> 1.2.0'
|
23
|
+
gem.add_dependency 'activemodel', '~> 3.2.0'
|
24
|
+
gem.add_development_dependency 'rake'
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidateAcceptanceTest < MiniTest::Unit::TestCase
|
4
|
+
test 'must validate acceptance of an attribute' do
|
5
|
+
assert_must validate_acceptance_of(:eula), User
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'must not validate acceptance of an attribute' do
|
9
|
+
assert_wont validate_acceptance_of(:not_accepted), User
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'must validate acceptance of an attribute with an accepted value' do
|
13
|
+
assert_must validate_acceptance_of(:terms_of_service).accept_with(true), User
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'must not validate acceptance of an attribute with an unaccepted value' do
|
17
|
+
assert_wont validate_acceptance_of(:terms_of_service).accept_with('1'), User
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidateConfirmationMatcherTest < MiniTest::Unit::TestCase
|
4
|
+
test 'must validate confirmation of an attribute' do
|
5
|
+
assert_must validate_confirmation_of(:password), User
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'must not validate confirmation of an optional attribute' do
|
9
|
+
assert_wont validate_confirmation_of(:no_confirm), User
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidateFormatMatcherTest < MiniTest::Unit::TestCase
|
4
|
+
test 'raises ArgumentError when no option is given' do
|
5
|
+
assert_raises(ArgumentError) { assert_must validate_format_of(:email), User}
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'raises if to_allow and to_not_allow are used' do
|
9
|
+
assert_raises RuntimeError do
|
10
|
+
assert_must validate_format_of(:email).to_allow('').to_not_allow(''), User
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'must validate valid format of an attribute' do
|
15
|
+
assert_must validate_format_of(:email).to_allow('foo@bar.com'), User
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'must not validate valid format of an attribute' do
|
19
|
+
assert_wont validate_format_of(:email).to_allow('foo_bar_com'), User
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'must validate invalid format of an attribute' do
|
23
|
+
assert_must validate_format_of(:email).to_not_allow('foo_bar_com'), User
|
24
|
+
end
|
25
|
+
|
26
|
+
test 'must not validate invalid format of an attribute' do
|
27
|
+
assert_wont validate_format_of(:email).to_not_allow('foo@bar.com'), User
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidateLengthMatcher < MiniTest::Unit::TestCase
|
4
|
+
test 'raises ArgumentError when no option is given' do
|
5
|
+
assert_raises(ArgumentError) { assert_must validate_length_of(:name), User }
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'must validate minimum length of an attribute' do
|
9
|
+
assert_must validate_length_of(:name).with_minimum(10), User
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'must not validate minimum length of an attribute' do
|
13
|
+
assert_wont validate_length_of(:name).with_minimum(100), User
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'with_minimum must be aliased as with_min' do
|
17
|
+
assert_must validate_length_of(:name).with_min(10), User
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'with_minimum must be aliased as is_at_least' do
|
21
|
+
assert_must ensure_length_of(:name).is_at_least(10), User
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'must validate maximum length of an attribute' do
|
25
|
+
assert_must validate_length_of(:name).with_maximum(100), User
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'must not validate maximum length of an attribute' do
|
29
|
+
assert_wont validate_length_of(:name).with_maximum(10), User
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'must validate minimum and maximum length of an attribute' do
|
33
|
+
assert_must validate_length_of(:name).within(10..100), User
|
34
|
+
end
|
35
|
+
|
36
|
+
test 'must not validate minimum and maximum length of an attribute' do
|
37
|
+
assert_wont validate_length_of(:name).within(100..10), User
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'within must be aliased as in' do
|
41
|
+
assert_must validate_length_of(:name).in(10..100), User
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'within must be a range' do
|
45
|
+
assert_raises ArgumentError do
|
46
|
+
assert_must validate_length_of(:name).within(10), User
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'must validate exact length of an attribute' do
|
51
|
+
assert_must validate_length_of(:password).is(8), User
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'must not validate exact length of an attribute' do
|
55
|
+
assert_wont validate_length_of(:password).is(7), User
|
56
|
+
end
|
57
|
+
|
58
|
+
test 'is must be aliased as is_equal_to' do
|
59
|
+
assert_must validate_length_of(:password).is_equal_to(8), User
|
60
|
+
end
|
61
|
+
|
62
|
+
test 'must be aliased as validate_size_of' do
|
63
|
+
assert_must validate_size_of(:name).with_max(100), User
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'must be aliased as ensure_length_of' do
|
67
|
+
assert_must ensure_length_of(:name).with_max(100), User
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'must be aliased as ensure_size_of' do
|
71
|
+
assert_must ensure_size_of(:name).is_at_most(100), User
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'must be aliased as validates_size_of' do
|
75
|
+
assert_must validate_length_of(:lastname).with_max(100), User
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidatePresenceMatcherTest < MiniTest::Unit::TestCase
|
4
|
+
test 'must validate presence of a required attribute' do
|
5
|
+
assert_must validate_presence_of(:name), User
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'must not validate presence of an optional attribute' do
|
9
|
+
assert_wont validate_presence_of(:not_required), User
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationMatcherTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@validator = ValidationMatcher.new :attr, :type
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'returns false if validation type not exists' do
|
9
|
+
subject = create_subject :attr, :another_type
|
10
|
+
|
11
|
+
assert !@validator.matches?(subject)
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'returns true if validation type exists' do
|
15
|
+
subject = create_subject :attr, :type
|
16
|
+
|
17
|
+
assert @validator.matches? subject
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'set failure message if validation type not exists' do
|
21
|
+
subject = create_subject :attr, :another_type
|
22
|
+
|
23
|
+
@validator.matches? subject
|
24
|
+
|
25
|
+
assert_match 'no type validator for attr', @validator.failure_message
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'set negative failure message if validation type exists' do
|
29
|
+
subject = create_subject :attr, :type
|
30
|
+
|
31
|
+
@validator.matches? subject
|
32
|
+
|
33
|
+
assert_match 'type validator for attr', @validator.negative_failure_message
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def create_subject attr, type
|
39
|
+
arg = MiniTest::Mock.new
|
40
|
+
arg.expect :kind, type
|
41
|
+
|
42
|
+
subject = MiniTest::Mock.new
|
43
|
+
subject.expect :is_a?, true, [Class]
|
44
|
+
subject.expect :validators_on, [arg], [attr]
|
45
|
+
end
|
46
|
+
end
|
data/test/models/user.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class User
|
4
|
+
include ActiveModel::Validations
|
5
|
+
|
6
|
+
validates_acceptance_of :eula
|
7
|
+
validates_acceptance_of :terms_of_service, accept: true
|
8
|
+
|
9
|
+
validates_confirmation_of :password
|
10
|
+
|
11
|
+
validates_format_of :email, with: /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
|
12
|
+
|
13
|
+
validates_length_of :name, minimum: 10, maximum: 100
|
14
|
+
validates_length_of :password, is: 8
|
15
|
+
validates_size_of :lastname, maximum: 100
|
16
|
+
|
17
|
+
validates_presence_of :name
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-activemodel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Francesco Rodriguez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest-matchers
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.2.0
|
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: 1.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activemodel
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.2.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: MiniTest Matchers for ActiveModel::Validations
|
79
|
+
email:
|
80
|
+
- lrodriguezsanc@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files:
|
84
|
+
- CHANGELOG.md
|
85
|
+
- LICENSE.md
|
86
|
+
- README.md
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- .travis.yml
|
90
|
+
- CHANGELOG.md
|
91
|
+
- CONTRIBUTING.md
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.md
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/minitest-activemodel.rb
|
97
|
+
- lib/minitest-activemodel/matchers/validate_acceptance_matcher.rb
|
98
|
+
- lib/minitest-activemodel/matchers/validate_confirmation_matcher.rb
|
99
|
+
- lib/minitest-activemodel/matchers/validate_format_matcher.rb
|
100
|
+
- lib/minitest-activemodel/matchers/validate_length_matcher.rb
|
101
|
+
- lib/minitest-activemodel/matchers/validate_presence_matcher.rb
|
102
|
+
- lib/minitest-activemodel/matchers/validation_matcher.rb
|
103
|
+
- lib/minitest-activemodel/version.rb
|
104
|
+
- minitest-activemodel.gemspec
|
105
|
+
- test/cases/validate_acceptance_matcher_test.rb
|
106
|
+
- test/cases/validate_confirmation_matcher_test.rb
|
107
|
+
- test/cases/validate_format_matcher_test.rb
|
108
|
+
- test/cases/validate_length_matcher_test.rb
|
109
|
+
- test/cases/validate_presence_matcher_test.rb
|
110
|
+
- test/cases/validation_test.rb
|
111
|
+
- test/models/user.rb
|
112
|
+
- test/test_helper.rb
|
113
|
+
homepage: https://github.com/frodsan/minitest-activemodel
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options:
|
117
|
+
- --charset=UTF-8
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.8.23
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: MiniTest Matchers for ActiveModel::Validations
|
138
|
+
test_files:
|
139
|
+
- test/cases/validate_acceptance_matcher_test.rb
|
140
|
+
- test/cases/validate_confirmation_matcher_test.rb
|
141
|
+
- test/cases/validate_format_matcher_test.rb
|
142
|
+
- test/cases/validate_length_matcher_test.rb
|
143
|
+
- test/cases/validate_presence_matcher_test.rb
|
144
|
+
- test/cases/validation_test.rb
|
145
|
+
- test/models/user.rb
|
146
|
+
- test/test_helper.rb
|