-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
validate_numericality_of_matcher.rb
676 lines (619 loc) · 20.3 KB
/
validate_numericality_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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
module Shoulda
module Matchers
module ActiveModel
# The `validate_numericality_of` matcher tests usage of the
# `validates_numericality_of` validation.
#
# class Person
# include ActiveModel::Model
# attr_accessor :gpa
#
# validates_numericality_of :gpa
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it { should validate_numericality_of(:gpa) }
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:gpa)
# end
#
# #### Qualifiers
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class Person
# include ActiveModel::Model
# attr_accessor :number_of_dependents
#
# validates_numericality_of :number_of_dependents, on: :create
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:number_of_dependents).
# on(:create)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:number_of_dependents).on(:create)
# end
#
# ##### only_integer
#
# Use `only_integer` to test usage of the `:only_integer` option. This
# asserts that your attribute only allows integer numbers and disallows
# non-integer ones.
#
# class Person
# include ActiveModel::Model
# attr_accessor :age
#
# validates_numericality_of :age, only_integer: true
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it { should validate_numericality_of(:age).only_integer }
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:age).only_integer
# end
#
# ##### is_less_than
#
# Use `is_less_than` to test usage of the the `:less_than` option. This
# asserts that the attribute can take a number which is less than the
# given value and cannot take a number which is greater than or equal to
# it.
#
# class Person
# include ActiveModel::Model
# attr_accessor :number_of_cars
#
# validates_numericality_of :number_of_cars, less_than: 2
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:number_of_cars).
# is_less_than(2)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:number_of_cars).
# is_less_than(2)
# end
#
# ##### is_less_than_or_equal_to
#
# Use `is_less_than_or_equal_to` to test usage of the
# `:less_than_or_equal_to` option. This asserts that the attribute can
# take a number which is less than or equal to the given value and cannot
# take a number which is greater than it.
#
# class Person
# include ActiveModel::Model
# attr_accessor :birth_year
#
# validates_numericality_of :birth_year, less_than_or_equal_to: 1987
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:birth_year).
# is_less_than_or_equal_to(1987)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:birth_year).
# is_less_than_or_equal_to(1987)
# end
#
# ##### is_equal_to
#
# Use `is_equal_to` to test usage of the `:equal_to` option. This asserts
# that the attribute can take a number which is equal to the given value
# and cannot take a number which is not equal.
#
# class Person
# include ActiveModel::Model
# attr_accessor :weight
#
# validates_numericality_of :weight, equal_to: 150
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it { should validate_numericality_of(:weight).is_equal_to(150) }
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:weight).is_equal_to(150)
# end
#
# ##### is_greater_than_or_equal_to
#
# Use `is_greater_than_or_equal_to` to test usage of the
# `:greater_than_or_equal_to` option. This asserts that the attribute can
# take a number which is greater than or equal to the given value and
# cannot take a number which is less than it.
#
# class Person
# include ActiveModel::Model
# attr_accessor :height
#
# validates_numericality_of :height, greater_than_or_equal_to: 55
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:height).
# is_greater_than_or_equal_to(55)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:height).
# is_greater_than_or_equal_to(55)
# end
#
# ##### is_greater_than
#
# Use `is_greater_than` to test usage of the `:greater_than` option.
# This asserts that the attribute can take a number which is greater than
# the given value and cannot take a number less than or equal to it.
#
# class Person
# include ActiveModel::Model
# attr_accessor :legal_age
#
# validates_numericality_of :legal_age, greater_than: 21
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:legal_age).
# is_greater_than(21)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:legal_age).
# is_greater_than(21)
# end
#
# ##### is_other_than
#
# Use `is_other_than` to test usage of the `:other_than` option.
# This asserts that the attribute can take a number which is not equal to
# the given value.
#
# class Person
# include ActiveModel::Model
# attr_accessor :legal_age
#
# validates_numericality_of :legal_age, other_than: 21
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:legal_age).
# is_other_than(21)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:legal_age).
# is_other_than(21)
# end
#
# ##### even
#
# Use `even` to test usage of the `:even` option. This asserts that the
# attribute can take odd numbers and cannot take even ones.
#
# class Person
# include ActiveModel::Model
# attr_accessor :birth_month
#
# validates_numericality_of :birth_month, even: true
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it { should validate_numericality_of(:birth_month).even }
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:birth_month).even
# end
#
# ##### odd
#
# Use `odd` to test usage of the `:odd` option. This asserts that the
# attribute can take a number which is odd and cannot take a number which
# is even.
#
# class Person
# include ActiveModel::Model
# attr_accessor :birth_day
#
# validates_numericality_of :birth_day, odd: true
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it { should validate_numericality_of(:birth_day).odd }
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:birth_day).odd
# end
#
# ##### is_in
#
# Use `is_in` to test usage of the `:in` option.
# This asserts that the attribute can take a number which is contained
# in the given range.
#
# class Person
# include ActiveModel::Model
# attr_accessor :legal_age
#
# validates_numericality_of :birth_month, in: 1..12
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:birth_month).
# is_in(1..12)
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:birth_month).
# is_in(1..12)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
#
# class Person
# include ActiveModel::Model
# attr_accessor :number_of_dependents
#
# validates_numericality_of :number_of_dependents,
# message: 'Number of dependents must be a number'
# end
#
# # RSpec
# RSpec.describe Person, type: :model do
# it do
# should validate_numericality_of(:number_of_dependents).
# with_message('Number of dependents must be a number')
# end
# end
#
# # Minitest (Shoulda)
# class PersonTest < ActiveSupport::TestCase
# should validate_numericality_of(:number_of_dependents).
# with_message('Number of dependents must be a number')
# end
#
# ##### allow_nil
#
# Use `allow_nil` to assert that the attribute allows nil.
#
# class Post
# include ActiveModel::Model
# attr_accessor :age
#
# validates_numericality_of :age, allow_nil: true
# end
#
# # RSpec
# RSpec.describe Post, type: :model do
# it { should validate_numericality_of(:age).allow_nil }
# end
#
# # Minitest (Shoulda)
# class PostTest < ActiveSupport::TestCase
# should validate_numericality_of(:age).allow_nil
# end
#
# @return [ValidateNumericalityOfMatcher]
#
def validate_numericality_of(attr)
ValidateNumericalityOfMatcher.new(attr)
end
# @private
class ValidateNumericalityOfMatcher < ValidationMatcher
NUMERIC_NAME = 'number'.freeze
DEFAULT_DIFF_TO_COMPARE = 1
attr_reader :diff_to_compare
def initialize(attribute)
super
@submatchers = []
@diff_to_compare = DEFAULT_DIFF_TO_COMPARE
@expects_to_allow_nil = false
@allowed_type_adjective = nil
@allowed_type_name = 'number'
add_disallow_non_numeric_value_matcher
end
def only_integer
prepare_submatcher(
NumericalityMatchers::OnlyIntegerMatcher.new(self, attribute),
)
self
end
def allow_nil
@expects_to_allow_nil = true
prepare_submatcher(allow_value_matcher(nil, :not_a_number))
self
end
def expects_to_allow_nil?
@expects_to_allow_nil
end
def odd
prepare_submatcher(
NumericalityMatchers::OddNumberMatcher.new(self, attribute),
)
self
end
def even
prepare_submatcher(
NumericalityMatchers::EvenNumberMatcher.new(self, attribute),
)
self
end
def is_greater_than(value)
prepare_submatcher(comparison_matcher_for(value, :>).for(attribute))
self
end
def is_greater_than_or_equal_to(value)
prepare_submatcher(comparison_matcher_for(value, :>=).for(attribute))
self
end
def is_equal_to(value)
prepare_submatcher(comparison_matcher_for(value, :==).for(attribute))
self
end
def is_less_than(value)
prepare_submatcher(comparison_matcher_for(value, :<).for(attribute))
self
end
def is_less_than_or_equal_to(value)
prepare_submatcher(comparison_matcher_for(value, :<=).for(attribute))
self
end
def is_other_than(value)
prepare_submatcher(comparison_matcher_for(value, :!=).for(attribute))
self
end
def is_in(range)
prepare_submatcher(
NumericalityMatchers::RangeMatcher.new(self, attribute, range),
)
self
end
def matches?(subject)
matches_or_does_not_match?(subject)
first_submatcher_that_fails_to_match.nil?
end
def does_not_match?(subject)
matches_or_does_not_match?(subject)
first_submatcher_that_fails_to_not_match.nil?
end
def simple_description
String.new.tap do |description|
description << "validate that :#{attribute} looks like "
description << Shoulda::Matchers::Util.a_or_an(full_allowed_type)
if range_description.present?
description << " #{range_description}"
end
if comparison_descriptions.present?
description << " #{comparison_descriptions}"
end
end
end
def failure_message
overall_failure_message.dup.tap do |message|
message << "\n"
message << failure_message_for_first_submatcher_that_fails_to_match
end
end
def failure_message_when_negated
overall_failure_message_when_negated.dup.tap do |message|
message << "\n"
message <<
failure_message_for_first_submatcher_that_fails_to_not_match
end
end
def given_numeric_column?
attribute_is_active_record_column? &&
[:integer, :float, :decimal].include?(column_type)
end
private
def matches_or_does_not_match?(subject)
@subject = subject
@number_of_submatchers = @submatchers.size
qualify_submatchers
end
def attribute_is_active_record_column?
columns_hash.key?(attribute.to_s)
end
def column_type
columns_hash[attribute.to_s].type
end
def columns_hash
if subject.class.respond_to?(:columns_hash)
subject.class.columns_hash
else
{}
end
end
def add_disallow_non_numeric_value_matcher
disallow_value_matcher = DisallowValueMatcher.
new(non_numeric_value).
for(attribute).
with_message(:not_a_number)
add_submatcher(disallow_value_matcher)
end
def prepare_submatcher(submatcher)
add_submatcher(submatcher)
submatcher
end
def comparison_matcher_for(value, operator)
ComparisonMatcher.
new(self, value, operator).
for(attribute)
end
def add_submatcher(submatcher)
if submatcher.respond_to?(:allowed_type_name)
@allowed_type_name = submatcher.allowed_type_name
end
if submatcher.respond_to?(:allowed_type_adjective)
@allowed_type_adjective = submatcher.allowed_type_adjective
end
if submatcher.respond_to?(:diff_to_compare)
@diff_to_compare = [
@diff_to_compare,
submatcher.diff_to_compare,
].max
end
@submatchers << submatcher
end
def qualify_submatchers
@submatchers.each do |submatcher|
if @expects_strict
submatcher.strict(@expects_strict)
end
if @expected_message.present?
submatcher.with_message(@expected_message)
end
if context
submatcher.on(context)
end
submatcher.ignoring_interference_by_writer(
ignore_interference_by_writer,
)
end
end
def number_of_submatchers_for_failure_message
if has_been_qualified?
@submatchers.size - 1
else
@submatchers.size
end
end
def has_been_qualified?
@submatchers.any? { |submatcher| submatcher_qualified?(submatcher) }
end
def submatcher_qualified?(submatcher)
Shoulda::Matchers::RailsShim.parent_of(submatcher.class) ==
NumericalityMatchers || submatcher.instance_of?(ComparisonMatcher)
end
def first_submatcher_that_fails_to_match
@_first_submatcher_that_fails_to_match ||=
@submatchers.detect do |submatcher|
!submatcher.matches?(subject)
end
end
def first_submatcher_that_fails_to_not_match
@_first_submatcher_that_fails_to_not_match ||=
@submatchers.detect do |submatcher|
!submatcher.does_not_match?(subject)
end
end
def failure_message_for_first_submatcher_that_fails_to_match
build_submatcher_failure_message_for(
first_submatcher_that_fails_to_match,
:failure_message,
)
end
def failure_message_for_first_submatcher_that_fails_to_not_match
build_submatcher_failure_message_for(
first_submatcher_that_fails_to_not_match,
:failure_message_when_negated,
)
end
def build_submatcher_failure_message_for(
submatcher,
failure_message_method
)
failure_message = submatcher.public_send(failure_message_method)
submatcher_description = submatcher.simple_description.
sub(/\bvalidate that\b/, 'validates').
sub(/\bdisallow\b/, 'disallows').
sub(/\ballow\b/, 'allows')
submatcher_message =
if number_of_submatchers_for_failure_message > 1
"In checking that #{model.name} #{submatcher_description}, " +
failure_message[0].downcase +
failure_message[1..]
else
failure_message
end
Shoulda::Matchers.word_wrap(submatcher_message, indent: 2)
end
def full_allowed_type
"#{@allowed_type_adjective} #{@allowed_type_name}".strip
end
def comparison_descriptions
description_array = submatcher_comparison_descriptions
if description_array.empty?
''
else
submatcher_comparison_descriptions.join(' and ')
end
end
def submatcher_comparison_descriptions
@submatchers.inject([]) do |arr, submatcher|
if submatcher.respond_to? :comparison_description
arr << submatcher.comparison_description
end
arr
end
end
def range_description
range_submatcher = @submatchers.detect do |submatcher|
submatcher.respond_to? :range_description
end
range_submatcher&.range_description
end
def non_numeric_value
'abcd'
end
end
end
end
end