shoulda-matchers 2.6.1 → 2.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +9 -0
- data/.yardopts +8 -0
- data/Appraisals +82 -33
- data/Gemfile +18 -2
- data/Gemfile.lock +13 -1
- data/NEWS.md +27 -2
- data/README.md +83 -1329
- data/Rakefile +118 -1
- data/cucumber.yml +1 -0
- data/doc_config/gh-pages/index.html.erb +9 -0
- data/doc_config/yard/setup.rb +22 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +5967 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +12 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/global.css +45 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +69 -0
- data/doc_config/yard/templates/default/fulldoc/html/css/style.css +283 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +32 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +1 -0
- data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +8 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/app.js +298 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +1 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +289 -0
- data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +6 -0
- data/doc_config/yard/templates/default/fulldoc/html/setup.rb +8 -0
- data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +14 -0
- data/doc_config/yard/templates/default/layout/html/fonts.erb +1 -0
- data/doc_config/yard/templates/default/layout/html/layout.erb +23 -0
- data/doc_config/yard/templates/default/layout/html/search.erb +13 -0
- data/doc_config/yard/templates/default/layout/html/setup.rb +40 -0
- data/doc_config/yard/templates/default/method_details/html/source.erb +10 -0
- data/doc_config/yard/templates/default/module/html/box_info.erb +31 -0
- data/docs.watchr +5 -0
- data/features/rails_integration.feature +32 -0
- data/features/step_definitions/rails_steps.rb +55 -9
- data/features/support/env.rb +1 -0
- data/gemfiles/3.0.gemfile +13 -1
- data/gemfiles/3.0.gemfile.lock +13 -1
- data/gemfiles/3.1.gemfile +17 -2
- data/gemfiles/3.1.gemfile.lock +31 -2
- data/gemfiles/3.1_1.9.2.gemfile +33 -0
- data/gemfiles/3.1_1.9.2.gemfile.lock +203 -0
- data/gemfiles/3.2.gemfile +18 -2
- data/gemfiles/3.2.gemfile.lock +32 -2
- data/gemfiles/3.2_1.9.2.gemfile +32 -0
- data/gemfiles/3.2_1.9.2.gemfile.lock +200 -0
- data/gemfiles/4.0.0.gemfile +20 -1
- data/gemfiles/4.0.0.gemfile.lock +46 -2
- data/gemfiles/4.0.1.gemfile +20 -1
- data/gemfiles/4.0.1.gemfile.lock +46 -2
- data/gemfiles/4.1.gemfile +21 -2
- data/gemfiles/4.1.gemfile.lock +47 -4
- data/lib/shoulda/matchers/action_controller.rb +0 -20
- data/lib/shoulda/matchers/action_controller/callback_matcher.rb +119 -28
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +22 -6
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +43 -10
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +40 -13
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +63 -11
- data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +34 -1
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +84 -15
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +84 -28
- data/lib/shoulda/matchers/action_controller/route_params.rb +4 -3
- data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +76 -13
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +147 -13
- data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +148 -2
- data/lib/shoulda/matchers/active_model.rb +0 -25
- data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +66 -9
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +161 -19
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb +92 -13
- data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +218 -16
- data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +198 -32
- data/lib/shoulda/matchers/active_model/errors.rb +5 -2
- data/lib/shoulda/matchers/active_model/exception_message_finder.rb +1 -1
- data/lib/shoulda/matchers/active_model/have_secure_password_matcher.rb +29 -8
- data/lib/shoulda/matchers/active_model/helpers.rb +20 -8
- data/lib/shoulda/matchers/active_model/numericality_matchers.rb +9 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +4 -6
- data/lib/shoulda/matchers/active_model/numericality_matchers/even_number_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb +3 -2
- data/lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +52 -14
- data/lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb +51 -13
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +53 -7
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +275 -19
- data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +84 -14
- data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +170 -41
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +20 -15
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -2
- data/lib/shoulda/matchers/active_record.rb +1 -12
- data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +89 -15
- data/lib/shoulda/matchers/active_record/association_matcher.rb +726 -70
- data/lib/shoulda/matchers/active_record/association_matchers.rb +9 -0
- data/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +2 -1
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +4 -5
- data/lib/shoulda/matchers/active_record/association_matchers/order_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/source_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb +4 -3
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +79 -15
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +64 -15
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +21 -7
- data/lib/shoulda/matchers/active_record/serialize_matcher.rb +85 -10
- data/lib/shoulda/matchers/assertion_error.rb +7 -1
- data/lib/shoulda/matchers/doublespeak.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/double.rb +3 -1
- data/lib/shoulda/matchers/doublespeak/double_collection.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/double_implementation_registry.rb +1 -0
- data/lib/shoulda/matchers/doublespeak/object_double.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/proxy_implementation.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/structs.rb +2 -0
- data/lib/shoulda/matchers/doublespeak/stub_implementation.rb +2 -1
- data/lib/shoulda/matchers/doublespeak/world.rb +3 -4
- data/lib/shoulda/matchers/error.rb +1 -0
- data/lib/shoulda/matchers/independent/delegate_matcher.rb +108 -20
- data/lib/shoulda/matchers/independent/delegate_matcher/stubbed_target.rb +4 -3
- data/lib/shoulda/matchers/integrations/nunit_test_case_detection.rb +3 -0
- data/lib/shoulda/matchers/rails_shim.rb +3 -2
- data/lib/shoulda/matchers/version.rb +2 -1
- data/lib/shoulda/matchers/warn.rb +1 -0
- data/script/SUPPORTED_VERSIONS +1 -0
- data/script/install_gems_in_all_appraisals +14 -0
- data/script/run_all_tests +14 -0
- data/shoulda-matchers.gemspec +0 -10
- data/spec/report_warnings.rb +7 -0
- data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +1 -1
- data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +9 -0
- data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +0 -36
- data/spec/shoulda/matchers/active_model/validation_message_finder_spec.rb +2 -2
- data/spec/shoulda/matchers/doublespeak/double_spec.rb +1 -1
- data/spec/shoulda/matchers/doublespeak/world_spec.rb +11 -29
- data/spec/shoulda/matchers/doublespeak_spec.rb +3 -3
- data/spec/spec_helper.rb +17 -0
- data/spec/support/class_builder.rb +4 -0
- data/spec/support/test_application.rb +1 -1
- data/spec/warnings_spy.rb +64 -0
- data/spec/warnings_spy/filesystem.rb +45 -0
- data/spec/warnings_spy/partitioner.rb +29 -0
- data/spec/warnings_spy/reader.rb +64 -0
- data/spec/warnings_spy/reporter.rb +87 -0
- metadata +49 -134
@@ -1,29 +1,225 @@
|
|
1
1
|
require 'bigdecimal'
|
2
2
|
|
3
|
-
module Shoulda
|
3
|
+
module Shoulda
|
4
4
|
module Matchers
|
5
|
-
module ActiveModel
|
6
|
-
|
7
|
-
#
|
5
|
+
module ActiveModel
|
6
|
+
# The `ensure_inclusion_of` matcher tests usage of the
|
7
|
+
# `validates_inclusion_of` validation, asserting that an attribute can
|
8
|
+
# take a whitelist of values and cannot take values outside of this list.
|
9
|
+
#
|
10
|
+
# If your whitelist is an array of values, use `in_array`:
|
11
|
+
#
|
12
|
+
# class Issue
|
13
|
+
# include ActiveModel::Model
|
14
|
+
# attr_accessor :state
|
15
|
+
#
|
16
|
+
# validates_inclusion_of :state, in: %w(open resolved unresolved)
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# # RSpec
|
20
|
+
# describe Issue do
|
21
|
+
# it do
|
22
|
+
# should ensure_inclusion_of(:state).
|
23
|
+
# in_array(%w(open resolved unresolved))
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# # Test::Unit
|
28
|
+
# class IssueTest < ActiveSupport::TestCase
|
29
|
+
# should ensure_inclusion_of(:state).
|
30
|
+
# in_array(%w(open resolved unresolved))
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# If your whitelist is a range of values, use `in_range`:
|
34
|
+
#
|
35
|
+
# class Issue
|
36
|
+
# include ActiveModel::Model
|
37
|
+
# attr_accessor :priority
|
38
|
+
#
|
39
|
+
# validates_inclusion_of :priority, in: 1..5
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # RSpec
|
43
|
+
# describe Issue do
|
44
|
+
# it { should ensure_inclusion_of(:state).in_range(1..5) }
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# # Test::Unit
|
48
|
+
# class IssueTest < ActiveSupport::TestCase
|
49
|
+
# should ensure_inclusion_of(:state).in_range(1..5)
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# #### Optional qualifiers
|
53
|
+
#
|
54
|
+
# ##### with_message
|
55
|
+
#
|
56
|
+
# Use `with_message` if you are using a custom validation message.
|
57
|
+
#
|
58
|
+
# class Issue
|
59
|
+
# include ActiveModel::Model
|
60
|
+
# attr_accessor :severity
|
61
|
+
#
|
62
|
+
# validates_inclusion_of :severity,
|
63
|
+
# in: %w(low medium high),
|
64
|
+
# message: 'Severity must be low, medium, or high'
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# # RSpec
|
68
|
+
# describe Issue do
|
69
|
+
# it do
|
70
|
+
# should ensure_inclusion_of(:severity).
|
71
|
+
# in_array(%w(low medium high)).
|
72
|
+
# with_message('Severity must be low, medium, or high')
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# # Test::Unit
|
77
|
+
# class IssueTest < ActiveSupport::TestCase
|
78
|
+
# should ensure_inclusion_of(:severity).
|
79
|
+
# in_array(%w(low medium high)).
|
80
|
+
# with_message('Severity must be low, medium, or high')
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# ##### with_low_message
|
84
|
+
#
|
85
|
+
# Use `with_low_message` if you have a custom validation message for when
|
86
|
+
# a given value is too low.
|
87
|
+
#
|
88
|
+
# class Person
|
89
|
+
# include ActiveModel::Model
|
90
|
+
# attr_accessor :age
|
91
|
+
#
|
92
|
+
# validate :age_must_be_valid
|
93
|
+
#
|
94
|
+
# private
|
95
|
+
#
|
96
|
+
# def age_must_be_valid
|
97
|
+
# if age < 65
|
98
|
+
# self.errors.add :age, 'You do not receive any benefits'
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# # RSpec
|
104
|
+
# describe Person do
|
105
|
+
# it do
|
106
|
+
# should ensure_inclusion_of(:age).
|
107
|
+
# in_range(0..65).
|
108
|
+
# with_low_message('You do not receive any benefits')
|
109
|
+
# end
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# # Test::Unit
|
113
|
+
# class PersonTest < ActiveSupport::TestCase
|
114
|
+
# should ensure_inclusion_of(:age).
|
115
|
+
# in_range(0..65).
|
116
|
+
# with_low_message('You do not receive any benefits')
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# ##### with_high_message
|
120
|
+
#
|
121
|
+
# Use `with_high_message` if you have a custom validation message for
|
122
|
+
# when a given value is too high.
|
123
|
+
#
|
124
|
+
# class Person
|
125
|
+
# include ActiveModel::Model
|
126
|
+
# attr_accessor :age
|
127
|
+
#
|
128
|
+
# validate :age_must_be_valid
|
129
|
+
#
|
130
|
+
# private
|
131
|
+
#
|
132
|
+
# def age_must_be_valid
|
133
|
+
# if age > 21
|
134
|
+
# self.errors.add :age, "You're too old for this stuff"
|
135
|
+
# end
|
136
|
+
# end
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# # RSpec
|
140
|
+
# describe Person do
|
141
|
+
# it do
|
142
|
+
# should ensure_inclusion_of(:age).
|
143
|
+
# in_range(0..21).
|
144
|
+
# with_high_message("You're too old for this stuff")
|
145
|
+
# end
|
146
|
+
# end
|
147
|
+
#
|
148
|
+
# # Test::Unit
|
149
|
+
# class PersonTest < ActiveSupport::TestCase
|
150
|
+
# should ensure_inclusion_of(:age).
|
151
|
+
# in_range(0..21).
|
152
|
+
# with_high_message("You're too old for this stuff")
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# ##### allow_nil
|
156
|
+
#
|
157
|
+
# Use `allow_nil` to assert that the attribute allows nil.
|
158
|
+
#
|
159
|
+
# class Issue
|
160
|
+
# include ActiveModel::Model
|
161
|
+
# attr_accessor :state
|
162
|
+
#
|
163
|
+
# validates_presence_of :state
|
164
|
+
# validates_inclusion_of :state,
|
165
|
+
# in: %w(open resolved unresolved),
|
166
|
+
# allow_nil: true
|
167
|
+
# end
|
168
|
+
#
|
169
|
+
# # RSpec
|
170
|
+
# describe Issue do
|
171
|
+
# it do
|
172
|
+
# should ensure_inclusion_of(:state).
|
173
|
+
# in_array(%w(open resolved unresolved)).
|
174
|
+
# allow_nil
|
175
|
+
# end
|
176
|
+
# end
|
177
|
+
#
|
178
|
+
# # Test::Unit
|
179
|
+
# class IssueTest < ActiveSupport::TestCase
|
180
|
+
# should ensure_inclusion_of(:state).
|
181
|
+
# in_array(%w(open resolved unresolved)).
|
182
|
+
# allow_nil
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# ##### allow_blank
|
186
|
+
#
|
187
|
+
# Use `allow_blank` to assert that the attribute allows blank.
|
188
|
+
#
|
189
|
+
# class Issue
|
190
|
+
# include ActiveModel::Model
|
191
|
+
# attr_accessor :state
|
192
|
+
#
|
193
|
+
# validates_presence_of :state
|
194
|
+
# validates_inclusion_of :state,
|
195
|
+
# in: %w(open resolved unresolved),
|
196
|
+
# allow_blank: true
|
197
|
+
# end
|
198
|
+
#
|
199
|
+
# # RSpec
|
200
|
+
# describe Issue do
|
201
|
+
# it do
|
202
|
+
# should ensure_inclusion_of(:state).
|
203
|
+
# in_array(%w(open resolved unresolved)).
|
204
|
+
# allow_blank
|
205
|
+
# end
|
206
|
+
# end
|
8
207
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# * <tt>with_high_message</tt> - value the test expects to find in
|
16
|
-
# <tt>errors.on(:attribute)</tt>. Regexp or string. Defaults to the
|
17
|
-
# translation for :inclusion.
|
208
|
+
# # Test::Unit
|
209
|
+
# class IssueTest < ActiveSupport::TestCase
|
210
|
+
# should ensure_inclusion_of(:state).
|
211
|
+
# in_array(%w(open resolved unresolved)).
|
212
|
+
# allow_blank
|
213
|
+
# end
|
18
214
|
#
|
19
|
-
#
|
20
|
-
# it { should ensure_inclusion_of(:age).in_range(0..100) }
|
215
|
+
# @return [EnsureInclusionOfMatcher]
|
21
216
|
#
|
22
217
|
def ensure_inclusion_of(attr)
|
23
218
|
EnsureInclusionOfMatcher.new(attr)
|
24
219
|
end
|
25
220
|
|
26
|
-
|
221
|
+
# @private
|
222
|
+
class EnsureInclusionOfMatcher < ValidationMatcher
|
27
223
|
ARBITRARY_OUTSIDE_STRING = 'shouldamatchersteststring'
|
28
224
|
ARBITRARY_OUTSIDE_FIXNUM = 123456789
|
29
225
|
ARBITRARY_OUTSIDE_DECIMAL = BigDecimal.new('0.123456789')
|
@@ -45,6 +241,12 @@ EOT
|
|
45
241
|
def initialize(attribute)
|
46
242
|
super(attribute)
|
47
243
|
@options = {}
|
244
|
+
@array = nil
|
245
|
+
@range = nil
|
246
|
+
@minimum = nil
|
247
|
+
@maximum = nil
|
248
|
+
@low_message = nil
|
249
|
+
@high_message = nil
|
48
250
|
end
|
49
251
|
|
50
252
|
def in_array(array)
|
@@ -1,45 +1,211 @@
|
|
1
|
-
module Shoulda
|
1
|
+
module Shoulda
|
2
2
|
module Matchers
|
3
|
-
module ActiveModel
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
3
|
+
module ActiveModel
|
4
|
+
# The `ensure_length_of` matcher tests usage of the `validates_length_of`
|
5
|
+
# matcher. Note that this matcher is intended to be used against string
|
6
|
+
# columns and not integer columns.
|
7
|
+
#
|
8
|
+
# #### Qualifiers
|
9
|
+
#
|
10
|
+
# ##### is_at_least
|
11
|
+
#
|
12
|
+
# Use `is_at_least` to test usage of the `:minimum` option. This asserts
|
13
|
+
# that the attribute can take a string which is equal to or longer than
|
14
|
+
# the given length and cannot take a string which is shorter.
|
15
|
+
#
|
16
|
+
# class User
|
17
|
+
# include ActiveModel::Model
|
18
|
+
# attr_accessor :bio
|
19
|
+
#
|
20
|
+
# validates_length_of :bio, minimum: 15
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# # RSpec
|
24
|
+
#
|
25
|
+
# describe User do
|
26
|
+
# it { should ensure_length_of(:bio).is_at_least(15) }
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # Test::Unit
|
30
|
+
#
|
31
|
+
# class UserTest < ActiveSupport::TestCase
|
32
|
+
# should ensure_length_of(:bio).is_at_least(15)
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# ##### is_at_most
|
36
|
+
#
|
37
|
+
# Use `is_at_most` to test usage of the `:maximum` option. This asserts
|
38
|
+
# that the attribute can take a string which is equal to or shorter than
|
39
|
+
# the given length and cannot take a string which is longer.
|
40
|
+
#
|
41
|
+
# class User
|
42
|
+
# include ActiveModel::Model
|
43
|
+
# attr_accessor :status_update
|
44
|
+
#
|
45
|
+
# validates_length_of :status_update, maximum: 140
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# # RSpec
|
49
|
+
# describe User do
|
50
|
+
# it { should ensure_length_of(:status_update).is_at_most(140) }
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# # Test::Unit
|
54
|
+
# class UserTest < ActiveSupport::TestCase
|
55
|
+
# should ensure_length_of(:status_update).is_at_most(140)
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# ##### is_equal_to
|
59
|
+
#
|
60
|
+
# Use `is_at_equal` to test usage of the `:is` option. This asserts that
|
61
|
+
# the attribute can take a string which is exactly equal to the given
|
62
|
+
# length and cannot take a string which is shorter or longer.
|
63
|
+
#
|
64
|
+
# class User
|
65
|
+
# include ActiveModel::Model
|
66
|
+
# attr_accessor :favorite_superhero
|
67
|
+
#
|
68
|
+
# validates_length_of :favorite_superhero, is: 6
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# # RSpec
|
72
|
+
# describe User do
|
73
|
+
# it { should ensure_length_of(:favorite_superhero).is_equal_to(6) }
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# # Test::Unit
|
77
|
+
# class UserTest < ActiveSupport::TestCase
|
78
|
+
# should ensure_length_of(:favorite_superhero).is_equal_to(6)
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# ##### is_at_least + is_at_most
|
82
|
+
#
|
83
|
+
# Use `is_at_least` and `is_at_most` together to test usage of the `:in`
|
84
|
+
# option.
|
85
|
+
#
|
86
|
+
# class User
|
87
|
+
# include ActiveModel::Model
|
88
|
+
# attr_accessor :password
|
89
|
+
#
|
90
|
+
# validates_length_of :password, in: 5..30
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# # RSpec
|
94
|
+
# describe User do
|
95
|
+
# it do
|
96
|
+
# should ensure_length_of(:password).
|
97
|
+
# is_at_least(5).is_at_most(30)
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# # Test::Unit
|
102
|
+
# class UserTest < ActiveSupport::TestCase
|
103
|
+
# should ensure_length_of(:password).
|
104
|
+
# is_at_least(5).is_at_most(30)
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# ##### with_message
|
108
|
+
#
|
109
|
+
# Use `with_message` if you are using a custom validation message.
|
110
|
+
#
|
111
|
+
# class User
|
112
|
+
# include ActiveModel::Model
|
113
|
+
# attr_accessor :api_token
|
114
|
+
#
|
115
|
+
# validates_length_of :api_token,
|
116
|
+
# minimum: 10,
|
117
|
+
# message: "Password isn't long enough"
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# # RSpec
|
121
|
+
# describe User do
|
122
|
+
# it do
|
123
|
+
# should ensure_length_of(:password).
|
124
|
+
# is_at_least(10).
|
125
|
+
# with_message("Password isn't long enough")
|
126
|
+
# end
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# # Test::Unit
|
130
|
+
# class UserTest < ActiveSupport::TestCase
|
131
|
+
# should ensure_length_of(:password).
|
132
|
+
# is_at_least(10).
|
133
|
+
# with_message("Password isn't long enough")
|
134
|
+
# end
|
135
|
+
#
|
136
|
+
# ##### with_short_message
|
137
|
+
#
|
138
|
+
# Use `with_short_message` if you are using a custom "too short" message.
|
139
|
+
#
|
140
|
+
# class User
|
141
|
+
# include ActiveModel::Model
|
142
|
+
# attr_accessor :secret_key
|
143
|
+
#
|
144
|
+
# validates_length_of :secret_key,
|
145
|
+
# in: 15..100,
|
146
|
+
# too_short: 'Secret key must be more than 15 characters'
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# # RSpec
|
150
|
+
# describe User do
|
151
|
+
# it do
|
152
|
+
# should ensure_length_of(:secret_key).
|
153
|
+
# is_at_least(15).
|
154
|
+
# with_short_message('Secret key must be more than 15 characters')
|
155
|
+
# end
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
# # Test::Unit
|
159
|
+
# class UserTest < ActiveSupport::TestCase
|
160
|
+
# should ensure_length_of(:secret_key).
|
161
|
+
# is_at_least(15).
|
162
|
+
# with_short_message('Secret key must be more than 15 characters')
|
163
|
+
# end
|
164
|
+
#
|
165
|
+
# ##### with_long_message
|
166
|
+
#
|
167
|
+
# Use `with_long_message` if you are using a custom "too long" message.
|
168
|
+
#
|
169
|
+
# class User
|
170
|
+
# include ActiveModel::Model
|
171
|
+
# attr_accessor :secret_key
|
172
|
+
#
|
173
|
+
# validates_length_of :secret_key,
|
174
|
+
# in: 15..100,
|
175
|
+
# too_long: 'Secret key must be less than 100 characters'
|
176
|
+
# end
|
177
|
+
#
|
178
|
+
# # RSpec
|
179
|
+
# describe User do
|
180
|
+
# it do
|
181
|
+
# should ensure_length_of(:secret_key).
|
182
|
+
# is_at_most(100).
|
183
|
+
# with_long_message('Secret key must be less than 100 characters')
|
184
|
+
# end
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# # Test::Unit
|
188
|
+
# class UserTest < ActiveSupport::TestCase
|
189
|
+
# should ensure_length_of(:secret_key).
|
190
|
+
# is_at_most(100).
|
191
|
+
# with_long_message('Secret key must be less than 100 characters')
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# @return [EnsureLengthOfMatcher]
|
195
|
+
#
|
33
196
|
def ensure_length_of(attr)
|
34
197
|
EnsureLengthOfMatcher.new(attr)
|
35
198
|
end
|
36
199
|
|
37
|
-
|
200
|
+
# @private
|
201
|
+
class EnsureLengthOfMatcher < ValidationMatcher
|
38
202
|
include Helpers
|
39
203
|
|
40
204
|
def initialize(attribute)
|
41
205
|
super(attribute)
|
42
206
|
@options = {}
|
207
|
+
@short_message = nil
|
208
|
+
@long_message = nil
|
43
209
|
end
|
44
210
|
|
45
211
|
def is_at_least(length)
|