forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_length_of_matcher.rb
552 lines (507 loc) · 16.2 KB
/
validate_length_of_matcher.rb
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
module Shoulda
module Matchers
module ActiveModel
# The `validate_length_of` matcher tests usage of the
# `validates_length_of` matcher. Note that this matcher is intended to be
# used against string columns and associations and not integer columns.
#
# #### Qualifiers
#
# Use `on` if your validation applies only under a certain context.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_length_of :password, minimum: 10, on: :create
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_length_of(:password).
# is_at_least(10).
# on(:create)
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:password).
# is_at_least(10).
# on(:create)
# end
#
# ##### is_at_least
#
# Use `is_at_least` to test usage of the `:minimum` option. This asserts
# that the attribute can take a string which is equal to or longer than
# the given length and cannot take a string which is shorter. This qualifier
# also works for associations.
#
# class User
# include ActiveModel::Model
# attr_accessor :bio
#
# validates_length_of :bio, minimum: 15
# end
#
# # RSpec
#
# RSpec.describe User, type: :model do
# it { should validate_length_of(:bio).is_at_least(15) }
# end
#
# # Minitest (Shoulda)
#
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:bio).is_at_least(15)
# end
#
# ##### is_at_most
#
# Use `is_at_most` to test usage of the `:maximum` option. This asserts
# that the attribute can take a string which is equal to or shorter than
# the given length and cannot take a string which is longer. This qualifier
# also works for associations.
#
# class User
# include ActiveModel::Model
# attr_accessor :status_update
#
# validates_length_of :status_update, maximum: 140
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it { should validate_length_of(:status_update).is_at_most(140) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:status_update).is_at_most(140)
# end
#
# ##### is_equal_to
#
# Use `is_equal_to` to test usage of the `:is` option. This asserts that
# the attribute can take a string which is exactly equal to the given
# length and cannot take a string which is shorter or longer. This qualifier
# also works for associations.
#
# class User
# include ActiveModel::Model
# attr_accessor :favorite_superhero
#
# validates_length_of :favorite_superhero, is: 6
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it { should validate_length_of(:favorite_superhero).is_equal_to(6) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:favorite_superhero).is_equal_to(6)
# end
#
# ##### is_at_least + is_at_most
#
# Use `is_at_least` and `is_at_most` together to test usage of the `:in`
# option. This qualifies also works for associations.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_length_of :password, in: 5..30
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_length_of(:password).
# is_at_least(5).is_at_most(30)
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:password).
# is_at_least(5).is_at_most(30)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class User
# include ActiveModel::Model
# attr_accessor :password
#
# validates_length_of :password,
# minimum: 10,
# message: "Password isn't long enough"
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_length_of(:password).
# is_at_least(10).
# with_message("Password isn't long enough")
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:password).
# is_at_least(10).
# with_message("Password isn't long enough")
# end
#
# ##### with_short_message
#
# Use `with_short_message` if you are using a custom "too short" message.
#
# class User
# include ActiveModel::Model
# attr_accessor :secret_key
#
# validates_length_of :secret_key,
# in: 15..100,
# too_short: 'Secret key must be more than 15 characters'
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_length_of(:secret_key).
# is_at_least(15).
# with_short_message('Secret key must be more than 15 characters')
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:secret_key).
# is_at_least(15).
# with_short_message('Secret key must be more than 15 characters')
# end
#
# ##### with_long_message
#
# Use `with_long_message` if you are using a custom "too long" message.
#
# class User
# include ActiveModel::Model
# attr_accessor :secret_key
#
# validates_length_of :secret_key,
# in: 15..100,
# too_long: 'Secret key must be less than 100 characters'
# end
#
# # RSpec
# RSpec.describe User, type: :model do
# it do
# should validate_length_of(:secret_key).
# is_at_most(100).
# with_long_message('Secret key must be less than 100 characters')
# end
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:secret_key).
# is_at_most(100).
# with_long_message('Secret key must be less than 100 characters')
# end
#
# ##### allow_nil
#
# Use `allow_nil` to assert that the attribute allows nil.
#
# class User
# include ActiveModel::Model
# attr_accessor :bio
#
# validates_length_of :bio, minimum: 15, allow_nil: true
# end
#
# # RSpec
# describe User do
# it { should validate_length_of(:bio).is_at_least(15).allow_nil }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:bio).is_at_least(15).allow_nil
# end
#
# @return [ValidateLengthOfMatcher]
#
# ##### allow_blank
#
# Use `allow_blank` to assert that the attribute allows blank.
#
# class User
# include ActiveModel::Model
# attr_accessor :bio
#
# validates_length_of :bio, minimum: 15, allow_blank: true
# end
#
# # RSpec
# describe User do
# it { should validate_length_of(:bio).is_at_least(15).allow_blank }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:bio).is_at_least(15).allow_blank
# end
#
# ##### as_array
#
# Use `as_array` if you have an ActiveModel model and the attribute being validated
# is designed to store an array. (This is not necessary if you have an ActiveRecord
# model with an array column, as the matcher will detect this automatically.)
#
# class User
# include ActiveModel::Model
# attribute :arr, array: true
#
# validates_length_of :arr, minimum: 15
# end
#
# # RSpec
# describe User do
# it { should validate_length_of(:arr).as_array.is_at_least(15) }
# end
#
# # Minitest (Shoulda)
# class UserTest < ActiveSupport::TestCase
# should validate_length_of(:arr).as_array.is_at_least(15)
# end
#
def validate_length_of(attr)
ValidateLengthOfMatcher.new(attr)
end
# @private
class ValidateLengthOfMatcher < ValidationMatcher
include Helpers
def initialize(attribute)
super(attribute)
@options = {}
@short_message = nil
@long_message = nil
end
def as_array
@options[:array] = true
self
end
def is_at_least(length)
@options[:minimum] = length
@short_message ||= :too_short
self
end
def is_at_most(length)
@options[:maximum] = length
@long_message ||= :too_long
self
end
def is_equal_to(length)
@options[:minimum] = length
@options[:maximum] = length
@short_message ||= :wrong_length
@long_message ||= :wrong_length
self
end
def with_message(message)
if message
@expects_custom_validation_message = true
@short_message = message
@long_message = message
end
self
end
def with_short_message(message)
if message
@expects_custom_validation_message = true
@short_message = message
end
self
end
def with_long_message(message)
if message
@expects_custom_validation_message = true
@long_message = message
end
self
end
def allow_nil
@options[:allow_nil] = true
self
end
def simple_description
description = "validate that the length of :#{@attribute}"
if @options.key?(:minimum) && @options.key?(:maximum)
if @options[:minimum] == @options[:maximum]
description << " is #{@options[:minimum]}"
else
description << " is between #{@options[:minimum]}"
description << " and #{@options[:maximum]}"
end
elsif @options.key?(:minimum)
description << " is at least #{@options[:minimum]}"
elsif @options.key?(:maximum)
description << " is at most #{@options[:maximum]}"
end
description
end
def matches?(subject)
super(subject)
lower_bound_matches? &&
upper_bound_matches? &&
allow_nil_matches? &&
allow_blank_matches?
end
def does_not_match?(subject)
super(subject)
lower_bound_does_not_match? ||
upper_bound_does_not_match? ||
allow_nil_does_not_match? ||
allow_blank_does_not_match?
end
private
def expects_to_allow_nil?
@options[:allow_nil]
end
def lower_bound_matches?
disallows_lower_length? && allows_minimum_length?
end
def lower_bound_does_not_match?
allows_lower_length? || disallows_minimum_length?
end
def upper_bound_matches?
disallows_higher_length? && allows_maximum_length?
end
def upper_bound_does_not_match?
allows_higher_length? || disallows_maximum_length?
end
def allows_lower_length?
@options.key?(:minimum) &&
@options[:minimum] > 0 &&
allows_length_of?(
@options[:minimum] - 1,
translated_short_message,
)
end
def disallows_lower_length?
[email protected]?(:minimum) ||
@options[:minimum] == 0 ||
(@options[:minimum] == 1 && expects_to_allow_blank?) ||
disallows_length_of?(
@options[:minimum] - 1,
translated_short_message,
)
end
def allows_higher_length?
@options.key?(:maximum) &&
allows_length_of?(
@options[:maximum] + 1,
translated_long_message,
)
end
def disallows_higher_length?
[email protected]?(:maximum) ||
disallows_length_of?(
@options[:maximum] + 1,
translated_long_message,
)
end
def allows_minimum_length?
[email protected]?(:minimum) ||
allows_length_of?(@options[:minimum], translated_short_message)
end
def disallows_minimum_length?
@options.key?(:minimum) &&
disallows_length_of?(@options[:minimum], translated_short_message)
end
def allows_maximum_length?
[email protected]?(:maximum) ||
allows_length_of?(@options[:maximum], translated_long_message)
end
def disallows_maximum_length?
@options.key?(:maximum) &&
disallows_length_of?(@options[:maximum], translated_long_message)
end
def allow_nil_matches?
!expects_to_allow_nil? || allows_value_of(nil)
end
def allow_nil_does_not_match?
expects_to_allow_nil? && disallows_value_of(nil)
end
def allows_length_of?(length, message)
allows_value_of(value_of_length(length), message)
end
def disallows_length_of?(length, message)
disallows_value_of(value_of_length(length), message)
end
def value_of_length(length)
if array_column?
['x'] * length
elsif collection_association?
Array.new(length) { association_reflection.klass.new }
else
'x' * length
end
end
def array_column?
@options[:array] || super
end
def collection_association?
association? && [:has_many, :has_and_belongs_to_many].include?(
association_reflection.macro,
)
end
def association?
association_reflection.present?
end
def association_reflection
model.try(:reflect_on_association, @attribute)
end
def translated_short_message
@_translated_short_message ||=
if @short_message.is_a?(Symbol)
default_error_message(
@short_message,
model_name: @subject.class.to_s.underscore,
instance: @subject,
attribute: @attribute,
count: @options[:minimum],
)
else
@short_message
end
end
def translated_long_message
@_translated_long_message ||=
if @long_message.is_a?(Symbol)
default_error_message(
@long_message,
model_name: @subject.class.to_s.underscore,
instance: @subject,
attribute: @attribute,
count: @options[:maximum],
)
else
@long_message
end
end
end
end
end
end